Skip to content

Commit f8929e9

Browse files
committed
inline unescape_{unicode,mixed} and move docs
1 parent 9fbb174 commit f8929e9

File tree

1 file changed

+36
-51
lines changed

1 file changed

+36
-51
lines changed

src/lib.rs

Lines changed: 36 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -134,71 +134,75 @@ pub fn unescape_for_errors(
134134
}
135135
}
136136

137+
/// Takes the contents of a raw string literal (without quotes)
138+
/// and produces a sequence of characters or errors,
139+
/// which are returned by invoking `callback`.
140+
/// NOTE: Does no escaping, but produces errors for bare carriage return ('\r').
137141
pub fn check_raw_str(src: &str, mut callback: impl FnMut(Range<usize>, Result<char, EscapeError>)) {
138-
unescape_unicode(src, Mode::RawStr, &mut callback)
142+
check_raw_common(src, Mode::RawStr, &mut callback)
139143
}
140144

145+
/// Takes the contents of a raw byte string literal (without quotes)
146+
/// and produces a sequence of bytes or errors,
147+
/// which are returned by invoking `callback`.
148+
/// NOTE: Does no escaping, but produces errors for bare carriage return ('\r').
141149
pub fn check_raw_byte_str(
142150
src: &str,
143151
mut callback: impl FnMut(Range<usize>, Result<u8, EscapeError>),
144152
) {
145-
unescape_unicode(src, Mode::RawByteStr, &mut |r, res| {
153+
check_raw_common(src, Mode::RawByteStr, &mut |r, res| {
146154
callback(r, res.map(byte_from_char))
147155
})
148156
}
149157

158+
/// Takes the contents of a raw C string literal (without quotes)
159+
/// and produces a sequence of characters or errors,
160+
/// which are returned by invoking `callback`.
161+
/// NOTE: Does no escaping, but produces errors for bare carriage return ('\r').
150162
pub fn check_raw_c_str(
151163
src: &str,
152164
mut callback: impl FnMut(Range<usize>, Result<char, EscapeError>),
153165
) {
154-
unescape_unicode(src, Mode::RawCStr, &mut callback)
166+
check_raw_common(src, Mode::RawCStr, &mut |r, mut result| {
167+
if let Ok('\0') = result {
168+
result = Err(EscapeError::NulInCStr);
169+
}
170+
callback(r, result)
171+
})
155172
}
156173

174+
/// Takes the contents of a string literal (without quotes)
175+
/// and produces a sequence of escaped characters or errors,
176+
/// which are returned by invoking `callback`.
157177
pub fn unescape_str(src: &str, mut callback: impl FnMut(Range<usize>, Result<char, EscapeError>)) {
158-
unescape_unicode(src, Mode::Str, &mut callback)
178+
unescape_non_raw_common(src, Mode::Str, &mut callback)
159179
}
160180

181+
/// Takes the contents of a byte string literal (without quotes)
182+
/// and produces a sequence of escaped bytes or errors,
183+
/// which are returned by invoking `callback`.
161184
pub fn unescape_byte_str(
162185
src: &str,
163186
mut callback: impl FnMut(Range<usize>, Result<u8, EscapeError>),
164187
) {
165-
unescape_unicode(src, Mode::ByteStr, &mut |r, res| {
188+
unescape_non_raw_common(src, Mode::ByteStr, &mut |r, res| {
166189
callback(r, res.map(byte_from_char))
167190
})
168191
}
169192

193+
/// Takes the contents of a C string literal (without quotes)
194+
/// and produces a sequence of escaped MixedUnits or errors,
195+
/// which are returned by invoking `callback`.
170196
pub fn unescape_c_str(
171197
src: &str,
172198
mut callback: impl FnMut(Range<usize>, Result<MixedUnit, EscapeError>),
173199
) {
174-
unescape_mixed(src, Mode::CStr, &mut callback)
175-
}
176-
177-
/// Takes the contents of a unicode-only (non-mixed-utf8) literal (without
178-
/// quotes) and produces a sequence of escaped characters or errors.
179-
///
180-
/// Values are returned by invoking `callback`. For `Char` and `Byte` modes,
181-
/// the callback will be called exactly once.
182-
fn unescape_unicode<F>(src: &str, mode: Mode, callback: &mut F)
183-
where
184-
F: FnMut(Range<usize>, Result<char, EscapeError>),
185-
{
186-
match mode {
187-
Char | Byte => {
188-
let mut chars = src.chars();
189-
let res = unescape_char_or_byte(&mut chars, mode);
190-
callback(0..(src.len() - chars.as_str().len()), res);
200+
unescape_non_raw_common(src, Mode::CStr, &mut |r, mut result| {
201+
if let Ok(MixedUnit::Char('\0')) = result {
202+
result = Err(EscapeError::NulInCStr);
191203
}
192-
Str | ByteStr => unescape_non_raw_common(src, mode, callback),
193-
RawStr | RawByteStr => check_raw_common(src, mode, callback),
194-
RawCStr => check_raw_common(src, mode, &mut |r, mut result| {
195-
if let Ok('\0') = result {
196-
result = Err(EscapeError::NulInCStr);
197-
}
198-
callback(r, result)
199-
}),
200-
CStr => unreachable!(),
201-
}
204+
callback(r, result)
205+
})
202206
}
203207

204208
/// Used for mixed utf8 string literals, i.e. those that allow both unicode
@@ -236,25 +240,6 @@ impl From<u8> for MixedUnit {
236240
}
237241
}
238242

239-
/// Takes the contents of a mixed-utf8 literal (without quotes) and produces
240-
/// a sequence of escaped characters or errors.
241-
///
242-
/// Values are returned by invoking `callback`.
243-
fn unescape_mixed<F>(src: &str, mode: Mode, callback: &mut F)
244-
where
245-
F: FnMut(Range<usize>, Result<MixedUnit, EscapeError>),
246-
{
247-
match mode {
248-
CStr => unescape_non_raw_common(src, mode, &mut |r, mut result| {
249-
if let Ok(MixedUnit::Char('\0')) = result {
250-
result = Err(EscapeError::NulInCStr);
251-
}
252-
callback(r, result)
253-
}),
254-
Char | Byte | Str | RawStr | ByteStr | RawByteStr | RawCStr => unreachable!(),
255-
}
256-
}
257-
258243
/// Takes a contents of a char literal (without quotes), and returns an
259244
/// unescaped char or an error.
260245
pub fn unescape_char(src: &str) -> Result<char, EscapeError> {

0 commit comments

Comments
 (0)