Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add $.hex codec #154

Merged
merged 1 commit into from
Apr 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions codecs/bench/hex.bench.ts
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))
}
29 changes: 29 additions & 0 deletions codecs/fixtures/hex.rs
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,
],
);
49 changes: 49 additions & 0 deletions codecs/hex.ts
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`)
}
},
}),
)
}
1 change: 1 addition & 0 deletions codecs/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export * from "./compact.ts"
export * from "./constant.ts"
export * from "./deferred.ts"
export * from "./float.ts"
export * from "./hex.ts"
export * from "./instance.ts"
export * from "./int.ts"
export * from "./iterable.ts"
Expand Down
8,876 changes: 8,876 additions & 0 deletions codecs/test/__snapshots__/hex.test.ts.snap

Large diffs are not rendered by default.

47 changes: 47 additions & 0 deletions codecs/test/hex.test.ts
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",
])
2 changes: 1 addition & 1 deletion common/mod.ts
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"
Expand Down
3 changes: 3 additions & 0 deletions fixtures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ pub mod deferred_fixtures;
#[path = "./codecs/fixtures/float.rs"]
pub mod float_fixtures;

#[path = "./codecs/fixtures/hex.rs"]
pub mod hex_fixtures;

#[path = "./codecs/fixtures/int.rs"]
pub mod int_fixtures;

Expand Down