Skip to content

Commit

Permalink
Replace ad-hoc eq_ignore_ascii_case with slice::eq_ignore_ascii_case
Browse files Browse the repository at this point in the history
The latter has been available since rust 1.23.0.
  • Loading branch information
glandium committed Jun 8, 2022
1 parent f3ff2d9 commit 975d38a
Showing 1 changed file with 2 additions and 21 deletions.
23 changes: 2 additions & 21 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -538,32 +538,13 @@ fn ok_or<T, E>(t: Option<T>, e: E) -> Result<T, E> {
}
}

// Reimplemented here because std::ascii is not available in libcore
fn eq_ignore_ascii_case(a: &str, b: &str) -> bool {
fn to_ascii_uppercase(c: u8) -> u8 {
if c >= b'a' && c <= b'z' {
c - b'a' + b'A'
} else {
c
}
}

if a.len() == b.len() {
a.bytes()
.zip(b.bytes())
.all(|(a, b)| to_ascii_uppercase(a) == to_ascii_uppercase(b))
} else {
false
}
}

impl FromStr for Level {
type Err = ParseLevelError;
fn from_str(level: &str) -> Result<Level, Self::Err> {
ok_or(
LOG_LEVEL_NAMES
.iter()
.position(|&name| eq_ignore_ascii_case(name, level))
.position(|&name| name.eq_ignore_ascii_case(level))
.into_iter()
.filter(|&idx| idx != 0)
.map(|idx| Level::from_usize(idx).unwrap())
Expand Down Expand Up @@ -744,7 +725,7 @@ impl FromStr for LevelFilter {
ok_or(
LOG_LEVEL_NAMES
.iter()
.position(|&name| eq_ignore_ascii_case(name, level))
.position(|&name| name.eq_ignore_ascii_case(level))
.map(|p| LevelFilter::from_usize(p).unwrap()),
ParseLevelError(()),
)
Expand Down

0 comments on commit 975d38a

Please sign in to comment.