Beispiel #1
0
// ValidateStoreCode checks if a store code is valid. Returns an ErrStoreCodeInvalid if the
// first letter is not a-zA-Z and followed by a-zA-Z0-9_ or store code length is greater than 32 characters.
func ValidateStoreCode(c string) error {
	if c == "" || len(c) > 32 {
		return ErrStoreCodeInvalid
	}
	c1 := c[0]
	if false == ((c1 >= 'a' && c1 <= 'z') || (c1 >= 'A' && c1 <= 'Z')) {
		return ErrStoreCodeInvalid
	}
	if false == utils.IsAlphaNumeric(c) {
		return ErrStoreCodeInvalid
	}
	return nil
}
Beispiel #2
0
// IsAlphaNumeric returns true if a string consists of characters a-zA-Z0-9_
func TestIsAlphaNumeric(t *testing.T) {
	tests := []struct {
		have string
		want bool
	}{
		{"Hello World", false},
		{"HelloWorld", true},
		{"Hello1World", true},
		{"Hello0123456789", true},
		{"Hello0123456789€", false},
		{" Hello0123456789", false},
	}

	for _, test := range tests {
		assert.True(t, utils.IsAlphaNumeric(test.have) == test.want, "%#v", test)
	}
}
Beispiel #3
0
// BenchmarkIsAlphaNumeric	10000000	       132 ns/op	       0 B/op	       0 allocs/op
func BenchmarkIsAlphaNumeric(b *testing.B) {
	b.ReportAllocs()
	for i := 0; i < b.N; i++ {
		benchIsAlphaNumeric = utils.IsAlphaNumeric("Hello1WorldOfGophers")
	}
}