Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

const-oid: return all matched names when looking up oid #1129

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions const-oid/src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ const fn eq_case(lhs: &[u8], rhs: &[u8]) -> bool {
}

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

impl<'a> Database<'a> {
Expand Down Expand Up @@ -82,6 +83,43 @@ impl<'a> Database<'a> {

None
}

/// Return the list of matched name for the OID.
pub const fn find_names_for_oid(&self, oid: ObjectIdentifier) -> Names<'a> {
Names {
database: *self,
oid,
position: 0,
}
}
}

/// Iterator returning the multiple names that may be associated with an OID.
pub struct Names<'a> {
database: Database<'a>,
oid: ObjectIdentifier,
position: usize,
}

impl<'a> Iterator for Names<'a> {
type Item = &'a str;

fn next(&mut self) -> Option<&'a str> {
let mut i = self.position;

while i < self.database.0.len() {
let lhs = self.database.0[i].0;

if lhs.buffer.eq(&self.oid.buffer) {
self.position = i + 1;
return Some(self.database.0[i].1);
}

i += 1;
}

None
}
}

#[cfg(test)]
Expand Down