{"version":3,"file":"baseXReslice.cjs","sources":["../../src/baseXReslice.ts"],"sourcesContent":["/* eslint-disable no-restricted-syntax */\n/* eslint-disable no-bitwise */\nimport type { Serializer } from '@metaplex-foundation/umi-serializers-core';\nimport { InvalidBaseStringError } from './errors';\n\n/**\n * A string serializer that reslices bytes into custom chunks\n * of bits that are then mapped to a custom alphabet.\n *\n * This can be used to create serializers whose alphabet\n * is a power of 2 such as base16 or base64.\n *\n * @category Serializers\n */\nexport const baseXReslice = (\n  alphabet: string,\n  bits: number\n): Serializer<string> => {\n  const base = alphabet.length;\n  const reslice = (\n    input: number[],\n    inputBits: number,\n    outputBits: number,\n    useRemainder: boolean\n  ): number[] => {\n    const output = [];\n    let accumulator = 0;\n    let bitsInAccumulator = 0;\n    const mask = (1 << outputBits) - 1;\n    for (const value of input) {\n      accumulator = (accumulator << inputBits) | value;\n      bitsInAccumulator += inputBits;\n      while (bitsInAccumulator >= outputBits) {\n        bitsInAccumulator -= outputBits;\n        output.push((accumulator >> bitsInAccumulator) & mask);\n      }\n    }\n    if (useRemainder && bitsInAccumulator > 0) {\n      output.push((accumulator << (outputBits - bitsInAccumulator)) & mask);\n    }\n    return output;\n  };\n\n  return {\n    description: `base${base}`,\n    fixedSize: null,\n    maxSize: null,\n    serialize(value: string): Uint8Array {\n      // Check if the value is valid.\n      if (!value.match(new RegExp(`^[${alphabet}]*$`))) {\n        throw new InvalidBaseStringError(value, base);\n      }\n      if (value === '') return new Uint8Array();\n      const charIndices = [...value].map((c) => alphabet.indexOf(c));\n      const bytes = reslice(charIndices, bits, 8, false);\n      return Uint8Array.from(bytes);\n    },\n    deserialize(buffer, offset = 0): [string, number] {\n      if (buffer.length === 0) return ['', 0];\n      const bytes = [...buffer.slice(offset)];\n      const charIndices = reslice(bytes, 8, bits, true);\n      return [charIndices.map((i) => alphabet[i]).join(''), buffer.length];\n    },\n  };\n};\n"],"names":["baseXReslice","alphabet","bits","base","length","reslice","input","inputBits","outputBits","useRemainder","output","accumulator","bitsInAccumulator","mask","value","push","description","fixedSize","maxSize","serialize","match","RegExp","InvalidBaseStringError","Uint8Array","charIndices","map","c","indexOf","bytes","from","deserialize","buffer","offset","slice","i","join"],"mappings":";;;;;;AAAA;;AAKA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;MACaA,YAAY,GAAG,CAC1BC,QAAgB,EAChBC,IAAY,KACW;AACvB,EAAA,MAAMC,IAAI,GAAGF,QAAQ,CAACG,MAAM,CAAA;EAC5B,MAAMC,OAAO,GAAG,CACdC,KAAe,EACfC,SAAiB,EACjBC,UAAkB,EAClBC,YAAqB,KACR;IACb,MAAMC,MAAM,GAAG,EAAE,CAAA;IACjB,IAAIC,WAAW,GAAG,CAAC,CAAA;IACnB,IAAIC,iBAAiB,GAAG,CAAC,CAAA;AACzB,IAAA,MAAMC,IAAI,GAAG,CAAC,CAAC,IAAIL,UAAU,IAAI,CAAC,CAAA;AAClC,IAAA,KAAK,MAAMM,KAAK,IAAIR,KAAK,EAAE;AACzBK,MAAAA,WAAW,GAAIA,WAAW,IAAIJ,SAAS,GAAIO,KAAK,CAAA;AAChDF,MAAAA,iBAAiB,IAAIL,SAAS,CAAA;MAC9B,OAAOK,iBAAiB,IAAIJ,UAAU,EAAE;AACtCI,QAAAA,iBAAiB,IAAIJ,UAAU,CAAA;QAC/BE,MAAM,CAACK,IAAI,CAAEJ,WAAW,IAAIC,iBAAiB,GAAIC,IAAI,CAAC,CAAA;AACxD,OAAA;AACF,KAAA;AACA,IAAA,IAAIJ,YAAY,IAAIG,iBAAiB,GAAG,CAAC,EAAE;MACzCF,MAAM,CAACK,IAAI,CAAEJ,WAAW,IAAKH,UAAU,GAAGI,iBAAkB,GAAIC,IAAI,CAAC,CAAA;AACvE,KAAA;AACA,IAAA,OAAOH,MAAM,CAAA;GACd,CAAA;EAED,OAAO;IACLM,WAAW,EAAG,CAAMb,IAAAA,EAAAA,IAAK,CAAC,CAAA;AAC1Bc,IAAAA,SAAS,EAAE,IAAI;AACfC,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