(value: string, nul = false)
| 533 | } |
| 534 | |
| 535 | utf8(value: string, nul = false): void { |
| 536 | const length = value.length |
| 537 | if (length <= asciiEncodeLimit) { |
| 538 | this.reserve(length + (nul ? 1 : 0)) |
| 539 | const bytes = this.bytes |
| 540 | const start = this.offset |
| 541 | let i = 0 |
| 542 | for (; i < length; i++) { |
| 543 | const code = value.charCodeAt(i) |
| 544 | if (code > 0x7f) break |
| 545 | bytes[start + i] = code |
| 546 | } |
| 547 | if (i === length) { |
| 548 | const offset = start + length |
| 549 | if (nul) bytes[offset] = 0 |
| 550 | this.offset = offset + (nul ? 1 : 0) |
| 551 | return |
| 552 | } |
| 553 | } |
| 554 | // UTF-8 takes at most three bytes per UTF-16 code unit, and four for the |
| 555 | // two units of a surrogate pair, so this covers any string. |
| 556 | this.reserve(length * 3 + (nul ? 1 : 0)) |
| 557 | this.offset += textEncoder.encodeInto(value, this.bytes.subarray(this.offset)).written |
| 558 | if (nul) this.bytes[this.offset++] = 0 |
| 559 | } |
| 560 | |
| 561 | cString(value: string): void { |
| 562 | this.utf8(value, true) |
no test coverage detected