-
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
108 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,47 @@ | ||
import { fCode, tCode } from "../../src/chars"; | ||
import { unsafeCharCodeAt } from "../../src/util"; | ||
import { JSON } from "../.."; | ||
import { fCode, leftBracketCode, rightBracketCode, tCode } from "../../src/chars"; | ||
import { getArrayDepth, unsafeCharCodeAt } from "../../src/util"; | ||
import { deserializeBooleanArray } from "./boolean"; | ||
import { deserializeNumberArray } from "./number"; | ||
import { deserializeStringArray } from "./string"; | ||
|
||
/** | ||
* 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>(); | ||
|
||
@inline export function deserializeArray<T extends any[]>(data: string, start: i32 = 0, end: i32 = data.length): T { | ||
console.log("data: " + data.slice(start, end)) | ||
if (isArray<T>() && end - start < 3) return instantiate<T>(); | ||
/*if (isString<valueof<T>>()) { | ||
// @ts-ignore | ||
return deserializeStringArray(data); | ||
} else if (isBoolean<valueof<T>>()) { | ||
return deserializeBooleanArray<T>(data); | ||
} else if (isInteger<valueof<T>>() || isFloat<valueof<T>>()) { | ||
return deserializeNumberArray<T>(data); | ||
} else if (idof<T>() === idof<JSON.Value[]>()) {*/ | ||
let result = instantiate<T>(); | ||
let lastPos: i32 = 0; | ||
let depth: i32 = 0; | ||
for (let i = start + 1; i < end - 1; i++) { | ||
const char = unsafeCharCodeAt(data, i); | ||
if (char === leftBracketCode) { | ||
if (depth === 0) { | ||
lastPos = i; | ||
} | ||
// Shifting is 6% faster than incrementing | ||
depth++; | ||
} else if (char === rightBracketCode) { | ||
depth--; | ||
if (depth === 0) { | ||
i++; | ||
if (isArray<T>()) result.push(deserializeArray<valueof<T>>(data, lastPos, i)); | ||
} | ||
} | ||
} | ||
//} | ||
|
||
return instantiate<T>() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { isSpace } from "util/string"; | ||
import { commaCode, rightBracketCode } from "../../src/chars"; | ||
import { unsafeCharCodeAt } from "../../src/util"; | ||
import { deserializeNumber } from "../number"; | ||
|
||
// @ts-ignore: Decorator | ||
@inline export function deserializeNumberArray<T extends number[]>(data: string): T { | ||
const result = instantiate<T>(); | ||
let lastPos = 0; | ||
let i = 1; | ||
for (; i < data.length - 1; i++) { | ||
const char = unsafeCharCodeAt(data, i); | ||
if (lastPos === 0 && ((char >= 48 && char <= 57) || char === 45)) { | ||
lastPos = i; | ||
} else if ((isSpace(char) || char == commaCode) && lastPos > 0) { | ||
result.push(deserializeNumber<valueof<T>>(data, lastPos, i)); | ||
lastPos = 0; | ||
} | ||
} | ||
for (; i > lastPos - 1; i--) { | ||
const char = unsafeCharCodeAt(data, i); | ||
if (char !== rightBracketCode) { | ||
result.push(deserializeNumber<valueof<T>>(data, lastPos, i + 1)); | ||
break; | ||
} | ||
} | ||
return result; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { Sink } from "../src/sink"; | ||
|
||
// @ts-ignore: Decorator | ||
@inline export function serializeClass<T>(data: T, out: Sink | null = null): Sink { | ||
if (data) { | ||
if (out) return out.write("true")!; | ||
return Sink.fromStringLiteral("true"); | ||
} else { | ||
if (out) return out.write("false")!; | ||
return Sink.fromStringLiteral("false"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters