Skip to content

Commit 83eba93

Browse files
committed
Added readChars and writeChars methods
1 parent 4f669a5 commit 83eba93

File tree

10 files changed

+86
-4
lines changed

10 files changed

+86
-4
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# **streambuf** Changelog
22

3+
### [2.1.0] (27-dec-2025)
4+
5+
- Added `readChars` and `writeChars` methods
6+
37
### [2.0.0] (4-jul-2024)
48

59
- Removed support for non-new constructor, changed export

README.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,16 @@ Reads a single character from the buffer according to the specified character en
103103
Writes a single character to the buffer according to the specified character encoding. Multi-byte characters are not written - use `writeString` for that instead.
104104
'encoding' defaults to utf8.
105105

106+
## .readChars(length, [encoding])
107+
108+
Reads a fixed number of characters from the buffer according to the specified character encoding. The resulting string strips any trailing null characters.
109+
'encoding' defaults to utf8.
110+
111+
## .writeChars(str, length, [encoding])
112+
113+
Writes a string to the underlying buffer with the specified encoding, padding with null characters (0) if the string is shorter than length.
114+
'encoding' defaults to utf8.
115+
106116
## .readString([length, [encoding]])
107117

108118
Decodes to a string according to the specified character encoding in encoding and length.
@@ -120,7 +130,7 @@ Functions the same way as .readString(), but does not update the offset.
120130

121131
## .readString0([encoding])
122132

123-
Reads a string from the buffer according to the specified character encoding, stopping at the first zero (0) byte. Similar to calling .readString() without a length parameter, but more implicit.
133+
Reads a string from the buffer according to the specified character encoding, stopping at the first zero (0) byte. Similar to `.readString()` or `.readString(undefined, encoding)`, but more implicit.
124134
'encoding' defaults to utf8.
125135

126136
### Example:

lib/streambuf.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,12 @@ export declare class StreamBuffer {
5858
write(src: Buffer): Buffer<ArrayBufferLike>;
5959
readString(length?: number, encoding?: BufferEncoding): string;
6060
readChar(encoding?: BufferEncoding): string;
61+
readChars(length: number, encoding?: BufferEncoding): string;
6162
readString7(encoding?: BufferEncoding): string;
6263
readString0(encoding?: BufferEncoding): string;
6364
writeString(val: string, encoding?: BufferEncoding): string;
6465
writeChar(val: string, encoding?: BufferEncoding): string;
66+
writeChars(val: string, length: number, encoding?: BufferEncoding): string;
6567
writeString7(val: string, encoding?: BufferEncoding): string;
6668
writeString0(val: string, encoding?: BufferEncoding): string;
6769
peekString(length?: number, encoding?: BufferEncoding): string;

lib/streambuf.js

Lines changed: 16 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/streambuf.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/test.js

Lines changed: 15 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/test.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"scripts": {
77
"test": "aqa",
88
"test:watch": "aqa --watch",
9+
"tsc:watch": "npx tsc --watch",
910
"test:coverage": "c8 npm test & c8 report -r lcov"
1011
},
1112
"repository": {
@@ -41,4 +42,4 @@
4142
"aqa": "^1.6.12",
4243
"c8": "^10.1.3"
4344
}
44-
}
45+
}

streambuf.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,14 @@ export class StreamBuffer {
261261
readChar(encoding?: BufferEncoding) {
262262
return this.readString(1, encoding)
263263
}
264+
readChars(length: number, encoding?: BufferEncoding) {
265+
const str = this.readString(length, encoding)
266+
const nullIndex = str.indexOf('\0')
267+
if (nullIndex !== -1) {
268+
return str.substring(0, nullIndex)
269+
}
270+
return str
271+
}
264272
readString7(encoding?: BufferEncoding) {
265273
const len = this.read7BitInt()
266274
return this.readString(len, encoding)
@@ -276,6 +284,14 @@ export class StreamBuffer {
276284
this.#pos = this.#pos + this.#buf.write(val, this.#pos, 1, encoding)
277285
return val
278286
}
287+
writeChars(val: string, length: number, encoding?: BufferEncoding) {
288+
const bytesWritten = this.#buf.write(val, this.#pos, length, encoding)
289+
this.#pos = this.#pos + bytesWritten
290+
for (let i = bytesWritten; i < length; i++) {
291+
this.writeByte(0)
292+
}
293+
return val
294+
}
279295
writeString7(val: string, encoding?: BufferEncoding) {
280296
const len = Buffer.byteLength(val, encoding)
281297
this.write7BitInt(len)

test.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,7 @@ test('correct position increase (numeric methods)', t => {
373373
'readString7',
374374
'readString0',
375375
'readChar',
376+
'readChars',
376377
'read7BitInt',
377378
'readIntLE',
378379
'readIntBE',
@@ -389,6 +390,7 @@ test('correct position increase (numeric methods)', t => {
389390
'writeString7',
390391
'writeString0',
391392
'writeChar',
393+
'writeChars',
392394
'write7BitInt',
393395
'writeIntLE',
394396
'writeIntBE',
@@ -692,6 +694,22 @@ test('writeChar - multibyte utf8 strings', t => {
692694
t.deepEqual(buffer, [0, 0, 0, 0])
693695
})
694696

697+
test('readChars', t => {
698+
const buffer = Buffer.from('hi\x00\x00\x00world')
699+
const sb = new StreamBuffer(buffer)
700+
701+
t.is(sb.readChars(5), 'hi')
702+
t.is(sb.readChars(5), 'world')
703+
})
704+
705+
test('writeChars', t => {
706+
const buffer = Buffer.alloc(10)
707+
const sb = new StreamBuffer(buffer)
708+
sb.writeChars('hi', 5)
709+
sb.writeChars('world', 5)
710+
t.deepEqual(buffer, Buffer.from('hi\x00\x00\x00world'))
711+
})
712+
695713
test('isEOF tests', t => {
696714
const buffer = Buffer.from([0, 1, 2])
697715
const sb = new StreamBuffer(buffer)

0 commit comments

Comments
 (0)