validate.go

 1package text
 2
 3import (
 4	"net/url"
 5	"strings"
 6	"unicode"
 7)
 8
 9// Empty tell if the string is considered empty once space
10// and not graphics characters are removed
11func Empty(s string) bool {
12	trim := strings.TrimFunc(s, func(r rune) bool {
13		return unicode.IsSpace(r) || !unicode.IsGraphic(r)
14	})
15
16	return trim == ""
17}
18
19// Safe will tell if a character in the string is considered unsafe
20// Currently trigger on unicode control character except \n, \t and \r
21func Safe(s string) bool {
22	for _, r := range s {
23		switch r {
24		case '\t', '\r', '\n':
25			continue
26		}
27
28		if unicode.IsControl(r) {
29			return false
30		}
31	}
32
33	return true
34}
35
36// ValidUrl will tell if the string contains what seems to be a valid URL
37func ValidUrl(s string) bool {
38	if strings.Contains(s, "\n") {
39		return false
40	}
41
42	_, err := url.ParseRequestURI(s)
43	return err == nil
44}