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
/// Remove Location for remove_char function
/// Removes the first or last character of a string if it matches the given target character.
///
/// # Arguments
///
/// * `begin` - A boolean indicating whether to remove the first (`true`) or last (`false`) character.
/// * `input` - A `String` to process.
/// * `target` - The character to remove.
///
/// # Returns
///
/// Returns a new `String` with the character removed if it matched.
///
/// # Examples
///
/// ```
/// use bt_string_utils::cleanser::{remove_char, RemoveLocationEnum};
/// let modified = remove_char(RemoveLocationEnum::Begin, &"hello".to_string(), 'h');
/// assert_eq!(modified, "ello");
///
/// let modified = remove_char(RemoveLocationEnum::End, &"world!".to_string(), '!');
/// assert_eq!(modified, "world");
/// ```
///
/// If the character doesn't match, the original string is returned:
///
/// ```
/// use bt_string_utils::cleanser::{remove_char, RemoveLocationEnum};
/// let modified = remove_char(RemoveLocationEnum::Begin, &"rust".to_string(), 'x');
/// assert_eq!(modified, "rust");
/// ```
/// The remove_first_n function removes the first n characters from a string slice,
/// returning a new string slice that starts from the character after the nth character.
/// This function properly handles Unicode characters by working with character indices rather than byte indices,
/// ensuring that multi-byte UTF-8 characters are correctly handled.
///
/// # Parameters
/// s: &str - A string slice from which characters will be removed
/// n: usize - The number of characters to remove from the beginning of the string
///
/// # Returns
/// &str - A string slice containing the original string with the first n characters removed
///
/// # Examples
/// ```
/// use bt_string_utils::cleanser::remove_first_n_characters;
/// ```
/// Remove first 3 characters
/// ```
/// use bt_string_utils::cleanser::remove_first_n_characters;
/// let result = remove_first_n_characters("Hello, World!", 3);
/// assert_eq!(result, "lo, World!"); // 'Hel' removed
/// ```
/// Remove more characters than exist
/// ```
/// use bt_string_utils::cleanser::remove_first_n_characters;
/// let result = remove_first_n_characters("Hi", 5);
/// assert_eq!(result, ""); // Empty string returned
/// ```
/// Handle Unicode characters
/// ```
/// use bt_string_utils::cleanser::remove_first_n_characters;
/// let result = remove_first_n_characters("🌟Hello", 1);
/// assert_eq!(result, "Hello"); // The emoji (2 bytes) is properly skipped
/// ```
/// Removes all `<custom> ... </custom>` sections from the input string,
/// returning a new `String` with the tags and their contents removed.
/// # Parameters
///
/// * `s: &str`
/// The input string to process.
/// This may contain zero, one, or multiple `<custom> ... </custom>`
/// tag pairs. The function treats tags literally and does not interpret
/// nested or malformed markup.
/// * `open_tag: &str`
/// The open tag (e.g. <custom>). `<custom> ... </custom>`
/// * `close_tag: &str`
/// The close tag (e.g. </custom>). `<custom> ... </custom>`
/// # Behavior
///
/// - Every occurrence of `<custom>` starts a removal region.
/// - The function removes everything from `<custom>` up to and including
/// the next `</custom>` tag.
/// - If a `<custom>` tag appears **without** a matching `</custom>`,
/// the function removes the `<custom>` tag and **drops the remainder**
/// of the string.
/// - Text outside of `<custom> ... </custom>` is preserved exactly,
/// including whitespace and UTF‑8 characters.
/// - The first closing tag always ends the removal region.
///
/// # Returns
///
/// A new `String` containing the original text with all `<custom>`
/// sections removed.
///
/// # Performance
///
/// # Examples
///
/// ## Basic removal
/// ```
/// use bt_string_utils::cleanser::remove_tags;
/// let result = remove_tags("Hello <custom>secret</custom> world", "<custom>","</custom>");
/// assert_eq!(result, "Hello world");
/// ```
///
/// ## Multiple tags
/// ```
/// use bt_string_utils::cleanser::remove_tags;
/// let result = remove_tags("1 <custom>a</custom> 2 <custom>b</custom> 3", "<custom>","</custom>");
/// assert_eq!(result, "1 2 3");
/// ```
///
/// ## Missing closing tag
/// ```
/// use bt_string_utils::cleanser::remove_tags;
/// let result = remove_tags("before <custom>unfinished", "<custom>","</custom>");
/// assert_eq!(result, "before ");
/// ```
///
/// ## UTF‑8 characters preserved
/// ```
/// use bt_string_utils::cleanser::remove_tags;
/// let result = remove_tags("héllo <custom>x</custom> wørld 🌍", "<custom>","</custom>");
/// assert_eq!(result, "héllo wørld 🌍");
/// ```
///
/// ## No tags present
/// ```
/// use bt_string_utils::cleanser::remove_tags;
/// let result = remove_tags("nothing to remove", "<custom>","</custom>");
/// assert_eq!(result, "nothing to remove");
/// ```
/// Replaces all Unicode whitespace characters in the given string slice
/// with the specified replacement character.
///
/// This function treats any character for which `char::is_whitespace()`
/// returns `true` as whitespace. That includes spaces, tabs, newlines,
/// and a variety of Unicode whitespace characters.
///
/// # Arguments
///
/// * `input` - The string slice to process.
/// * `replacement` - The character that will replace each whitespace character.
///
/// # Returns
///
/// A new `String` where every whitespace character has been replaced
/// by the provided `replacement` character.
///
/// # Examples
///
/// ```
/// use bt_string_utils::cleanser::replace_whitespace;
/// let result = replace_whitespace("Hello world\nRust!", '_');
/// assert_eq!(result, "Hello___world_Rust!");
/// ```
///Removed any no visible UTF-8 character from string