Skip to content

Commit

Permalink
Add a readStringNT method
Browse files Browse the repository at this point in the history
  • Loading branch information
extremeheat committed May 5, 2024
1 parent a3b860c commit 4927953
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -4,7 +4,7 @@
[![Try it on gitpod](https://img.shields.io/static/v1.svg?label=try&message=on%20gitpod&color=brightgreen&logo=gitpod)](https://gitpod.io/#https://github.com/extremeheat/node-binarystream)
[![PrismarineJS Discord](https://img.shields.io/static/v1.svg?label=PrismarineJS&message=Discord&color=blue&logo=discord)](https://discord.gg/GsEFRM8)

A simple zero-dep binary byte stream implementation for Node.js with support for reading and writing numbers, strings, and varints (zigzag or unsigned) in both little and big endian encoding.
A simple zero-dep binary byte stream implementation for Node.js and the browser with support for reading and writing numbers, strings, and varints (zigzag or unsigned) in both little and big endian encoding.

## Install
```js
Expand Down
1 change: 1 addition & 0 deletions index.d.ts
Expand Up @@ -80,6 +80,7 @@ declare module "bytewriter" {

// Strings
writeStringNT(value: string, encoding = 'utf8'): void
readStringNT(encoding = 'utf8'): string

writeStringRaw(value: string, encoding = 'utf8'): void

Expand Down
10 changes: 10 additions & 0 deletions src/browser.js
Expand Up @@ -220,6 +220,16 @@ class ByteStream {
this.writeOffset += encodedString.length + 1
}

readStringNT (encoding = 'utf8') {
let length = 0
while (this.buffer[this.readOffset + length] !== 0) {
length += 1
}
const value = this.buffer.subarray(this.readOffset, this.readOffset + length)
this.readOffset += length + 1
return new TextDecoder(encoding).decode(value)
}

writeStringRaw (value, encoding = 'utf8') {
const encoder = new TextEncoder(encoding)
const encodedString = encoder.encode(value)
Expand Down
10 changes: 10 additions & 0 deletions src/node.js
Expand Up @@ -259,6 +259,16 @@ class ByteStream {
this.writeOffset += value.length + 1
}

readStringNT (encoding = 'utf8') {
let offset = this.readOffset
while (this.buffer[offset] !== 0) {
offset++
}
const value = this.buffer.toString(encoding, this.readOffset, offset)
this.readOffset = offset + 1
return value
}

writeStringRaw (value, encoding = 'utf8') {
const byteLength = Buffer.byteLength(value, encoding)
this.resizeForWriteIfNeeded(byteLength)
Expand Down
2 changes: 2 additions & 0 deletions test/basic.test.js
Expand Up @@ -27,6 +27,7 @@ describe('basic tests - node', () => {
it('NT string writing', () => {
const stream = new ByteStream()
stream.writeStringNT('hello world!')
assert(stream.readStringNT() === 'hello world!')
assert(stream.getBuffer().equals(Buffer.from('hello world!\0')))
assert(ByteWriter.buffersEqual(stream.getBuffer(), Buffer.from('hello world!\0')))
})
Expand Down Expand Up @@ -74,6 +75,7 @@ describe('basic tests - browser', () => {
it('NT string writing', () => {
const stream = new ByteStream()
stream.writeStringNT('hello world!')
assert(stream.readStringNT() === 'hello world!')
const expected = new Uint8Array([104, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100, 33, 0])
assert(ByteWriter.buffersEqual(stream.getBuffer(), expected))
})
Expand Down

0 comments on commit 4927953

Please sign in to comment.