arg.go

 1package sql3util
 2
 3import "strings"
 4
 5// NamedArg splits an named arg into a key and value,
 6// around an equals sign.
 7// Spaces are trimmed around both key and value.
 8func NamedArg(arg string) (key, val string) {
 9	key, val, _ = strings.Cut(arg, "=")
10	key = strings.TrimSpace(key)
11	val = strings.TrimSpace(val)
12	return
13}
14
15// Unquote unquotes a string.
16//
17// https://sqlite.org/lang_keywords.html
18func Unquote(val string) string {
19	if len(val) < 2 {
20		return val
21	}
22	fst := val[0]
23	lst := val[len(val)-1]
24	rst := val[1 : len(val)-1]
25	if fst == '[' && lst == ']' {
26		return rst
27	}
28	if fst != lst {
29		return val
30	}
31	var old, new string
32	switch fst {
33	default:
34		return val
35	case '`':
36		old, new = "``", "`"
37	case '"':
38		old, new = `""`, `"`
39	case '\'':
40		old, new = `''`, `'`
41	}
42	return strings.ReplaceAll(rst, old, new)
43}
44
45// ParseBool parses a boolean.
46//
47// https://sqlite.org/pragma.html#syntax
48func ParseBool(s string) (b, ok bool) {
49	if len(s) == 0 {
50		return false, false
51	}
52	if s[0] == '0' {
53		return false, true
54	}
55	if '1' <= s[0] && s[0] <= '9' {
56		return true, true
57	}
58	switch strings.ToLower(s) {
59	case "true", "yes", "on":
60		return true, true
61	case "false", "no", "off":
62		return false, true
63	}
64	return false, false
65}