summaryrefslogtreecommitdiff
path: root/vendor/github.com/cretz/bine/torutil/string.go
blob: 86cee96e17c5dad18910818e0ec5d77601d90844 (plain)
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package torutil

import (
	"fmt"
	"strings"
)

// PartitionString returns the two parts of a string delimited by the first
// occurrence of ch. If ch does not exist, the second string is empty and the
// resulting bool is false. Otherwise it is true.
func PartitionString(str string, ch byte) (string, string, bool) {
	index := strings.IndexByte(str, ch)
	if index == -1 {
		return str, "", false
	}
	return str[:index], str[index+1:], true
}

// PartitionStringFromEnd is same as PartitionString except it delimts by the
// last occurrence of ch instead of the first.
func PartitionStringFromEnd(str string, ch byte) (string, string, bool) {
	index := strings.LastIndexByte(str, ch)
	if index == -1 {
		return str, "", false
	}
	return str[:index], str[index+1:], true
}

// EscapeSimpleQuotedStringIfNeeded calls EscapeSimpleQuotedString only if the
// string contains a space, backslash, double quote, newline, or carriage return
// character.
func EscapeSimpleQuotedStringIfNeeded(str string) string {
	if strings.ContainsAny(str, " \\\"\r\n") {
		return EscapeSimpleQuotedString(str)
	}
	return str
}

var simpleQuotedStringEscapeReplacer = strings.NewReplacer(
	"\\", "\\\\",
	"\"", "\\\"",
	"\r", "\\r",
	"\n", "\\n",
)

// EscapeSimpleQuotedString calls EscapeSimpleQuotedStringContents and then
// surrounds the entire string with double quotes.
func EscapeSimpleQuotedString(str string) string {
	return "\"" + EscapeSimpleQuotedStringContents(str) + "\""
}

// EscapeSimpleQuotedStringContents escapes backslashes, double quotes,
// newlines, and carriage returns in str.
func EscapeSimpleQuotedStringContents(str string) string {
	return simpleQuotedStringEscapeReplacer.Replace(str)
}

// UnescapeSimpleQuotedStringIfNeeded calls UnescapeSimpleQuotedString only if
// str is surrounded with double quotes.
func UnescapeSimpleQuotedStringIfNeeded(str string) (string, error) {
	if len(str) >= 2 && str[0] == '"' && str[len(str)-1] == '"' {
		return UnescapeSimpleQuotedString(str)
	}
	return str, nil
}

// UnescapeSimpleQuotedString removes surrounding double quotes and calls
// UnescapeSimpleQuotedStringContents.
func UnescapeSimpleQuotedString(str string) (string, error) {
	if len(str) < 2 || str[0] != '"' || str[len(str)-1] != '"' {
		return "", fmt.Errorf("Missing quotes")
	}
	return UnescapeSimpleQuotedStringContents(str[1 : len(str)-1])
}

// UnescapeSimpleQuotedStringContents unescapes backslashes, double quotes,
// newlines, and carriage returns. Also errors if those aren't escaped.
func UnescapeSimpleQuotedStringContents(str string) (string, error) {
	ret := ""
	escaping := false
	for _, c := range str {
		switch c {
		case '\\':
			if escaping {
				ret += "\\"
			}
			escaping = !escaping
		case '"':
			if !escaping {
				return "", fmt.Errorf("Unescaped quote")
			}
			ret += "\""
			escaping = false
		case '\r', '\n':
			return "", fmt.Errorf("Unescaped newline or carriage return")
		default:
			if escaping {
				if c == 'r' {
					ret += "\r"
				} else if c == 'n' {
					ret += "\n"
				} else {
					return "", fmt.Errorf("Unexpected escape")
				}
			} else {
				ret += string(c)
			}
			escaping = false
		}
	}
	return ret, nil
}