From dc6e0c3591709d6f8887bb672af1de54dfc8a974 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Iwa=C5=84czuk?= Date: Sun, 15 Mar 2020 15:31:55 +0100 Subject: feat: Deno.core.{encode,decode}; standalone UTF-8 encoding/decoding (#4349) This commits add two new methods to "Deno.core" namespace: "encode" and "decode". Those methods are bound in Rust to provide a) fast b) generally available of encoding and decoding UTF-8 strings. Both methods are now used in "cli/js/dispatch_json.ts". --- core/encode_decode_test.js | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 core/encode_decode_test.js (limited to 'core/encode_decode_test.js') diff --git a/core/encode_decode_test.js b/core/encode_decode_test.js new file mode 100644 index 000000000..8a366dd66 --- /dev/null +++ b/core/encode_decode_test.js @@ -0,0 +1,40 @@ +// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. + +function assertArrayEquals(a1, a2) { + if (a1.length !== a2.length) throw Error("assert"); + + for (const index in a1) { + if (a1[index] !== a2[index]) { + throw Error("assert"); + } + } +} + +function main() { + // prettier-ignore + const fixture1 = [ + 0xf0, 0x9d, 0x93, 0xbd, + 0xf0, 0x9d, 0x93, 0xae, + 0xf0, 0x9d, 0x94, 0x81, + 0xf0, 0x9d, 0x93, 0xbd + ]; + // prettier-ignore + const fixture2 = [ + 72, 101, 108, 108, + 111, 32, 239, 191, + 189, 239, 191, 189, + 32, 87, 111, 114, + 108, 100 + ]; + + assertArrayEquals(Array.from(Deno.core.encode("𝓽𝓮𝔁𝓽")), fixture1); + assertArrayEquals( + Array.from(Deno.core.encode("Hello \udc12\ud834 World")), + fixture2 + ); + + assert(Deno.core.decode(new Uint8Array(fixture1)) === "𝓽𝓮𝔁𝓽"); + assert(Deno.core.decode(new Uint8Array(fixture2)) === "Hello �� World"); +} + +main(); -- cgit v1.2.3