import { BRACE_LEFT, BRACE_RIGHT, BRACKET_LEFT, BRACKET_RIGHT, } from "../../../custom/chars"; import { JSON } from "../../.."; import { isSpace } from "util/string"; export function deserializeStaticArrayStruct>( srcStart: usize, srcEnd: usize, dst: usize, ): T { while (srcEnd > srcStart && isSpace(load(srcEnd - 2))) srcEnd -= 2; if (srcStart - srcEnd == 0) throw new Error("Input string had zero length or was all whitespace"); if (load(srcStart) != BRACKET_LEFT) throw new Error( "Expected '[' at start of object at position " + (srcEnd - srcStart).toString(), ); if (load(srcEnd - 2) != BRACKET_RIGHT) throw new Error( "Expected ']' at end of object at position " + (srcEnd - srcStart).toString(), ); // First pass: count elements using same logic as Array deserializer let count: i32 = 0; let depth: u32 = 0; let ptr = srcStart; while (ptr < srcEnd) { const code = load(ptr); if (code == BRACE_LEFT && depth++ == 0) { // start of object } else if (code == BRACE_RIGHT && --depth == 0) { count++; } ptr += 2; } // Allocate StaticArray with correct size const outSize = count * sizeof>(); const out = changetype>(dst || __new(outSize, idof())); // Second pass: populate values let index = 0; let lastIndex: usize = 0; depth = 0; while (srcStart < srcEnd) { const code = load(srcStart); if (code == BRACE_LEFT && depth++ == 0) { lastIndex = srcStart; } else if (code == BRACE_RIGHT && --depth == 0) { unchecked( (out[index++] = JSON.__deserialize>( lastIndex, (srcStart += 2), )), ); } srcStart += 2; } return out; }