MCPcopy Index your code
hub / github.com/segmentio/encoding / escapeIndex

Function escapeIndex

json/string.go:18–41  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

16// the chars <, > and & also require escaping. If no chars in `s` require
17// escaping, the return value is -1.
18func 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
43func escapeByteRepr(b byte) byte {
44 switch b {

Callers 2

benchmarkEscapeIndexFunction · 0.85
encodeStringMethod · 0.85

Calls 3

stringToUint64Function · 0.85
belowFunction · 0.85
containsFunction · 0.85

Tested by 1

benchmarkEscapeIndexFunction · 0.68

Used in the wild real call sites across dependent graphs

searching dependent graphs…