forked from denoland/deno
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Deno.core.{encode,decode}; standalone UTF-8 encoding/decoding (d…
…enoland#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".
- Loading branch information
1 parent
ec3f445
commit dc6e0c3
Showing
8 changed files
with
198 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
const mixed = new TextEncoder().encode("@Ā๐😀"); | ||
|
||
function generateRandom(bytes) { | ||
const result = new Uint8Array(bytes); | ||
let i = 0; | ||
while (i < bytes) { | ||
const toAdd = Math.floor(Math.random() * Math.min(4, bytes - i)); | ||
switch (toAdd) { | ||
case 0: | ||
result[i] = mixed[0]; | ||
i++; | ||
break; | ||
case 1: | ||
result[i] = mixed[1]; | ||
result[i + 1] = mixed[2]; | ||
i += 2; | ||
break; | ||
case 2: | ||
result[i] = mixed[3]; | ||
result[i + 1] = mixed[4]; | ||
result[i + 2] = mixed[5]; | ||
i += 3; | ||
break; | ||
case 3: | ||
result[i] = mixed[6]; | ||
result[i + 1] = mixed[7]; | ||
result[i + 2] = mixed[8]; | ||
result[i + 3] = mixed[9]; | ||
i += 4; | ||
break; | ||
} | ||
} | ||
return result; | ||
} | ||
|
||
const randomData = generateRandom(1024); | ||
for (let i = 0; i < 10_000; i++) Deno.core.decode(randomData); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
const mixed = "@Ā๐😀"; | ||
|
||
function generateRandom(bytes) { | ||
let result = ""; | ||
let i = 0; | ||
while (i < bytes) { | ||
const toAdd = Math.floor(Math.random() * Math.min(4, bytes - i)); | ||
switch (toAdd) { | ||
case 0: | ||
result += mixed[0]; | ||
i++; | ||
break; | ||
case 1: | ||
result += mixed[1]; | ||
i++; | ||
break; | ||
case 2: | ||
result += mixed[2]; | ||
i++; | ||
break; | ||
case 3: | ||
result += mixed[3]; | ||
result += mixed[4]; | ||
i += 2; | ||
break; | ||
} | ||
} | ||
return result; | ||
} | ||
|
||
const randomData = generateRandom(1024); | ||
for (let i = 0; i < 10_000; i++) Deno.core.encode(randomData); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters