(text: string, numCodeUnits?: number)
| 20 | * @returns The number of bytes. |
| 21 | */ |
| 22 | export function numCodeUnitsToNumUtf8Bytes(text: string, numCodeUnits?: number): number { |
| 23 | if (numCodeUnits === 0) { |
| 24 | return 0; |
| 25 | } |
| 26 | let curNumUtf8Bytes = 0; |
| 27 | let curNumCodeUnits = 0; |
| 28 | for (const codePoint of text) { |
| 29 | curNumCodeUnits += codePoint.length; |
| 30 | // TODO(prem): Is the ! safe here? |
| 31 | // eslint-disable-next-line @typescript-eslint/no-non-null-assertion |
| 32 | curNumUtf8Bytes += numUtf8BytesForCodePoint(codePoint.codePointAt(0)!); |
| 33 | if (numCodeUnits !== undefined && curNumCodeUnits >= numCodeUnits) { |
| 34 | break; |
| 35 | } |
| 36 | } |
| 37 | return curNumUtf8Bytes; |
| 38 | } |
| 39 | |
| 40 | export function numUtf8BytesToNumCodeUnits(text: string, numUtf8Bytes?: number): number { |
| 41 | if (numUtf8Bytes === 0) { |
no test coverage detected