Skip to content

Commit

Permalink
chore: use Option<u8> for c32 table rather than i8
Browse files Browse the repository at this point in the history
  • Loading branch information
zone117x committed Apr 19, 2022
1 parent b491970 commit ab381fc
Showing 1 changed file with 135 additions and 14 deletions.
149 changes: 135 additions & 14 deletions stacks-common/src/address/c32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,29 +32,151 @@ const C32_CHARACTERS: &[u8; 32] = b"0123456789ABCDEFGHJKMNPQRSTVWXYZ";
///
/// Table can be generated with:
/// ```
/// let mut table: [isize; 128] = [-1; 128];
/// let mut table: [Option<u8>; 128] = [None; 128];
/// let alphabet = "0123456789ABCDEFGHJKMNPQRSTVWXYZ";
/// for (i, x) in alphabet.as_bytes().iter().enumerate() {
/// table[*x as usize] = i as isize;
/// table[*x as usize] = Some(i as u8);
/// }
/// let alphabet_lower = alphabet.to_lowercase();
/// for (i, x) in alphabet_lower.as_bytes().iter().enumerate() {
/// table[*x as usize] = i as isize;
/// table[*x as usize] = Some(i as u8);
/// }
/// let specials = [('O', '0'), ('L', '1'), ('I', '1')];
/// for pair in specials {
/// let i = alphabet.find(|a| a == pair.1).unwrap() as isize;
/// table[pair.0 as usize] = i;
/// table[pair.0.to_ascii_lowercase() as usize] = i;
/// table[pair.0 as usize] = Some(i as u8);
/// table[pair.0.to_ascii_lowercase() as usize] = Some(i as u8);
/// }
/// ```
const C32_CHARACTERS_MAP: [i8; 128] = [
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1, -1, 10, 11, 12, 13, 14, 15, 16, 17, 1,
18, 19, 1, 20, 21, 0, 22, 23, 24, 25, 26, -1, 27, 28, 29, 30, 31, -1, -1, -1, -1, -1, -1, 10,
11, 12, 13, 14, 15, 16, 17, 1, 18, 19, 1, 20, 21, 0, 22, 23, 24, 25, 26, -1, 27, 28, 29, 30,
31, -1, -1, -1, -1, -1,
const C32_CHARACTERS_MAP: [Option<u8>; 128] = [
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
Some(0),
Some(1),
Some(2),
Some(3),
Some(4),
Some(5),
Some(6),
Some(7),
Some(8),
Some(9),
None,
None,
None,
None,
None,
None,
None,
Some(10),
Some(11),
Some(12),
Some(13),
Some(14),
Some(15),
Some(16),
Some(17),
Some(1),
Some(18),
Some(19),
Some(1),
Some(20),
Some(21),
Some(0),
Some(22),
Some(23),
Some(24),
Some(25),
Some(26),
None,
Some(27),
Some(28),
Some(29),
Some(30),
Some(31),
None,
None,
None,
None,
None,
None,
Some(10),
Some(11),
Some(12),
Some(13),
Some(14),
Some(15),
Some(16),
Some(17),
Some(1),
Some(18),
Some(19),
Some(1),
Some(20),
Some(21),
Some(0),
Some(22),
Some(23),
Some(24),
Some(25),
Some(26),
None,
Some(27),
Some(28),
Some(29),
Some(30),
Some(31),
None,
None,
None,
None,
None,
];

fn c32_encode(input_bytes: &[u8]) -> String {
Expand Down Expand Up @@ -120,8 +242,7 @@ fn c32_decode_ascii(input_str: &str) -> Result<Vec<u8>, Error> {
.as_bytes()
.iter()
.rev()
.filter_map(|x| C32_CHARACTERS_MAP.get(*x as usize))
.filter_map(|v| u8::try_from(*v).ok())
.filter_map(|x| C32_CHARACTERS_MAP[*x as usize])
.collect();

if input_str.len() != iter_c32_digits.len() {
Expand Down

0 comments on commit ab381fc

Please sign in to comment.