Skip to content

Commit f9d54bc

Browse files
author
Michael Hayes
committed
add char code constants
1 parent ac45580 commit f9d54bc

2 files changed

Lines changed: 16 additions & 8 deletions

File tree

jsonparse.js

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,14 @@ var KEY = C.KEY = 0x72;
4040
// Parser Modes
4141
var OBJECT = C.OBJECT = 0x81;
4242
var ARRAY = C.ARRAY = 0x82;
43+
// Character constants
44+
var BACK_SLASH = "\\".charCodeAt(0);
45+
var FORWARD_SLASH = "\/".charCodeAt(0);
46+
var BACKSPACE = "\b".charCodeAt(0);
47+
var FORM_FEED = "\f".charCodeAt(0);
48+
var NEWLINE = "\n".charCodeAt(0);
49+
var CARRIAGE_RETURN = "\r".charCodeAt(0);
50+
var TAB = "\t".charCodeAt(0);
4351

4452
var STRING_BUFFER_SIZE = 64 * 1024;
4553

@@ -184,13 +192,13 @@ proto.write = function (buffer) {
184192
}else if (this.tState === STRING2){ // After backslash
185193
n = buffer[i];
186194
if(n === 0x22){ this.appendStringChar(n); this.tState = STRING1;
187-
}else if(n === 0x5c){ this.appendStringChar("\\".charCodeAt(0)); this.tState = STRING1;
188-
}else if(n === 0x2f){ this.appendStringChar("\/".charCodeAt(0)); this.tState = STRING1;
189-
}else if(n === 0x62){ this.appendStringChar("\b".charCodeAt(0)); this.tState = STRING1;
190-
}else if(n === 0x66){ this.appendStringChar("\f".charCodeAt(0)); this.tState = STRING1;
191-
}else if(n === 0x6e){ this.appendStringChar("\n".charCodeAt(0)); this.tState = STRING1;
192-
}else if(n === 0x72){ this.appendStringChar("\r".charCodeAt(0)); this.tState = STRING1;
193-
}else if(n === 0x74){ this.appendStringChar("\t".charCodeAt(0)); this.tState = STRING1;
195+
}else if(n === 0x5c){ this.appendStringChar(BACK_SLASH); this.tState = STRING1;
196+
}else if(n === 0x2f){ this.appendStringChar(FORWARD_SLASH); this.tState = STRING1;
197+
}else if(n === 0x62){ this.appendStringChar(BACKSPACE); this.tState = STRING1;
198+
}else if(n === 0x66){ this.appendStringChar(FORM_FEED); this.tState = STRING1;
199+
}else if(n === 0x6e){ this.appendStringChar(NEWLINE); this.tState = STRING1;
200+
}else if(n === 0x72){ this.appendStringChar(CARRIAGE_RETURN); this.tState = STRING1;
201+
}else if(n === 0x74){ this.appendStringChar(TAB); this.tState = STRING1;
194202
}else if(n === 0x75){ this.unicode = ""; this.tState = STRING3;
195203
}else{
196204
return this.charError(buffer, i);

test/big-token.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ var test = require('tape');
44

55
test('can handle large tokens without running out of memory', function (t) {
66
var parser = new JsonParse();
7-
var chunkSize = 1024
7+
var chunkSize = 1024;
88
var chunks = 1024 * 200; // 200mb
99
var quote = Buffer.from ? Buffer.from('"') : new Buffer('"');
1010
t.plan(1);

0 commit comments

Comments
 (0)