const_str/lib.rs
1//! Compile-time string operations.
2//! See the [macro list](#macros) for what you need.
3//!
4//! ## MSRV history
5//!
6//! Current: Rust 1.77.0
7//!
8//! - `v0.6.0`: Rust 1.77.0
9//! - `v0.5.7`: Rust 1.65.0
10//! - `v0.5.0`: Rust 1.64.0
11//! - `v0.4.0`: Rust 1.61.0
12//!
13//! ## Troubleshoot
14//!
15//! You don't have to care about this section
16//! unless you come across some compile errors about const evaluation.
17//!
18//! ```txt
19//! error[E0435]: attempt to use a non-constant value in a constant
20//! ```
21//!
22//! There are mainly two kinds of macros in this crate,
23//! which have different requirements for the arguments.
24//! - [const-context only](#const-context-only)
25//! - [const-fn compatible](#const-fn-compatible)
26//!
27//! ### const-context only
28//!
29//! These macros can only be used in [const contexts][const-context].
30//! The expanded code is equivalent to compute new [constant items][const-item].
31//! It implies that the *arguments* of these macros must be constant values,
32//! similar to [`consteval`][consteval] in C++ world.
33//!
34//! The following examples will not work:
35//! ```compile_fail
36//! const fn foo(a: &str, b: &str) -> &str {
37//! const_str::concat!(a, b)
38//! }
39//! ```
40//! ```compile_fail
41//! const C: &str = {
42//! let a = "Hello";
43//! let b = "World";
44//! const_str::concat!(a, b);
45//! };
46//! ```
47//!
48//! Instead, this way will work:
49//! ```
50//! const A: &str = "Hello";
51//! const B: &str = "World";
52//! const C: &str = const_str::concat!(A, " ", B);
53//! assert_eq!(C, "Hello World");
54//! ```
55//!
56//! ### const-fn compatible
57//!
58//! These macros can be used in [const contexts][const-context] and [const functions][const-fn].
59//! The expanded code is equivalent to calling const functions.
60//! It implies that the *arguments* of these macros can be any expressions,
61//! similar to [`constexpr`][constexpr] in C++ world.
62//!
63//! ```
64//! const fn calc(y: &str, m: &str, d: &str) -> u64 {
65//! let y = const_str::parse!(y, u64);
66//! let m = const_str::parse!(m, u64);
67//! let d = const_str::parse!(d, u64);
68//! (y * 10000 + m * 100 + d)
69//! }
70//! const TIME: u64 = calc("2025", "01", "26");
71//! assert_eq!(TIME, 20250126);
72//! ```
73//!
74//! You can also use these macros in normal functions,
75//! but they may be much slower than the runtime equivalents.
76//! It's recommended to use them only if you need compile-time evaluation.
77//!
78//! [const-context]: https://doc.rust-lang.org/reference/const_eval.html#const-context
79//! [const-fn]: https://doc.rust-lang.org/reference/const_eval.html#const-functions
80//! [const-item]: https://doc.rust-lang.org/reference/items/constant-items.html
81//! [consteval]: https://en.cppreference.com/w/cpp/language/consteval
82//! [constexpr]: https://en.cppreference.com/w/cpp/language/constexpr
83//!
84#![deny(unsafe_code, missing_docs, clippy::all, clippy::cargo)]
85#![allow(
86 clippy::missing_docs_in_private_items,
87 clippy::missing_inline_in_public_items,
88 clippy::implicit_return
89)]
90#![cfg_attr(not(any(test, feature = "std")), no_std)]
91#![cfg_attr(docsrs, feature(doc_cfg))]
92
93#[allow(unused_macros)]
94macro_rules! cfg_group {
95 ($($item:item)*) => {
96 $($item)*
97 }
98}
99
100mod ascii;
101mod bytes;
102mod printable;
103mod slice;
104mod str;
105mod utf16;
106mod utf8;
107
108#[doc(hidden)]
109#[cfg(feature = "proc")]
110pub mod __proc {
111 mod case;
112 pub use self::case::*;
113
114 mod fmt;
115 pub use self::fmt::*;
116
117 #[cfg(feature = "http")]
118 cfg_group! {
119 mod http;
120 pub use self::http::*;
121 }
122
123 #[cfg(feature = "regex")]
124 cfg_group! {
125 mod regex;
126 pub use self::regex::*;
127 }
128}
129
130#[doc(hidden)]
131pub mod __ctfe {
132 mod ascii_case;
133 pub use self::ascii_case::*;
134
135 mod chain;
136 // pub use self::chain::*;
137
138 mod compare;
139 pub use self::compare::*;
140
141 mod concat;
142 pub use self::concat::*;
143
144 mod concat_bytes;
145 pub use self::concat_bytes::*;
146
147 mod cstr;
148 pub use self::cstr::*;
149
150 mod encode;
151 pub use self::encode::*;
152
153 mod equal;
154 pub use self::equal::*;
155
156 mod find;
157 pub use self::find::*;
158
159 mod fmt;
160 pub use self::fmt::*;
161
162 mod hex;
163 pub use self::hex::*;
164
165 mod net;
166 pub use self::net::*;
167
168 mod parse;
169 pub use self::parse::*;
170
171 mod repeat;
172 pub use self::repeat::*;
173
174 mod replace;
175 pub use self::replace::*;
176
177 mod str;
178 pub use self::str::*;
179
180 mod to_byte_array;
181 pub use self::to_byte_array::*;
182
183 mod to_char_array;
184 pub use self::to_char_array::*;
185
186 mod to_str;
187 pub use self::to_str::*;
188
189 mod sorted;
190 pub use self::sorted::*;
191
192 mod split;
193 pub use self::split::*;
194
195 mod squish;
196 pub use self::squish::*;
197
198 mod is_ascii;
199 pub use self::is_ascii::*;
200
201 mod eq_ignore_ascii_case;
202 pub use self::eq_ignore_ascii_case::*;
203
204 mod unwrap;
205 pub use self::unwrap::*;
206}