Debian Codegraph @

torutil/string.go

raw symbols SCIP
1 package torutilscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/torutil`/
2
3 import (
4 "fmtscip-go gomod github.com/golang/go/src go1.15 fmt/"
5 "stringsscip-go gomod github.com/golang/go/src go1.15 strings/"
6 )
7
8 // PartitionString returns the two parts of a string delimited by the first
9 // occurrence of ch. If ch does not exist, the second string is empty and the
10 // resulting bool is false. Otherwise it is true.
11 func PartitionStringscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/torutil`/PartitionString().(str string, ch byte) (string, string, bool) {
12 index := stringsscip-go gomod github.com/golang/go/src go1.15 strings/.IndexBytescip-go gomod github.com/golang/go/src go1.15 strings/IndexByte().(str, ch)
13 if index == -1 {
14 return str, "", false
15 }
16 return str[:index], str[index+1:], true
17 }
18
19 // PartitionStringFromEnd is same as PartitionString except it delimts by the
20 // last occurrence of ch instead of the first.
21 func PartitionStringFromEndscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/torutil`/PartitionStringFromEnd().(str string, ch byte) (string, string, bool) {
22 index := stringsscip-go gomod github.com/golang/go/src go1.15 strings/.LastIndexBytescip-go gomod github.com/golang/go/src go1.15 strings/LastIndexByte().(str, ch)
23 if index == -1 {
24 return str, "", false
25 }
26 return str[:index], str[index+1:], true
27 }
28
29 // EscapeSimpleQuotedStringIfNeeded calls EscapeSimpleQuotedString only if the
30 // string contains a space, backslash, double quote, newline, or carriage return
31 // character.
32 func EscapeSimpleQuotedStringIfNeededscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/torutil`/EscapeSimpleQuotedStringIfNeeded().(str string) string {
33 if stringsscip-go gomod github.com/golang/go/src go1.15 strings/.ContainsAnyscip-go gomod github.com/golang/go/src go1.15 strings/ContainsAny().(str, " \\\"\r\n") {
34 return EscapeSimpleQuotedStringscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/torutil`/EscapeSimpleQuotedString().(str)
35 }
36 return str
37 }
38
39 var simpleQuotedStringEscapeReplacerscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/torutil`/simpleQuotedStringEscapeReplacer. = stringsscip-go gomod github.com/golang/go/src go1.15 strings/.NewReplacerscip-go gomod github.com/golang/go/src go1.15 strings/NewReplacer().(
40 "\\", "\\\\",
41 "\"", "\\\"",
42 "\r", "\\r",
43 "\n", "\\n",
44 )
45
46 // EscapeSimpleQuotedString calls EscapeSimpleQuotedStringContents and then
47 // surrounds the entire string with double quotes.
48 func EscapeSimpleQuotedStringscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/torutil`/EscapeSimpleQuotedString().(str string) string {
49 return "\"" + EscapeSimpleQuotedStringContentsscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/torutil`/EscapeSimpleQuotedStringContents().(str) + "\""
50 }
51
52 // EscapeSimpleQuotedStringContents escapes backslashes, double quotes,
53 // newlines, and carriage returns in str.
54 func EscapeSimpleQuotedStringContentsscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/torutil`/EscapeSimpleQuotedStringContents().(str string) string {
55 return simpleQuotedStringEscapeReplacerscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/torutil`/simpleQuotedStringEscapeReplacer..Replacescip-go gomod github.com/golang/go/src go1.15 strings/Replacer#Replace().(str)
56 }
57
58 // UnescapeSimpleQuotedStringIfNeeded calls UnescapeSimpleQuotedString only if
59 // str is surrounded with double quotes.
60 func UnescapeSimpleQuotedStringIfNeededscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/torutil`/UnescapeSimpleQuotedStringIfNeeded().(str string) (string, error) {
61 if len(str) >= 2 && str[0] == '"' && str[len(str)-1] == '"' {
62 return UnescapeSimpleQuotedStringscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/torutil`/UnescapeSimpleQuotedString().(str)
63 }
64 return str, nil
65 }
66
67 // UnescapeSimpleQuotedString removes surrounding double quotes and calls
68 // UnescapeSimpleQuotedStringContents.
69 func UnescapeSimpleQuotedStringscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/torutil`/UnescapeSimpleQuotedString().(str string) (string, error) {
70 if len(str) < 2 || str[0] != '"' || str[len(str)-1] != '"' {
71 return "", fmtscip-go gomod github.com/golang/go/src go1.15 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.15 fmt/Errorf().("Missing quotes")
72 }
73 return UnescapeSimpleQuotedStringContentsscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/torutil`/UnescapeSimpleQuotedStringContents().(str[1 : len(str)-1])
74 }
75
76 // UnescapeSimpleQuotedStringContents unescapes backslashes, double quotes,
77 // newlines, and carriage returns. Also errors if those aren't escaped.
78 func UnescapeSimpleQuotedStringContentsscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/torutil`/UnescapeSimpleQuotedStringContents().(str string) (string, error) {
79 ret := ""
80 escaping := false
81 for _, c := range str {
82 switch c {
83 case '\\':
84 if escaping {
85 ret += "\\"
86 }
87 escaping = !escaping
88 case '"':
89 if !escaping {
90 return "", fmtscip-go gomod github.com/golang/go/src go1.15 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.15 fmt/Errorf().("Unescaped quote")
91 }
92 ret += "\""
93 escaping = false
94 case '\r', '\n':
95 return "", fmtscip-go gomod github.com/golang/go/src go1.15 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.15 fmt/Errorf().("Unescaped newline or carriage return")
96 default:
97 if escaping {
98 if c == 'r' {
99 ret += "\r"
100 } else if c == 'n' {
101 ret += "\n"
102 } else {
103 return "", fmtscip-go gomod github.com/golang/go/src go1.15 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.15 fmt/Errorf().("Unexpected escape")
104 }
105 } else {
106 ret += string(c)
107 }
108 escaping = false
109 }
110 }
111 return ret, nil
112 }