UNPKG

typia

Version:

Superfast runtime validators with only one line

84 lines (83 loc) 1.95 kB
//#region src/internal/_ProtobufSizer.ts var _ProtobufSizer = class { constructor(length = 0) { this.len = length; this.pos = []; this.varlen = []; this.varlenidx = []; } bool() { this.len += 1; } int32(value) { if (value < 0) this.len += 10; else this.varint32(value); } sint32(value) { this.varint32(value << 1 ^ value >> 31); } uint32(value) { this.varint32(value); } int64(value) { this.varint64(typeof value === "number" ? BigInt(value) : value); } sint64(value) { if (typeof value === "number") value = BigInt(value); this.varint64(value << BigInt(1) ^ value >> BigInt(63)); } uint64(value) { this.varint64(typeof value === "number" ? BigInt(value) : value); } float(_value) { this.len += 4; } double(_value) { this.len += 8; } bytes(value) { this.uint32(value.byteLength); this.len += value.byteLength; } string(value) { const len = strlen(value); this.varlen.push(len); this.uint32(len); this.len += len; } fork() { this.pos.push(this.len); this.varlenidx.push(this.varlen.length); this.varlen.push(0); } ldelim() { if (!(this.pos.length && this.varlenidx.length)) throw new Error("Error on typia.protobuf.encode(): missing fork() before ldelim() call."); const endPos = this.len; const startPos = this.pos.pop(); const idx = this.varlenidx.pop(); const len = endPos - startPos; this.varlen[idx] = len; this.uint32(len); } reset() { this.len = 0; this.pos.length = 0; this.varlen.length = 0; this.varlenidx.length = 0; } varint32(value) { this.len += value < 0 ? 10 : value < 128 ? 1 : value < 16384 ? 2 : value < 2097152 ? 3 : value < 268435456 ? 4 : 5; } varint64(val) { val = BigInt.asUintN(64, val); while (val > BigInt(127)) { ++this.len; val = val >> BigInt(7); } ++this.len; } }; const strlen = (str) => new Blob([str]).size; //#endregion export { _ProtobufSizer }; //# sourceMappingURL=_ProtobufSizer.mjs.map