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
use alloc::{borrow::Cow, str::from_utf8_unchecked, vec::Vec};
/// To extend `str` and `Cow<str>` to have `escape_characters` and `escape_ascii_characters` method.
///
/// Typical use cases include preparing strings for SQL `LIKE` queries or other contexts where certain characters need to be escaped.
pub trait EscapeCharacters<'a> {
/// Escapes all occurrences of the specified characters within a string.
///
/// This function scans the input string for any character that matches one of
/// the `escaped_characters` or the `escape_character` itself, and prefixes
/// those characters with the provided `escape_character`.
fn escape_characters(self, escape_character: char, escaped_characters: &[char])
-> Cow<'a, str>;
/// Escapes ASCII characters within a UTF-8 string.
///
/// Similar to [`EscapeCharacters::escape_characters`], but operates directly on bytes instead of Unicode scalar values.
/// This version is optimized for ASCII-only escaping and avoids unnecessary Unicode conversions.
fn escape_ascii_characters(
self,
escape_character: u8,
escaped_characters: &[u8],
) -> Cow<'a, str>;
}
impl<'a> EscapeCharacters<'a> for &'a str {
fn escape_characters(
self,
escape_character: char,
escaped_characters: &[char],
) -> Cow<'a, str> {
let s = self;
if escaped_characters.is_empty() {
return Cow::Borrowed(s);
}
let mut p = 0;
let mut chars = s.chars();
let need_escape = |c: char| {
c == escape_character
|| escaped_characters.iter().any(|escaped_character| c.eq(escaped_character))
};
let first_c = loop {
let c = if let Some(c) = chars.next() {
c
} else {
return Cow::Borrowed(s);
};
if need_escape(c) {
break c;
}
p += c.len_utf8();
};
let mut new_s = String::with_capacity(s.len() + 1);
new_s.push_str(unsafe { from_utf8_unchecked(&s.as_bytes()[0..p]) });
new_s.push(escape_character);
new_s.push(first_c);
for c in chars {
if need_escape(c) {
new_s.push(escape_character);
}
new_s.push(c);
}
Cow::Owned(new_s)
}
fn escape_ascii_characters(
self,
escape_character: u8,
escaped_characters: &[u8],
) -> Cow<'a, str> {
let s = self;
if escaped_characters.is_empty() {
return Cow::Borrowed(s);
}
let bytes = s.as_bytes();
let length = bytes.len();
let mut p = 0;
let need_escape = |b: u8| {
b == escape_character
|| escaped_characters.iter().any(|escaped_character| b.eq(escaped_character))
};
loop {
if p == length {
return Cow::Borrowed(s);
}
let e = bytes[p];
let width = unsafe { utf8_width::get_width_assume_valid(e) };
if width == 1 && need_escape(e) {
break;
}
p += width;
}
let mut new_v = Vec::with_capacity(bytes.len() + 1);
new_v.extend_from_slice(&bytes[..p]);
new_v.push(escape_character);
let mut start = p;
p += 1;
loop {
if p == length {
break;
}
let e = bytes[p];
let width = unsafe { utf8_width::get_width_assume_valid(e) };
if width == 1 && need_escape(e) {
new_v.extend_from_slice(&bytes[start..p]);
start = p + 1;
new_v.push(escape_character);
new_v.push(e);
}
p += width;
}
new_v.extend_from_slice(&bytes[start..p]);
Cow::Owned(unsafe { String::from_utf8_unchecked(new_v) })
}
}
impl<'a> EscapeCharacters<'a> for Cow<'a, str> {
#[inline]
fn escape_characters(
self,
escape_character: char,
escaped_characters: &[char],
) -> Cow<'a, str> {
match self {
Cow::Borrowed(s) => s.escape_characters(escape_character, escaped_characters),
Cow::Owned(s) => {
match s.escape_characters(escape_character, escaped_characters) {
Cow::Borrowed(_) => {
// it changes nothing
// if there were any characters that needed to be escaped, it had to be `Cow::Owned`
Cow::Owned(s)
},
Cow::Owned(s) => Cow::Owned(s),
}
},
}
}
#[inline]
fn escape_ascii_characters(
self,
escape_character: u8,
escaped_characters: &[u8],
) -> Cow<'a, str> {
match self {
Cow::Borrowed(s) => s.escape_ascii_characters(escape_character, escaped_characters),
Cow::Owned(s) => {
match s.escape_ascii_characters(escape_character, escaped_characters) {
Cow::Borrowed(_) => {
// it changes nothing
// if there were any characters that needed to be escaped, it had to be `Cow::Owned`
Cow::Owned(s)
},
Cow::Owned(s) => Cow::Owned(s),
}
},
}
}
}