Skip to content

Commit

Permalink
feat(oiddb): make name search case-insensitive (#469)
Browse files Browse the repository at this point in the history
Signed-off-by: Nathaniel McCallum <nathaniel@profian.com>
  • Loading branch information
npmccallum committed Mar 7, 2022
1 parent f7270a0 commit 6c7860f
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions const-oid/src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,24 @@ const fn eq(lhs: &[u8], rhs: &[u8]) -> bool {
true
}

/// A const implementation of case-insensitive ASCII equals.
const fn eq_case(lhs: &[u8], rhs: &[u8]) -> bool {
if lhs.len() != rhs.len() {
return false;
}

let mut i = 0usize;
while i < lhs.len() {
if !lhs[i].eq_ignore_ascii_case(&rhs[i]) {
return false;
}

i += 1;
}

true
}

/// A query interface for OIDs/Names.
pub struct Database<'a>(&'a [(&'a ObjectIdentifier, &'a str)]);

Expand Down Expand Up @@ -72,7 +90,7 @@ impl<'a> Database<'a> {

while i < self.0.len() {
let lhs = self.0[i].1;
if eq(lhs.as_bytes(), name.as_bytes()) {
if eq_case(lhs.as_bytes(), name.as_bytes()) {
return Some(self.0[i].0);
}

Expand Down Expand Up @@ -100,7 +118,7 @@ mod tests {

#[test]
fn by_name() {
let cn = super::DB.by_name("cn").expect("cn not found");
let cn = super::DB.by_name("CN").expect("cn not found");
assert_eq!(&CN, cn);

assert_eq!(None, super::DB.by_name("purplePeopleEater"));
Expand Down

0 comments on commit 6c7860f

Please sign in to comment.