nv-buf-jstr-pool
v1.0.1
Published
nv-buf-jstr-pool ======================= - a utf-16 buffer - for test string transfer in nvlang. normally USELESS. - nvlang NOT support full-utf-16 AND utf-8; ONLY support ascii | latin-1 | utf-32 | partly support utf16(without surrogate) | and-speci
Downloads
2
Readme
nv-buf-jstr-pool
- a utf-16 buffer
- for test string transfer in nvlang. normally USELESS.
- nvlang NOT support full-utf-16 AND utf-8; ONLY support ascii | latin-1 | utf-32 | partly support utf16(without surrogate) | and-special-format-1byte-str: /<ascii + 256-chinese-character/>
install
- npm install nv-buf-jstr-pool
splitted
usage
const { creat } = require("nv-buf-jstr-pool");
example
creat
creat(ab:ArrayBuffer, from_offset_at_ab:Uint, byte_length:Uint, is_le=true ) : Spool
var ab = new ArrayBuffer(1024*1024*8);
var s1 = "abcd"
var s2 = "ÿ我𝑒"
var s3 = "\b\t\n\f\r"
var s4= "\x00\x01\x02\x03\x04\x05\x06\x07\b\t\n\x0B\f\r\x0E\x0F\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1A\x1B\x1C\x1D\x1E\x1F"
var spool = creat(ab,1024*1024*4, 4096)
add new
Spool'add(s:String): Uint
var id1 = spool.add(s1);
var id2 = spool.add(s2);
var id3 = spool.add(s3);
var id4 = spool.add(s4);
/*
1,2,3,4 id is always from 1, for compatible with nvlang, 0 is used as none .
*/
/*
> spool.buf_str_cnt_
4
> spool.buf_ch16_cnt_
45
> spool.buf_byte_cnt_
90
>
*/
get
Spool'get(id:Uint): String
/* ---- slow will be copied
> spool.get(1)
'abcd'
> spool.get(2)
'ÿ我𝑒'
> spool.get(3)
'\b\t\n\f\r'
> spool.get(4)
'\x00\x01\x02\x03\x04\x05\x06\x07\b\t\n\x0B\f\r\x0E\x0F\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1A\x1B\x1C\x1D\x1E\x1F'
>
>
>
*/
set
Spool'set(id:Uint, s:String): [Boolean,<string-reason-if-failed>| <null-if-success>]
IF utf16-byte-length OF the new s:String IS <= the-max-permiter-size(which decided by add operation) : success
ELSE : failed
/* slow u16-by-u16 to write to buf
> spool.get_curr_sz(1)
8
>
> spool.set(1,"xyz")
[ true, 1 ]
>
> spool.get(1) // 2*3 <=8 IF size <= the original("abcd") can update
'xyz'
>
> spool.get_curr_sz(1)
6
>
> spool.get_max_sz(1)
8
>
spool.set(1,"vwxyz") // cant, 2*5 > 8
[ false, 'orig_str_length_not_enough' ]
>
spool.set(1,"abcd")
*/
ser
Spool'ser(id:Uint) : Uint8Array
/* --- fast just read not copy
> spool.ser(2)
Uint8Array(8) [
255, 0, 17, 98,
53, 216, 82, 220
]
>
> (new TextDecoder("utf-16")).decode(spool.ser(2))
'ÿ我𝑒'
>
*/
others
/* "ÿ我𝑒" \u00ff\u6211 "\ud835\udc52"
> spool.get_byte_cnt(2) // same as .length *2
8
> spool.get_ch16_cnt(2) // same as .length
4
> spool.get_char_cnt(2) // same as Array.from(s).length
3
>
> spool.get_char_codes(2)
[ 255, 25105, 55349, 56402 ]
>
> for(let i=0;i<"ÿ我𝑒".length;++i) {console.log("ÿ我𝑒".charCodeAt(i))}
255
25105
55349
56402
> spool.get_code_points(2)
[ 255, 25105, 119890 ]
>
> for(let ch of "ÿ我𝑒") {console.log(ch.codePointAt(0))}
255
25105
119890
*/
METHODS
spool.add spool.back_store_ei_ spool.back_store_si_ spool.buf_byte_cnt_ spool.buf_ch16_cnt_ spool.buf_str_cnt_ spool.capacity_
spool.cursor_ spool.get spool.get_byte_cnt spool.get_ch16_cnt spool.get_char_cnt spool.get_char_codes spool.get_code_points spool.get_curr_sz
spool.get_max_sz spool.is_le spool.meta_ spool.ser spool.set
APIS
- creat(ab:ArrayBuffer, from_offset_at_ab:Uint, byte_length:Uint, is_le=true ) : Spool
LICENSE
- ISC