Skip to content

Commit

Permalink
setup array deserialization
Browse files Browse the repository at this point in the history
  • Loading branch information
JairusSW committed Jun 6, 2024
1 parent 0e402ff commit 16bf1a9
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 84 deletions.
13 changes: 13 additions & 0 deletions assembly/deserialize/array/array.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { fCode, tCode } from "../../src/chars";
import { unsafeCharCodeAt } from "../../src/util";

/**
* Deserialize a string to type array
* @param data data to parse
* @returns boolean
*/
// @ts-ignore: Decorator
@inline export function deserializeArray<T extends any[]>(data: string, start: i32 = 0, end = data.length): T {
if (start - end < 3) return instantiate<T>();

}
19 changes: 19 additions & 0 deletions assembly/deserialize/array/boolean.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { eCode, fCode, tCode } from "../../src/chars";
import { unsafeCharCodeAt } from "../../src/util";
import { deserializeBoolean } from "../boolean";

// @ts-ignore: Decorator
@inline export function deserializeooleanArray<T extends bool[]>(data: string): T {
const result = instantiate<T>();
let lastPos = 1;
for (let i = 1; i < data.length - 1; i++) {
const char = unsafeCharCodeAt(data, i);
if (char === tCode || char === fCode) {
lastPos = i;
} else if (char === eCode) {
i++;
result.push(deserializeBoolean(data, lastPos, i));
}
}
return result;
}
29 changes: 29 additions & 0 deletions assembly/deserialize/array/string.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { backSlashCode, quoteCode } from "../../src/chars";
import { unsafeCharCodeAt } from "../../src/util";
import { deserializeString } from "../string";

// @ts-ignore: Decorator
@inline export function deserializeStringArray(data: string): string[] {
const result: string[] = [];
let lastPos = 0;
let instr = false;
let escaping = false;v
for (let i = 1; i < data.length - 1; i++) {
const char = unsafeCharCodeAt(data, i);
if (char === backSlashCode && !escaping) {
escaping = true;
} else {
if (char === quoteCode && !escaping) {
if (instr === false) {
instr = true;
lastPos = i;
} else {
instr = false;
result.push(deserializeString(data, lastPos, i));
}
}
escaping = false;
}
}
return result;
}
11 changes: 7 additions & 4 deletions assembly/deserialize/boolean.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@ import { unsafeCharCodeAt } from "../src/util";
* @returns boolean
*/
// @ts-ignore: Decorator
@inline export function deserializeBoolean(data: string): boolean {
const firstChar = unsafeCharCodeAt(data, 0);
if (data.length === 4 && firstChar === tCode && load<u64>(changetype<usize>(data)) === 28429475166421108) return true;
else if (data.length === 5 && firstChar === fCode && load<u64>(changetype<usize>(data), 2) === 28429466576093281) return false;
@inline export function deserializeBoolean(data: string, start: i32 = 0, end: i32 = 0): boolean {
if (!end) end = data.length;
const len = end - start;
const ptr = changetype<usize>(data) + <usize>(start << 1);
const firstChar = unsafeCharCodeAt(data, start);
if (len === 4 && firstChar === tCode && load<u64>(ptr) === 28429475166421108) return true;
else if (len === 5 && firstChar === fCode && load<u64>(ptr, 2) === 28429466576093281) return false;
return false//ERROR(`Expected to find boolean, but found "${data.slice(0, 100)}" instead!`);
}
99 changes: 19 additions & 80 deletions assembly/deserialize/string.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,27 @@
import { StringSink } from "as-string-sink/assembly";
import { __atoi_fast } from "../src/util";
import { unsafeCharCodeAt } from "../src/util";
import { bCode, backSlashCode, backspaceCode, carriageReturnCode, fCode, formFeedCode, forwardSlashCode, nCode, newLineCode, quoteCode, rCode, tCode, tabCode, uCode } from "../src/chars";
import { Product, ProductValue } from "../product";
import {
bCode,
fCode,
nCode,
uCode,
rCode,
tCode,
tabCode,
quoteCode,
newLineCode,
formFeedCode,
backSlashCode,
backspaceCode,
forwardSlashCode,
carriageReturnCode
} from "../src/chars";
import { Sink } from "../src/sink";

// @ts-ignore: Decorator
@inline export function deserializeString(data: string, start: i32 = 0, end: i32 = 0): string {
end = end || data.length - 1;
let result = StringSink.withCapacity(end - start - 1);
let result = Sink.withCapacity(end - start - 1);
const firstChar = unsafeCharCodeAt(data, start);
const lastChar = unsafeCharCodeAt(data, end);
if (firstChar !== quoteCode || lastChar !== quoteCode) {
Expand Down Expand Up @@ -76,80 +91,4 @@ import { Product, ProductValue } from "../product";
result.write(data, last, end);
}
return result.toString();
}


// @ts-ignore: Decorator
@inline export function _deserializeString(data: string, start: i32 = 0, end: i32 = 0): string {
end = end || data.length - 1;
let result = StringSink.withCapacity(end - start - 1);
const firstChar = unsafeCharCodeAt(data, start);
const lastChar = unsafeCharCodeAt(data, end);
if (firstChar !== quoteCode || lastChar !== quoteCode) {
abort(`Expected string to start and end with ", but got ${data.slice(0, 100)} instead!`);
}
let last = start + 1;
for (let i = last; i < end; i++) {
if (unsafeCharCodeAt(data, i) !== backSlashCode) {
continue;
}
const char = unsafeCharCodeAt(data, ++i);
result.write(data, last, i - 1);
switch (char) {
case quoteCode: {
result.writeCodePoint(quoteCode);
last = i + 1;
break;
}
case backSlashCode: {
result.writeCodePoint(backSlashCode);
last = i + 1;
break;
}
case forwardSlashCode: {
result.writeCodePoint(forwardSlashCode);
last = i + 1;
break;
}
case bCode: {
result.writeCodePoint(backspaceCode);
last = i + 1;
break;
}
case fCode: {
result.writeCodePoint(formFeedCode);
last = i + 1;
break;
}
case nCode: {
result.writeCodePoint(newLineCode);
last = i + 1;
break;
}
case rCode: {
result.writeCodePoint(carriageReturnCode);
last = i + 1;
break;
}
case tCode: {
result.writeCodePoint(tabCode);
last = i + 1;
break;
}
case uCode: {
const code = u16.parse(data.slice(i + 1, i + 5), 16);
result.writeCodePoint(code);
i += 4;
last = i + 1;
break;
}
default: {
abort(`Cannot parse "${data.slice(0, 100)}" as string. Invalid escape sequence: \\${data.charAt(i)}`);
}
}
}
if (end > last) {
result.write(data, last, end);
}
return result.toString();
}
12 changes: 12 additions & 0 deletions assembly/src/sink.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,18 @@ export class Sink {
return this;
}

@inline writeCodePoint16(code: i32): Sink {
this.ensureCapacity(2);

let offset = this.offset;
let dest = changetype<usize>(this.buffer) + offset;

store<u16>(dest, <u16>code);
this.offset = offset + 2;

return this;
}

@inline writeCodePointUnsafe(code: i32): Sink {
this.ensureCapacity(2);

Expand Down

0 comments on commit 16bf1a9

Please sign in to comment.