nd-str
v1.0.13
Published
nd-str ======================= - simple wrap of v8::String - same as Buffer.from buf.toString() - just for test small-string - expose v8::internal::String which can make a string mutable(just for test nvlang's StringRef feature)
Downloads
19
Readme
nd-str
- simple wrap of v8::String
- same as Buffer.from buf.toString()
- just for test small-string
- expose v8::internal::String which can make a string mutable(just for test nvlang's StringRef feature)
install
- npm install nd-str
splitted
usage
const x = require("nd-str");
example
make string-literal mutable
const {set_u16cd_at} = x
> const fixed_str = "A B C D E F";
> fixed_str
'A B C D E F'
> set_u16cd_at(fixed_str,2,65)
> fixed_str
'A A C D E F'
>
const {
utf8ab_to_str_always_aloc_new_mem,
utf8ab_to_str_created_in_old_space,
utf16ab_to_str_always_aloc_new_mem,
utf16ab_to_str_created_in_old_space,
latin1ab_to_str_always_aloc_new_mem,
latin1ab_to_str_created_in_old_space,
} = x;
const {sync} = require("nv-facutil-simple-test");
var s = "aÿ我𝑒";
const s2ab = (s,encd="utf8")=> {
let buf =Buffer.from(s,encd);
let ab = buf.buffer;
return(ab.slice(buf.offset,buf.offset+buf.length))
}
var utf8ab = s2ab("aÿ我𝑒","utf8");
console.log(utf8ab_to_str_always_aloc_new_mem(utf8ab));
console.log(sync(10000000,utf8ab_to_str_always_aloc_new_mem,utf8ab))
var f1= (ab)=>Buffer.from(ab).toString("utf8");
console.log(f1(utf8ab));
console.log(sync(10000000,f1,utf8ab))
var utf16ab = s2ab("aÿ我a我","ucs2");
console.log(utf16ab_to_str_always_aloc_new_mem(utf16ab));
var f2 = (ab)=>Buffer.from(ab).toString("ucs2");
console.log(f2(utf16ab));
console.log(sync(10000000,utf16ab_to_str_always_aloc_new_mem,utf16ab))
console.log(sync(10000000,f2,utf16ab))
var latin1ab = s2ab("aÿaÿbbbb","latin1");
var f3 = (ab)=>Buffer.from(ab).toString("latin1");
console.log(latin1ab_to_str_always_aloc_new_mem(latin1ab));
console.log(f3(latin1ab));
console.log(sync(10000000,latin1ab_to_str_always_aloc_new_mem,latin1ab))
console.log(sync(10000000,f3,latin1ab))
/*
aÿ我𝑒
{
rounds: 10000000,
f: [Function: utf8ab_to_str_always_aloc_new_mem],
costed: 1659.392198085785
}
aÿ我𝑒
{ rounds: 10000000, f: [Function: f1], costed: 2637.066288948059 }
aÿ我a我
aÿ我a我
{
rounds: 10000000,
f: [Function: utf16ab_to_str_always_aloc_new_mem],
costed: 962.3775280714035
}
{ rounds: 10000000, f: [Function: f2], costed: 1963.8920789957047 }
aÿaÿbbbb
aÿaÿbbbb
{
rounds: 10000000,
f: [Function: latin1ab_to_str_always_aloc_new_mem],
costed: 924.0457830429077
}
{ rounds: 10000000, f: [Function: f3], costed: 1893.5889741182327 }
*/
str to array-buffer
with exist ab
> var ab = new ArrayBuffer(16)
> x.str_to_utf8ab("123",ab,5)
/*
ArrayBuffer {
[Uint8Contents]: <00 00 00 00 00 31 32 33 00 00 00 00 00 00 00 00>,
byteLength: 16
}
creat new ab
> x.str_to_utf8ab("123我")
ArrayBuffer { [Uint8Contents]: <31 32 33 e6 88 91>, byteLength: 6 }
>
*/
METHODS
String.prototype.utf16_blen
String.prototype.utf8_blen
APIS
{
kMaxLength: 268435440,
UNKNOWN_ENCODING: 1,
TWO_BYTE_ENCODING: 0,
ONE_BYTE_ENCODING: 8,
is_one_byte: [Function (anonymous)],
is_one_byte_or_unknown: [Function (anonymous)],
utf8_blen: [Function (anonymous)],
utf16_blen: [Function: utf16_blen],
is_extern: [Function (anonymous)],
is_one_byte_extern: [Function (anonymous)],
is_two_byte_extern: [Function (anonymous)],
can_make_one_byte_extern: [Function (anonymous)],
can_make_two_byte_extern: [Function (anonymous)],
utf8ab_to_str_always_aloc_new_mem: [Function: utf8ab_to_str_always_aloc_new_mem],
utf8ab_to_str_created_in_old_space: [Function: utf8ab_to_str_created_in_old_space],
utf16ab_to_str_always_aloc_new_mem: [Function: utf16ab_to_str_always_aloc_new_mem],
utf16ab_to_str_created_in_old_space: [Function: utf16ab_to_str_created_in_old_space],
latin1ab_to_str_always_aloc_new_mem: [Function: latin1ab_to_str_always_aloc_new_mem],
latin1ab_to_str_created_in_old_space: [Function: latin1ab_to_str_created_in_old_space],
NO_OPTIONS: 0,
HINT_MANY_WRITES_EXPECTED: 1,
NO_NULL_TERMINATION: 2,
PRESERVE_ONE_BYTE_NULL: 4,
REPLACE_INVALID_UTF8: 8,
str_to_utf8ab: [Function: str_to_utf8ab],
str_to_utf16ab: [Function: str_to_utf16ab],
str_to_latin1ab: [Function: str_to_latin1ab]
}
LICENSE
- ISC