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 +++++++++++++++++++++++++++++++++++++++++++++ server/channel/channel.go | 2 +- server/object/object.go | 2 +- server/user/user.go | 2 +- server/validate/validate.go | 49 --------------------------------------------- 5 files changed, 52 insertions(+), 52 deletions(-) create mode 100644 proto/validate/validate.go delete mode 100644 server/validate/validate.go 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()) +} diff --git a/server/channel/channel.go b/server/channel/channel.go index c9620bf..e2a18c5 100644 --- a/server/channel/channel.go +++ b/server/channel/channel.go @@ -4,7 +4,7 @@ import ( "citrons.xyz/talk/proto" "citrons.xyz/talk/server/object" "citrons.xyz/talk/server/session" - "citrons.xyz/talk/server/validate" + "citrons.xyz/talk/proto/validate" "citrons.xyz/talk/server/user" "strings" "strconv" diff --git a/server/object/object.go b/server/object/object.go index cd7ba84..70b84c2 100644 --- a/server/object/object.go +++ b/server/object/object.go @@ -3,7 +3,7 @@ package object import ( "citrons.xyz/talk/proto" "citrons.xyz/talk/server/session" - "citrons.xyz/talk/server/validate" + "citrons.xyz/talk/proto/validate" "bufio" "bytes" "log" diff --git a/server/user/user.go b/server/user/user.go index d2d5724..198fc0f 100644 --- a/server/user/user.go +++ b/server/user/user.go @@ -3,8 +3,8 @@ package user import ( "citrons.xyz/talk/server/object" "citrons.xyz/talk/server/session" - "citrons.xyz/talk/server/validate" "citrons.xyz/talk/server/passwords" + "citrons.xyz/talk/proto/validate" "citrons.xyz/talk/proto" bolt "go.etcd.io/bbolt" "log" diff --git a/server/validate/validate.go b/server/validate/validate.go deleted file mode 100644 index 7251895..0000000 --- a/server/validate/validate.go +++ /dev/null @@ -1,49 +0,0 @@ -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