| Crates.io | cheetah-string |
| lib.rs | cheetah-string |
| version | 1.0.1 |
| created_at | 2024-11-08 13:45:47.118223+00 |
| updated_at | 2026-01-06 08:04:12.046813+00 |
| description | A lightweight, high-performance string manipulation library optimized for speed-sensitive applications |
| homepage | https://github.com/mxsm/cheetah-string |
| repository | https://github.com/mxsm/cheetah-string |
| max_upload_size | |
| id | 1441224 |
| size | 159,760 |
A lightweight, high-performance string type optimized for real-world use cases.
CheetahString is a versatile string type that goes beyond the standard library's String, providing zero-allocation optimizations for common patterns and seamless interoperability with various string representations. It's designed for both std and no_std environments.
🚀 Zero-Allocation Optimization
'static lifetime🔧 Rich API
starts_with, ends_with, contains, find, rfindto_uppercase, to_lowercase, replace, trimsplit, lines, charswith_capacity, push_str, reserve+ and += operators🌐 Flexible Integration
bytes support for zero-copy interop with the bytes crateserde support for serialization/deserializationno_std compatible (requires alloc)⚡ Performance Focused
🛡️ Safe & Correct
try_from_bytes, try_from_vec)Add this to your Cargo.toml:
[dependencies]
cheetah-string = "1.0.0"
[dependencies]
cheetah-string = { version = "1.0.0", features = ["bytes", "serde", "simd"] }
Available features:
std (default): Enable standard library supportbytes: Integration with the bytes crateserde: Serialization support via serdesimd: SIMD-accelerated string operations (x86_64 SSE2)use cheetah_string::CheetahString;
// Create from various sources
let s1 = CheetahString::from("hello"); // From &str
let s2 = CheetahString::from(String::from("world")); // From String
let s3 = CheetahString::from_static_str("static"); // From 'static str (zero-cost)
// Small strings (≤ 23 bytes) use no heap allocation
let small = CheetahString::from("short"); // Stored inline!
// String operations
let s = CheetahString::from("Hello, World!");
assert!(s.starts_with("Hello")); // Supports &str
assert!(s.starts_with('H')); // Also supports char
assert!(s.contains("World"));
assert!(s.contains('W'));
assert_eq!(s.to_lowercase(), "hello, world!");
// Concatenation
let greeting = CheetahString::from("Hello");
let name = CheetahString::from(" Rust");
let message = greeting + name.as_str(); // "Hello Rust"
// Builder pattern for efficient construction
let mut builder = CheetahString::with_capacity(100);
builder.push_str("Hello");
builder.push_str(", ");
builder.push_str("World!");
// Safe UTF-8 validation
let bytes = b"hello";
let s = CheetahString::try_from_bytes(bytes).unwrap();
CheetahString is designed with performance in mind:
Arc<str> for cheap cloningsimd feature): String matching operations (starts_with, ends_with, contains, find, equality comparisons) are accelerated using SSE2 SIMD instructions on x86_64 platforms. The implementation automatically falls back to scalar code for small inputs or when SIMD is not available.Run benchmarks:
cargo bench
# With SIMD feature
cargo bench --features simd
CheetahString intelligently chooses the most efficient storage:
| String Type | Storage | Heap Allocations | Use Case |
|---|---|---|---|
| ≤ 23 bytes | Inline (SSO) | 0 | Short strings, identifiers |
| Static | &'static str |
0 | String literals |
| Dynamic | Arc<str> |
1 | Long strings, shared data |
| From Arc | Arc<String> |
1 | Interop with existing Arc |
| Bytes | bytes::Bytes |
1 | Network buffers (with feature) |
new(), empty(), default() - Create empty stringsfrom(s) - From &str, String, &String, char, etc.from_static_str(s) - Zero-cost wrapper for 'static strfrom_string(s) - From owned Stringtry_from_bytes(b) - Safe construction from bytes with UTF-8 validationwith_capacity(n) - Pre-allocate capacitylen(), is_empty(), as_str(), as_bytes()starts_with(), ends_with(), contains() - Support both &str and char patternsfind(), rfind()to_uppercase(), to_lowercase()replace(), replacen()trim(), trim_start(), trim_end()substring(), repeat()chars() - Iterate over characters (double-ended iterator)split() - Split by pattern (supports &str and char)lines() - Iterate over linespush_str() - Append string slicereserve() - Reserve additional capacity+ - Concatenation+= - Append in-place (optimized)==, != - Equality comparison with str, String, etc.CheetahString is ideal for:
bytes crateContributions are welcome! Here's how you can help:
# Clone the repository
git clone https://github.com/mxsm/cheetah-string.git
cd cheetah-string
# Run tests
cargo test
# Run benchmarks
cargo bench
# Run with all features
cargo test --all-features
This project is licensed under either of:
at your option.
CheetahString is inspired by the need for a flexible, high-performance string type in Rust that bridges the gap between String, &str, Arc<str>, and specialized types like bytes::Bytes.