Optional length when encoding
This commit is contained in:
parent
8605cf6e41
commit
8071a7b931
2 changed files with 21 additions and 3 deletions
|
@ -81,10 +81,19 @@ const BASE38_CHARACTERS_NEEDED_IN_NBYTES_CHUNK: [u8; 3] = [2, 4, 5];
|
||||||
const RADIX: u32 = BASE38_CHARS.len() as u32;
|
const RADIX: u32 = BASE38_CHARS.len() as u32;
|
||||||
|
|
||||||
/// Encode a byte array into a base38 string.
|
/// Encode a byte array into a base38 string.
|
||||||
pub fn encode(bytes: &[u8], length: usize) -> String {
|
///
|
||||||
|
/// # Arguments
|
||||||
|
/// * `bytes` - byte array to encode
|
||||||
|
/// * `length` - optional length of the byte array to encode. If not specified, the entire byte array is encoded.
|
||||||
|
pub fn encode(bytes: &[u8], length: Option<usize>) -> String {
|
||||||
let mut offset = 0;
|
let mut offset = 0;
|
||||||
let mut result = String::new();
|
let mut result = String::new();
|
||||||
|
|
||||||
|
// if length is specified, use it, otherwise use the length of the byte array
|
||||||
|
// if length is specified but is greater than the length of the byte array, use the length of the byte array
|
||||||
|
let b_len = bytes.len();
|
||||||
|
let length = length.map(|l| l.min(b_len)).unwrap_or(b_len);
|
||||||
|
|
||||||
while offset < length {
|
while offset < length {
|
||||||
let remaining = length - offset;
|
let remaining = length - offset;
|
||||||
match remaining.cmp(&2) {
|
match remaining.cmp(&2) {
|
||||||
|
@ -125,6 +134,11 @@ fn encode_base38(mut value: u32, char_count: u8) -> String {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Decode a base38-encoded string into a byte slice
|
/// Decode a base38-encoded string into a byte slice
|
||||||
|
///
|
||||||
|
/// # Arguments
|
||||||
|
/// * `base38_str` - base38-encoded string to decode
|
||||||
|
///
|
||||||
|
/// Fails if the string contains invalid characters
|
||||||
pub fn decode(base38_str: &str) -> Result<Vec<u8>, Error> {
|
pub fn decode(base38_str: &str) -> Result<Vec<u8>, Error> {
|
||||||
let mut result = Vec::new();
|
let mut result = Vec::new();
|
||||||
let mut base38_characters_number: usize = base38_str.len();
|
let mut base38_characters_number: usize = base38_str.len();
|
||||||
|
@ -197,7 +211,11 @@ mod tests {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn can_base38_encode() {
|
fn can_base38_encode() {
|
||||||
assert_eq!(encode(&DECODED, 11), ENCODED);
|
assert_eq!(encode(&DECODED, None), ENCODED);
|
||||||
|
assert_eq!(encode(&DECODED, Some(11)), ENCODED);
|
||||||
|
|
||||||
|
// length is greater than the length of the byte array
|
||||||
|
assert_eq!(encode(&DECODED, Some(12)), ENCODED);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
|
|
@ -354,7 +354,7 @@ fn payload_base38_representation_with_tlv(
|
||||||
}
|
}
|
||||||
|
|
||||||
let bytes_written = generate_bit_set(payload, bits, tlv_data)?;
|
let bytes_written = generate_bit_set(payload, bits, tlv_data)?;
|
||||||
let base38_encoded = base38::encode(&*bits, bytes_written);
|
let base38_encoded = base38::encode(&*bits, Some(bytes_written));
|
||||||
Ok(format!("MT:{}", base38_encoded))
|
Ok(format!("MT:{}", base38_encoded))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue