-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
9,042 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import * as $ from "../../mod.ts" | ||
import { decodeHex, encodeHex } from "../../mod.ts" | ||
import { benchCodec } from "../../test-util.ts" | ||
|
||
const words = await Deno.readFile("words.txt") | ||
const cargoLock = await Deno.readFile("Cargo.lock") | ||
const cases = [ | ||
new Uint8Array(), | ||
new Uint8Array(128), | ||
words, | ||
cargoLock, | ||
new Uint8Array(Array(1000).fill([...words]).flat()), | ||
] | ||
|
||
Deno.bench(" ", () => {}) | ||
|
||
for (const data of cases) { | ||
const encoded = encodeHex(data) | ||
decodeHex(encoded) | ||
Deno.bench(`encode ${data.length} bytes`, () => { | ||
encodeHex(data) | ||
}) | ||
Deno.bench(`decode ${data.length} bytes`, () => { | ||
decodeHex(encoded) | ||
}) | ||
} | ||
|
||
const $unsizedHex = $.hex($.uint8Array) | ||
for (const data of cases) { | ||
benchCodec(`$unsizedHex ${data.length} bytes`, $unsizedHex, encodeHex(data)) | ||
} | ||
|
||
for (const data of cases) { | ||
const $sizedHex = $.hex($.sizedUint8Array(data.length)) | ||
benchCodec(`$sizedHex ${data.length} bytes`, $sizedHex, encodeHex(data)) | ||
} |
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,29 @@ | ||
crate::fixtures!( | ||
<Vec<u8>>::new(), | ||
vec![0, 1, 2, 3, 4, 5u8], | ||
vec![0xde, 0xad, 0xbe, 0xef_u8], | ||
crate::CARGO_LOCK, | ||
[0u8], | ||
[1u8], | ||
[255u8], | ||
[ | ||
0xdd, 0x00, 0x00, 0xdd, 0x00, 0x00, 0xee, 0xee, 0xee, 0xee, 0x00, 0x00, | ||
0x0c, 0xc0, 0x00, 0xcc_u8, | ||
], | ||
[ | ||
0xdd, 0x00, 0x00, 0xdd, 0x00, 0x00, 0xee, 0x00, 0x00, 0x00, 0x00, 0x00, | ||
0x00, 0xcc, 0xcc, 0x00_u8, | ||
], | ||
[ | ||
0xdd, 0xdd, 0xdd, 0xdd, 0x00, 0x00, 0xee, 0xee, 0xee, 0xee, 0x00, 0x00, | ||
0x00, 0x0c, 0xc0, 0x00_u8, | ||
], | ||
[ | ||
0xdd, 0x00, 0x00, 0xdd, 0x00, 0x00, 0xee, 0x00, 0x00, 0x00, 0x00, 0x00, | ||
0x00, 0xcc, 0xcc, 0x00_u8, | ||
], | ||
[ | ||
0xdd, 0x00, 0x00, 0xdd, 0x00, 0x00, 0xee, 0xee, 0xee, 0xee, 0x00, 0x00, | ||
0x0c, 0xc0, 0x00, 0xcc_u8, | ||
], | ||
); |
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,49 @@ | ||
import { Codec, metadata, ScaleAssertError, withMetadata } from "../mod.ts" | ||
import { transform } from "./transform.ts" | ||
|
||
const encodeLookup = Array.from({ length: 256 }, (_, i) => i.toString(16).padStart(2, "0")) | ||
const decodeLookup = Array.from({ length: 128 }, (_, i) => parseInt(String.fromCharCode(i), 16) | 0) | ||
|
||
export function encodeHex(bytes: Uint8Array): string { | ||
let str = "" | ||
for (let i = 0; i < bytes.length; i++) { | ||
str += encodeLookup[bytes[i]!] | ||
} | ||
return str | ||
} | ||
|
||
export function encodeHexPrefixed(bytes: Uint8Array) { | ||
let str = "0x" | ||
for (let i = 0; i < bytes.length; i++) { | ||
str += encodeLookup[bytes[i]!] | ||
} | ||
return str | ||
} | ||
|
||
export function decodeHex(hex: string): Uint8Array { | ||
if (hex.startsWith("0x")) hex = hex.slice(2) | ||
if (hex.length % 2 === 1) hex = "0" + hex | ||
const array = new Uint8Array(hex.length >> 1) | ||
for (let i = 0; i < array.length; i++) { | ||
array[i] = (decodeLookup[hex.charCodeAt(i << 1)!]! << 4) | decodeLookup[hex.charCodeAt(i << 1 | 1)!]! | ||
} | ||
return array | ||
} | ||
|
||
const hexRegex = /^(?:0x)?[\da-f]*$/i | ||
export function hex($inner: Codec<Uint8Array>): Codec<string> { | ||
return withMetadata( | ||
metadata("$.hex", hex, $inner), | ||
transform({ | ||
$base: $inner, | ||
encode: decodeHex, | ||
decode: encodeHex, | ||
assert(assert) { | ||
assert.typeof(this, "string") | ||
if (!hexRegex.test(assert.value as string)) { | ||
throw new ScaleAssertError(this, assert.value, `${assert.path}: invalid hex`) | ||
} | ||
}, | ||
}), | ||
) | ||
} |
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
Large diffs are not rendered by default.
Oops, something went wrong.
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,47 @@ | ||
import * as $ from "../../mod.ts" | ||
import { testCodec, testInvalid } from "../../test-util.ts" | ||
import { encodeHex } from "../hex.ts" | ||
|
||
const $unsizedHex = $.hex($.uint8Array) | ||
|
||
testCodec($unsizedHex, [ | ||
"", | ||
"000102030405", | ||
"deadbeef", | ||
encodeHex(await Deno.readFile("Cargo.lock")), | ||
]) | ||
|
||
testCodec($.hex($.sizedUint8Array(1)), [ | ||
"00", | ||
"01", | ||
"ff", | ||
]) | ||
|
||
testCodec($.hex($.sizedUint8Array(16)), [ | ||
"dd0000dd0000eeeeeeee00000cc000cc", | ||
"dd0000dd0000ee000000000000cccc00", | ||
"dddddddd0000eeeeeeee0000000cc000", | ||
"dd0000dd0000ee000000000000cccc00", | ||
"dd0000dd0000eeeeeeee00000cc000cc", | ||
]) | ||
|
||
testInvalid($unsizedHex, [ | ||
null, | ||
0x1234, | ||
"0y00", | ||
"hex", | ||
"0x000000000000000000000.", | ||
new Uint8Array([0, 1, 2, 3, 4]), | ||
]) | ||
|
||
testInvalid($.hex($.sizedUint8Array(1)), [ | ||
null, | ||
0x1234, | ||
"0y00", | ||
"hex", | ||
"0x000000000000000000000.", | ||
new Uint8Array([0, 1, 2, 3, 4]), | ||
"0000", | ||
"", | ||
"-1", | ||
]) |
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
// moderate --exclude *.test.ts | ||
// moderate | ||
|
||
export * from "./assert.ts" | ||
export * from "./buffer.ts" | ||
|
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