forked from mhr3/jsoniter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rawstring.go
61 lines (53 loc) · 1.25 KB
/
rawstring.go
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
package jsoniter
type RawString struct {
buf []byte
isRaw bool
hasEscapes bool
}
func (r *RawString) IsNil() bool {
return r.buf == nil
}
// Realize turns a direct view buffer into a copy.
func (r *RawString) Realize() {
if r.isRaw {
bufCpy := make([]byte, len(r.buf))
copy(bufCpy, r.buf)
r.buf = bufCpy
r.isRaw = false
}
}
// String decodes escape sequences and returns the string.
func (r *RawString) String() string {
if r.buf == nil {
return ""
}
if !r.hasEscapes {
return string(r.buf[:len(r.buf)-1])
}
iter := Iterator{
buf: r.buf,
tail: len(r.buf),
}
res := iter.readStringInner()
if iter.Error != nil {
// should never happen
panic(iter.Error)
}
return res
}
// Bytes returns a buffer and true if this is a direct view into the iterator,
// or false if the buffer is a copy.
// Note that a direct view buffer is only valid until the next read
// from the iterator. Use Realize before reading further from the iterator
// to preserve the contents.
func (r *RawString) Bytes() ([]byte, bool) {
raw := r.buf
if len(raw) > 0 {
raw = raw[:len(raw)-1]
}
return raw, r.isRaw
}
// ContainsEscapes returns true if the string contains escape sequences.
func (r *RawString) ContainsEscapes() bool {
return r.hasEscapes
}