| 1 |
// Copyright (c) 2019, Maxime Soulé |
| 2 |
// All rights reserved. |
| 3 |
// |
| 4 |
// This source code is licensed under the BSD-style license found in the |
| 5 |
// LICENSE file in the root directory of this source tree. |
| 6 |
|
| 7 |
package util |
| 8 |
|
| 9 |
import ( |
| 10 |
"errors" |
| 11 |
"unicode" |
| 12 |
) |
| 13 |
|
| 14 |
// ErrTagEmpty is the error returned by [CheckTag] for an empty tag. |
| 15 |
var ErrTagEmpty = errors.New("a tag cannot be empty") |
| 16 |
|
| 17 |
// ErrTagInvalid is the error returned by [CheckTag] for an invalid tag. |
| 18 |
var ErrTagInvalid = errors.New("invalid tag, should match (Letter|_)(Letter|_|Number)*") |
| 19 |
|
| 20 |
// CheckTag checks that tag is a valid tag (see operator [Tag]) or not. |
| 21 |
// |
| 22 |
// [Tag]: https://go-testdeep.zetta.rocks/operators/tag/ |
| 23 |
func CheckTag(tag string) error { |
| 24 |
if tag == "" { |
| 25 |
return ErrTagEmpty |
| 26 |
} |
| 27 |
|
| 28 |
for i, r := range tag { |
| 29 |
if !unicode.IsLetter(r) && r != '_' && (i == 0 || !unicode.IsNumber(r)) { |
| 30 |
return ErrTagInvalid |
| 31 |
} |
| 32 |
} |
| 33 |
|
| 34 |
return nil |
| 35 |
} |