Skip to content

Commit

Permalink
Merge pull request #1400 from adamdecaf/perf-includesValidCharacters
Browse files Browse the repository at this point in the history
perf: improve includesValidCharacters lookups
  • Loading branch information
adamdecaf authored Mar 27, 2024
2 parents bc805dd + 386688a commit bf42a2e
Showing 1 changed file with 24 additions and 10 deletions.
34 changes: 24 additions & 10 deletions validators.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,20 @@ var (
asciiCharacters = ` !"#$%&'()*+,-./:;<=>?@[\]^_{|}~` + "`"
ebcdicExtraCharacters = `¢¬¦±`

validAlphaNumericCharacters = lowerAlphaCharacters + strings.ToUpper(lowerAlphaCharacters) + numericCharacters + asciiCharacters + ebcdicExtraCharacters
validUppercaseAlphaNumericCharacters = strings.ToUpper(lowerAlphaCharacters) + numericCharacters + asciiCharacters + ebcdicExtraCharacters
validAlphaNumericCharacters map[rune]bool
validUppercaseAlphaNumericCharacters map[rune]bool
)

func init() {
validAlphaNumericCharacters = setupCharacterMap(
lowerAlphaCharacters, strings.ToUpper(lowerAlphaCharacters), numericCharacters, asciiCharacters, ebcdicExtraCharacters,
)

validUppercaseAlphaNumericCharacters = setupCharacterMap(
strings.ToUpper(lowerAlphaCharacters), numericCharacters, asciiCharacters, ebcdicExtraCharacters,
)
}

// validator is common validation and formatting of golang types to ach type strings
type validator struct{}

Expand Down Expand Up @@ -425,15 +435,19 @@ func (v *validator) isTransactionTypeCode(s string) error {
return ErrTransactionTypeCode
}

func (v *validator) includesValidCharacters(input string, charset string) error {
for _, i := range input {
var found bool
for _, c := range charset {
if i == c {
found = true
break
}
func setupCharacterMap(inputs ...string) map[rune]bool {
out := make(map[rune]bool)
for _, input := range inputs {
for _, r := range input {
out[r] = true
}
}
return out
}

func (v *validator) includesValidCharacters(input string, charset map[rune]bool) error {
for _, i := range input {
_, found := charset[i]
if !found {
return fmt.Errorf("invalid character: %v", i)
}
Expand Down

0 comments on commit bf42a2e

Please sign in to comment.