escapeIndex finds the index of the first char in `s` that requires escaping. A char requires escaping if it's outside of the range of [0x20, 0x7F] or if it includes a double quote or backslash. If the escapeHTML mode is enabled, the chars <, > and & also require escaping. If no chars in `s` require
(s string, escapeHTML bool)
| 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 { |
searching dependent graphs…