Skip to content

Commit 7ef10fc

Browse files
committed
Simplify code.
1 parent 7b9ffb1 commit 7ef10fc

File tree

1 file changed

+58
-37
lines changed

1 file changed

+58
-37
lines changed

library/core/src/escape.rs

Lines changed: 58 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -233,29 +233,66 @@ impl<const N: usize, ESCAPING> EscapeIterInner<N, ESCAPING> {
233233
usize::from(self.alive.end - self.alive.start)
234234
}
235235

236+
#[inline]
236237
pub(crate) fn advance_by(&mut self, n: usize) -> Result<(), NonZero<usize>> {
237238
self.alive.advance_by(n)
238239
}
239240

241+
#[inline]
240242
pub(crate) fn advance_back_by(&mut self, n: usize) -> Result<(), NonZero<usize>> {
241243
self.alive.advance_back_by(n)
242244
}
245+
246+
/// Returns a `char` if `self.data` contains one in its `literal` variant.
247+
#[inline]
248+
const fn to_char(&self) -> Option<char> {
249+
if self.alive.end > Self::LITERAL_ESCAPE_START {
250+
// SAFETY: We just checked that `self.data` contains a `char` in
251+
// its `literal` variant.
252+
return Some(unsafe { self.data.literal });
253+
}
254+
255+
None
256+
}
257+
258+
/// Returns a string of printable ASCII characters in `self.data.escape_seq`.
259+
///
260+
/// # Safety
261+
///
262+
/// - `self.data` must contain printable ASCII characters in its `escape_seq` variant.
263+
/// - `self.alive` must be a valid range for `self.data.escape_seq`.
264+
#[inline]
265+
unsafe fn to_str_unchecked(&self) -> &str {
266+
// SAFETY: The caller guarantees `self.data` contains printable ASCII
267+
// characters in its `escape_seq` variant, and `self.alive` is
268+
// a valid range for `self.data.escape_seq`.
269+
unsafe {
270+
self.data
271+
.escape_seq
272+
.get_unchecked(usize::from(self.alive.start)..usize::from(self.alive.end))
273+
.as_str()
274+
}
275+
}
243276
}
244277

245278
impl<const N: usize> EscapeIterInner<N, AlwaysEscaped> {
246279
pub(crate) fn next(&mut self) -> Option<u8> {
247280
let i = self.alive.next()?;
248281

249-
// SAFETY: The `AlwaysEscaped` marker guarantees that `self.data.escape_seq` is valid,
250-
// and `i` is guaranteed to be a valid index for `self.data.escape_seq`.
282+
// SAFETY: The `AlwaysEscaped` marker guarantees that `self.data`
283+
// contains printable ASCII characters in its `escape_seq`
284+
// variant, and `i` is guaranteed to be a valid index for
285+
// `self.data.escape_seq`.
251286
unsafe { Some(self.data.escape_seq.get_unchecked(usize::from(i)).to_u8()) }
252287
}
253288

254289
pub(crate) fn next_back(&mut self) -> Option<u8> {
255290
let i = self.alive.next_back()?;
256291

257-
// SAFETY: The `AlwaysEscaped` marker guarantees that `self.data.escape_seq` is valid,
258-
// and `i` is guaranteed to be a valid index for `self.data.escape_seq`.
292+
// SAFETY: The `AlwaysEscaped` marker guarantees that `self.data`
293+
// contains printable ASCII characters in its `escape_seq`
294+
// variant, and `i` is guaranteed to be a valid index for
295+
// `self.data.escape_seq`.
259296
unsafe { Some(self.data.escape_seq.get_unchecked(usize::from(i)).to_u8()) }
260297
}
261298
}
@@ -276,63 +313,47 @@ impl<const N: usize> EscapeIterInner<N, MaybeEscaped> {
276313
pub(crate) fn next(&mut self) -> Option<char> {
277314
let i = self.alive.next()?;
278315

279-
if self.alive.end > Self::LITERAL_ESCAPE_START {
280-
// SAFETY: We just checked that `self.data.literal` is valid.
281-
return Some(unsafe { self.data.literal });
316+
if let Some(c) = self.to_char() {
317+
return Some(c);
282318
}
283319

284-
// SAFETY: At this point, `self.data.escape_seq` must be valid, and
285-
// `i` is guaranteed to be a valid index for `self.data.escape_seq`.
320+
// SAFETY: At this point, `self.data` must contain printable ASCII
321+
// characters in its `escape_seq` variant, and `i` is
322+
// guaranteed to be a valid index for `self.data.escape_seq`.
286323
Some(char::from(unsafe { self.data.escape_seq.get_unchecked(usize::from(i)).to_u8() }))
287324
}
288325
}
289326

290327
impl<const N: usize> fmt::Display for EscapeIterInner<N, AlwaysEscaped> {
291328
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
292-
// SAFETY: The `AlwaysEscaped` marker guarantees that `self.data.escape_seq` is valid,
293-
// and `self.alive` is guaranteed to be a valid range for `self.data.escape_seq`.
294-
f.write_str(unsafe {
295-
self.data.escape_seq[usize::from(self.alive.start)..usize::from(self.alive.end)]
296-
.as_str()
297-
})
329+
// SAFETY: The `AlwaysEscaped` marker guarantees that `self.data`
330+
// contains printable ASCII chars, and `self.alive` is
331+
// guaranteed to be a valid range for `self.data`.
332+
f.write_str(unsafe { self.to_str_unchecked() })
298333
}
299334
}
300335

