Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 5651407

Browse files
committed
Rework CStrUnit.
- Rename it as `MixedUnit`, because it will soon be used in more than just C string literals. - Change the `Byte` variant to `HighByte` and use it only for `\x80`..`\xff` cases. This fixes the old inexactness where ASCII chars could be encoded with either `Byte` or `Char`. - Add useful comments. - Remove `is_ascii`, in favour of `u8::is_ascii`.
1 parent 85d56ee commit 5651407

File tree

1 file changed

+4
-5
lines changed

1 file changed

+4
-5
lines changed

crates/syntax/src/ast/token_ext.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use std::{
66
};
77

88
use rustc_lexer::unescape::{
9-
unescape_byte, unescape_c_string, unescape_char, unescape_literal, CStrUnit, Mode,
9+
unescape_byte, unescape_c_string, unescape_char, unescape_literal, MixedUnit, Mode,
1010
};
1111

1212
use crate::{
@@ -336,10 +336,9 @@ impl ast::CString {
336336
let mut buf = Vec::new();
337337
let mut prev_end = 0;
338338
let mut has_error = false;
339-
let mut char_buf = [0u8; 4];
340-
let mut extend_unit = |buf: &mut Vec<u8>, unit: CStrUnit| match unit {
341-
CStrUnit::Byte(b) => buf.push(b),
342-
CStrUnit::Char(c) => buf.extend(c.encode_utf8(&mut char_buf).as_bytes()),
339+
let extend_unit = |buf: &mut Vec<u8>, unit: MixedUnit| match unit {
340+
MixedUnit::Char(c) => buf.extend(c.encode_utf8(&mut [0; 4]).as_bytes()),
341+
MixedUnit::HighByte(b) => buf.push(b),
343342
};
344343
unescape_c_string(text, Self::MODE, &mut |char_range, unescaped| match (
345344
unescaped,

0 commit comments

Comments
 (0)