mirror of
https://github.com/fluencelabs/assemblyscript-json
synced 2025-07-02 07:51:40 +00:00
Merge pull request #4 from nearprotocol/improve-string-handling
Improve performance of assembly JSON by improving string handling
This commit is contained in:
@ -196,16 +196,21 @@ export class JSONDecoder<JSONHandlerT extends JSONHandler> {
|
||||
private readString(): string {
|
||||
assert(this.readChar() == '"'.charCodeAt(0), "Expected double-quoted string");
|
||||
let savedIndex = this.state.readIndex;
|
||||
let stringParts: Array<string> = new Array<string>();
|
||||
let stringParts: Array<string> = null;
|
||||
for (;;) {
|
||||
let byte = this.readChar();
|
||||
assert(byte >= 0x20, "Unexpected control character");
|
||||
if (byte == '"'.charCodeAt(0)) {
|
||||
stringParts.push(
|
||||
String.fromUTF8(this.state.buffer.buffer.data + savedIndex, this.state.readIndex - savedIndex - 1));
|
||||
let s = String.fromUTF8(this.state.buffer.buffer.data + savedIndex, this.state.readIndex - savedIndex - 1);
|
||||
if (stringParts == null) {
|
||||
return s;
|
||||
}
|
||||
stringParts.push(s);
|
||||
return stringParts.join("");
|
||||
}
|
||||
if (byte == "\\".charCodeAt(0)) {
|
||||
} else if (byte == "\\".charCodeAt(0)) {
|
||||
if (stringParts == null) {
|
||||
stringParts = new Array<string>();
|
||||
}
|
||||
if (this.state.readIndex > savedIndex + 1) {
|
||||
stringParts.push(
|
||||
String.fromUTF8(this.state.buffer.buffer.data + savedIndex, this.state.readIndex - savedIndex - 1));
|
||||
|
@ -13,11 +13,9 @@ export class JSONEncoder {
|
||||
// TODO: Write directly to UTF8 bytes
|
||||
let result = this.toString();
|
||||
let utf8ptr = result.toUTF8();
|
||||
let buffer = new Uint8Array(result.lengthUTF8);
|
||||
for (let i = 0; i < buffer.length; i++) {
|
||||
buffer[i] = load<u8>(utf8ptr + i);
|
||||
}
|
||||
return buffer.subarray(0, buffer.length - 1);
|
||||
let buffer = new Uint8Array(result.lengthUTF8 - 1);
|
||||
memory.copy(buffer.buffer.data, utf8ptr, buffer.byteLength);
|
||||
return buffer;
|
||||
}
|
||||
|
||||
toString(): String {
|
||||
|
Reference in New Issue
Block a user