301336
impl<const N: usize> fmt::Display for EscapeIterInner<N, MaybeEscaped> {
302337
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
303-
if self.alive.end > Self::LITERAL_ESCAPE_START {
304-
// SAFETY: We just checked that `self.data.literal` is valid.
305-
return f.write_char(unsafe { self.data.literal });
338+
if let Some(c) = self.to_char() {
339+
return f.write_char(c);
306340
}
307341

308-
// SAFETY: At this point, `self.data.escape_seq` must be valid,
309-
// and `self.alive` is guaranteed to be a valid range for `self.data.escape_seq`.
310-
f.write_str(unsafe {
311-
self.data.escape_seq[usize::from(self.alive.start)..usize::from(self.alive.end)]
312-
.as_str()
313-
})
342+
// SAFETY: At this point, `self.data` must contain printable ASCII
343+
// characters in its `escape_seq` variant, and `self.alive`
344+
// is guaranteed to be a valid range for `self.data`.
345+
f.write_str(unsafe { self.to_str_unchecked() })
314346
}
315347
}
316348

317349
impl<const N: usize> fmt::Debug for EscapeIterInner<N, AlwaysEscaped> {
318350
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
319-
f.debug_tuple("EscapeIterInner")
320-
// SAFETY: The `AlwaysEscaped` marker guarantees that `self.data.escape_seq` is valid.
321-
.field(unsafe { &self.data.escape_seq })
322-
.finish()
351+
f.debug_tuple("EscapeIterInner").field(&format_args!("{:?}", self)).finish()
323352
}
324353
}
325354

326355
impl<const N: usize> fmt::Debug for EscapeIterInner<N, MaybeEscaped> {
327356
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
328-
let mut d = f.debug_tuple("EscapeIterInner");
329-
if self.alive.end > Self::LITERAL_ESCAPE_START {
330-
// SAFETY: We just checked that `self.data.literal` is valid.
331-
d.field(unsafe { &self.data.literal });
332-
} else {
333-
// SAFETY: At this point, `self.data.escape_seq` must be valid.
334-
d.field(unsafe { &self.data.escape_seq });
335-
}
336-
d.finish()
357+
f.debug_tuple("EscapeIterInner").field(&format_args!("{:?}", self)).finish()
337358
}
338359
}

0 commit comments

Comments
 (0)