1
Fork 0

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`.
This commit is contained in:
Nicholas Nethercote 2024-01-23 12:27:56 +11:00
parent ef1e2228cf
commit a1c07214f0
3 changed files with 52 additions and 42 deletions

View file

@ -3,7 +3,7 @@
use crate::ast::{self, LitKind, MetaItemLit, StrStyle};
use crate::token::{self, Token};
use rustc_lexer::unescape::{
byte_from_char, unescape_byte, unescape_c_string, unescape_char, unescape_literal, CStrUnit,
byte_from_char, unescape_byte, unescape_c_string, unescape_char, unescape_literal, MixedUnit,
Mode,
};
use rustc_span::symbol::{kw, sym, Symbol};
@ -127,10 +127,10 @@ impl LitKind {
let s = symbol.as_str();
let mut buf = Vec::with_capacity(s.len());
unescape_c_string(s, Mode::CStr, &mut |_span, c| match c {
Ok(CStrUnit::Byte(b)) => buf.push(b),
Ok(CStrUnit::Char(c)) => {
Ok(MixedUnit::Char(c)) => {
buf.extend_from_slice(c.encode_utf8(&mut [0; 4]).as_bytes())
}
Ok(MixedUnit::HighByte(b)) => buf.push(b),
Err(err) => {
assert!(!err.is_fatal(), "failed to unescape C string literal")
}