1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
//! The static_str_ops crate solves a longstanding issue about how to
//! perform non-const string operations, e.g., `format!()`, `concat!()`, etc.
//! and return static string, i.e., `&'static str`.
//!
//! Internally, the crate uses a global static HashSet to store all the
//! static strings, and return the reference to the string in the HashSet
//! if the string has been staticized before.
//!
//! This create provides the following macros and functions:
//!
//! - `staticize(s: &str) -> &'static str`
//!
//! Convert a string to a static string. If the string has been staticized
//! before, return the reference to the string in the HashSet.
//!
//! This function is the most basic usage of this crate, e.g.,
//!
//! ```rust
//! use static_str_ops::staticize;
//!
//! let s: &'static str = staticize(&String::from("hello world!"));
//! ```
//!
//! - `is_staticized(s: &str) -> bool`
//!
//! Check if a string has been staticized before.
//!
//! - `destaticize(s: &str) -> bool`
//!
//! Remove a static string from the internal HashSet. Return `true` if was present.
//!
//! - `static_concat!(s1: expr, s2: expr, ...) -> &'static str`
//!
//! Concatenate multiple strings into a static string. The arguments can
//! be either a string literal.
//!
//! Like `concat!()`, but returns a static string.
//!
//! - `static_format!(s: expr, ...) -> &'static str`
//!
//! Format a string into a static string. The arguments can be whatever
//! the builtin macro `format!()` can accept.
//!
//! Like `format!()`, but returns a static string.
//!
//! - `staticize_once!(expr: expr) -> &'static str`
//!
//! Similar to staticize(), but the expr will be evaluated only once. Under
//! the hood, `std::sync::Once` is used.
//!
//! The function will be useful if you have a function that want to return
//! a static string, while the generate logic is non-trivial, and you want
//! this process only happen once, e.g.,
//!
//! ```rust
//! use static_str_ops::*;
//!
//! let make_string = || {
//! staticize_once!({
//! let s = ""; // can be some expensive computation
//! s
//! })
//! };
//!
//! let s1: &'static str = make_string();
//! let s2: &'static str = make_string();
//! ```
//!
//! When you call `make_string()` for multiple times, the body will be
//! guaranteed to be evaluated only once.
use HashSet;
use Mutex;
use lazy_static;
/// Re-export the gensym symbol to avoid introducing a new dependency (gensym)
/// in callers.
pub use gensym;
lazy_static!
/// Converts a string slice to a static string slice.
///
/// This function takes a string slice and returns a static string slice with the same contents.
/// If the same string slice has been previously converted, the function returns the previously
/// converted static string slice. Otherwise, it creates a new static string slice and returns it.
///
/// # Arguments
///
/// * `s` - A string slice to be converted to a static string slice.
///
/// # Examples
///
/// ```
/// use static_str_ops::staticize;
///
/// let s = "hello";
/// let static_s = staticize(s);
///
/// assert_eq!(static_s, "hello");
/// ```
/// Checks if a given string is a static string.
///
/// # Arguments
///
/// * `s` - A string slice to check.
///
/// # Returns
///
/// Returns `true` if the given string is a static string, `false` otherwise.
/// Removes a static string from the internal set of static strings.
///
/// # Arguments
///
/// * `s` - A string slice that represents the static string to be removed.
///
/// # Returns
///
/// A boolean value indicating whether the static string was present.
///
/// Concatenates the given string literals into a single static string slice.
///
/// # Examples
///
/// ```
/// use static_str_ops::static_concat;
///
/// let hello_world: &'static str = static_concat!("Hello", ", ", "world!");
/// assert_eq!(hello_world, "Hello, world!");
/// ```
///
/// # Panics
///
/// This macro will panic if any of the input expressions is not a string literal.
/// A macro that takes a format string and arguments, and returns a static string slice.
///
/// # Examples
///
/// ```
/// use static_str_ops::static_format;
///
/// let name = "John";
/// let age = 30;
/// let message = static_format!("My name is {} and I'm {} years old.", name, age);
///
/// assert_eq!(message, "My name is John and I'm 30 years old.");
/// ```
///
/// This macro is similar to the `format!` macro, but it returns a static string slice
/// instead of a `String`, and can take whatever the `format!` macro recognizes.
/// Internally used by `staticize_once!()`.
/// Macro to generate a unique identifier for a given expression, which can be used to create a static variable
/// that is initialized once with the result of the expression. This macro ensures that the expression is only
/// evaluated once at runtime, and the result is stored in a static variable for future use.
///
/// # Arguments
///
/// * `expr` - An expression of type `&str` or `String`.
///
/// # Examples
///
/// ```
/// #![feature(stmt_expr_attributes)]
///
/// use static_str_ops::*;
///
/// let s = "hello world";
/// let static_str = staticize_once!(s.to_uppercase());
/// assert_eq!(static_str, "HELLO WORLD");
/// ```
///
/// # Notes
///
/// This macro uses the `gensym` crate to generate a unique identifier for the expression. The `gensym` crate
/// is a compile-time code generation library that generates unique identifiers based on the source code
/// location where the macro is invoked. This ensures that the identifier is unique across the entire crate,
/// and is not accidentally used elsewhere.