1.0.1 • Published 2 years ago
nv-buf-utf8-ab v1.0.1
nv-buf-utf8-ab
- get/decode code-points directly from valid-utf8-array-buffer
- similiar to for(let ch of str) { yield(ch.codePointAt(0))} , just from array-buffer, NOT from string
- this pkg just rewrite v8/src/third_party/utf8-decoder to js.
- just for test, its slow than c version
install
- npm install nv-buf-utf8-ab
splitted
usage
const { creat_mach, ab2cds } = require("nv-buf-utf8-ab");
example
var buf = Buffer.from("aÿ我𝑒")
/*
> buf
<Buffer 61 c3 bf e6 88 91 f0 9d 91 92>
*/
one-by-one (fast)
var mach = x.creat_mach()
/*
> mach.input(0x61)
97
> mach.input(0xc3)
undefined
> mach.input(0xbf)
255
> mach.input(0xe6)
undefined
> mach.input(0x88)
undefined
> mach.input(0x91)
25105
> mach.input(0xf0)
undefined
> mach.input(0x9d)
undefined
> mach.input(0x91)
undefined
> mach.input(0x92)
119890
>
*/
get detail description (slow)
var arr =[
97, 195, 191, 230,
136, 145, 240, 157,
145, 146
]
var u8a = new Uint8Array(arr)
var ab = u8a.buffer
var mach = creat_mach()
var f1 = (ab)=>x.ab2cds(ab,mach);
console.dir(f1(ab))
/*
{
codes: [ 97, 255, 25105, 119890 ],
offsets: [ 0, 1, 3, 6 ], // byte index of each-utf8-char
sizes: [ 1, 2, 3, 4 ], // byte length of each-utf8-char
}
*/
METHODS
APIS
LICENSE
- ISC
1.0.1
2 years ago