tinystr/lib.rs
1// This file is part of ICU4X. For terms of use, please see the file
2// called LICENSE at the top level of the ICU4X source tree
3// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ).
4
5// https://github.com/unicode-org/icu4x/blob/main/documents/process/boilerplate.md#library-annotations
6#![cfg_attr(not(any(test, doc)), no_std)]
7#![cfg_attr(
8 not(test),
9 deny(
10 clippy::indexing_slicing,
11 clippy::unwrap_used,
12 clippy::expect_used,
13 clippy::panic,
14 )
15)]
16// #![warn(missing_docs)]
17
18//! `tinystr` is a utility crate of the [`ICU4X`] project.
19//!
20//! It includes [`TinyAsciiStr`], a core API for representing small ASCII-only bounded length strings.
21//!
22//! It is optimized for operations on strings of size 8 or smaller. When use cases involve comparison
23//! and conversion of strings for lowercase/uppercase/titlecase, or checking
24//! numeric/alphabetic/alphanumeric, `TinyAsciiStr` is the edge performance library.
25//!
26//! # Examples
27//!
28//! ```rust
29//! use tinystr::TinyAsciiStr;
30//!
31//! let s1: TinyAsciiStr<4> = "tEsT".parse().expect("Failed to parse.");
32//!
33//! assert_eq!(s1, "tEsT");
34//! assert_eq!(s1.to_ascii_uppercase(), "TEST");
35//! assert_eq!(s1.to_ascii_lowercase(), "test");
36//! assert_eq!(s1.to_ascii_titlecase(), "Test");
37//! assert!(s1.is_ascii_alphanumeric());
38//! assert!(!s1.is_ascii_numeric());
39//!
40//! let s2 = TinyAsciiStr::<8>::try_from_raw(*b"New York")
41//! .expect("Failed to parse.");
42//!
43//! assert_eq!(s2, "New York");
44//! assert_eq!(s2.to_ascii_uppercase(), "NEW YORK");
45//! assert_eq!(s2.to_ascii_lowercase(), "new york");
46//! assert_eq!(s2.to_ascii_titlecase(), "New york");
47//! assert!(!s2.is_ascii_alphanumeric());
48//! ```
49//!
50//! # Details
51//!
52//! When strings are of size 8 or smaller, the struct transforms the strings as `u32`/`u64` and uses
53//! bitmasking to provide basic string manipulation operations:
54//! * `is_ascii_numeric`
55//! * `is_ascii_alphabetic`
56//! * `is_ascii_alphanumeric`
57//! * `to_ascii_lowercase`
58//! * `to_ascii_uppercase`
59//! * `to_ascii_titlecase`
60//! * `PartialEq`
61//!
62//! `TinyAsciiStr` will fall back to `u8` character manipulation for strings of length greater than 8.
63
64//!
65//! [`ICU4X`]: ../icu/index.html
66
67mod macros;
68
69mod ascii;
70mod asciibyte;
71mod error;
72mod int_ops;
73mod unvalidated;
74
75#[cfg(feature = "serde")]
76mod serde;
77
78#[cfg(feature = "databake")]
79mod databake;
80
81#[cfg(feature = "zerovec")]
82mod ule;
83
84#[cfg(feature = "alloc")]
85extern crate alloc;
86
87pub use ascii::TinyAsciiStr;
88pub use error::ParseError;
89pub use unvalidated::UnvalidatedTinyAsciiStr;
90
91/// These are temporary compatability reexports that will be removed
92/// in a future version.
93pub type TinyStr4 = TinyAsciiStr<4>;
94/// These are temporary compatability reexports that will be removed
95/// in a future version.
96pub type TinyStr8 = TinyAsciiStr<8>;
97/// These are temporary compatability reexports that will be removed
98/// in a future version.
99pub type TinyStr16 = TinyAsciiStr<16>;
100
101#[test]
102fn test_size() {
103 assert_eq!(size_of::<TinyStr4>(), size_of::<Option<TinyStr4>>());
104 assert_eq!(size_of::<TinyStr8>(), size_of::<Option<TinyStr8>>());
105}