Skip to content

Commit

Permalink
Refactor Check if all the characters in a base32 encoded ULID are par…
Browse files Browse the repository at this point in the history
…t of the expected base32 character set into its own function
  • Loading branch information
el10savio authored Dec 28, 2023
1 parent a1d104f commit bd95e00
Showing 1 changed file with 13 additions and 27 deletions.
40 changes: 13 additions & 27 deletions ulid.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,33 +177,7 @@ func parse(v []byte, strict bool, id *ULID) error {

// Check if all the characters in a base32 encoded ULID are part of the
// expected base32 character set.
if strict &&
(dec[v[0]] == 0xFF ||
dec[v[1]] == 0xFF ||
dec[v[2]] == 0xFF ||
dec[v[3]] == 0xFF ||
dec[v[4]] == 0xFF ||
dec[v[5]] == 0xFF ||
dec[v[6]] == 0xFF ||
dec[v[7]] == 0xFF ||
dec[v[8]] == 0xFF ||
dec[v[9]] == 0xFF ||
dec[v[10]] == 0xFF ||
dec[v[11]] == 0xFF ||
dec[v[12]] == 0xFF ||
dec[v[13]] == 0xFF ||
dec[v[14]] == 0xFF ||
dec[v[15]] == 0xFF ||
dec[v[16]] == 0xFF ||
dec[v[17]] == 0xFF ||
dec[v[18]] == 0xFF ||
dec[v[19]] == 0xFF ||
dec[v[20]] == 0xFF ||
dec[v[21]] == 0xFF ||
dec[v[22]] == 0xFF ||
dec[v[23]] == 0xFF ||
dec[v[24]] == 0xFF ||
dec[v[25]] == 0xFF) {
if strict && isNotBase32CharacterSet(v, dec) {
return ErrInvalidCharacters
}

Expand Down Expand Up @@ -242,6 +216,18 @@ func parse(v []byte, strict bool, id *ULID) error {
return nil
}

func isNotBase32CharacterSet(v []byte, dec [256]byte) bool {
count := 25

for index := 0; index <= count; index++ {
if dec[v[index]] == 0xFF {
return true
}
}

return false
}

// MustParse is a convenience function equivalent to Parse that panics on failure
// instead of returning an error.
func MustParse(ulid string) ULID {
Expand Down

0 comments on commit bd95e00

Please sign in to comment.