|
1 | | -# 🐆 Cheetah String |
| 1 | +# 🐆 CheetahString |
2 | 2 |
|
3 | | -**A lightweight, high-performance string manipulation library optimized for speed-sensitive applications.** |
| 3 | +[](https://crates.io/crates/cheetah-string) |
| 4 | +[](https://docs.rs/cheetah-string) |
| 5 | +[](https://github.com/mxsm/cheetah-string) |
| 6 | +[](https://www.rust-lang.org) |
4 | 7 |
|
5 | | -Cheetah String is designed to provide fast and efficient string operations with minimal resource usage, ideal for performance-critical and memory-constrained environments. It offers a range of easy-to-use, high-speed string handling functions. |
| 8 | +**A lightweight, high-performance string type optimized for real-world use cases.** |
6 | 9 |
|
7 | | -## Features |
| 10 | +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. |
8 | 11 |
|
9 | | -- **Highly optimized**: Performance-tuned across platforms to support low-latency needs. |
10 | | -- **Comprehensive API**: Includes common string operations (e.g., split, concatenate, transform, format, etc.). |
11 | | -- **Easy to integrate**: Simple, intuitive design to minimize the learning curve. |
12 | | -- **Cross-platform**: Compatible with major operating systems and development environments. |
| 12 | +## ✨ Features |
13 | 13 |
|
14 | | -## How to use |
| 14 | +- **🚀 Zero-Allocation Optimization** |
| 15 | + - Small String Optimization (SSO): Strings ≤ 23 bytes stored inline (no heap allocation) |
| 16 | + - Static string support with `'static` lifetime |
| 17 | + - Efficient Arc-based sharing for larger strings |
15 | 18 |
|
16 | | -To use **`Cheetah-String`**, first add this to your `Cargo.toml`: |
| 19 | +- **🔧 Rich API** |
| 20 | + - Query methods: `starts_with`, `ends_with`, `contains`, `find`, `rfind` |
| 21 | + - Transformation: `to_uppercase`, `to_lowercase`, `replace`, `trim` |
| 22 | + - Iteration: `split`, `lines`, `chars` |
| 23 | + - Builder pattern: `with_capacity`, `push_str`, `reserve` |
| 24 | + - String concatenation with `+` and `+=` operators |
| 25 | + |
| 26 | +- **🌐 Flexible Integration** |
| 27 | + - Optional `bytes` support for zero-copy interop with the `bytes` crate |
| 28 | + - Optional `serde` support for serialization/deserialization |
| 29 | + - `no_std` compatible (requires `alloc`) |
| 30 | + |
| 31 | +- **⚡ Performance Focused** |
| 32 | + - Optimized for common string operations |
| 33 | + - Reduced memory allocations via intelligent internal representation |
| 34 | + - Benchmarked against standard library types |
| 35 | + |
| 36 | +- **🛡️ Safe & Correct** |
| 37 | + - UTF-8 validation with safe constructors (`try_from_bytes`, `try_from_vec`) |
| 38 | + - Comprehensive test coverage |
| 39 | + - Well-documented API with examples |
| 40 | + |
| 41 | +## 📦 Installation |
| 42 | + |
| 43 | +Add this to your `Cargo.toml`: |
17 | 44 |
|
18 | 45 | ```toml |
19 | 46 | [dependencies] |
20 | | -cheetah-string = "0.1.0" |
| 47 | +cheetah-string = "0.1" |
21 | 48 | ``` |
22 | 49 |
|
23 | | -### Bytes support |
24 | | - |
25 | | -**Bytes** support is optional and disabled by default. To enable use the feature `bytes`. |
| 50 | +### Optional Features |
26 | 51 |
|
27 | 52 | ```toml |
28 | 53 | [dependencies] |
29 | | -cheetah-string = { version = "1", features = ["bytes"] } |
| 54 | +cheetah-string = { version = "0.1", features = ["bytes", "serde"] } |
30 | 55 | ``` |
31 | 56 |
|
32 | | -### Serde support |
| 57 | +Available features: |
| 58 | +- `std` (default): Enable standard library support |
| 59 | +- `bytes`: Integration with the `bytes` crate |
| 60 | +- `serde`: Serialization support via serde |
33 | 61 |
|
34 | | -**serde** support is optional and disabled by default. To enable use the feature `serde`. |
| 62 | +## 🚀 Quick Start |
35 | 63 |
|
36 | | -```toml |
37 | | -[dependencies] |
38 | | -cheetah-string = { version = "1", features = ["serde"] } |
| 64 | +```rust |
| 65 | +use cheetah_string::CheetahString; |
| 66 | + |
| 67 | +// Create from various sources |
| 68 | +let s1 = CheetahString::from("hello"); // From &str |
| 69 | +let s2 = CheetahString::from(String::from("world")); // From String |
| 70 | +let s3 = CheetahString::from_static_str("static"); // From 'static str (zero-cost) |
| 71 | + |
| 72 | +// Small strings (≤ 23 bytes) use no heap allocation |
| 73 | +let small = CheetahString::from("short"); // Stored inline! |
| 74 | + |
| 75 | +// String operations |
| 76 | +let s = CheetahString::from("Hello, World!"); |
| 77 | +assert!(s.starts_with("Hello")); |
| 78 | +assert!(s.contains("World")); |
| 79 | +assert_eq!(s.to_lowercase(), "hello, world!"); |
| 80 | + |
| 81 | +// Concatenation |
| 82 | +let greeting = CheetahString::from("Hello"); |
| 83 | +let name = CheetahString::from(" Rust"); |
| 84 | +let message = greeting + name.as_str(); // "Hello Rust" |
| 85 | + |
| 86 | +// Builder pattern for efficient construction |
| 87 | +let mut builder = CheetahString::with_capacity(100); |
| 88 | +builder.push_str("Hello"); |
| 89 | +builder.push_str(", "); |
| 90 | +builder.push_str("World!"); |
| 91 | + |
| 92 | +// Safe UTF-8 validation |
| 93 | +let bytes = b"hello"; |
| 94 | +let s = CheetahString::try_from_bytes(bytes).unwrap(); |
| 95 | +``` |
| 96 | + |
| 97 | +## 📊 Performance |
| 98 | + |
| 99 | +CheetahString is designed with performance in mind: |
| 100 | + |
| 101 | +- **Small String Optimization (SSO)**: Strings up to 23 bytes are stored inline without heap allocation |
| 102 | +- **Efficient Sharing**: Large strings use `Arc<str>` for cheap cloning |
| 103 | +- **Optimized Operations**: Common operations like concatenation have fast-path implementations |
| 104 | + |
| 105 | +Run benchmarks: |
| 106 | +```bash |
| 107 | +cargo bench |
| 108 | +``` |
| 109 | + |
| 110 | +## 🔍 Internal Representation |
| 111 | + |
| 112 | +CheetahString intelligently chooses the most efficient storage: |
| 113 | + |
| 114 | +| String Type | Storage | Heap Allocations | Use Case | |
| 115 | +|-------------|---------|------------------|----------| |
| 116 | +| ≤ 23 bytes | Inline (SSO) | 0 | Short strings, identifiers | |
| 117 | +| Static | `&'static str` | 0 | String literals | |
| 118 | +| Dynamic | `Arc<str>` | 1 | Long strings, shared data | |
| 119 | +| From Arc | `Arc<String>` | 1 | Interop with existing Arc | |
| 120 | +| Bytes | `bytes::Bytes` | 1 | Network buffers (with feature) | |
| 121 | + |
| 122 | +## 🔧 API Overview |
| 123 | + |
| 124 | +### Construction |
| 125 | +- `new()`, `empty()`, `default()` - Create empty strings |
| 126 | +- `from(s)` - From `&str`, `String`, `&String`, `char`, etc. |
| 127 | +- `from_static_str(s)` - Zero-cost wrapper for `'static str` |
| 128 | +- `from_string(s)` - From owned `String` |
| 129 | +- `try_from_bytes(b)` - Safe construction from bytes with UTF-8 validation |
| 130 | +- `with_capacity(n)` - Pre-allocate capacity |
| 131 | + |
| 132 | +### Query Methods |
| 133 | +- `len()`, `is_empty()`, `as_str()`, `as_bytes()` |
| 134 | +- `starts_with()`, `ends_with()`, `contains()` |
| 135 | +- `find()`, `rfind()` |
| 136 | + |
| 137 | +### Transformation |
| 138 | +- `to_uppercase()`, `to_lowercase()` |
| 139 | +- `replace()`, `replacen()` |
| 140 | +- `trim()`, `trim_start()`, `trim_end()` |
| 141 | +- `substring()`, `repeat()` |
| 142 | + |
| 143 | +### Iteration |
| 144 | +- `chars()` - Iterate over characters |
| 145 | +- `split()` - Split by pattern |
| 146 | +- `lines()` - Iterate over lines |
| 147 | + |
| 148 | +### Mutation |
| 149 | +- `push_str()` - Append string slice |
| 150 | +- `reserve()` - Reserve additional capacity |
| 151 | + |
| 152 | +### Operators |
| 153 | +- `+` - Concatenation |
| 154 | +- `+=` - Append in-place (optimized) |
| 155 | +- `==`, `!=` - Equality comparison with `str`, `String`, etc. |
| 156 | + |
| 157 | +## 🎯 Use Cases |
| 158 | + |
| 159 | +CheetahString is ideal for: |
| 160 | + |
| 161 | +- **High-performance servers**: Reduce allocations in hot paths |
| 162 | +- **Memory-constrained environments**: Efficient memory usage with SSO |
| 163 | +- **Network protocols**: Integration with `bytes` crate |
| 164 | +- **Configuration systems**: Fast handling of static and dynamic strings |
| 165 | +- **No-std applications**: Embedded systems and WASM |
| 166 | + |
| 167 | +## 🏗️ Projects Using CheetahString |
| 168 | + |
| 169 | +- [**RocketMQ Rust**](https://github.com/mxsm/rocketmq-rust) - Apache RocketMQ Rust implementation |
| 170 | + |
| 171 | +## 🤝 Contributing |
| 172 | + |
| 173 | +Contributions are welcome! Here's how you can help: |
| 174 | + |
| 175 | +1. **Report Issues**: Found a bug? [Open an issue](https://github.com/mxsm/cheetah-string/issues) |
| 176 | +2. **Submit PRs**: Improvements and bug fixes are appreciated |
| 177 | +3. **Add Benchmarks**: Help us track performance across use cases |
| 178 | +4. **Improve Documentation**: Better docs help everyone |
| 179 | + |
| 180 | +### Development Setup |
| 181 | + |
| 182 | +```bash |
| 183 | +# Clone the repository |
| 184 | +git clone https://github.com/mxsm/cheetah-string.git |
| 185 | +cd cheetah-string |
| 186 | + |
| 187 | +# Run tests |
| 188 | +cargo test |
| 189 | + |
| 190 | +# Run benchmarks |
| 191 | +cargo bench |
| 192 | + |
| 193 | +# Run with all features |
| 194 | +cargo test --all-features |
39 | 195 | ``` |
40 | 196 |
|
41 | | -## Projects used |
| 197 | +## 📝 License |
42 | 198 |
|
43 | | -- [**Rocketmq-rust**](https://github.com/mxsm/rocketmq-rust) |
| 199 | +This project is licensed under either of: |
44 | 200 |
|
45 | | -## Contributing |
| 201 | +- Apache License, Version 2.0 ([LICENSE](LICENSE) or http://www.apache.org/licenses/LICENSE-2.0) |
| 202 | +- MIT license ([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT) |
46 | 203 |
|
47 | | -We welcome issues and pull requests to help build a more efficient string manipulation library together! |
| 204 | +at your option. |
48 | 205 |
|
49 | | -## License |
| 206 | +## 🙏 Acknowledgments |
50 | 207 |
|
51 | | -**cheetah-string** is licensed under the [Apache License 2.0](https://github.com/mxsm/cheetah-string/blob/main/LICENSE) and [MIT license](https://github.com/mxsm/cheetah-string/blob/main/LICENSE-MIT) |
| 208 | +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`. |
0 commit comments