kriszyp / msgpackr

Ultra-fast MessagePack implementation with extension for record and structural cloning / msgpack.org[JavaScript/NodeJS]

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

pack string and unpack slice string

b2whats opened this issue · comments

It is correct code ?

import { unpack, pack } from 'msgpackr';

let text = '1234567890'
let buffer = pack(text)
unpack(buffer.slice(2,4))

i get a mistake

Uncaught Error: Data read, but end of buffer not reached 50
    at checkedRead (unpack.js:225:10)
    at unpack (unpack.js:113:10)

No, it is not correct (you can't random slice off portions of an encode message). What are you trying to do?

i write tokenizer and want to get string from binary sequence

          constructor(source) {
            this.buffer = new Uint8Array(source.length)
            new TextEncoder().encodeInto(source, this.buffer)
          }

          next() { return this.charCode = this.buffer[++this.cursor] }
          peek(shift) { return this.buffer[this.cursor + shift] }
          slice(start, end = this.cursor) { return String.fromCharCode.apply(null, this.buffer.slice(start, end)) }

this code works for me