#[inline] fn finish(self) -> Ordering { if matches!(self.result, Ordering::Equal) && !self.code_units.is_empty() { // Self is longer than Other
Ordering::Greater
} else { self.result
}
}
}
/// Compares the contents of a [`Writeable`] to the given UTF-8 bytes without allocating memory. /// /// For more details, see: [`cmp_str`] pubfn cmp_utf8(writeable: &impl Writeable, other: &[u8]) -> Ordering { letmut wc = WriteComparator::new(other); let _ = writeable.write_to(&mut wc);
wc.finish().reverse()
}
/// Compares the contents of a `Writeable` to the given bytes /// without allocating a String to hold the `Writeable` contents. /// /// This returns a lexicographical comparison, the same as if the Writeable /// were first converted to a String and then compared with `Ord`. For a /// string ordering suitable for display to end users, use a localized /// collation crate, such as `icu_collator`. /// /// # Examples /// /// ``` /// use core::cmp::Ordering; /// use core::fmt; /// use writeable::Writeable; /// /// struct WelcomeMessage<'s> { /// pub name: &'s str, /// } /// /// impl<'s> Writeable for WelcomeMessage<'s> { /// // see impl in Writeable docs /// # fn write_to<W: fmt::Write + ?Sized>(&self, sink: &mut W) -> fmt::Result { /// # sink.write_str("Hello, ")?; /// # sink.write_str(self.name)?; /// # sink.write_char('!')?; /// # Ok(()) /// # } /// } /// /// let message = WelcomeMessage { name: "Alice" }; /// let message_str = message.write_to_string(); /// /// assert_eq!(Ordering::Equal, writeable::cmp_str(&message, "Hello, Alice!")); /// /// assert_eq!(Ordering::Greater, writeable::cmp_str(&message, "Alice!")); /// assert_eq!(Ordering::Greater, (*message_str).cmp("Alice!")); /// /// assert_eq!(Ordering::Less, writeable::cmp_str(&message, "Hello, Bob!")); /// assert_eq!(Ordering::Less, (*message_str).cmp("Hello, Bob!")); /// ``` #[inline] pubfn cmp_str(writeable: &impl Writeable, other: &str) -> Ordering {
cmp_utf8(writeable, other.as_bytes())
}
#[cfg(test)] mod tests { usesuper::*; use core::fmt::Write;
mod data {
include!("../tests/data/data.rs");
}
#[test] fn test_write_char() { for a in data::KEBAB_CASE_STRINGS { for b in data::KEBAB_CASE_STRINGS { letmut wc = WriteComparator::new(a.as_bytes()); for ch in b.chars() {
wc.write_char(ch).unwrap();
}
assert_eq!(a.cmp(b), wc.finish(), "{a} <=> {b}");
}
}
}
#[test] fn test_write_str() { for a in data::KEBAB_CASE_STRINGS { for b in data::KEBAB_CASE_STRINGS { letmut wc = WriteComparator::new(a.as_bytes());
wc.write_str(b).unwrap();
assert_eq!(a.cmp(b), wc.finish(), "{a} <=> {b}");
}
}
}
#[test] fn test_mixed() { for a in data::KEBAB_CASE_STRINGS { for b in data::KEBAB_CASE_STRINGS { letmut wc = WriteComparator::new(a.as_bytes()); letmut first = true; for substr in b.split('-') { if first {
first = false;
} else {
wc.write_char('-').unwrap();
}
wc.write_str(substr).unwrap();
}
assert_eq!(a.cmp(b), wc.finish(), "{a} <=> {b}");
}
}
}
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.