cheetah_string/lib.rs
1#![cfg_attr(not(feature = "std"), no_std)]
2#![deny(unsafe_op_in_unsafe_fn)]
3#![warn(missing_docs)]
4
5//! An immutable, clone-cheap UTF-8 value for latency-sensitive systems.
6//!
7//! [`CheetahString`] has one constructor-independent value contract:
8//!
9//! - explicit static values borrow their `&'static str`;
10//! - other values up to 23 bytes are stored inline;
11//! - other long values use a shared `Arc<str>` backing.
12//!
13//! Long clones are bounded O(1) and allocate zero times. Append-heavy
14//! construction belongs to [`CheetahBuilder`]; call
15//! [`CheetahBuilder::finish`] to freeze the value or
16//! [`CheetahBuilder::into_string`] when mutation or spare capacity must
17//! continue. `from_string` freezes its input and does not retain a mutable
18//! `String` representation. Long `Arc<str>` inputs can be adopted without
19//! allocation through [`CheetahString::from_arc_str`].
20//!
21//! The crate supports `no_std + alloc`. Optional `serde` integration preserves
22//! the text contract, while the `bytes` feature exposes `CheetahBytes` for
23//! byte-oriented data and `CheetahText`, UTF-8 text backed by `bytes::Bytes`.
24//! Validating a `Bytes` buffer into `CheetahText`, and slicing it, never copy
25//! the text; conversion into `CheetahString` copies.
26//!
27//! # Split capability
28//!
29//! [`CheetahString::split_char`] returns a double-ended standard iterator.
30//! [`CheetahString::split_str`] is forward-only, so unsupported reverse
31//! iteration fails at compile time rather than panicking at runtime.
32//!
33//! # Comparison and search
34//!
35//! Equality, prefix, and suffix comparisons of up to 64 bytes use
36//! overlapping fixed-size loads instead of a `memcmp` call; longer inputs use
37//! `memcmp`. Two inline values are ordered by comparing their buffers as
38//! words, also without a `memcmp` call.
39//!
40//! Substring search picks its backend by haystack and needle length, with
41//! thresholds measured on x86_64 and aarch64. Needles of 2 to 32 bytes in
42//! short text use a screened search that needs no setup: it checks 16
43//! positions at a time against the needle's first and last bytes and compares
44//! the middle only where both match. `find()` and `rfind()` screen text under
45//! 256 bytes; on aarch64 `find()` stops sooner for needles under 16 bytes.
46//! `contains()` keeps the standard library's SIMD search once the text spans
47//! a 16-byte block plus the needle and screens only shorter text, or text
48//! under 256 bytes on targets without that search. Every other search uses
49//! `memchr::memmem`, except that `rfind()` uses `str::rfind` once the text is
50//! at least `24 + 2 * needle` bytes long. A needle at least as long as the
51//! text is answered by one comparison. Use [`CheetahFinder`] to search for one
52//! needle repeatedly.
53//!
54//! # Retired experimental features
55//!
56//! The former `experimental-packed` flag is a compatibility no-op. Its v1
57//! public type was removed because integer-to-pointer reconstruction did not
58//! satisfy Rust's strict-provenance model.
59//!
60//! ```compile_fail
61//! use cheetah_string::packed::PackedCheetahString;
62//! ```
63//!
64//! The `experimental-simd` flag and its `simd` alias are compatibility
65//! no-ops as well. Their x86_64 SSE2 comparison loop was replaced by the
66//! portable comparison every build uses, which needs no `unsafe` code.
67//!
68//! # Example
69//!
70//! ```rust
71//! use cheetah_string::{CheetahBuilder, CheetahString};
72//!
73//! let topic = CheetahString::from_static_str("orders");
74//! assert!(topic.starts_with("ord"));
75//!
76//! let mut builder = CheetahBuilder::new();
77//! builder.push_str(topic.as_str());
78//! builder.push('@');
79//! builder.push_str("group-a");
80//!
81//! let route = builder.finish();
82//! assert_eq!(route, "orders@group-a");
83//! ```
84extern crate alloc;
85
86mod builder;
87mod cheetah_string;
88mod compare;
89mod error;
90mod inline;
91mod search;
92
93#[cfg(feature = "bytes")]
94#[path = "bytes.rs"]
95mod cheetah_bytes;
96
97#[cfg(feature = "serde")]
98mod serde;
99
100#[cfg(feature = "bytes")]
101mod text;
102
103#[cfg(feature = "bytes")]
104pub use cheetah_bytes::{CheetahBytes, FromUtf8BytesError};
105#[cfg(feature = "bytes")]
106pub use text::CheetahText;
107
108pub use builder::CheetahBuilder;
109pub use cheetah_string::{CheetahString, SplitPattern, SplitStr, StrPattern};
110pub use error::{Error, Result};
111pub use search::CheetahFinder;
112
113/// Deprecated v3 compatibility name for [`CheetahString`].
114///
115/// `CheetahString` is now itself immutable and clone-cheap, so a second value
116/// type no longer carries a distinct contract.
117#[deprecated(since = "3.0.0", note = "use CheetahString")]
118pub type CheetahStr = CheetahString;