Improve text encoding API

This commit is contained in:
dcode
2019-03-28 18:57:35 +01:00
parent 0dcfcc7935
commit aa38d06c31
15 changed files with 3304 additions and 1577 deletions

View File

@ -2358,7 +2358,7 @@
if
i32.const 0
i32.const 1648
i32.const 189
i32.const 190
i32.const 4
call $~lib/env/abort
unreachable

View File

@ -3424,7 +3424,7 @@
if
i32.const 0
i32.const 1648
i32.const 189
i32.const 190
i32.const 4
call $~lib/env/abort
unreachable

View File

@ -142,7 +142,7 @@
if
i32.const 0
i32.const 64
i32.const 164
i32.const 165
i32.const 4
call $~lib/env/abort
unreachable

View File

@ -219,7 +219,7 @@
if
i32.const 0
i32.const 64
i32.const 164
i32.const 165
i32.const 4
call $~lib/env/abort
unreachable

View File

@ -6229,7 +6229,7 @@
if
i32.const 0
i32.const 4376
i32.const 189
i32.const 190
i32.const 4
call $~lib/env/abort
unreachable

View File

@ -8951,7 +8951,7 @@
if
i32.const 0
i32.const 4376
i32.const 39
i32.const 40
i32.const 4
call $~lib/env/abort
unreachable
@ -9590,7 +9590,7 @@
if
i32.const 0
i32.const 4376
i32.const 189
i32.const 190
i32.const 4
call $~lib/env/abort
unreachable

View File

@ -0,0 +1,73 @@
import "allocator/arena";
var str = "𐐷hi𤭢"; // -> f0 90 90 b7 68 69 f0 a4 ad a2 [00]
function testUTF8Length(): void {
assert(UTF8.length(str) == 10);
assert(UTF8.length(str, true) == 11);
}
testUTF8Length();
function testUTF8Encode(): void {
var buf = UTF8.encode(str);
var ptr = changetype<usize>(buf);
assert(buf.byteLength == 10);
assert(load<u8>(ptr, 0) == 0xf0);
assert(load<u8>(ptr, 1) == 0x90);
assert(load<u8>(ptr, 2) == 0x90);
assert(load<u8>(ptr, 3) == 0xb7);
assert(load<u8>(ptr, 4) == 0x68);
assert(load<u8>(ptr, 5) == 0x69);
assert(load<u8>(ptr, 6) == 0xf0);
assert(load<u8>(ptr, 7) == 0xa4);
assert(load<u8>(ptr, 8) == 0xad);
assert(load<u8>(ptr, 9) == 0xa2);
}
testUTF8Encode();
function testUTF8EncodeDelimited(): void {
var buf = UTF8.encode(str, true);
var ptr = changetype<usize>(buf);
assert(buf.byteLength == 11);
assert(load<u8>(ptr, 0) == 0xf0);
assert(load<u8>(ptr, 1) == 0x90);
assert(load<u8>(ptr, 2) == 0x90);
assert(load<u8>(ptr, 3) == 0xb7);
assert(load<u8>(ptr, 4) == 0x68);
assert(load<u8>(ptr, 5) == 0x69);
assert(load<u8>(ptr, 6) == 0xf0);
assert(load<u8>(ptr, 7) == 0xa4);
assert(load<u8>(ptr, 8) == 0xad);
assert(load<u8>(ptr, 9) == 0xa2);
assert(load<u8>(ptr, 10) == 0x00);
}
testUTF8EncodeDelimited();
function testUTF8Decode(): void {
var buf = UTF8.encode(str);
assert(UTF8.decode(buf) == str);
}
testUTF8Decode();
function testUTF8DecodeDelimited(): void {
var buf = UTF8.encode(str, true);
assert(UTF8.decode(buf, true) == str);
}
testUTF8DecodeDelimited();
function testUTF8Raw(): void {
var buf = changetype<usize>(UTF8.encode(str));
assert(UTF8.decodeRaw(buf, 0) == "");
assert(UTF8.decodeRaw(buf, UTF8.length(str)) == str);
assert(UTF8.decodeRaw(buf, 4) == "𐐷");
assert(UTF8.decodeRaw(buf + 4, 2) == "hi");
assert(UTF8.decodeRaw(buf + 6, 4) == "𤭢");
assert(UTF8.decodeRaw(buf + 10, 0) == "");
store<u8>(buf, 0, 10); // don't try this at home
assert(UTF8.decodeRawDelimited(buf + 4) == "hi𤭢");
assert(UTF8.decodeRawDelimited(buf + 6) == "𤭢");
assert(UTF8.decodeRawDelimited(buf + 10) == "");
}
testUTF8Raw();

View File

