Skip to content
This repository has been archived by the owner on Mar 16, 2024. It is now read-only.

Commit

Permalink
Fix edge case in range syntax sugar
Browse files Browse the repository at this point in the history
Treat mixed case and backwards ranges (e.g. `A-z`, `0-A`, `z-a`) as
individual characters in the given set. This simplifies the UX and
makes ranges easier to reason about.

Signed-off-by: Nick Hale <[email protected]>
  • Loading branch information
njhale committed Oct 17, 2023
1 parent 87ccaa2 commit 23a4f3b
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 19 deletions.
36 changes: 23 additions & 13 deletions pkg/secrets/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"crypto/rand"
"math/big"
"sort"
"unicode"

"k8s.io/apimachinery/pkg/util/sets"
)
Expand Down Expand Up @@ -48,26 +49,22 @@ func inflateRanges(characterSet string) string {
)
for i := 0; i < len(runeSet); i++ {
cur := runeSet[i]
if alphanumeric(cur) {
// Alphanumeric character detected
if i+2 < len(runeSet) && runeSet[i+1] == '-' && alphanumeric(runeSet[i+2]) {
// Range detected, convert to full set of characters
start, end := cur, runeSet[i+2]
if start > end {
// Swap start and end if they're out of order
start, end = end, start
}

// Alphanumeric character detected
if alphanumeric(cur) && (i+2 < len(runeSet) && runeSet[i+1] == '-' && alphanumeric(runeSet[i+2])) {
// Range detected, convert to full set of characters
start, end := cur, runeSet[i+2]
if start <= end && !mixedRange(start, end) {
for c := start; c <= end; c++ {
if alphanumeric(c) {
inflated.Insert(c)
}
}

// Skip the next two characters since we've already processed them
// Skip the next two characters since they were part of the range
i += 2
continue
}

}

inflated.Insert(cur)
Expand All @@ -83,6 +80,19 @@ func inflateRanges(characterSet string) string {

// alphanumeric returns true IFF the given rune is alphanumeric; e.g. [A-z0-9] .
func alphanumeric(r rune) bool {
cv := int(r)
return (cv >= int('A') && cv <= int('Z')) || (cv >= int('a') && cv <= int('z')) || (cv >= int('0') && cv <= int('9'))
return (r >= 'A' && r <= 'Z') || (r >= 'a' && r <= 'z') || (r >= '0' && r <= '9')
}

// mixedRange returns true IFF the given runes don't have the same casing or aren't both numbers.
func mixedRange(a, b rune) bool {
switch {
case unicode.IsNumber(a):
return !unicode.IsNumber(b)
case unicode.IsUpper(a):
return !unicode.IsUpper(b)
case unicode.IsLower(a):
return !unicode.IsLower(b)
}

return true
}
24 changes: 18 additions & 6 deletions pkg/secrets/generate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ func TestInflateRanges(t *testing.T) {
name: "-",
want: "-",
},
{
name: "0-a",
want: "-0a",
},
{
name: "A-",
want: "-A",
Expand All @@ -33,22 +37,30 @@ func TestInflateRanges(t *testing.T) {
},
{
name: "Z-A",
want: "ABCDEFGHIJKLMNOPQRSTUVWXYZ",
want: "-AZ",
},
{
name: "A-z",
want: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
want: "-Az",
},
{
name: "z-A",
want: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
want: "-Az",
},
{
name: "a-z",
want: "abcdefghijklmnopqrstuvwxyz",
name: "a-Z",
want: "-Za",
},
{
name: "Z-a",
want: "-Za",
},
{
name: "z-a",
want: "-az",
},
{
name: "a-z",
want: "abcdefghijklmnopqrstuvwxyz",
},
{
Expand All @@ -57,7 +69,7 @@ func TestInflateRanges(t *testing.T) {
},
{
name: "9-0",
want: "0123456789",
want: "-09",
},
{
name: "0-9A-Z",
Expand Down

0 comments on commit 23a4f3b

Please sign in to comment.