From 71722ee7c2f833f6244410f6ddad688a2b5c20c3 Mon Sep 17 00:00:00 2001 From: raven Date: Wed, 22 Oct 2025 19:37:40 -0500 Subject: validation as a subpackage of proto --- proto/validate/validate.go | 49 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 proto/validate/validate.go (limited to 'proto') diff --git a/proto/validate/validate.go b/proto/validate/validate.go new file mode 100644 index 0000000..7251895 --- /dev/null +++ b/proto/validate/validate.go @@ -0,0 +1,49 @@ +package validate + +import ( + "strings" + "unicode" +) + +func Name(name string) bool { + if len(Fold(name)) == 0 || len(name) > 64 { + return false + } + for _, r := range name { + if unicode.IsControl(r) { + return false + } + } + return true +} + +func Fold(s string) string { + var sb strings.Builder + var wasSpace bool + for _, r := range s { + for { + f := unicode.SimpleFold(r) + if f <= r { + r = f + break + } + r = f + } + r = unicode.ToLower(r) + + if !unicode.IsPrint(r) { + continue + } + + if r == ' ' { + if wasSpace { + continue + } + wasSpace = true + } else { + wasSpace = false + } + sb.WriteRune(r) + } + return strings.TrimSpace(sb.String()) +} -- cgit v1.2.3