crax_utils/concat_string.rs
1// https://github.com/FaultyRAM/concat-string
2//
3// Copyright (c) 2017-2018 FaultyRAM
4//
5// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license <LICENSE-MIT or
7// http://opensource.org/licenses/MIT>, at your option. This file may not be copied, modified, or
8// distributed except according to those terms.
9
10//! Macros for concatenating string slices into owned strings.
11//!
12//! This crate provides the `concat_string!` macro for efficiently concatenating string slices into
13//! owned strings. `concat_string!` accepts any number of arguments that implement `AsRef<str>` and
14//! creates a `String` with the appropriate capacity, without the need for format strings and their
15//! associated runtime overhead.
16//!
17//! # Example
18//!
19//! ```rust
20//! #[macro_use(concat_string)]
21//! extern crate concat_string;
22//!
23//! fn main() {
24//! println!("{}", concat_string!("Hello", String::from(" "), "world"));
25//! }
26//! ```
27
28#[macro_export]
29/// Concatenates a series of string slices into an owned string.
30///
31/// This macro accepts zero or more arguments, where each argument implements `AsRef<str>`, and
32/// efficiently combines their string representations into a `String` in order of declaration.
33///
34/// This is mainly useful for cases where the cost of parsing a format string outweighs the cost
35/// of converting its arguments. Because `concat_string` avoids format strings entirely, it can
36/// achieve a higher level of performance than using `format!` or other formatting utilities that
37/// return a `String`.
38///
39/// # Example
40///
41/// ```rust
42/// use rolldown_utils::concat_string;
43///
44/// println!("{}", concat_string!("Hello", String::from(" "), "world"));
45/// ```
46macro_rules! concat_string {
47 () => { String::with_capacity(0) };
48 ($($s:expr),+) => {{
49 use std::ops::AddAssign;
50 let mut len = 0;
51 $(len.add_assign(AsRef::<str>::as_ref(&$s).len());)+
52 let mut buf = String::with_capacity(len);
53 $(buf.push_str($s.as_ref());)+
54 buf
55 }};
56}
57
58#[cfg(test)]
59mod tests {
60 #[test]
61 fn concat_string_0_args() {
62 let s = concat_string!();
63 assert_eq!(s, String::new());
64 }
65
66 #[test]
67 fn concat_string_1_arg() {
68 let s = concat_string!("foo");
69 assert_eq!(s, String::from("foo"));
70 }
71
72 #[test]
73 fn concat_string_str_string() {
74 let s = concat_string!("foo", String::from("bar"));
75 assert_eq!(s, String::from("foobar"));
76 }
77}