| 38 | * @returns Decoded data |
| 39 | */ |
| 40 | export function decodeCSV(input: string): number[] { |
| 41 | const trimmed = input.trim(); |
| 42 | |
| 43 | // count commas to pre-allocate (avoids array resizing) |
| 44 | let count = 1; |
| 45 | for (let i = 0, len = trimmed.length; i < len; i++) { |
| 46 | if (trimmed.charCodeAt(i) === 44 /* comma */) { |
| 47 | count++; |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | const result = new Array<number>(count); |
| 52 | let idx = 0; |
| 53 | let start = 0; |
| 54 | |
| 55 | for (let i = 0, len = trimmed.length; i <= len; i++) { |
| 56 | const ch = i < len ? trimmed.charCodeAt(i) : 44; // treat end-of-string as comma |
| 57 | // skip newlines (10 = \n, 13 = \r) |
| 58 | if (ch === 10 || ch === 13) { |
| 59 | continue; |
| 60 | } |
| 61 | if (ch === 44 /* comma */) { |
| 62 | result[idx++] = +trimmed.slice(start, i); |
| 63 | start = i + 1; |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | // trim trailing empty entries from trailing commas |
| 68 | result.length = idx; |
| 69 | return result; |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Decode a base64 encoded string into a byte array |