From 7bec0b1f134d213e7505fc2ac03ffea26f2193cc Mon Sep 17 00:00:00 2001 From: Michael Muré Date: Sat, 15 Sep 2018 13:15:00 +0200 Subject: bug: add a data validation process to avoid merging incorrect operations --- util/text/validate.go | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 util/text/validate.go (limited to 'util') 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 +} -- cgit