Skip to content

Commit

Permalink
prep for github
Browse files Browse the repository at this point in the history
  • Loading branch information
rogusdev committed Dec 7, 2023
1 parent 494c6f1 commit e13376d
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 1 deletion.
16 changes: 15 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,16 @@
target
# Generated by Cargo
# will have compiled files and executables
debug/
target/

# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html
Cargo.lock

# These are backup files generated by rustfmt
**/*.rs.bk

# MSVC Windows builds of rustc generate these, which store debugging information
*.pdb

.vscode
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2023 Chris Rogus

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# fast32

Base32 and base64 encoding in Rust. Primarily for integer (u64, u128) and UUID identifiers (behind feature `uuid`), as well as arbitrary bytes. And do it all very quickly (more on this below).

Note that by default, encoding an integer into base32 or base64 via normal algorithms does not "look like" a number -- notably the rightmost character usually looks off, and there are sometimes more characters than there needs to be. This might be a plus for obfuscation, barely, but it makes them hard to reason about quickly, and it's also more efficient (slightly) to process them as integers rather than arbitrary arrays of bytes (because we know upfront that integers are always a small size).

For example, the integer `31` processed normally, as bytes, into official [RFC 4648](https://datatracker.ietf.org/doc/html/rfc4648) base32 hex, without padding, will come out as `"D4"`. In contrast, processing it as an integer, as this library can, will come out as `"Z"` which is more intuitively one less than `32` at `"10"`, as one might hope (vs `"EA"` in base32 hex -- note that's an `A` not a `4` so string changed nonintuitively for an increment of 1). This is helpful with "nice looking" urls of base32 encodings of identifiers, etc.

### Speed

This is intended to be as fast as basically possible, while still keeping an intuitive interface. It is, per the bench comparisons in this repo, on my machine, about 55-100%+ faster than the closest [alternative](https://github.com/archer884/crockford) for decoding u64s, and 20-60% faster for encoding u64s. It is also 26-28% faster on decoding raw bytes and 37-40% faster on encoding raw bytes than the closest [other alternative](https://github.com/ia0/data-encoding) (that is significantly more mature but does not offer this crate's integer encoding/decoding, on top of the slower performance). There is an earlier and [seemingly long abandoned](https://github.com/andreasots/base32) alternative that is vastly slower than all of these, but still somewhat popular.

### Summary

In short, this crate should do everything you want for base32 and base64 encoding (please [raise an issue](https://github.com/rogusdev/fast32/issues) if it doesn't!) while doing all of it very quickly and conveniently.
9 changes: 9 additions & 0 deletions tests/base32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,15 @@ fn both_bytes_32() {
assert_eq!(CROCKFORD.decode_bytes(s.as_bytes()).unwrap(), n);
}

#[test]
fn both_bytes_32_rfc4648() {
let n = &[0x20];
let x = "EA";
let s = RFC4648_NOPAD.encode_bytes(n);
assert_eq!(s, x);
assert_eq!(RFC4648_NOPAD.decode_bytes(s.as_bytes()).unwrap(), n);
}

#[test]
fn both_u64_33() {
let n = 33;
Expand Down

0 comments on commit e13376d

Please sign in to comment.