AppendJSONString appends json-encoded string s to dst and returns the result. If addQuotes is true, then the appended json string is wrapped into double quotes.
(dst []byte, s string, addQuotes bool)
| 22 | // |
| 23 | // If addQuotes is true, then the appended json string is wrapped into double quotes. |
| 24 | func AppendJSONString(dst []byte, s string, addQuotes bool) []byte { |
| 25 | if !hasSpecialChars(s) { |
| 26 | // Fast path - nothing to escape. |
| 27 | if !addQuotes { |
| 28 | return append(dst, s...) |
| 29 | } |
| 30 | dst = append(dst, '"') |
| 31 | dst = append(dst, s...) |
| 32 | dst = append(dst, '"') |
| 33 | return dst |
| 34 | } |
| 35 | |
| 36 | // Slow path - there are chars to escape. |
| 37 | if addQuotes { |
| 38 | dst = append(dst, '"') |
| 39 | } |
| 40 | dst = jsonReplacer.AppendReplace(dst, s) |
| 41 | if addQuotes { |
| 42 | dst = append(dst, '"') |
| 43 | } |
| 44 | return dst |
| 45 | } |
| 46 | |
| 47 | var jsonReplacer = newByteReplacer(func() ([]byte, []string) { |
| 48 | oldChars := []byte("\n\r\t\b\f\"\\<'") |
searching dependent graphs…