1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
//! ASCII case folding comparisons for byte content resolved from `Symbol`s.

use artichoke_core::intern::Intern;
use core::cmp::Ordering;

/// Compare the byte contents of two symbols using ASCII case-insensitive
/// comparison.
///
/// The byteslice associated with each symbol is resolved via the given
/// interner. Unresolved symbols are compared as if they resolve to `&[]`.
///
/// This function can be used to implement [`Symbol#casecmp`] for the [`Symbol`]
/// type defined in Ruby Core.
///
/// # Errors
///
/// If the interner returns an error while retrieving a symbol, that error is
/// returned. See [`Intern::lookup_symbol`].
///
/// [`Symbol#casecmp`]: https://ruby-doc.org/core-2.6.3/Symbol.html#method-i-casecmp
/// [`Symbol`]: https://ruby-doc.org/core-2.6.3/Symbol.html
#[inline]
#[cfg_attr(docsrs, doc(cfg(feature = "artichoke")))]
pub fn casecmp<T, U>(interner: &T, left: U, right: U) -> Result<Ordering, T::Error>
where
    T: Intern<Symbol = U>,
    U: Copy,
{
    let left = interner.lookup_symbol(left)?.unwrap_or_default();
    let right = interner.lookup_symbol(right)?.unwrap_or_default();
    Ok(focaccia::ascii_casecmp(left, right))
}