forked from Matir/adifparser
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
92 lines (84 loc) · 1.86 KB
/
util.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package adifparser
import (
"bytes"
)
// ASCII lowercase converter
// For a byte as an ASCII character
func charToLower(c byte) byte {
if 'A' <= c && c <= 'Z' {
c += 'a' - 'A'
}
return c
}
// Strictly-ASCII-only lowercase converter
// For a byte sequence
// No Unicode processing
// See bytes.ToLower() source code
func bStrictToLower(s []byte) []byte {
hasUpper := false
for i := 0; i < len(s); i++ {
c := s[i]
hasUpper = hasUpper || ('A' <= c && c <= 'Z')
}
if !hasUpper {
return append([]byte(""), s...)
}
b := make([]byte, len(s))
for i := 0; i < len(s); i++ {
c := s[i]
if 'A' <= c && c <= 'Z' {
c += 'a' - 'A'
}
b[i] = c
}
return b
}
// ASCII uppercase converter
// For a byte as an ASCII character
func charToUpper(c byte) byte {
if 'a' <= c && c <= 'z' {
c -= 'a' - 'A'
}
return c
}
// Strictly-ASCII-only uppercase converter
// For a byte sequence
// No Unicode processing
// See bytes.ToUpper() source code
func bStrictToUpper(s []byte) []byte {
hasLower := false
for i := 0; i < len(s); i++ {
c := s[i]
hasLower = hasLower || ('a' <= c && c <= 'z')
}
if !hasLower {
return append([]byte(""), s...)
}
b := make([]byte, len(s))
for i := 0; i < len(s); i++ {
c := s[i]
if 'a' <= c && c <= 'z' {
c -= 'a' - 'A'
}
b[i] = c
}
return b
}
// Case-insensitive bytes.Index
// This function only handles ASCII bytes - no Unicode-specific conversion
func bIndexCI(b, subslice []byte) int {
return bytes.Index(bStrictToLower(b), bStrictToLower(subslice))
}
// Case-insensitive bytes.Contains
// This function only handles ASCII bytes - no Unicode-specific conversion
func bContainsCI(b, subslice []byte) bool {
return bytes.Contains(bStrictToLower(b), bStrictToLower(subslice))
}
// Find start of next tag
func tagStartPos(b []byte) int {
nextStart := bytes.IndexByte(b, '<')
if nextStart == -1 {
return 0
}
return nextStart
}