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
use alloc::{borrow::Cow, vec::Vec};
/// To extend `str` and `Cow<str>` to have `replace_newlines_with_space` method.
///
/// This can be useful when you need to normalize multiline strings into a single line for logging, database storage, or display purposes.
pub trait ReplaceNewlinesWithSpace<'a> {
/// Returns a `Cow<str>` where all newline sequences are replaced with a space.
///
/// Replaces Windows-style newlines (`\r\n`), old Mac-style (`\r`), and Unix-style (`\n`).
fn replace_newlines_with_space(self) -> Cow<'a, str>;
}
impl<'a> ReplaceNewlinesWithSpace<'a> for &'a str {
fn replace_newlines_with_space(self) -> Cow<'a, str> {
let s = self;
let bytes = s.as_bytes();
let length = bytes.len();
let mut p = 0;
let first_len = loop {
if p == length {
return Cow::from(s);
}
let e = bytes[p];
match e {
b'\r' => {
if p < length - 1 && bytes[p + 1] == b'\n' {
break 2; // CRLF
} else {
break 1; // CR
}
},
b'\n' => {
break 1; // LF
},
_ => (),
}
p += 1;
};
let mut new_v = Vec::with_capacity(bytes.len());
new_v.extend_from_slice(&bytes[..p]);
new_v.push(b' ');
p += first_len;
let mut start = p;
loop {
if p == length {
break;
}
let e = bytes[p];
match e {
b'\r' => {
new_v.extend_from_slice(&bytes[start..p]);
if p < length - 1 && bytes[p + 1] == b'\n' {
// CRLF
p += 1;
start = p + 1;
} else {
// CR
start = p + 1;
}
new_v.push(b' ');
},
b'\n' => {
// LF
new_v.extend_from_slice(&bytes[start..p]);
start = p + 1;
new_v.push(b' ');
},
_ => (),
}
p += 1;
}
new_v.extend_from_slice(&bytes[start..p]);
Cow::from(unsafe { String::from_utf8_unchecked(new_v) })
}
}
impl<'a> ReplaceNewlinesWithSpace<'a> for Cow<'a, str> {
#[inline]
fn replace_newlines_with_space(self) -> Cow<'a, str> {
match self {
Cow::Borrowed(s) => s.replace_newlines_with_space(),
Cow::Owned(s) => {
match s.replace_newlines_with_space() {
Cow::Borrowed(_) => {
// it changes nothing
// if there were any characters that needed to be replaced, it had to be `Cow::Owned`
Cow::Owned(s)
},
Cow::Owned(s) => Cow::Owned(s),
}
},
}
}
}