@ -1,30 +0,0 @@
import "allocator/arena";
var str = "𐐷hi𤭢"; // -> f0 90 90 b7 68 69 f0 a4 ad a2 00
var len = str.lengthUTF8;
assert(len == 11);
var ptr = str.toUTF8(); // toUTF8 is zero-terminated
assert(load<u8>(ptr, 0) == 0xf0);
assert(load<u8>(ptr, 1) == 0x90);
assert(load<u8>(ptr, 2) == 0x90);
assert(load<u8>(ptr, 3) == 0xb7);
assert(load<u8>(ptr, 4) == 0x68);
assert(load<u8>(ptr, 5) == 0x69);
assert(load<u8>(ptr, 6) == 0xf0);
assert(load<u8>(ptr, 7) == 0xa4);
assert(load<u8>(ptr, 8) == 0xad);
assert(load<u8>(ptr, 9) == 0xa2);
assert(load<u8>(ptr, 10) == 0);
assert(String.fromUTF8(ptr, 0) == ""); // fromUTF8 is not zero-terminated
assert(String.fromUTF8(ptr, len - 1) == str);
assert(String.fromUTF8(ptr, 4) == "𐐷");
assert(String.fromUTF8(ptr + 4, 2) == "hi");
assert(String.fromUTF8(ptr + 6, 4) == "𤭢");
assert(String.fromUTF8(ptr + 10, 1) == "\0");
memory.free(ptr);

View File

@ -575,7 +575,7 @@
if
i32.const 0
i32.const 216
i32.const 24
i32.const 25
i32.const 4
call $~lib/env/abort
unreachable
@ -630,7 +630,7 @@
if
i32.const 0
i32.const 216
i32.const 164
i32.const 165
i32.const 4
call $~lib/env/abort
unreachable
@ -678,7 +678,7 @@
if
i32.const 0
i32.const 216
i32.const 77
i32.const 78
i32.const 4
call $~lib/env/abort
unreachable
@ -726,7 +726,7 @@
if
i32.const 0
i32.const 216
i32.const 133
i32.const 134
i32.const 4
call $~lib/env/abort
unreachable
@ -1877,7 +1877,7 @@
if
i32.const 0
i32.const 216
i32.const 281
i32.const 282
i32.const 4
call $~lib/env/abort
unreachable
@ -1972,7 +1972,7 @@
if
i32.const 0
i32.const 216
i32.const 302
i32.const 303
i32.const 4
call $~lib/env/abort
unreachable
@ -2068,7 +2068,7 @@
if
i32.const 0
i32.const 216
i32.const 149
i32.const 150
i32.const 4
call $~lib/env/abort
unreachable
@ -2490,7 +2490,7 @@
if
i32.const 0
i32.const 216
i32.const 569
i32.const 464
i32.const 10
call $~lib/env/abort
unreachable
@ -2762,7 +2762,7 @@
if
i32.const 0
i32.const 216
i32.const 323
i32.const 324
i32.const 4
call $~lib/env/abort
unreachable
@ -2792,7 +2792,7 @@
if
i32.const 0
i32.const 216
i32.const 328
i32.const 329
i32.const 6
call $~lib/env/abort
unreachable
@ -3354,7 +3354,7 @@
if
i32.const 0
i32.const 216
i32.const 350
i32.const 351
i32.const 4
call $~lib/env/abort
unreachable
@ -5072,7 +5072,7 @@
if
i32.const 0
i32.const 216
i32.const 189
i32.const 190
i32.const 4
call $~lib/env/abort
unreachable

View File

@ -524,7 +524,7 @@
if
i32.const 0
i32.const 216
i32.const 24
i32.const 25
i32.const 4
call $~lib/env/abort
unreachable
@ -597,7 +597,7 @@
if
i32.const 0
i32.const 216
i32.const 164
i32.const 165
i32.const 4
call $~lib/env/abort
unreachable
@ -663,7 +663,7 @@
if
i32.const 0
i32.const 216
i32.const 77
i32.const 78
i32.const 4
call $~lib/env/abort
unreachable
@ -727,7 +727,7 @@
if
i32.const 0
i32.const 216
i32.const 133
i32.const 134
i32.const 4
call $~lib/env/abort
unreachable
@ -2288,7 +2288,7 @@
if
i32.const 0
i32.const 216
i32.const 281
i32.const 282
i32.const 4
call $~lib/env/abort
unreachable
@ -2397,7 +2397,7 @@
if
i32.const 0
i32.const 216
i32.const 302
i32.const 303
i32.const 4
call $~lib/env/abort
unreachable
@ -2507,7 +2507,7 @@
if
i32.const 0
i32.const 216
i32.const 149
i32.const 150
i32.const 4
call $~lib/env/abort
unreachable
@ -3021,7 +3021,7 @@
if
i32.const 0
i32.const 216
i32.const 569
i32.const 464
i32.const 10
call $~lib/env/abort
unreachable
@ -3311,7 +3311,7 @@
if
i32.const 0
i32.const 216
i32.const 323
i32.const 324
i32.const 4
call $~lib/env/abort
unreachable
@ -3339,7 +3339,7 @@
if
i32.const 0
i32.const 216
i32.const 328
i32.const 329
i32.const 6
call $~lib/env/abort
unreachable
@ -4110,7 +4110,7 @@
if
i32.const 0
i32.const 216
i32.const 350
i32.const 351
i32.const 4
call $~lib/env/abort
unreachable
@ -6587,7 +6587,7 @@
if
i32.const 0
i32.const 216
i32.const 189
i32.const 190
i32.const 4
call $~lib/env/abort
unreachable