{"version":3,"file":"_ProtobufSizer.mjs","names":[],"sources":["../../src/internal/_ProtobufSizer.ts"],"sourcesContent":["import { _IProtobufWriter } from \"./_IProtobufWriter\";\n\n/// @reference https://github.com/piotr-oles/as-proto/blob/main/packages/as-proto/assembly/internal/FixedSizer.ts\n/**\n * Measures a Protocol Buffer message before it is written.\n *\n * It receives the same calls as {@link _ProtobufWriter}, counts the bytes and\n * records the byte length of every string and length-delimited message, which\n * the writer reads back in order. The writer then allocates exactly one\n * buffer.\n *\n * @evidence contracts/common.md#principled-implementation The sizer replays the encoder's calls to compute the exact byte length and, for each string or length-delimited message, the prefix lengths, which the writer then consumes in the same order, so the buffer is allocated once and filled without a growth step. Varint lengths follow from the value ranges, a negative int32 takes ten bytes and 64-bit values are measured after reduction to an unsigned 64-bit pattern.\n * @evidence contracts/common.md#clear-and-simple-design One class holding the total length, a position stack for open messages and the list of recorded lengths; its private varint measurers are shared by the scalar methods.\n * @evidence contracts/common.md#prohibited-implementation-shortcuts The size rules are the protobuf wire format's, with no caller special-cased. An ldelim call without an open fork throws; callers must finish any forks they open.\n * @evidence contracts/common.md#meaningful-documentation A comment explains the two-pass role with the writer, and the fields have comments.\n */\nexport class _ProtobufSizer implements _IProtobufWriter {\n  /** Total measured byte length, including length prefixes. */\n  public len: number;\n\n  /** Byte positions at which still-open messages started. */\n  public readonly pos: Array<number>;\n\n  /** String and message byte lengths in the writer's consumption order. */\n  public readonly varlen: Array<number>;\n\n  /** Reserved length-list indices for still-open messages. */\n  public readonly varlenidx: Array<number>;\n\n  /** Start measuring from an optional existing byte count. */\n  public constructor(length: number = 0) {\n    this.len = length;\n    this.pos = [];\n    this.varlen = [];\n    this.varlenidx = [];\n  }\n\n  /**\n   * Count one byte.\n   *\n   * @evidence contracts/common.md#principled-implementation A boolean always takes one byte.\n   * @evidence contracts/common.md#clear-and-simple-design One increment.\n   * @evidence contracts/common.md#prohibited-implementation-shortcuts A fixed wire size.\n   * @evidence contracts/common.md#meaningful-documentation A one-line comment states what is counted.\n   */\n  public bool(): void {\n    this.len += 1;\n  }\n  /**\n   * Count the varint of a signed 32-bit integer, which is ten bytes when\n   * negative.\n   *\n   * @evidence contracts/common.md#principled-implementation A negative value takes ten bytes because it is sign-extended to 64 bits, otherwise the varint length of the value is used.\n   * @evidence contracts/common.md#clear-and-simple-design One branch over the shared measurer.\n   * @evidence contracts/common.md#prohibited-implementation-shortcuts The rule is the wire format's.\n   * @evidence contracts/common.md#meaningful-documentation A one-line comment states what is counted.\n   */\n  public int32(value: number): void {\n    if (value < 0) {\n      // 10 bytes to encode negative number\n      this.len += 10;\n    } else {\n      this.varint32(value);\n    }\n  }\n  /**\n   * Count the varint of the unsigned ZigZag form of a signed 32-bit integer.\n   *\n   * @evidence contracts/common.md#principled-implementation The ZigZag mapping is taken as an unsigned 32-bit value before measuring, so magnitudes of 2^30 or more are measured as five bytes and not as a negative number; the writer applies the same mapping.\n   * @evidence contracts/common.md#clear-and-simple-design One expression over the shared measurer.\n   * @evidence contracts/common.md#prohibited-implementation-shortcuts The mapping is the protobuf definition and is not special-cased.\n   * @evidence contracts/common.md#meaningful-documentation A comment states the unsigned mapping and the reason for it.\n   */\n  public sint32(value: number): void {\n    // The ZigZag value is unsigned, so it never takes the negative-number length.\n    this.varint32(((value << 1) ^ (value >> 31)) >>> 0);\n  }\n  /**\n   * Count the varint of an unsigned 32-bit integer.\n   *\n   * @evidence contracts/common.md#principled-implementation The varint length of an unsigned 32-bit value, one to five bytes.\n   * @evidence contracts/common.md#clear-and-simple-design One delegation.\n   * @evidence contracts/common.md#prohibited-implementation-shortcuts The rule is the wire format's.\n   * @evidence contracts/common.md#meaningful-documentation A one-line comment states what is counted.\n   */\n  public uint32(value: number): void {\n    this.varint32(value);\n  }\n\n  /**\n   * Count the varint of a signed 64-bit integer.\n   *\n   * @evidence contracts/common.md#principled-implementation A number is converted to a bigint and measured as an unsigned 64-bit pattern, which gives ten bytes for a negative value.\n   * @evidence contracts/common.md#clear-and-simple-design One delegation to the 64-bit measurer.\n   * @evidence contracts/common.md#prohibited-implementation-shortcuts The rule is the wire format's.\n   * @evidence contracts/common.md#meaningful-documentation A one-line comment states what is counted.\n   */\n  public int64(value: bigint | number): void {\n    this.varint64(typeof value === \"number\" ? BigInt(value) : value);\n  }\n  /**\n   * Count the varint of the ZigZag form of a signed 64-bit integer.\n   *\n   * @evidence contracts/common.md#principled-implementation The ZigZag mapping on a bigint, `(n << 1) ^ (n >> 63)`, is measured as an unsigned 64-bit pattern.\n   * @evidence contracts/common.md#clear-and-simple-design One expression over the 64-bit measurer.\n   * @evidence contracts/common.md#prohibited-implementation-shortcuts The mapping is the protobuf definition.\n   * @evidence contracts/common.md#meaningful-documentation A one-line comment states what is counted.\n   */\n  public sint64(value: bigint | number): void {\n    if (typeof value === \"number\") value = BigInt(value);\n    this.varint64((value << BigInt(1)) ^ (value >> BigInt(63)));\n  }\n  /**\n   * Count the varint of an unsigned 64-bit integer.\n   *\n   * @evidence contracts/common.md#principled-implementation A number or bigint is measured as an unsigned 64-bit varint.\n   * @evidence contracts/common.md#clear-and-simple-design One delegation to the 64-bit measurer.\n   * @evidence contracts/common.md#prohibited-implementation-shortcuts The rule is the wire format's.\n   * @evidence contracts/common.md#meaningful-documentation A one-line comment states what is counted.\n   */\n  public uint64(value: bigint | number): void {\n    this.varint64(typeof value === \"number\" ? BigInt(value) : value);\n  }\n\n  // public fixed32(_value: number): void {\n  //     this.len += 4;\n  // }\n  // public sfixed32(_value: number): void {\n  //     this.len += 4;\n  // }\n  // public fixed64(_value: number | bigint): void {\n  //     this.len += 8;\n  // }\n  // public sfixed64(_value: number | bigint): void {\n  //     this.len += 8;\n  // }\n  /**\n   * Count four bytes.\n   *\n   * @evidence contracts/common.md#principled-implementation A float takes four bytes regardless of value.\n   * @evidence contracts/common.md#clear-and-simple-design One increment.\n   * @evidence contracts/common.md#prohibited-implementation-shortcuts A fixed wire size.\n   * @evidence contracts/common.md#meaningful-documentation A one-line comment states what is counted.\n   */\n  public float(_value: number): void {\n    this.len += 4;\n  }\n  /**\n   * Count eight bytes.\n   *\n   * @evidence contracts/common.md#principled-implementation A double takes eight bytes regardless of value.\n   * @evidence contracts/common.md#clear-and-simple-design One increment.\n   * @evidence contracts/common.md#prohibited-implementation-shortcuts A fixed wire size.\n   * @evidence contracts/common.md#meaningful-documentation A one-line comment states what is counted.\n   */\n  public double(_value: number): void {\n    this.len += 8;\n  }\n\n  /**\n   * Count the length prefix and the bytes.\n   *\n   * @evidence contracts/common.md#principled-implementation The length prefix is measured as an unsigned 32-bit varint and the byte length is added.\n   * @evidence contracts/common.md#clear-and-simple-design Two increments.\n   * @evidence contracts/common.md#prohibited-implementation-shortcuts The rule is the wire format's.\n   * @evidence contracts/common.md#meaningful-documentation A one-line comment states what is counted.\n   */\n  public bytes(value: Uint8Array): void {\n    this.uint32(value.byteLength);\n    this.len += value.byteLength;\n  }\n  /**\n   * Count the length prefix and the UTF-8 bytes, and record the byte length for\n   * the writer.\n   *\n   * @evidence contracts/common.md#principled-implementation The UTF-8 byte length is measured by encoding through a Blob, recorded in the length list so the writer reuses it and not recomputes it, and added with its varint prefix.\n   * @evidence contracts/common.md#clear-and-simple-design One function using a module-level measuring helper.\n   * @evidence contracts/common.md#prohibited-implementation-shortcuts The measure counts UTF-8 bytes and not code units.\n   * @evidence contracts/common.md#meaningful-documentation A one-line comment states what is counted.\n   */\n  public string(value: string): void {\n    const len: number = strlen(value);\n    this.varlen.push(len);\n    this.uint32(len);\n    this.len += len;\n  }\n\n  /**\n   * Save the position and reserve a length slot for a message that is starting.\n   *\n   * @evidence contracts/common.md#principled-implementation The current length and the length list's size are saved on stacks and a zero is reserved in the list, to be filled when the message ends.\n   * @evidence contracts/common.md#clear-and-simple-design Three stack operations.\n   * @evidence contracts/common.md#prohibited-implementation-shortcuts The bookkeeping is the two-pass design's.\n   * @evidence contracts/common.md#meaningful-documentation A one-line comment states what is saved.\n   */\n  public fork(): void {\n    this.pos.push(this.len); // save current position\n    this.varlenidx.push(this.varlen.length); // save current index in varlen array\n    this.varlen.push(0); // add 0 length to varlen array (to be updated in ldelim())\n  }\n\n  /**\n   * Fill the reserved length slot with the size of the message that ends here,\n   * and count its length prefix.\n   *\n   * @throws Error when no `fork` is open\n   *\n   * @evidence contracts/common.md#principled-implementation The length of the message since its fork is stored in the reserved list slot and its varint prefix is counted; an unmatched call throws a typed error instead of corrupting the stacks.\n   * @evidence contracts/common.md#clear-and-simple-design One function paired with fork.\n   * @evidence contracts/common.md#prohibited-implementation-shortcuts The unmatched case throws rather than being tolerated.\n   * @evidence contracts/common.md#meaningful-documentation A one-line comment states what is recorded.\n   */\n  public ldelim(): void {\n    if (!(this.pos.length && this.varlenidx.length))\n      throw new Error(\n        \"Error on typia.protobuf.encode(): missing fork() before ldelim() call.\",\n      );\n\n    const endPos = this.len; // current position is end position\n    const startPos = this.pos.pop()!; // get start position from stack\n    const idx = this.varlenidx.pop()!; // get varlen index from stack\n    const len = endPos - startPos; // calculate length\n\n    this.varlen[idx] = len; // update variable length\n    this.uint32(len); // add uint32 that should be called in fork()\n  }\n\n  /**\n   * Clear the counters and stacks so the sizer can measure another message.\n   *\n   * @evidence contracts/common.md#principled-implementation The counters and stacks are cleared in place, so the same sizer can measure another message without allocating new arrays.\n   * @evidence contracts/common.md#clear-and-simple-design Four assignments.\n   * @evidence contracts/common.md#prohibited-implementation-shortcuts No hidden state is kept.\n   * @evidence contracts/common.md#meaningful-documentation A one-line comment states that the arrays are reused.\n   */\n  public reset(): void {\n    this.len = 0;\n    // re-use arrays\n    this.pos.length = 0;\n    this.varlen.length = 0;\n    this.varlenidx.length = 0;\n  }\n\n  private varint32(value: number): void {\n    this.len +=\n      value < 0\n        ? 10 // 10 bits with leading 1's\n        : value < 0x80\n          ? 1\n          : value < 0x4000\n            ? 2\n            : value < 0x200000\n              ? 3\n              : value < 0x10000000\n                ? 4\n                : 5;\n  }\n\n  private varint64(val: bigint): void {\n    val = BigInt.asUintN(64, val);\n    while (val > BigInt(0x7f)) {\n      ++this.len;\n      val = val >> BigInt(0x07);\n    }\n    ++this.len;\n  }\n}\n\nconst strlen = (str: string): number => new Blob([str]).size;\n"],"mappings":";;;;;;;;;;;;;;AAgBA,IAAa,iBAAb,MAAwD;;CActD,YAAmB,SAAiB,GAAG;EACrC,KAAK,MAAM;EACX,KAAK,MAAM,CAAC;EACZ,KAAK,SAAS,CAAC;EACf,KAAK,YAAY,CAAC;CACpB;;;;;;;;;CAUA,OAAoB;EAClB,KAAK,OAAO;CACd;;;;;;;;;;CAUA,MAAa,OAAqB;EAChC,IAAI,QAAQ,GAEV,KAAK,OAAO;OAEZ,KAAK,SAAS,KAAK;CAEvB;;;;;;;;;CASA,OAAc,OAAqB;EAEjC,KAAK,UAAW,SAAS,IAAM,SAAS,QAAS,CAAC;CACpD;;;;;;;;;CASA,OAAc,OAAqB;EACjC,KAAK,SAAS,KAAK;CACrB;;;;;;;;;CAUA,MAAa,OAA8B;EACzC,KAAK,SAAS,OAAO,UAAU,WAAW,OAAO,KAAK,IAAI,KAAK;CACjE;;;;;;;;;CASA,OAAc,OAA8B;EAC1C,IAAI,OAAO,UAAU,UAAU,QAAQ,OAAO,KAAK;EACnD,KAAK,SAAU,SAAS,OAAO,CAAC,IAAM,SAAS,OAAO,EAAE,CAAE;CAC5D;;;;;;;;;CASA,OAAc,OAA8B;EAC1C,KAAK,SAAS,OAAO,UAAU,WAAW,OAAO,KAAK,IAAI,KAAK;CACjE;;;;;;;;;CAsBA,MAAa,QAAsB;EACjC,KAAK,OAAO;CACd;;;;;;;;;CASA,OAAc,QAAsB;EAClC,KAAK,OAAO;CACd;;;;;;;;;CAUA,MAAa,OAAyB;EACpC,KAAK,OAAO,MAAM,UAAU;EAC5B,KAAK,OAAO,MAAM;CACpB;;;;;;;;;;CAUA,OAAc,OAAqB;EACjC,MAAM,MAAc,OAAO,KAAK;EAChC,KAAK,OAAO,KAAK,GAAG;EACpB,KAAK,OAAO,GAAG;EACf,KAAK,OAAO;CACd;;;;;;;;;CAUA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