Skip to content

Use NonZero in MixedUnit for C strings #15

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ env:
RUST_BACKTRACE: 1

jobs:
stable-checks:
beta-checks:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@stable
- uses: dtolnay/rust-toolchain@beta
with:
toolchain: stable
toolchain: beta
components: rustfmt
- run: cargo fmt -- --check
- run: cargo test
Expand Down
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ edition = "2021"
description = "Provides code to unescape string literals"
license = "Apache-2.0 OR MIT"
repository = "https://github.com/rust-lang/literal-escaper"
rust-version = "1.89" # for NonZero<char>

[dependencies]
std = { version = '1.0.0', optional = true, package = 'rustc-std-workspace-std' }
Expand Down
58 changes: 29 additions & 29 deletions benches/benches.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
extern crate test;

use rustc_literal_escaper::*;

use std::num::NonZero;
use std::ops::Range;
use std::{array, iter};

Expand Down Expand Up @@ -58,7 +60,7 @@ macro_rules! fn_bench_check_raw {

fn_bench_check_raw!(bench_check_raw_str, char, check_raw_str);
fn_bench_check_raw!(bench_check_raw_byte_str, u8, check_raw_byte_str);
fn_bench_check_raw!(bench_check_raw_c_str, char, check_raw_c_str);
fn_bench_check_raw!(bench_check_raw_c_str, NonZero<char>, check_raw_c_str);

// raw str

Expand Down Expand Up @@ -98,25 +100,28 @@ fn bench_check_raw_byte_str_ascii(b: &mut test::Bencher) {

#[bench]
fn bench_check_raw_c_str_ascii(b: &mut test::Bencher) {
bench_check_raw_c_str(b, "a", &['a'; LEN]);
bench_check_raw_c_str(b, "a", &[NonZero::new('a').unwrap(); LEN]);
}

#[bench]
fn bench_check_raw_c_str_non_ascii(b: &mut test::Bencher) {
bench_check_raw_c_str(b, "🦀", &['🦀'; LEN]);
bench_check_raw_c_str(b, "🦀", &[NonZero::new('🦀').unwrap(); LEN]);
}

#[bench]
fn bench_check_raw_c_str_unicode(b: &mut test::Bencher) {
bench_check_raw_c_str(
b,
"a🦀🚀z",
&array::from_fn::<_, { 4 * LEN }, _>(|i| match i % 4 {
0 => 'a',
1 => '🦀',
2 => '🚀',
3 => 'z',
_ => unreachable!(),
&array::from_fn::<_, { 4 * LEN }, _>(|i| {
NonZero::new(match i % 4 {
0 => 'a',
1 => '🦀',
2 => '🚀',
3 => 'z',
_ => unreachable!(),
})
.unwrap()
}),
);
}
Expand Down Expand Up @@ -318,7 +323,7 @@ fn bench_unescape_c_str_ascii(b: &mut test::Bencher) {
bench_unescape_c_str(
b,
r"a",
&array::from_fn::<_, { LEN }, _>(|i| (i..i + 1, Ok(MixedUnit::Char('a')))),
&array::from_fn::<_, { LEN }, _>(|i| (i..i + 1, 'a'.try_into())),
);
}

Expand All @@ -327,7 +332,7 @@ fn bench_unescape_c_str_non_ascii(b: &mut test::Bencher) {
bench_unescape_c_str(
b,
r"🦀",
&array::from_fn::<_, LEN, _>(|i| (4 * i..4 * (i + 1), Ok(MixedUnit::Char('🦀')))),
&array::from_fn::<_, LEN, _>(|i| (4 * i..4 * (i + 1), '🦀'.try_into())),
);
}

Expand All @@ -339,10 +344,10 @@ fn bench_unescape_c_str_unicode(b: &mut test::Bencher) {
b,
input,
&array::from_fn::<_, { 4 * LEN }, _>(|i| match i % 4 {
0 => (i / 4 * l..i / 4 * l + 1, Ok(MixedUnit::Char('a'))),
1 => (i / 4 * l + 1..i / 4 * l + 5, Ok(MixedUnit::Char('🦀'))),
2 => (i / 4 * l + 5..i / 4 * l + 9, Ok(MixedUnit::Char('🚀'))),
3 => (i / 4 * l + 9..i / 4 * l + 10, Ok(MixedUnit::Char('z'))),
0 => (i / 4 * l..i / 4 * l + 1, 'a'.try_into()),
1 => (i / 4 * l + 1..i / 4 * l + 5, '🦀'.try_into()),
2 => (i / 4 * l + 5..i / 4 * l + 9, '🚀'.try_into()),
3 => (i / 4 * l + 9..i / 4 * l + 10, 'z'.try_into()),
_ => unreachable!(),
}),
);
Expand All @@ -353,7 +358,7 @@ fn bench_unescape_c_str_ascii_escape(b: &mut test::Bencher) {
bench_unescape_c_str(
b,
r"\n",
&array::from_fn::<_, { LEN }, _>(|i| (2 * i..2 * (i + 1), Ok(MixedUnit::Char('\n')))),
&array::from_fn::<_, { LEN }, _>(|i| (2 * i..2 * (i + 1), '\n'.try_into())),
);
}

Expand All @@ -362,7 +367,7 @@ fn bench_unescape_c_str_hex_escape_ascii(b: &mut test::Bencher) {
bench_unescape_c_str(
b,
r"\x22",
&array::from_fn::<_, { LEN }, _>(|i| (4 * i..4 * (i + 1), Ok(MixedUnit::Char('"')))),
&array::from_fn::<_, { LEN }, _>(|i| (4 * i..4 * (i + 1), '"'.try_into())),
);
}

Expand All @@ -371,9 +376,7 @@ fn bench_unescape_c_str_hex_escape_byte(b: &mut test::Bencher) {
bench_unescape_c_str(
b,
r"\xff",
&array::from_fn::<_, { LEN }, _>(|i| {
(4 * i..4 * (i + 1), Ok(MixedUnit::HighByte(b'\xff')))
}),
&array::from_fn::<_, { LEN }, _>(|i| (4 * i..4 * (i + 1), b'\xff'.try_into())),
);
}

Expand All @@ -382,7 +385,7 @@ fn bench_unescape_c_str_unicode_escape(b: &mut test::Bencher) {
bench_unescape_c_str(
b,
r"\u{1f980}",
&array::from_fn::<_, { LEN }, _>(|i| (9 * i..9 * (i + 1), Ok(MixedUnit::Char('🦀')))),
&array::from_fn::<_, { LEN }, _>(|i| (9 * i..9 * (i + 1), '🦀'.try_into())),
);
}

Expand All @@ -399,14 +402,11 @@ fn bench_unescape_c_str_mixed_escape(b: &mut test::Bencher) {
let mut i = 0;
move || {
let res = Some(match i % n {
0 => (i / n * l..i / n * l + 2, Ok(MixedUnit::Char('\n'))),
1 => (i / n * l + 2..i / n * l + 6, Ok(MixedUnit::Char('"'))),
2 => (i / n * l + 6..i / n * l + 15, Ok(MixedUnit::Char('🦀'))),
3 => (i / n * l + 15..i / n * l + 24, Ok(MixedUnit::Char('🚀'))),
4 => (
i / n * l + 24..i / n * l + 28,
Ok(MixedUnit::HighByte(b'\xff')),
),
0 => (i / n * l..i / n * l + 2, '\n'.try_into()),
1 => (i / n * l + 2..i / n * l + 6, '"'.try_into()),
2 => (i / n * l + 6..i / n * l + 15, '🦀'.try_into()),
3 => (i / n * l + 15..i / n * l + 24, '🚀'.try_into()),
4 => (i / n * l + 24..i / n * l + 28, b'\xff'.try_into()),
r if r >= n => unreachable!(),
_ => unimplemented!(),
});
Expand Down
119 changes: 76 additions & 43 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
//! turning escape sequences into the values they represent.

use std::ffi::CStr;
use std::num::NonZero;
use std::ops::Range;
use std::str::Chars;

Expand Down Expand Up @@ -105,7 +106,10 @@ pub fn check_raw_byte_str(src: &str, callback: impl FnMut(Range<usize>, Result<u
/// and produces a sequence of characters or errors,
/// which are returned by invoking `callback`.
/// NOTE: Does no escaping, but produces errors for bare carriage return ('\r').
pub fn check_raw_c_str(src: &str, callback: impl FnMut(Range<usize>, Result<char, EscapeError>)) {
pub fn check_raw_c_str(
src: &str,
callback: impl FnMut(Range<usize>, Result<NonZero<char>, EscapeError>),
) {
CStr::check_raw(src, callback);
}

Expand Down Expand Up @@ -181,15 +185,11 @@ fn char2byte(c: char) -> Result<u8, EscapeError> {
}

impl CheckRaw for CStr {
type RawUnit = char;
type RawUnit = NonZero<char>;

#[inline]
fn char2raw_unit(c: char) -> Result<Self::RawUnit, EscapeError> {
if c == '\0' {
Err(EscapeError::NulInCStr)
} else {
Ok(c)
}
NonZero::new(c).ok_or(EscapeError::NulInCStr)
}
}

Expand Down Expand Up @@ -253,42 +253,67 @@ pub enum MixedUnit {
/// For example, if '¥' appears in a string it is represented here as
/// `MixedUnit::Char('¥')`, and it will be appended to the relevant byte
/// string as the two-byte UTF-8 sequence `[0xc2, 0xa5]`
Char(char),
Char(NonZero<char>),

/// Used for high bytes (`\x80`..`\xff`).
///
/// For example, if `\xa5` appears in a string it is represented here as
/// `MixedUnit::HighByte(0xa5)`, and it will be appended to the relevant
/// byte string as the single byte `0xa5`.
HighByte(u8),
HighByte(NonZero<u8>),
}

impl From<char> for MixedUnit {
impl From<NonZero<char>> for MixedUnit {
#[inline]
fn from(c: char) -> Self {
fn from(c: NonZero<char>) -> Self {
MixedUnit::Char(c)
}
}

impl From<u8> for MixedUnit {
impl From<NonZero<u8>> for MixedUnit {
#[inline]
fn from(n: u8) -> Self {
if n.is_ascii() {
MixedUnit::Char(n as char)
fn from(byte: NonZero<u8>) -> Self {
if byte.get().is_ascii() {
MixedUnit::Char(NonZero::new(byte.get() as char).unwrap())
} else {
MixedUnit::HighByte(n)
MixedUnit::HighByte(byte)
}
}
}

impl TryFrom<char> for MixedUnit {
type Error = EscapeError;

#[inline]
fn try_from(c: char) -> Result<Self, EscapeError> {
NonZero::new(c)
.map(MixedUnit::Char)
.ok_or(EscapeError::NulInCStr)
}
}

impl TryFrom<u8> for MixedUnit {
type Error = EscapeError;

#[inline]
fn try_from(byte: u8) -> Result<Self, EscapeError> {
NonZero::new(byte)
.map(From::from)
.ok_or(EscapeError::NulInCStr)
}
}

/// Trait for unescaping escape sequences in strings
trait Unescape {
/// Unit type of the implementing string type (`char` for string, `u8` for byte string)
type Unit: From<u8>;
type Unit;

/// Result of unescaping the zero char ('\0')
const ZERO_RESULT: Result<Self::Unit, EscapeError>;

/// Converts non-zero bytes to the unit type
fn nonzero_byte2unit(b: NonZero<u8>) -> Self::Unit;

/// Converts chars to the unit type
fn char2unit(c: char) -> Result<Self::Unit, EscapeError>;

Expand Down Expand Up @@ -319,18 +344,20 @@ trait Unescape {
if c == '0' {
Self::ZERO_RESULT
} else {
simple_escape(c).map(|b| b.into()).or_else(|c| match c {
'x' => Self::hex2unit(hex_escape(chars)?),
'u' => Self::unicode2unit({
let value = unicode_escape(chars)?;
if value > char::MAX as u32 {
Err(EscapeError::OutOfRangeUnicodeEscape)
} else {
char::from_u32(value).ok_or(EscapeError::LoneSurrogateUnicodeEscape)
}
}),
_ => Err(EscapeError::InvalidEscape),
})
simple_escape(c)
.map(|b| Self::nonzero_byte2unit(b))
.or_else(|c| match c {
'x' => Self::hex2unit(hex_escape(chars)?),
'u' => Self::unicode2unit({
let value = unicode_escape(chars)?;
if value > char::MAX as u32 {
Err(EscapeError::OutOfRangeUnicodeEscape)
} else {
char::from_u32(value).ok_or(EscapeError::LoneSurrogateUnicodeEscape)
}
}),
_ => Err(EscapeError::InvalidEscape),
})
}
}

Expand Down Expand Up @@ -373,9 +400,9 @@ trait Unescape {
///
/// Parses the character of an ASCII escape (except nul) without the leading backslash.
#[inline] // single use in Unescape::unescape_1
fn simple_escape(c: char) -> Result<u8, char> {
fn simple_escape(c: char) -> Result<NonZero<u8>, char> {
// Previous character was '\\', unescape what follows.
Ok(match c {
Ok(NonZero::new(match c {
'"' => b'"',
'n' => b'\n',
'r' => b'\r',
Expand All @@ -384,6 +411,7 @@ fn simple_escape(c: char) -> Result<u8, char> {
'\'' => b'\'',
_ => Err(c)?,
})
.unwrap())
}

/// Interpret a hexadecimal escape
Expand Down Expand Up @@ -489,6 +517,11 @@ impl Unescape for str {

const ZERO_RESULT: Result<Self::Unit, EscapeError> = Ok('\0');

#[inline]
fn nonzero_byte2unit(b: NonZero<u8>) -> Self::Unit {
b.get().into()
}

#[inline]
fn char2unit(c: char) -> Result<Self::Unit, EscapeError> {
Ok(c)
Expand All @@ -514,6 +547,11 @@ impl Unescape for [u8] {

const ZERO_RESULT: Result<Self::Unit, EscapeError> = Ok(b'\0');

#[inline]
fn nonzero_byte2unit(b: NonZero<u8>) -> Self::Unit {
b.get()
}

#[inline]
fn char2unit(c: char) -> Result<Self::Unit, EscapeError> {
char2byte(c)
Expand All @@ -535,24 +573,19 @@ impl Unescape for CStr {

const ZERO_RESULT: Result<Self::Unit, EscapeError> = Err(EscapeError::NulInCStr);

#[inline]
fn nonzero_byte2unit(b: NonZero<u8>) -> Self::Unit {
b.into()
}

#[inline]
fn char2unit(c: char) -> Result<Self::Unit, EscapeError> {
if c == '\0' {
Err(EscapeError::NulInCStr)
} else {
Ok(MixedUnit::Char(c))
}
c.try_into()
}

#[inline]
fn hex2unit(byte: u8) -> Result<Self::Unit, EscapeError> {
if byte == b'\0' {
Err(EscapeError::NulInCStr)
} else if byte.is_ascii() {
Ok(MixedUnit::Char(byte as char))
} else {
Ok(MixedUnit::HighByte(byte))
}
byte.try_into()
}

#[inline]
Expand Down