| 1 | package torutil |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "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 PartitionString(str string, ch byte) (string, string, bool) { |
| 12 | index := 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 PartitionStringFromEnd(str string, ch byte) (string, string, bool) { |
| 22 | index := 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 EscapeSimpleQuotedStringIfNeeded(str string) string { |
| 33 | if strings.ContainsAny(str, " \\\"\r\n") { |
| 34 | return EscapeSimpleQuotedString(str) |
| 35 | } |
| 36 | return str |
| 37 | } |
| 38 | |
| 39 | var simpleQuotedStringEscapeReplacer = 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 EscapeSimpleQuotedString(str string) string { |
| 49 | return "\"" + EscapeSimpleQuotedStringContents(str) + "\"" |
| 50 | } |
| 51 | |
| 52 | // EscapeSimpleQuotedStringContents escapes backslashes, double quotes, |
| 53 | // newlines, and carriage returns in str. |
| 54 | func EscapeSimpleQuotedStringContents(str string) string { |
| 55 | return simpleQuotedStringEscapeReplacer.Replace(str) |
| 56 | } |
| 57 | |
| 58 | // UnescapeSimpleQuotedStringIfNeeded calls UnescapeSimpleQuotedString only if |
| 59 | // str is surrounded with double quotes. |
| 60 | func UnescapeSimpleQuotedStringIfNeeded(str string) (string, error) { |
| 61 | if len(str) >= 2 && str[0] == '"' && str[len(str)-1] == '"' { |
| 62 | return UnescapeSimpleQuotedString(str) |
| 63 | } |
| 64 | return str, nil |
| 65 | } |
| 66 | |
| 67 | // UnescapeSimpleQuotedString removes surrounding double quotes and calls |
| 68 | // UnescapeSimpleQuotedStringContents. |
| 69 | func UnescapeSimpleQuotedString(str string) (string, error) { |
| 70 | if len(str) < 2 || str[0] != '"' || str[len(str)-1] != '"' { |
| 71 | return "", fmt.Errorf("Missing quotes") |
| 72 | } |
| 73 | return 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 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 "", fmt.Errorf("Unescaped quote") |
| 91 | } |
| 92 | ret += "\"" |
| 93 | escaping = false |
| 94 | case '\r', '\n': |
| 95 | return "", 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 "", fmt.Errorf("Unexpected escape") |
| 104 | } |
| 105 | } else { |
| 106 | ret += string(c) |
| 107 | } |
| 108 | escaping = false |
| 109 | } |
| 110 | } |
| 111 | return ret, nil |
| 112 | } |