-
Notifications
You must be signed in to change notification settings - Fork 20
No alloc #34
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
No alloc #34
Changes from all commits
649d301
251e7dc
18958a3
8f3d7a9
a59e862
ee888d9
cedec9e
78b1c3a
0975714
0f0a6d4
f79b055
f88b8d2
d17e50f
b5f9af5
c25c21e
0bb7ff3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,21 @@ | ||
use data_encoding::Encoding; | ||
use data_encoding_macro::new_encoding; | ||
|
||
// At a later point (probably when floating point arithmatic in const fn is stable) the below functions can be const | ||
|
||
#[cfg(feature = "alloc")] | ||
/// math comes from here https://github.com/bitcoin/bitcoin/blob/f1e2f2a85962c1664e4e55471061af0eaa798d40/src/base58.cpp#L94 | ||
pub(crate) fn calc_encoded_size(base: usize, input_byte_size: usize) -> usize { | ||
(input_byte_size as f64 * (f64::log10(256.0) / f64::log10(base as f64))) as usize + 1 | ||
} | ||
|
||
#[cfg(feature = "alloc")] | ||
/// math comes from here https://github.com/bitcoin/bitcoin/blob/f1e2f2a85962c1664e4e55471061af0eaa798d40/src/base58.cpp#L48 | ||
pub(crate) fn calc_decoded_size(base: usize, input_byte_size: usize) -> usize { | ||
f64::ceil(input_byte_size as f64 * (f64::log10(base as f64) / f64::log10(256.0)) + 1.0) as usize | ||
// this shouldn't need an extra one or ceiling, something is wrong somewhere | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agreed. This should ideally be: (input_byte_size as f64 * (f64::log2(base as f64) / 8.0))) as usize
// or
(input_byte_size as f64 / (8.0 / f64::log2(base as f64))) as usize There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I definitely agree, but for whatever reason it fails all test cases without the round up and added 1... Just rounding up gives one short for base10 in the
I will double check to make sure I am doing the tests correctly before I think about possible issues with base-x There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's good that we have that test :) Your code is using |
||
} | ||
|
||
// Base2 (alphabet: 01) | ||
pub const BASE2: Encoding = new_encoding! { | ||
symbols: "01", | ||
|
@@ -11,6 +26,7 @@ pub const BASE8: Encoding = new_encoding! { | |
symbols: "01234567", | ||
}; | ||
|
||
#[cfg(feature = "alloc")] | ||
/// Base10 (alphabet: 0123456789) | ||
pub const BASE10: &str = "0123456789"; | ||
|
||
|
@@ -85,15 +101,19 @@ pub const BASE32Z: Encoding = new_encoding! { | |
symbols: "ybndrfg8ejkmcpqxot1uwisza345h769", | ||
}; | ||
|
||
#[cfg(feature = "alloc")] | ||
/// Base36, [0-9a-z] no padding (alphabet: 0123456789abcdefghijklmnopqrstuvwxyz). | ||
pub const BASE36_LOWER: &str = "0123456789abcdefghijklmnopqrstuvwxyz"; | ||
|
||
#[cfg(feature = "alloc")] | ||
/// Base36, [0-9A-Z] no padding (alphabet: 0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ). | ||
pub const BASE36_UPPER: &str = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; | ||
|
||
#[cfg(feature = "alloc")] | ||
// Base58 Flickr's alphabet for creating short urls from photo ids. | ||
pub const BASE58_FLICKR: &str = "123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ"; | ||
|
||
#[cfg(feature = "alloc")] | ||
// Base58 Bitcoin's alphabet as defined in their Base58Check encoding. | ||
pub const BASE58_BITCOIN: &str = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"; | ||
|
||
|
Uh oh!
There was an error while loading. Please reload this page.