rogusdev / fast32

Base 32 + 64 encoding and decoding identifiers + bytes in rust, quickly
MIT License
11 stars 0 forks source link

Provide lowercase alphabets #1

Closed russellbanks closed 11 months ago

russellbanks commented 11 months ago

I want to encode into lowercase crockford Base32. Currently I use to_ascii_lowercase to do this in-place. Although it avoids character tables and doesn't support crockford encoding, an alternative project, base32ct, provides a means of encoding directly into lowercase base32.

russellbanks commented 11 months ago

I've been able to achieve this using make_base32_alpha, although it would be nice if this was included by default.

const CROCKFORD_CHARACTER_TABLE: &[u8; 32] = b"0123456789abcdefghjkmnpqrstvwxyz";
make_base32_alpha!(
    CROCKFORD_LOWER,
    ENC_CROCKFORD_LOWER,
    CROCKFORD_CHARACTER_TABLE
);

One thing I noticed from the macro expansion of this is that the constants are public. As I'm using this in a library, I don't want to expose these to the user.

pub const ENC_CROCKFORD_LOWER: [u8; 256] = decoder_map_simple(CROCKFORD_CHARACTER_TABLE);
pub const CROCKFORD_LOWER: Alphabet32Nopad = Alphabet32Nopad::new(CROCKFORD_CHARACTER_TABLE, &ENC_CROCKFORD_LOWER);
rogusdev commented 11 months ago

Lowercase alphabets are examples in a variety of places throughout the documentation and tests, such as: https://github.com/rogusdev/fast32/blob/03778d938b821f59d52bb35578a122b17e32a328/tests/alphabets.rs#L7

You can look at those and see how crockford lower can be setup as a one liner, although I can make that available as part of the library if that is really desirable (I use the lowercase version as well).

As for your second point: when variables or constants are public inside your library, that does not expose them to users of your library. Instead, you must explicitly pub use... them to export them as part of the interface. You can see that here, for example: https://github.com/rogusdev/fast32/blob/03778d938b821f59d52bb35578a122b17e32a328/src/lib.rs#L123 and also https://github.com/rogusdev/fast32/blob/03778d938b821f59d52bb35578a122b17e32a328/src/base32.rs#L27 although this is a bit indirect, as the submodule must also be exported in https://github.com/rogusdev/fast32/blob/03778d938b821f59d52bb35578a122b17e32a328/src/lib.rs#L117 . So you don't need to worry about that.

rogusdev commented 11 months ago

I added this officially: https://github.com/rogusdev/fast32/commit/2e95be512064aca520d8594a12d2e38d3a5ae029 (and published 1.0.1 to include it)

russellbanks commented 11 months ago

Thanks @rogusdev! This library made it really easy to replace my manual implementation of crockford encoding - https://github.com/russellbanks/package-family-name/commit/c3cebe18f804a25fce6ea774617fa97d01627b42 :)