Skip to content

Commit 3b2e8a2

Browse files
authored
feat: unescape strings on parse to remove race (#3)
1 parent bb15f94 commit 3b2e8a2

File tree

2 files changed

+7
-16
lines changed

2 files changed

+7
-16
lines changed

arena.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,11 @@ func (a *Arena) NewArray() *Value {
5757
// The returned string is valid until Reset is called on a.
5858
func (a *Arena) NewString(s string) *Value {
5959
v := a.c.getValue()
60-
v.t = typeRawString
60+
v.t = TypeString
6161
bLen := len(a.b)
6262
a.b = escapeString(a.b, s)
6363
v.s = b2s(a.b[bLen+1 : len(a.b)-1])
64+
v.s = unescapeStringBestEffort(v.s)
6465
return v
6566
}
6667

@@ -69,10 +70,11 @@ func (a *Arena) NewString(s string) *Value {
6970
// The returned string is valid until Reset is called on a.
7071
func (a *Arena) NewStringBytes(b []byte) *Value {
7172
v := a.c.getValue()
72-
v.t = typeRawString
73+
v.t = TypeString
7374
bLen := len(a.b)
7475
a.b = escapeString(a.b, b2s(b))
7576
v.s = b2s(a.b[bLen+1 : len(a.b)-1])
77+
v.s = unescapeStringBestEffort(v.s)
7678
return v
7779
}
7880

parser.go

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,8 @@ func parseValue(s string, c *cache, depth int) (*Value, string, error) {
131131
return nil, tail, fmt.Errorf("cannot parse string: %s", err)
132132
}
133133
v := c.getValue()
134-
v.t = typeRawString
135-
v.s = ss
134+
v.t = TypeString
135+
v.s = unescapeStringBestEffort(ss)
136136
return v, tail, nil
137137
}
138138
if s[0] == 't' {
@@ -326,7 +326,7 @@ func escapeStringSlowPath(dst []byte, s string) []byte {
326326
dst = append(dst, []byte{'\\', 'u', '0', '0', '0', 0x57 + c}...)
327327
case c < 0x1a:
328328
dst = append(dst, []byte{'\\', 'u', '0', '0', '1', 0x20 + c}...)
329-
case c < 0x20:
329+
case c < 0x20: // lint:ignore
330330
dst = append(dst, []byte{'\\', 'u', '0', '0', '1', 0x47 + c}...)
331331
}
332332
}
@@ -618,11 +618,6 @@ type Value struct {
618618
// MarshalTo appends marshaled v to dst and returns the result.
619619
func (v *Value) MarshalTo(dst []byte) []byte {
620620
switch v.t {
621-
case typeRawString:
622-
dst = append(dst, '"')
623-
dst = append(dst, v.s...)
624-
dst = append(dst, '"')
625-
return dst
626621
case TypeObject:
627622
return v.o.MarshalTo(dst)
628623
case TypeArray:
@@ -688,8 +683,6 @@ const (
688683

689684
// TypeFalse is JSON false.
690685
TypeFalse Type = 6
691-
692-
typeRawString Type = 7
693686
)
694687

695688
// String returns string representation of t.
@@ -719,10 +712,6 @@ func (t Type) String() string {
719712

720713
// Type returns the type of the v.
721714
func (v *Value) Type() Type {
722-
if v.t == typeRawString {
723-
v.s = unescapeStringBestEffort(v.s)
724-
v.t = TypeString
725-
}
726715
return v.t
727716
}
728717

0 commit comments

Comments
 (0)