Skip to content

Commit 703b285

Browse files
committed
Replace magic number with constant.
1 parent e8c3b28 commit 703b285

File tree

1 file changed

+13
-10
lines changed

1 file changed

+13
-10
lines changed

library/core/src/escape.rs

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -181,23 +181,26 @@ pub(crate) struct MaybeEscaped;
181181
pub(crate) struct EscapeIterInner<const N: usize, ESCAPING> {
182182
// Invariant:
183183
//
184-
// If `alive.end <= 128`, `data.escape_seq` must be valid and
184+
// If `alive.end <= Self::LITERAL_ESCAPE_START`, `data.escape_seq` must be valid and
185185
// contain printable ASCII characters in the `alive` range.
186-
// If `alive.end > 128`, `data.literal` must be valid and
186+
// If `alive.end > Self::LITERAL_ESCAPE_START`, `data.literal` must be valid and
187187
// the `alive` range must have a length of at most `1`.
188188
data: MaybeEscapedCharacter<N>,
189189
alive: Range<u8>,
190190
escaping: PhantomData<ESCAPING>,
191191
}
192192

193193
impl<const N: usize, ESCAPING> EscapeIterInner<N, ESCAPING> {
194+
const LITERAL_ESCAPE_START: u8 = 128;
195+
194196
/// # Safety
195197
///
196198
/// `data.escape_seq` must contain an escape sequence in the range given by `alive`.
197199
#[inline]
198200
const unsafe fn new(data: MaybeEscapedCharacter<N>, alive: Range<u8>) -> Self {
199-
// Longer escape sequences are not useful given `alive.end` is at most 128.
200-
const { assert!(N < 128) };
201+
// Longer escape sequences are not useful given `alive.end` is at most
202+
// `Self::LITERAL_ESCAPE_START`.
203+
const { assert!(N < Self::LITERAL_ESCAPE_START) };
201204
Self { data, alive, escaping: PhantomData }
202205
}
203206

@@ -263,17 +266,17 @@ impl<const N: usize> EscapeIterInner<N, MaybeEscaped> {
263266
pub(crate) const fn printable(c: char) -> Self {
264267
Self {
265268
data: MaybeEscapedCharacter { literal: c },
266-
// Uphold invariant (`alive.end > 128`), and ensure `len` behaves
267-
// correctly for iterating through one character literal.
268-
alive: 128..129,
269+
// Uphold invariant (`alive.end > Self::LITERAL_ESCAPE_START`), and ensure `len`
270+
// behaves correctly for iterating through one character literal.
271+
alive: Self::LITERAL_ESCAPE_START..(Self::LITERAL_ESCAPE_START + 1),
269272
escaping: PhantomData,
270273
}
271274
}
272275

273276
pub(crate) fn next(&mut self) -> Option<char> {
274277
let i = self.alive.next()?;
275278

276-
if self.alive.end > 128 {
279+
if self.alive.end > Self::LITERAL_ESCAPE_START {
277280
// SAFETY: We just checked that `self.data.literal` is valid.
278281
return Some(unsafe { self.data.literal });
279282
}
@@ -297,7 +300,7 @@ impl<const N: usize> fmt::Display for EscapeIterInner<N, AlwaysEscaped> {
297300

298301
impl<const N: usize> fmt::Display for EscapeIterInner<N, MaybeEscaped> {
299302
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
300-
if self.alive.end > 128 {
303+
if self.alive.end > Self::LITERAL_ESCAPE_START {
301304
// SAFETY: We just checked that `self.data.literal` is valid.
302305
return f.write_char(unsafe { self.data.literal });
303306
}
@@ -323,7 +326,7 @@ impl<const N: usize> fmt::Debug for EscapeIterInner<N, AlwaysEscaped> {
323326
impl<const N: usize> fmt::Debug for EscapeIterInner<N, MaybeEscaped> {
324327
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
325328
let mut d = f.debug_tuple("EscapeIterInner");
326-
if self.alive.end > 128 {
329+
if self.alive.end > Self::LITERAL_ESCAPE_START {
327330
// SAFETY: We just checked that `self.data.literal` is valid.
328331
d.field(unsafe { &self.data.literal });
329332
} else {

0 commit comments

Comments
 (0)