Skip to content

Commit e1452a6

Browse files
committed
Handle empty input as per rfc4648
1 parent 0c64c24 commit e1452a6

File tree

1 file changed

+5
-9
lines changed

1 file changed

+5
-9
lines changed

src/lib.rs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,6 @@ pub enum DecodeError {
4040
/// The decoded number cannot fit into a [`u128`].
4141
ArithmeticOverflow,
4242

43-
/// The encoded input is an empty string.
44-
EmptyInput,
45-
4643
/// The encoded input contains the given invalid byte at the given index.
4744
InvalidBase62Byte(u8, usize),
4845
}
@@ -53,7 +50,6 @@ impl core::fmt::Display for DecodeError {
5350
DecodeError::ArithmeticOverflow => {
5451
f.write_str("Decoded number cannot fit into a `u128`")
5552
}
56-
DecodeError::EmptyInput => f.write_str("Encoded input is an empty string"),
5753
DecodeError::InvalidBase62Byte(ch, idx) => {
5854
use core::fmt::Write;
5955

@@ -346,7 +342,7 @@ macro_rules! internal_decoder_fn {
346342
($fn_name:ident, $numeric_start_value:expr, $uppercase_start_value:expr, $lowercase_start_value:expr) => {
347343
fn $fn_name(mut input: &[u8]) -> Result<u128, DecodeError> {
348344
if input.is_empty() {
349-
return Err(DecodeError::EmptyInput);
345+
return Ok(0);
350346
}
351347

352348
// Remove leading zeroes
@@ -823,7 +819,7 @@ mod tests {
823819

824820
quickcheck! {
825821
fn decode_bad(input: Vec<u8>) -> TestResult {
826-
if !input.is_empty() && input.iter().all(|ch| ch.is_ascii_alphanumeric()) {
822+
if input.iter().all(|ch| ch.is_ascii_alphanumeric()) {
827823
TestResult::discard()
828824
} else {
829825
TestResult::from_bool(decode(&input).is_err())
@@ -833,7 +829,7 @@ mod tests {
833829

834830
quickcheck! {
835831
fn decode_good(input: Vec<u8>) -> TestResult {
836-
if !input.is_empty() && input.iter().all(|ch| ch.is_ascii_alphanumeric()) {
832+
if input.iter().all(|ch| ch.is_ascii_alphanumeric()) {
837833
TestResult::from_bool(decode(&input).is_ok())
838834
} else {
839835
TestResult::discard()
@@ -880,7 +876,7 @@ mod tests {
880876

881877
#[test]
882878
fn test_decode_empty_input() {
883-
assert_eq!(decode(""), Err(DecodeError::EmptyInput));
879+
assert_eq!(decode(""), Ok(0));
884880
}
885881

886882
#[test]
@@ -965,7 +961,7 @@ mod tests {
965961

966962
#[test]
967963
fn test_decode_alternative_empty_input() {
968-
assert_eq!(decode_alternative(""), Err(DecodeError::EmptyInput));
964+
assert_eq!(decode_alternative(""), Ok(0));
969965
}
970966

971967
#[test]

0 commit comments

Comments
 (0)