File tree Expand file tree Collapse file tree 3 files changed +26
-3
lines changed Expand file tree Collapse file tree 3 files changed +26
-3
lines changed Original file line number Diff line number Diff line change @@ -40,6 +40,13 @@ for (let i = 0; i < SHORT_ARRAY_SIZE; i++) {
40
40
}
41
41
42
42
const benchmarks = [
43
+ {
44
+ name : 'empty string' ,
45
+ schema : {
46
+ type : 'string'
47
+ } ,
48
+ input : ''
49
+ } ,
43
50
{
44
51
name : 'short string' ,
45
52
schema : {
Original file line number Diff line number Diff line change 1
1
'use strict'
2
2
3
3
// eslint-disable-next-line
4
- const STR_ESCAPE = / [ \u0000 - \u001f \u0022 \u005c \ud800 - \udfff ] /
4
+ const ASCII_ESCAPE = / [ \u0000 - \u001f \u0022 \u005c ] /
5
5
6
6
module . exports = class Serializer {
7
7
constructor ( options ) {
@@ -92,6 +92,7 @@ module.exports = class Serializer {
92
92
93
93
asString ( str ) {
94
94
const len = str . length
95
+
95
96
if ( len === 0 ) {
96
97
return '""'
97
98
} else if ( len < 42 ) {
@@ -113,13 +114,13 @@ module.exports = class Serializer {
113
114
last === - 1 && ( last = 0 )
114
115
result += str . slice ( last , i ) + '\\'
115
116
last = i
116
- } else if ( point < 32 || ( point >= 0xD800 && point <= 0xDFFF ) ) {
117
+ } else if ( point < 32 || ( point >= 0xd800 && point <= 0xdfff ) ) {
117
118
// The current character is non-printable characters or a surrogate.
118
119
return JSON . stringify ( str )
119
120
}
120
121
}
121
122
return ( last === - 1 && ( '"' + str + '"' ) ) || ( '"' + result + str . slice ( last ) + '"' )
122
- } else if ( len < 5000 && STR_ESCAPE . test ( str ) === false ) {
123
+ } else if ( len < 5000 && str . isWellFormed ( ) && ASCII_ESCAPE . test ( str ) === false ) {
123
124
// Only use the regular expression for shorter input. The overhead is otherwise too much.
124
125
return '"' + str + '"'
125
126
} else {
Original file line number Diff line number Diff line change @@ -34,6 +34,21 @@ test('serialize short string', (t) => {
34
34
t . assert . equal ( JSON . parse ( output ) , input )
35
35
} )
36
36
37
+ test ( 'serialize medium string' , ( t ) => {
38
+ t . plan ( 2 )
39
+
40
+ const schema = {
41
+ type : 'string'
42
+ }
43
+
44
+ const input = new Array ( 150 ) . fill ( '\x00' ) . join ( '' )
45
+ const stringify = build ( schema )
46
+ const output = stringify ( input )
47
+
48
+ t . assert . equal ( output , `"${ new Array ( 150 ) . fill ( '\\u0000' ) . join ( '' ) } "` )
49
+ t . assert . equal ( JSON . parse ( output ) , input )
50
+ } )
51
+
37
52
test ( 'serialize long string' , ( t ) => {
38
53
t . plan ( 2 )
39
54
You can’t perform that action at this time.
0 commit comments