Expand description
This crate provides Unicode case mapping routines and iterators for conventionally UTF-8 binary strings.
Unicode case mapping or case conversion can be used to transform the characters in a string. To quote the Unicode FAQ:
Case mapping or case conversion is a process whereby strings are converted to a particular form—uppercase, lowercase, or titlecase—possibly for display to the user.
Roe supports full Unicode, Turkic, and ASCII lowercase, uppercase, and
capitalization and swapcase mappings. Full Unicode case folding is available through
LowercaseMode::Fold. Invalid UTF-8 is preserved.
Mappings use bundled Unicode 18.0.0 tables, independent of the Rust compiler. Like MRI Ruby, mappings are context-independent and Lithuanian mode is an alias for full Unicode mapping. The API is still evolving ahead of 1.0.
§Usage
You can convert case like:
assert_eq!(
roe::lowercase(b"Artichoke Ruby", LowercaseMode::Ascii).collect::<Vec<_>>(),
b"artichoke ruby"
);
assert_eq!(
roe::uppercase("Αύριο".as_bytes(), UppercaseMode::Full).collect::<Vec<_>>(),
"ΑΎΡΙΟ".as_bytes()
);
assert_eq!(
capitalize("ffi".as_bytes(), CapitalizeMode::Full).collect::<Vec<_>>(),
"Ffi".as_bytes()
);Roe provides fast path routines that assume the byte slice is ASCII-only.
To detect a change without allocating, compare the mapped byte iterator with the original bytes:
let input = b"artichoke";
assert!(lowercase(input, LowercaseMode::Full).eq(input.iter().copied()));§Crate Features
Roe is no_std compatible with an optional dependency on the alloc
crate.
The alloc feature is enabled by default and provides APIs that allocate
String or Vec. Disable default features to use Roe without allocation.
Modules§
- ruby
- Case mapping with Ruby string semantics.
- unicode_
terms doc - Roe is derived from Unicode Data Files and is subject to Unicode License v3.
Structs§
- Invalid
Case Mapping Mode - Error that indicates a failure to parse a
LowercaseMode,UppercaseMode,SwapcaseMode, orruby::CapitalizeMode. - Lowercase
- An iterator that yields the lowercase equivalent of a conventionally UTF-8 byte string.
- Swapcase
- An iterator yielding bytes with the case of each character swapped.
- Uppercase
- An iterator that yields the uppercase equivalent of a conventionally UTF-8 byte string.
Enums§
- Lowercase
Mode - Options to configure the behavior of
lowercase. - Swapcase
Mode - Options to configure the behavior of
swapcase. - Uppercase
Mode - Options to configure the behavior of
uppercase.
Constants§
- UNICODE_
VERSION - The bundled Unicode case mapping version as (major, minor, patch).
Functions§
- lowercase
- Returns an iterator that yields a copy of the bytes in the given slice with all uppercase letters replaced with their lowercase counterparts.
- make_
ascii_ lowercase - Converts the given slice to its ASCII lower case equivalent in-place.
- make_
ascii_ swapcase - Swap the case of ASCII letters in-place, preserving all other bytes.
- make_
ascii_ uppercase - Converts the given slice to its ASCII upper case equivalent in-place.
- swapcase
- Returns an iterator that yields a copy of the bytes in the given slice with the case of each character swapped.
- to_
ascii_ lowercase alloc - Returns a vector containing a copy of the given slice where each byte is mapped to its ASCII lower case equivalent.
- to_
ascii_ swapcase alloc - Return a copy with ASCII letters case-swapped and all other bytes preserved.
- to_
ascii_ uppercase alloc - Returns a vector containing a copy of the given slice where each byte is mapped to its ASCII upper case equivalent.
- to_
titlecase - Take a
charand return its Unicode titlecase as 3chars. - try_
to_ lowercase alloc - Fallibly lowercase a byte string, borrowing it when no bytes change.
- try_
to_ swapcase alloc - Fallibly swap the case of a byte string, borrowing it when no bytes change.
- try_
to_ uppercase alloc - Fallibly uppercase a byte string, borrowing it when no bytes change.
- uppercase
- Returns an iterator that yields a copy of the bytes in the given slice with all lowercase letters replaced with their uppercase counterparts.