spinoso_symbol/casecmp/
ascii.rs

1//! ASCII case folding comparisons for byte content resolved from `Symbol`s.
2
3use core::cmp::Ordering;
4
5use artichoke_core::intern::Intern;
6
7/// Compare the byte contents of two symbols using ASCII case-insensitive
8/// comparison.
9///
10/// The byte slice associated with each symbol is resolved via the given
11/// interner. Unresolved symbols are compared as if they resolve to `&[]`.
12///
13/// This function can be used to implement [`Symbol#casecmp`] for the [`Symbol`]
14/// type defined in Ruby Core.
15///
16/// # Errors
17///
18/// If the interner returns an error while retrieving a symbol, that error is
19/// returned. See [`Intern::lookup_symbol`].
20///
21/// [`Symbol#casecmp`]: https://ruby-doc.org/core-3.1.2/Symbol.html#method-i-casecmp
22/// [`Symbol`]: https://ruby-doc.org/core-3.1.2/Symbol.html
23#[inline]
24#[cfg_attr(docsrs, doc(cfg(feature = "artichoke")))]
25pub fn casecmp<T, U>(interner: &T, left: U, right: U) -> Result<Ordering, T::Error>
26where
27    T: Intern<Symbol = U>,
28    U: Copy,
29{
30    let left = interner.lookup_symbol(left)?.unwrap_or_default();
31    let right = interner.lookup_symbol(right)?.unwrap_or_default();
32    Ok(focaccia::ascii_casecmp(left, right))
33}