Skip to content

Commit

Permalink
Add BorrowedBytes::cmp_first_item()
Browse files Browse the repository at this point in the history
  • Loading branch information
sile committed Nov 30, 2023
1 parent a4aed27 commit d2ec1a6
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ extern crate bitflags;
#[cfg(test)]
extern crate rand;

use std::cmp::Ordering;

pub use map::{GenericPatriciaMap, PatriciaMap, StringPatriciaMap};
pub use set::{GenericPatriciaSet, PatriciaSet, StringPatriciaSet};

Expand Down Expand Up @@ -70,6 +72,16 @@ pub trait BorrowedBytes {

/// Returns a suffix of this instance not containing the common prefix with the given bytes.
fn strip_common_prefix(&self, bytes: &[u8]) -> &Self;

/// Same as [`strip_common_prefix()`], but also returns the length of the common prefix.
fn strip_common_prefix_and_len(&self, bytes: &[u8]) -> (&Self, usize) {
let next = self.strip_common_prefix(bytes);
let common_prefix_len = self.as_bytes().len() - next.as_bytes().len();
(next, common_prefix_len)
}

/// Compares the first item of this instance with the first item represented in the the given bytes.
fn cmp_first_item(&self, bytes: &[u8]) -> Ordering;
}

impl BorrowedBytes for [u8] {
Expand All @@ -93,6 +105,10 @@ impl BorrowedBytes for [u8] {
.count();
&self[i..]
}

fn cmp_first_item(&self, bytes: &[u8]) -> Ordering {
self.first().cmp(&bytes.first())
}
}

impl BorrowedBytes for str {
Expand Down Expand Up @@ -120,4 +136,10 @@ impl BorrowedBytes for str {
}
""
}

fn cmp_first_item(&self, bytes: &[u8]) -> Ordering {
self.chars()
.next()
.cmp(&Self::from_bytes(bytes).chars().next())
}
}

0 comments on commit d2ec1a6

Please sign in to comment.