-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcase.go
74 lines (64 loc) · 1.8 KB
/
case.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package chars
/*
case.go
-John Taylor
Jan-21-2022
An attempt to create a case-insensitive glob for file name matching on Windows which is then to be used in conjunction
with filepath.Glob().
If the name passed into CaseInsensitive contains '[' or ']', then an error will be returned.
Any PRs to improve this are most welcomed!
It does this by changing a filename string:
from: c:\bin\test.exe
to: c:\[Bb][Ii][Nn]\[Tt][Ee][Ss][Tt].[Ee][Xx][Ee]
Caveats
-------
1) language.English is hard-coded in CaseInsensitive()
2) If a '[' or a ']' character is found in the filename, they are converted to '?' which means that filepath.Glob() may
match more than expected.
*/
import (
"bytes"
"fmt"
"os"
"strings"
"unicode"
"golang.org/x/text/cases"
"golang.org/x/text/language"
)
func IsLetter(r int32) bool {
if !unicode.IsLetter(r) {
return false
}
return true
}
func CaseInsensitive(name string) string {
if strings.Contains(name, "[") || strings.Contains(name, "]") {
//return "", fmt.Errorf("error: input not allowed to contain '[' or ']'")
_, _ = fmt.Fprintf(os.Stderr, "warning: filenames containing '[' or ']' can lead to unpredictable results; consider using wildcards\n")
return name
}
var newName bytes.Buffer
for i, ch := range name {
if len(name) > 1 && i == 0 && name[i+1] == ':' {
newName.WriteRune(ch)
} else if ch == '[' {
newName.WriteString(`?`)
} else if ch == ']' {
newName.WriteString(`?`)
} else if IsLetter(ch) {
u := cases.Upper(language.English).Bytes([]byte{byte(ch)})
l := cases.Lower(language.English).Bytes([]byte{byte(ch)})
newName.WriteString("[")
for _, b := range u {
newName.WriteByte(b)
}
for _, b := range l {
newName.WriteByte(b)
}
newName.WriteString("]")
} else {
newName.WriteRune(ch)
}
}
return newName.String()
}