Skip to content

Commit

Permalink
Normalize L as 1 (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
OidaTiftla authored Nov 24, 2023
1 parent 1e45100 commit 3446e9e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
6 changes: 3 additions & 3 deletions crockford.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func normUpper(c byte) byte {
switch c {
case '0', 'O', 'o':
return '0'
case '1', 'I', 'i':
case '1', 'I', 'i', 'L', 'l':
return '1'
case '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'V', 'W', 'X', 'Y', 'Z', '*', '~', '$', '=', 'U':
return c
Expand All @@ -82,14 +82,14 @@ func normUpper(c byte) byte {
}

// Normalized returns a normalized version of Crockford encoded bytes of src
// onto dst and returns the resulting slice. It replaces I with 1, o with 0,
// onto dst and returns the resulting slice. It replaces I and L with 1, o with 0,
// and removes invalid characters such as hyphens. The resulting slice is uppercase.
func Normalized(s string) string {
return string(AppendNormalized(nil, []byte(s)))
}

// AppendNormalized appends a normalized version of Crockford encoded bytes of src
// onto dst and returns the resulting slice. It replaces I with 1, o with 0,
// onto dst and returns the resulting slice. It replaces I and L with 1, o with 0,
// and removes invalid characters such as hyphens. The resulting slice is uppercase.
func AppendNormalized(dst, src []byte) []byte {
dst = grow(dst, len(src))
Expand Down
18 changes: 18 additions & 0 deletions crockford_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,3 +239,21 @@ func chunk(s string, size int) []string {
}
return append(res, s[(n-1)*size:])
}

func TestNormalized(t *testing.T) {
for _, tc := range []struct {
in, out string
}{
{"", ""},
{"a", "A"},
{"1IiLl", "11111"},
{"0Oo", "000"},
{"-", ""},
{"AB-C", "ABC"},
{"AB-C--DEF", "ABCDEF"},
{"0123456789abcdefghjkmnpqrstvwxyz*~$=u", "0123456789ABCDEFGHJKMNPQRSTVWXYZ*~$=U"},
} {
got := crockford.Normalized(tc.in)
be.Equal(t, tc.out, got)
}
}

0 comments on commit 3446e9e

Please sign in to comment.