aboutsummaryrefslogtreecommitdiffstats
path: root/util/text/validate.go
diff options
context:
space:
mode:
Diffstat (limited to 'util/text/validate.go')
-rw-r--r--util/text/validate.go33
1 files changed, 33 insertions, 0 deletions
diff --git a/util/text/validate.go b/util/text/validate.go
new file mode 100644
index 00000000..68bdf48b
--- /dev/null
+++ b/util/text/validate.go
@@ -0,0 +1,33 @@
+package text
+
+import (
+ "strings"
+ "unicode"
+)
+
+// Empty tell if the string is considered empty once space
+// and not graphics characters are removed
+func Empty(s string) bool {
+ trim := strings.TrimFunc(s, func(r rune) bool {
+ return unicode.IsSpace(r) || !unicode.IsGraphic(r)
+ })
+
+ return trim == ""
+}
+
+// Safe will tell if a character in the string is considered unsafe
+// Currently trigger on unicode control character except \n, \t and \r
+func Safe(s string) bool {
+ for _, r := range s {
+ switch r {
+ case '\t', '\r', '\n':
+ continue
+ }
+
+ if unicode.IsControl(r) {
+ return false
+ }
+ }
+
+ return true
+}