-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquote.go
More file actions
78 lines (73 loc) · 1.87 KB
/
quote.go
File metadata and controls
78 lines (73 loc) · 1.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package fmt
// Quote wraps a string in double quotes and escapes any special characters
// Example: Quote("hello \"world\"") returns "\"hello \\\"world\\\"\""
func (c *Conv) Quote() *Conv {
if c.hasContent(BuffErr) {
return c // Error chain interruption
}
if c.outLen == 0 {
c.ResetBuffer(BuffOut)
c.WrString(BuffOut, quoteStr)
return c
}
// Use work buffer to build quoted string, then swap to output
c.ResetBuffer(BuffWork)
c.wrByte(BuffWork, '"')
// Process buffer directly without string allocation (like capitalizeASCIIOptimized)
for i := 0; i < c.outLen; i++ {
char := c.out[i]
switch char {
case '"':
c.wrByte(BuffWork, '\\')
c.wrByte(BuffWork, '"')
case '\\':
c.wrByte(BuffWork, '\\')
c.wrByte(BuffWork, '\\')
case '\n':
c.wrByte(BuffWork, '\\')
c.wrByte(BuffWork, 'n')
case '\r':
c.wrByte(BuffWork, '\\')
c.wrByte(BuffWork, 'r')
case '\t':
c.wrByte(BuffWork, '\\')
c.wrByte(BuffWork, 't')
default:
c.wrByte(BuffWork, char)
}
}
c.wrByte(BuffWork, '"')
c.swapBuff(BuffWork, BuffOut)
return c
}
// JSONEscape writes s to b with JSON string escaping (without surrounding quotes).
// Escapes: " → \", \ → \\, newline → \n, carriage return → \r, tab → \t,
// control chars (< 0x20) → \u00XX.
//
// The caller is responsible for writing the surrounding double quotes.
// This design allows the caller to compose JSON strings without extra allocations.
func JSONEscape(s string, b *Builder) {
for i := 0; i < len(s); i++ {
c := s[i]
switch c {
case '"':
b.WriteString(`\"`)
case '\\':
b.WriteString(`\\`)
case '\n':
b.WriteString(`\n`)
case '\r':
b.WriteString(`\r`)
case '\t':
b.WriteString(`\t`)
default:
if c < 0x20 {
b.WriteString(`\u00`)
_ = b.WriteByte("0123456789abcdef"[c>>4])
_ = b.WriteByte("0123456789abcdef"[c&0xf])
} else {
_ = b.WriteByte(c)
}
}
}
}