aboutsummaryrefslogtreecommitdiffstats
path: root/util/text/validate.go
blob: 4c3f7065bd6d484817405b34496159515eaa6c01 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package text

import (
	"net/url"
	"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
}

// Safe will tell if a character in the string is considered unsafe
// Currently trigger on all unicode control character
func SafeOneLine(s string) bool {
	for _, r := range s {
		if unicode.IsControl(r) {
			return false
		}
	}

	return true
}

// ValidUrl will tell if the string contains what seems to be a valid URL
func ValidUrl(s string) bool {
	if strings.Contains(s, "\n") {
		return false
	}

	_, err := url.ParseRequestURI(s)
	return err == nil
}