mirror of
https://github.com/fluencelabs/assemblyscript
synced 2025-06-12 06:21:29 +00:00
Add String.fromUTF8 helper (see #291); Update dist files
This commit is contained in:
1
std/assembly/index.d.ts
vendored
1
std/assembly/index.d.ts
vendored
@ -490,6 +490,7 @@ declare class String {
|
||||
padEnd(targetLength: i32, padString?: string): string;
|
||||
repeat(count?: i32): string;
|
||||
toString(): string;
|
||||
static fromUTF8(ptr: usize, len: usize): string;
|
||||
toUTF8(): usize;
|
||||
}
|
||||
|
||||
|
@ -436,6 +436,49 @@ export class String {
|
||||
return len;
|
||||
}
|
||||
|
||||
static fromUTF8(ptr: usize, len: usize): String {
|
||||
if (len < 1) return changetype<String>("");
|
||||
var ptrPos = <usize>0;
|
||||
var buf = memory.allocate(<usize>len << 1);
|
||||
var bufPos = <usize>0;
|
||||
while (ptrPos < len) {
|
||||
let cp = <u32>load<u8>(ptr + ptrPos++);
|
||||
if (cp < 128) {
|
||||
store<u16>(buf + bufPos, cp);
|
||||
bufPos += 2;
|
||||
} else if (cp > 191 && cp < 224) {
|
||||
assert(ptrPos + 1 <= len);
|
||||
store<u16>(buf + bufPos, (cp & 31) << 6 | load<u8>(ptr + ptrPos++) & 63);
|
||||
bufPos += 2;
|
||||
} else if (cp > 239 && cp < 365) {
|
||||
assert(ptrPos + 3 <= len);
|
||||
cp = (
|
||||
(cp & 7) << 18 |
|
||||
(load<u8>(ptr + ptrPos++) & 63) << 12 |
|
||||
(load<u8>(ptr + ptrPos++) & 63) << 6 |
|
||||
load<u8>(ptr + ptrPos++) & 63
|
||||
) - 0x10000;
|
||||
store<u16>(buf + bufPos, 0xD800 + (cp >> 10));
|
||||
bufPos += 2;
|
||||
store<u16>(buf + bufPos, 0xDC00 + (cp & 1023));
|
||||
bufPos += 2;
|
||||
} else {
|
||||
assert(ptrPos + 2 <= len);
|
||||
store<u16>(buf + bufPos,
|
||||
(cp & 15) << 12 |
|
||||
(load<u8>(ptr + ptrPos++) & 63) << 6 |
|
||||
load<u8>(ptr + ptrPos++) & 63
|
||||
);
|
||||
bufPos += 2;
|
||||
}
|
||||
}
|
||||
assert(ptrPos == len);
|
||||
var str = allocateUnsafe(<u32>(bufPos >> 1));
|
||||
memory.copy(changetype<usize>(str) + HEADER_SIZE, buf, bufPos);
|
||||
memory.free(buf);
|
||||
return str;
|
||||
}
|
||||
|
||||
toUTF8(): usize {
|
||||
var buf = memory.allocate(<usize>this.lengthUTF8);
|
||||
var pos: usize = 0;
|
||||
|
Reference in New Issue
Block a user