MCPcopy Create free account
hub / github.com/denoland/std / decodeThree

Function decodeThree

cbor/_common_decode.ts:122–173  ·  view source on GitHub ↗
(
  input: Uint8Array,
  aI: number,
  offset: number,
)

Source from the content-addressed store, hash-verified

120}
121
122function decodeThree(
123 input: Uint8Array,
124 aI: number,
125 offset: number,
126): [string, number] {
127 if (aI < 24) {
128 return [
129 new TextDecoder().decode(input.subarray(offset, offset + aI)),
130 offset + aI,
131 ];
132 }
133 if (aI <= 27) {
134 const x = calcLength(input, aI, offset);
135 const start = x[1];
136 const end = Number(x[0]) + x[1];
137 return [new TextDecoder().decode(input.subarray(start, end)), end];
138 }
139 if (aI === 31) {
140 let byte = input[offset++];
141 if (byte == undefined) throw new RangeError("More bytes were expected");
142 const output: Uint8Array[] = [];
143 while (byte !== 0b111_11111) {
144 if (byte >> 5 !== 3) {
145 throw new TypeError(
146 `Cannot decode value (0b${(byte >> 5).toString(2).padStart(3, "0")}_${
147 (byte & 0b11111).toString(2).padStart(5, "0")
148 }) inside an indefinite length text string`,
149 );
150 }
151 const aI = byte & 0b000_11111;
152 if (aI === 31) {
153 throw new TypeError(
154 "Indefinite length text strings cannot contain indefinite length text strings",
155 );
156 }
157 if (aI > 27) {
158 throw new RangeError(
159 `Cannot decode value (0b011_${aI.toString(2).padStart(5, "0")})`,
160 );
161 }
162 const x = decodeTwo(input, aI, offset);
163 output.push(x[0]);
164 offset = x[1];
165 byte = input[offset++];
166 if (byte == undefined) throw new RangeError("More bytes were expected");
167 }
168 return [new TextDecoder().decode(concat(output)), offset + 1];
169 }
170 throw new RangeError(
171 `Cannot decode value (0b011_${aI.toString(2).padStart(5, "0")})`,
172 );
173}
174
175function decodeFour(
176 input: Uint8Array,

Callers 1

decodeFunction · 0.85

Calls 5

concatFunction · 0.90
calcLengthFunction · 0.85
decodeTwoFunction · 0.85
toStringMethod · 0.45
pushMethod · 0.45

Tested by

no test coverage detected