44 "crypto/rand"
55 "math/big"
66 "sort"
7+ "unicode"
78
89 "k8s.io/apimachinery/pkg/util/sets"
910)
@@ -48,23 +49,18 @@ func inflateRanges(characterSet string) string {
4849 )
4950 for i := 0 ; i < len (runeSet ); i ++ {
5051 cur := runeSet [i ]
51- if alphanumeric (cur ) {
52- // Alphanumeric character detected
53- if i + 2 < len (runeSet ) && runeSet [i + 1 ] == '-' && alphanumeric (runeSet [i + 2 ]) {
54- // Range detected, convert to full set of characters
55- start , end := cur , runeSet [i + 2 ]
56- if start > end {
57- // Swap start and end if they're out of order
58- start , end = end , start
59- }
60-
52+ // Alphanumeric character detected
53+ if alphanumeric (cur ) && (i + 2 < len (runeSet ) && runeSet [i + 1 ] == '-' && alphanumeric (runeSet [i + 2 ])) {
54+ // Range detected, convert to full set of characters
55+ start , end := cur , runeSet [i + 2 ]
56+ if start <= end && ! mixedRange (start , end ) {
6157 for c := start ; c <= end ; c ++ {
6258 if alphanumeric (c ) {
6359 inflated .Insert (c )
6460 }
6561 }
6662
67- // Skip the next two characters since we've already processed them
63+ // Skip the next two characters since they were part of the range
6864 i += 2
6965 continue
7066 }
@@ -83,6 +79,19 @@ func inflateRanges(characterSet string) string {
8379
8480// alphanumeric returns true IFF the given rune is alphanumeric; e.g. [A-z0-9] .
8581func alphanumeric (r rune ) bool {
86- cv := int (r )
87- return (cv >= int ('A' ) && cv <= int ('Z' )) || (cv >= int ('a' ) && cv <= int ('z' )) || (cv >= int ('0' ) && cv <= int ('9' ))
82+ return (r >= 'A' && r <= 'Z' ) || (r >= 'a' && r <= 'z' ) || (r >= '0' && r <= '9' )
83+ }
84+
85+ // mixedRange returns true IFF the given runes don't have the same casing or aren't both numbers.
86+ func mixedRange (a , b rune ) bool {
87+ switch {
88+ case unicode .IsNumber (a ):
89+ return ! unicode .IsNumber (b )
90+ case unicode .IsUpper (a ):
91+ return ! unicode .IsUpper (b )
92+ case unicode .IsLower (a ):
93+ return ! unicode .IsLower (b )
94+ }
95+
96+ return true
8897}
0 commit comments