macrors/
repeat.rs

1/*
2 * Copyright © 2024 the original author or authors.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *     http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17// ----------------------------------------------------------------
18
19/// A macro for generates a vector containing the repetition of the given expression `item` for `times` times.
20///
21/// This macro utilizes a loop to repeatedly push the evaluated `item` into a vector,
22/// effectively creating a collection with `times` instances of `item`.
23///
24/// # Arguments
25///
26/// - `item`: The expression to be repeated.
27/// - `times`: The number of times `$item` should be repeated.
28///
29/// # Returns
30///
31/// A vector containing the repeated expressions.
32///
33/// # Example
34///
35/// ```rust
36/// use macrors::repeat;
37///
38/// let repeat_1 = repeat!("A", 5);
39///  assert_eq!(vec!["A", "A", "A", "A", "A"], repeat_1);
40///
41/// let repeat_2 = repeat!(101, 5);
42/// assert_eq!(vec![101, 101, 101, 101, 101], repeat_2);
43/// ```
44///
45/// @since 0.2.0
46///
47#[macro_export]
48macro_rules! repeat {
49    ($item:expr, $times:expr) => {{
50        let mut v = Vec::new();
51        for _ in 0..$times {
52            v.push($item);
53        }
54
55        v
56    }};
57}
58
59// ----------------------------------------------------------------
60
61/// A macro for concatenates a string expression `item` repeated `times` with an optional separator `separator`.
62///
63/// The macro provides two forms of invocation. The first form is a shorthand that defaults the separator to an empty string.
64///
65/// # Arguments
66///
67/// * `item`: The string expression to be repeated.
68/// * `times`: The number of times to repeat the expression.
69/// * `separator`: An optional separator to insert between repetitions. Defaults to an empty string if not provided.
70///
71/// # Returns
72///
73/// A `String` consisting of `item` repeated `times` times, with `separator` inserted between each repetition if provided.
74///
75/// # Example
76///
77/// ```rust
78/// use macrors::repeat_str;
79///
80/// let repeat_str_1 = repeat_str!("A", 5);
81/// assert_eq!("AAAAA", repeat_str_1);
82///
83/// let repeat_str_2 = repeat_str!("A", 5, ",");
84/// assert_eq!("A,A,A,A,A", repeat_str_2);
85///
86/// let repeat_str_3 = repeat_str!(101, 5, ",");
87/// assert_eq!("101,101,101,101,101", repeat_str_3);
88/// ```
89///
90/// @since 0.2.0
91///
92#[macro_export]
93macro_rules! repeat_str {
94    ($item:expr, $times:expr) => {
95        repeat_str!($item, $times, "")
96    };
97
98    ($item:expr, $times:expr, $separator:expr) => {{
99        let mut rvt = String::new();
100        for i in 0..$times {
101            if i > 0 {
102                rvt.push_str($separator);
103            }
104            rvt.push_str(&format!("{}", $item));
105        }
106
107        rvt
108    }};
109}