| 2433 | // error will be thrown if not all the data can be written, and the SIZE has |
| 2434 | // been massaged to work with the input characters. |
| 2435 | function writeToFill(string, offset, end, encoding) { |
| 2436 | if (typeof offset === 'string') { |
| 2437 | encoding = offset |
| 2438 | offset = 0 |
| 2439 | end = buf2.length |
| 2440 | } else if (typeof end === 'string') { |
| 2441 | encoding = end |
| 2442 | end = buf2.length |
| 2443 | } else if (end === undefined) { |
| 2444 | end = buf2.length |
| 2445 | } |
| 2446 | |
| 2447 | // Should never be reached. |
| 2448 | if (offset < 0 || end > buf2.length) throw new Error('Should never be reached') |
| 2449 | |
| 2450 | if (end <= offset) return buf2 |
| 2451 | |
| 2452 | offset >>>= 0 |
| 2453 | end >>>= 0 |
| 2454 | assert(offset <= buf2.length) |
| 2455 | |
| 2456 | // Convert "end" to "length" (which write understands). |
| 2457 | const length = end - offset < 0 ? 0 : end - offset |
| 2458 | |
| 2459 | let wasZero = false |
| 2460 | do { |
| 2461 | const written = buf2.write(string, offset, length, encoding) |
| 2462 | offset += written |
| 2463 | // Safety check in case write falls into infinite loop. |
| 2464 | if (written === 0) { |
| 2465 | if (wasZero) throw new Error('Could not write all data to Buffer') |
| 2466 | else wasZero = true |
| 2467 | } |
| 2468 | } while (offset < buf2.length) |
| 2469 | |
| 2470 | return buf2 |
| 2471 | } |
| 2472 | |
| 2473 | function testBufs(string: any, offset?: any, length?: any, encoding?: any) { |
| 2474 | bufReset() |