| 1 |
package json |
| 2 |
|
| 3 |
import ( |
| 4 |
"math/bits" |
| 5 |
"unsafe" |
| 6 |
) |
| 7 |
|
| 8 |
const ( |
| 9 |
lsb = 0x0101010101010101 |
| 10 |
msb = 0x8080808080808080 |
| 11 |
) |
| 12 |
|
| 13 |
// escapeIndex finds the index of the first char in `s` that requires escaping. |
| 14 |
// A char requires escaping if it's outside of the range of [0x20, 0x7F] or if |
| 15 |
// it includes a double quote or backslash. If the escapeHTML mode is enabled, |
| 16 |
// the chars <, > and & also require escaping. If no chars in `s` require |
| 17 |
// escaping, the return value is -1. |
| 18 |
func escapeIndex(s string, escapeHTML bool) int { |
| 19 |
chunks := stringToUint64(s) |
| 20 |
for _, n := range chunks { |
| 21 |
// combine masks before checking for the MSB of each byte. We include |
| 22 |
// `n` in the mask to check whether any of the *input* byte MSBs were |
| 23 |
// set (i.e. the byte was outside the ASCII range). |
| 24 |
mask := n | below(n, 0x20) | contains(n, '"') | contains(n, '\\') |
| 25 |
if escapeHTML { |
| 26 |
mask |= contains(n, '<') | contains(n, '>') | contains(n, '&') |
| 27 |
} |
| 28 |
if (mask & msb) != 0 { |
| 29 |
return bits.TrailingZeros64(mask&msb) / 8 |
| 30 |
} |
| 31 |
} |
| 32 |
|
| 33 |
for i := len(chunks) * 8; i < len(s); i++ { |
| 34 |
c := s[i] |
| 35 |
if c < 0x20 || c > 0x7f || c == '"' || c == '\\' || (escapeHTML && (c == '<' || c == '>' || c == '&')) { |
| 36 |
return i |
| 37 |
} |
| 38 |
} |
| 39 |
|
| 40 |
return -1 |
| 41 |
} |
| 42 |
|
| 43 |
func escapeByteRepr(b byte) byte { |
| 44 |
switch b { |
| 45 |
case '\\', '"': |
| 46 |
return b |
| 47 |
case '\b': |
| 48 |
return 'b' |
| 49 |
case '\f': |
| 50 |
return 'f' |
| 51 |
case '\n': |
| 52 |
return 'n' |
| 53 |
case '\r': |
| 54 |
return 'r' |
| 55 |
case '\t': |
| 56 |
return 't' |
| 57 |
} |
| 58 |
|
| 59 |
return 0 |
| 60 |
} |
| 61 |
|
| 62 |
// below return a mask that can be used to determine if any of the bytes |
| 63 |
// in `n` are below `b`. If a byte's MSB is set in the mask then that byte was |
| 64 |
// below `b`. The result is only valid if `b`, and each byte in `n`, is below |
| 65 |
// 0x80. |
| 66 |
func below(n uint64, b byte) uint64 { |
| 67 |
return n - expand(b) |
| 68 |
} |
| 69 |
|
| 70 |
// contains returns a mask that can be used to determine if any of the |
| 71 |
// bytes in `n` are equal to `b`. If a byte's MSB is set in the mask then |
| 72 |
// that byte is equal to `b`. The result is only valid if `b`, and each |
| 73 |
// byte in `n`, is below 0x80. |
| 74 |
func contains(n uint64, b byte) uint64 { |
| 75 |
return (n ^ expand(b)) - lsb |
| 76 |
} |
| 77 |
|
| 78 |
// expand puts the specified byte into each of the 8 bytes of a uint64. |
| 79 |
func expand(b byte) uint64 { |
| 80 |
return lsb * uint64(b) |
| 81 |
} |
| 82 |
|
| 83 |
func stringToUint64(s string) []uint64 { |
| 84 |
return *(*[]uint64)(unsafe.Pointer(&sliceHeader{ |
| 85 |
Data: *(*unsafe.Pointer)(unsafe.Pointer(&s)), |
| 86 |
Len: len(s) / 8, |
| 87 |
Cap: len(s) / 8, |
| 88 |
})) |
| 89 |
} |