mirror of
https://github.com/json-iterator/go.git
synced 2025-04-23 11:37:32 +02:00
fix unicode and escape
This commit is contained in:
parent
caaa04195e
commit
ebed7df895
@ -232,7 +232,7 @@ func (stream *Stream) WriteStringWithHtmlEscaped(s string) {
|
|||||||
i := 0
|
i := 0
|
||||||
for ; i < toWriteLen; i++ {
|
for ; i < toWriteLen; i++ {
|
||||||
c := s[i]
|
c := s[i]
|
||||||
if c <= utf8.RuneSelf && htmlSafeSet[c] {
|
if c < utf8.RuneSelf && htmlSafeSet[c] {
|
||||||
stream.buf[n] = c
|
stream.buf[n] = c
|
||||||
n++
|
n++
|
||||||
} else {
|
} else {
|
||||||
@ -252,7 +252,7 @@ func (stream *Stream) WriteStringWithHtmlEscaped(s string) {
|
|||||||
func writeStringSlowPathWithHtmlEscaped(stream *Stream, i int, s string, valLen int) {
|
func writeStringSlowPathWithHtmlEscaped(stream *Stream, i int, s string, valLen int) {
|
||||||
start := i
|
start := i
|
||||||
// for the remaining parts, we process them char by char
|
// for the remaining parts, we process them char by char
|
||||||
for ; i < valLen; i++ {
|
for ; i < valLen; {
|
||||||
if b := s[i]; b < utf8.RuneSelf {
|
if b := s[i]; b < utf8.RuneSelf {
|
||||||
if htmlSafeSet[b] {
|
if htmlSafeSet[b] {
|
||||||
i++
|
i++
|
||||||
@ -351,7 +351,7 @@ func (stream *Stream) WriteString(s string) {
|
|||||||
func writeStringSlowPath(stream *Stream, i int, s string, valLen int) {
|
func writeStringSlowPath(stream *Stream, i int, s string, valLen int) {
|
||||||
start := i
|
start := i
|
||||||
// for the remaining parts, we process them char by char
|
// for the remaining parts, we process them char by char
|
||||||
for ; i < valLen; i++ {
|
for ; i < valLen; {
|
||||||
if b := s[i]; b < utf8.RuneSelf {
|
if b := s[i]; b < utf8.RuneSelf {
|
||||||
if safeSet[b] {
|
if safeSet[b] {
|
||||||
i++
|
i++
|
||||||
@ -382,10 +382,7 @@ func writeStringSlowPath(stream *Stream, i int, s string, valLen int) {
|
|||||||
start = i
|
start = i
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if start < i {
|
i++
|
||||||
stream.WriteRaw(s[start:i])
|
|
||||||
}
|
|
||||||
start = i
|
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if start < len(s) {
|
if start < len(s) {
|
||||||
|
@ -144,6 +144,26 @@ func Test_unicode(t *testing.T) {
|
|||||||
should.Equal(`{"a":"数字山谷"}`, output)
|
should.Equal(`{"a":"数字山谷"}`, output)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func Test_unicode_and_escape(t *testing.T) {
|
||||||
|
should := require.New(t)
|
||||||
|
output , err := MarshalToString(`"数字山谷"`)
|
||||||
|
should.Nil(err)
|
||||||
|
should.Equal(`"\"数字山谷\""`, output)
|
||||||
|
output , err = ConfigFastest.MarshalToString(`"数字山谷"`)
|
||||||
|
should.Nil(err)
|
||||||
|
should.Equal(`"\"数字山谷\""`, output)
|
||||||
|
}
|
||||||
|
|
||||||
|
func Test_unsafe_unicode(t *testing.T) {
|
||||||
|
should := require.New(t)
|
||||||
|
output , err := MarshalToString("he\u2029\u2028he")
|
||||||
|
should.Nil(err)
|
||||||
|
should.Equal(`"he\u2029\u2028he"`, output)
|
||||||
|
output , err = ConfigFastest.MarshalToString("he\u2029\u2028he")
|
||||||
|
should.Nil(err)
|
||||||
|
should.Equal("\"he\u2029\u2028he\"", output)
|
||||||
|
}
|
||||||
|
|
||||||
func Benchmark_jsoniter_unicode(b *testing.B) {
|
func Benchmark_jsoniter_unicode(b *testing.B) {
|
||||||
for n := 0; n < b.N; n++ {
|
for n := 0; n < b.N; n++ {
|
||||||
iter := ParseString(ConfigDefault, `"\ud83d\udc4a"`)
|
iter := ParseString(ConfigDefault, `"\ud83d\udc4a"`)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user