code complexity & repetition analysis tool
1package main
2
3import (
4 "fmt"
5 "strings"
6)
7
8// Example demonstrating code duplication (clones)
9// Multiple functions have similar logic
10
11func processUserInput(input string) string {
12 // Trim and validate input
13 trimmed := strings.TrimSpace(input)
14 if len(trimmed) == 0 {
15 return ""
16 }
17
18 // Convert to lowercase
19 lower := strings.ToLower(trimmed)
20
21 // Remove special characters
22 cleaned := ""
23 for _, char := range lower {
24 if (char >= 'a' && char <= 'z') || (char >= '0' && char <= '9') || char == ' ' {
25 cleaned += string(char)
26 }
27 }
28
29 return cleaned
30}
31
32func processProductName(name string) string {
33 // Trim and validate input
34 trimmed := strings.TrimSpace(name)
35 if len(trimmed) == 0 {
36 return ""
37 }
38
39 // Convert to lowercase
40 lower := strings.ToLower(trimmed)
41
42 // Remove special characters
43 cleaned := ""
44 for _, char := range lower {
45 if (char >= 'a' && char <= 'z') || (char >= '0' && char <= '9') || char == ' ' {
46 cleaned += string(char)
47 }
48 }
49
50 return cleaned
51}
52
53func sanitizeFileName(filename string) string {
54 // Trim and validate input
55 trimmed := strings.TrimSpace(filename)
56 if len(trimmed) == 0 {
57 return ""
58 }
59
60 // Convert to lowercase
61 lower := strings.ToLower(trimmed)
62
63 // Remove special characters
64 cleaned := ""
65 for _, char := range lower {
66 if (char >= 'a' && char <= 'z') || (char >= '0' && char <= '9') || char == ' ' {
67 cleaned += string(char)
68 }
69 }
70
71 return cleaned
72}
73
74func main() {
75 user := processUserInput(" Hello World! ")
76 product := processProductName(" Test Product 123 ")
77 file := sanitizeFileName(" my_file.txt ")
78
79 fmt.Println(user)
80 fmt.Println(product)
81 fmt.Println(file)
82}
83
84// This file has obvious code duplication across the three functions
85// Clone detection should identify the repeated token sequences