Comparing version 0.0.4 to 0.0.5
@@ -78,2 +78,5 @@ /*global Buffer*/ | ||
this.state = VALUE; | ||
this.bytes_remaining = 0; // number of bytes remaining in multi byte utf8 char to read after split boundary | ||
this.bytes_in_sequence = 0; // bytes in multi byte utf8 char to read | ||
this.temp_buffs = { "2": new Buffer(2), "3": new Buffer(3), "4": new Buffer(4) }; // for rebuilding chars split before boundary is reached | ||
} | ||
@@ -113,7 +116,26 @@ var proto = Parser.prototype; | ||
}else if (this.tState === STRING1){ // After open quote | ||
n = buffer[i]; | ||
if (n >= 128) { | ||
for (var j = i; buffer[j] >= 128 && j < buffer.length; j++); | ||
this.string += buffer.slice(i, j).toString(); | ||
i = j - 1; | ||
n = buffer[i]; // get current byte from buffer | ||
// check for carry over of a multi byte char split between data chunks | ||
// & fill temp buffer it with start of this data chunk up to the boundary limit set in the last iteration | ||
if (this.bytes_remaining > 0) { | ||
for (var j = 0; j < this.bytes_remaining; j++) { | ||
this.temp_buffs[this.bytes_in_sequence][this.bytes_in_sequence - this.bytes_remaining + j] = buffer[j]; | ||
} | ||
this.string += this.temp_buffs[this.bytes_in_sequence].toString(); | ||
this.bytes_in_sequence = this.bytes_remaining = 0; | ||
i = i + j - 1; | ||
} else if (this.bytes_remaining === 0 && n >= 128) { // else if no remainder bytes carried over, parse multi byte (>=128) chars one at a time | ||
if ((n >= 194) && (n <= 223)) this.bytes_in_sequence = 2; | ||
if ((n >= 224) && (n <= 239)) this.bytes_in_sequence = 3; | ||
if ((n >= 240) && (n <= 244)) this.bytes_in_sequence = 4; | ||
if ((this.bytes_in_sequence + i) > buffer.length) { // if bytes needed to complete char fall outside buffer length, we have a boundary split | ||
for (var k = 0; k <= (buffer.length - 1 - i); k++) { | ||
this.temp_buffs[this.bytes_in_sequence][k] = buffer[i + k]; // fill temp buffer of correct size with bytes available in this chunk | ||
} | ||
this.bytes_remaining = (i + this.bytes_in_sequence) - buffer.length; | ||
i = buffer.length - 1; | ||
} else { | ||
this.string += buffer.slice(i, (i + this.bytes_in_sequence)).toString(); | ||
i = i + this.bytes_in_sequence - 1; | ||
} | ||
} else if (n === 0x22) { this.tState = START; this.onToken(STRING, this.string); this.string = undefined; } | ||
@@ -120,0 +142,0 @@ else if (n === 0x5c) { this.tState = STRING2; } |
@@ -5,3 +5,3 @@ { | ||
"tags": ["json", "stream"], | ||
"version": "0.0.4", | ||
"version": "0.0.5", | ||
"author": "Tim Caswell <tim@creationix.com>", | ||
@@ -8,0 +8,0 @@ "repository": { |
33480
11
951