slash_formatter/file_separator.rs
1use alloc::{borrow::Cow, string::String};
2
3/// Delete an ending `FILE_SEPARATOR` in a string except for just `FILE_SEPARATOR`.
4///
5/// ```
6/// assert_eq!(
7/// "path",
8/// slash_formatter::delete_end_file_separator(concat!(
9/// "path",
10/// slash_formatter::file_separator!()
11/// ))
12/// );
13/// ```
14#[inline]
15pub fn delete_end_file_separator<S: ?Sized + AsRef<str>>(s: &S) -> &str {
16 #[cfg(unix)]
17 {
18 crate::delete_end_slash(s)
19 }
20
21 #[cfg(windows)]
22 {
23 crate::delete_end_backslash(s)
24 }
25}
26
27/// Delete an ending `FILE_SEPARATOR` in a string except for just `FILE_SEPARATOR`.
28///
29/// ```
30/// let mut s =
31/// String::from(concat!("path", slash_formatter::file_separator!()));
32///
33/// slash_formatter::delete_end_file_separator_in_place(&mut s);
34///
35/// assert_eq!("path", s);
36/// ```
37#[inline]
38pub fn delete_end_file_separator_in_place(s: &mut String) {
39 #[cfg(unix)]
40 {
41 crate::delete_end_slash_in_place(s)
42 }
43
44 #[cfg(windows)]
45 {
46 crate::delete_end_backslash_in_place(s)
47 }
48}
49
50/// Delete a starting `FILE_SEPARATOR` in a string except for just `FILE_SEPARATOR`.
51///
52/// ```
53/// assert_eq!(
54/// "path",
55/// slash_formatter::delete_start_file_separator(concat!(
56/// slash_formatter::file_separator!(),
57/// "path"
58/// ))
59/// );
60/// ```
61#[inline]
62pub fn delete_start_file_separator<S: ?Sized + AsRef<str>>(s: &S) -> &str {
63 #[cfg(unix)]
64 {
65 crate::delete_start_slash(s)
66 }
67
68 #[cfg(windows)]
69 {
70 crate::delete_start_backslash(s)
71 }
72}
73
74/// Delete a starting `FILE_SEPARATOR` in a string except for just `FILE_SEPARATOR`.
75///
76/// ```
77/// let mut s =
78/// String::from(concat!(slash_formatter::file_separator!(), "path"));
79///
80/// slash_formatter::delete_start_file_separator_in_place(&mut s);
81///
82/// assert_eq!("path", s);
83/// ```
84#[inline]
85pub fn delete_start_file_separator_in_place(s: &mut String) {
86 #[cfg(unix)]
87 {
88 crate::delete_start_slash_in_place(s)
89 }
90
91 #[cfg(windows)]
92 {
93 crate::delete_start_backslash_in_place(s)
94 }
95}
96
97/// Add a starting `FILE_SEPARATOR` into a string.
98///
99/// ```
100/// assert_eq!(
101/// concat!(slash_formatter::file_separator!(), "path"),
102/// slash_formatter::add_start_file_separator("path")
103/// );
104/// ```
105#[inline]
106pub fn add_start_file_separator<S: ?Sized + AsRef<str>>(s: &S) -> Cow<str> {
107 #[cfg(unix)]
108 {
109 crate::add_start_slash(s)
110 }
111
112 #[cfg(windows)]
113 {
114 crate::add_start_backslash(s)
115 }
116}
117
118/// Add a starting `FILE_SEPARATOR` into a string.
119///
120/// ```
121/// let mut s = String::from("path");
122///
123/// slash_formatter::add_start_file_separator_in_place(&mut s);
124///
125/// assert_eq!(concat!(slash_formatter::file_separator!(), "path"), s);
126/// ```
127#[inline]
128pub fn add_start_file_separator_in_place(s: &mut String) {
129 #[cfg(unix)]
130 {
131 crate::add_start_slash_in_place(s)
132 }
133
134 #[cfg(windows)]
135 {
136 crate::add_start_backslash_in_place(s)
137 }
138}
139
140/// Add an ending `FILE_SEPARATOR` into a string.
141///
142/// ```
143/// assert_eq!(
144/// concat!("path", slash_formatter::file_separator!()),
145/// slash_formatter::add_end_file_separator("path")
146/// );
147/// ```
148#[inline]
149pub fn add_end_file_separator<S: ?Sized + AsRef<str>>(s: &S) -> Cow<str> {
150 #[cfg(unix)]
151 {
152 crate::add_end_slash(s)
153 }
154
155 #[cfg(windows)]
156 {
157 crate::add_end_backslash(s)
158 }
159}
160
161/// Add an ending `FILE_SEPARATOR` into a string.
162///
163/// ```
164/// let mut s = String::from("path");
165///
166/// slash_formatter::add_end_file_separator_in_place(&mut s);
167///
168/// assert_eq!(concat!("path", slash_formatter::file_separator!()), s);
169/// ```
170#[inline]
171pub fn add_end_file_separator_in_place(s: &mut String) {
172 #[cfg(unix)]
173 {
174 crate::add_end_slash_in_place(s)
175 }
176
177 #[cfg(windows)]
178 {
179 crate::add_end_backslash_in_place(s)
180 }
181}
182
183/// Concatenate two strings with `FILE_SEPARATOR`.
184///
185/// ```
186/// assert_eq!(
187/// concat!("path", slash_formatter::file_separator!(), "to"),
188/// slash_formatter::concat_with_file_separator(
189/// "path",
190/// concat!("to", slash_formatter::file_separator!())
191/// )
192/// );
193/// ```
194#[inline]
195pub fn concat_with_file_separator<S1: Into<String>, S2: AsRef<str>>(s1: S1, s2: S2) -> String {
196 #[cfg(unix)]
197 {
198 crate::concat_with_slash(s1, s2)
199 }
200
201 #[cfg(windows)]
202 {
203 crate::concat_with_backslash(s1, s2)
204 }
205}
206
207/// Concatenate two strings with `FILE_SEPARATOR`.
208///
209/// ```
210/// let mut s = String::from("path");
211///
212/// slash_formatter::concat_with_file_separator_in_place(
213/// &mut s,
214/// concat!("to", slash_formatter::file_separator!()),
215/// );
216///
217/// assert_eq!(concat!("path", slash_formatter::file_separator!(), "to"), s);
218/// ```
219#[inline]
220pub fn concat_with_file_separator_in_place<S2: AsRef<str>>(s1: &mut String, s2: S2) {
221 #[cfg(unix)]
222 {
223 crate::concat_with_slash_in_place(s1, s2)
224 }
225
226 #[cfg(windows)]
227 {
228 crate::concat_with_backslash_in_place(s1, s2)
229 }
230}
231
232#[cfg(unix)]
233/**
234Concatenate multiple strings with `FILE_SEPARATOR`. It can also be used to get the literal `FILE_SEPARATOR`.
235
236```
237assert_eq!(slash_formatter::concat_with_file_separator!("path", "to", "file"), slash_formatter::file_separator!("path", concat!("to", slash_formatter::file_separator!()), concat!(slash_formatter::file_separator!(), "file", slash_formatter::file_separator!())));
238
239let s = String::from("path");
240
241let s = slash_formatter::file_separator!(s, concat!("to", slash_formatter::file_separator!()), concat!(slash_formatter::file_separator!(), "file", slash_formatter::file_separator!()));
242
243assert_eq!(slash_formatter::concat_with_file_separator!("path", "to", "file"), s);
244```
245*/
246#[macro_export]
247macro_rules! file_separator {
248 ($($t:tt)*) => {
249 $crate::slash!($($t)*)
250 };
251}
252
253#[cfg(windows)]
254/**
255Concatenate multiple strings with `FILE_SEPARATOR`. It can also be used to get the literal `FILE_SEPARATOR`.
256
257```
258assert_eq!(slash_formatter::concat_with_file_separator!("path", "to", "file"), slash_formatter::file_separator!("path", concat!("to", slash_formatter::file_separator!()), concat!(slash_formatter::file_separator!(), "file", slash_formatter::file_separator!())));
259
260let s = String::from("path");
261
262let s = slash_formatter::file_separator!(s, concat!("to", slash_formatter::file_separator!()), concat!(slash_formatter::file_separator!(), "file", slash_formatter::file_separator!()));
263
264assert_eq!(slash_formatter::concat_with_file_separator!("path", "to", "file"), s);
265```
266*/
267#[macro_export]
268macro_rules! file_separator {
269 ($($t:tt)*) => {
270 $crate::backslash!($($t)*)
271 };
272}
273
274#[cfg(unix)]
275/**
276Concatenate multiple strings with `FILE_SEPARATOR`. It can also be used to get the literal `FILE_SEPARATOR`.
277
278```
279let mut s = String::from("path");
280
281slash_formatter::file_separator_in_place!(&mut s, concat!("to", slash_formatter::file_separator!()), concat!(slash_formatter::file_separator!(), "file", slash_formatter::file_separator!()));
282
283assert_eq!(slash_formatter::concat_with_file_separator!("path", "to", "file"), s);
284```
285*/
286#[macro_export]
287macro_rules! file_separator_in_place {
288 ($($t:tt)*) => {
289 $crate::slash_in_place!($($t)*)
290 };
291}
292
293#[cfg(windows)]
294/**
295Concatenate multiple strings with `FILE_SEPARATOR`. It can also be used to get the literal `FILE_SEPARATOR`.
296
297```
298let mut s = String::from("path");
299
300slash_formatter::file_separator_in_place!(&mut s, concat!("to", slash_formatter::file_separator!()), concat!(slash_formatter::file_separator!(), "file", slash_formatter::file_separator!()));
301
302assert_eq!(slash_formatter::concat_with_file_separator!("path", "to", "file"), s);
303```
304*/
305#[macro_export]
306macro_rules! file_separator_in_place {
307 ($($t:tt)*) => {
308 $crate::backslash_in_place!($($t)*)
309 };
310}
311
312#[cfg(unix)]
313/**
314Concatenates literals into a static string slice separated by `FILE_SEPARATOR`. Prefixes and suffixes can also be added.
315
316```rust
317assert_eq!(concat!("test", slash_formatter::file_separator!(), 10, slash_formatter::file_separator!(), 'b', slash_formatter::file_separator!(), true), slash_formatter::concat_with_file_separator!("test", 10, 'b', true));
318```
319*/
320#[macro_export]
321macro_rules! concat_with_file_separator {
322 ($($t:tt)*) => {
323 $crate::concat_with_slash!($($t)*)
324 };
325}
326
327#[cfg(windows)]
328/**
329Concatenates literals into a static string slice separated by `FILE_SEPARATOR`. Prefixes and suffixes can also be added.
330
331```rust
332assert_eq!(concat!("test", slash_formatter::file_separator!(), 10, slash_formatter::file_separator!(), 'b', slash_formatter::file_separator!(), true), slash_formatter::concat_with_file_separator!("test", 10, 'b', true));
333```
334*/
335#[macro_export]
336macro_rules! concat_with_file_separator {
337 ($($t:tt)*) => {
338 $crate::concat_with_backslash!($($t)*)
339 };
340}