From ebed7df89503b43be3ce41dea44f401b8ea47493 Mon Sep 17 00:00:00 2001 From: Tao Wen Date: Thu, 22 Jun 2017 16:00:47 +0800 Subject: [PATCH] fix unicode and escape --- feature_stream_string.go | 11 ++++------- jsoniter_string_test.go | 20 ++++++++++++++++++++ 2 files changed, 24 insertions(+), 7 deletions(-) diff --git a/feature_stream_string.go b/feature_stream_string.go index 0e052ae..fdd8b9e 100644 --- a/feature_stream_string.go +++ b/feature_stream_string.go @@ -232,7 +232,7 @@ func (stream *Stream) WriteStringWithHtmlEscaped(s string) { i := 0 for ; i < toWriteLen; i++ { c := s[i] - if c <= utf8.RuneSelf && htmlSafeSet[c] { + if c < utf8.RuneSelf && htmlSafeSet[c] { stream.buf[n] = c n++ } else { @@ -252,7 +252,7 @@ func (stream *Stream) WriteStringWithHtmlEscaped(s string) { func writeStringSlowPathWithHtmlEscaped(stream *Stream, i int, s string, valLen int) { start := i // 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 htmlSafeSet[b] { i++ @@ -351,7 +351,7 @@ func (stream *Stream) WriteString(s string) { func writeStringSlowPath(stream *Stream, i int, s string, valLen int) { start := i // 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 safeSet[b] { i++ @@ -382,10 +382,7 @@ func writeStringSlowPath(stream *Stream, i int, s string, valLen int) { start = i continue } - if start < i { - stream.WriteRaw(s[start:i]) - } - start = i + i++ continue } if start < len(s) { diff --git a/jsoniter_string_test.go b/jsoniter_string_test.go index 22ae441..aa398f7 100644 --- a/jsoniter_string_test.go +++ b/jsoniter_string_test.go @@ -144,6 +144,26 @@ func Test_unicode(t *testing.T) { 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) { for n := 0; n < b.N; n++ { iter := ParseString(ConfigDefault, `"\ud83d\udc4a"`)