Skip to content

Remove unreachable #8

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 5 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
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
# 0.0.3

- Add `check_raw_str`, `check_raw_byte_str`, `check_raw_c_str`,
- Add `unescape_str`, `unescape_byte_str`, `unescape_c_str`,
- Add `unescape_for_errors`,
- Remove: `unescape_unicode` and `unescape_mixed`

# 0.0.2

- Add new `rustc-dep-of-std` feature to allow building `libproc-macro`
Expand Down
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "rustc-literal-escaper"
version = "0.0.2"
version = "0.0.3"
edition = "2021"
description = "Provides code to unescape string literals"
license = "Apache-2.0 OR MIT"
Expand Down
61 changes: 36 additions & 25 deletions benches/benches.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
extern crate test;

use rustc_literal_escaper::*;
use std::fmt::Debug;
use std::iter::repeat_n;
use std::ops::Range;

const LEN: usize = 10_000;

Expand All @@ -23,9 +25,7 @@ fn bench_skip_ascii_whitespace(b: &mut test::Bencher) {
// skip_ascii_whitespace(&mut input.chars(), 0, &mut |range, res| {
// output.push((range, res))
// });
unescape_unicode(&input, Mode::Str, &mut |range, res| {
output.push((range, res))
});
unescape_str(&input, |range, res| output.push((range, res)));
assert_eq!(
output,
[((0..LEN + 2), Err(EscapeError::MultipleSkippedLinesWarning))]
Expand All @@ -37,58 +37,71 @@ fn bench_skip_ascii_whitespace(b: &mut test::Bencher) {
// Check raw
//

fn bench_check_raw(b: &mut test::Bencher, c: char, mode: Mode) {
let input: String = test::black_box(repeat_n(c, LEN).collect());
assert_eq!(input.len(), LEN * c.len_utf8());
#[allow(clippy::type_complexity)]
fn bench_check_raw<UNIT: Into<char> + PartialEq + Debug + Copy>(
b: &mut test::Bencher,
c: UNIT,
check_raw: fn(&str, &mut dyn FnMut(Range<usize>, Result<UNIT, EscapeError>)),
) {
let input: String = test::black_box(repeat_n(c.into(), LEN).collect());
assert_eq!(input.len(), LEN * c.into().len_utf8());

b.iter(|| {
let mut output = vec![];
unescape_unicode(&input, mode, &mut |range, res| output.push((range, res)));

check_raw(&input, &mut |range, res| output.push((range, res)));
assert_eq!(output.len(), LEN);
assert_eq!(output[0], ((0..c.len_utf8()), Ok(c)));
assert_eq!(output[0], (0..c.into().len_utf8(), Ok(c)));
});
}

// raw str

#[bench]
fn bench_check_raw_str_ascii(b: &mut test::Bencher) {
bench_check_raw(b, 'a', Mode::RawStr);
bench_check_raw(b, 'a', |s, cb| check_raw_str(s, cb));
}

#[bench]
fn bench_check_raw_str_unicode(b: &mut test::Bencher) {
bench_check_raw(b, '🦀', Mode::RawStr);
bench_check_raw(b, '🦀', |s, cb| check_raw_str(s, cb));
}

// raw byte str

#[bench]
fn bench_check_raw_byte_str(b: &mut test::Bencher) {
bench_check_raw(b, 'a', Mode::RawByteStr);
bench_check_raw(b, b'a', |s, cb| check_raw_byte_str(s, cb));
}

// raw C str

#[bench]
fn bench_check_raw_c_str_ascii(b: &mut test::Bencher) {
bench_check_raw(b, 'a', Mode::RawCStr);
bench_check_raw(b, 'a', |s, cb| check_raw_c_str(s, cb));
}

#[bench]
fn bench_check_raw_c_str_unicode(b: &mut test::Bencher) {
bench_check_raw(b, '🦀', Mode::RawCStr);
bench_check_raw(b, '🦀', |s, cb| check_raw_c_str(s, cb));
}

//
// Unescape
//

fn bench_unescape(b: &mut test::Bencher, s: &str, mode: Mode, expected: char) {
#[allow(clippy::type_complexity)]
fn bench_unescape<UNIT: Into<char> + PartialEq + Debug + Copy>(
b: &mut test::Bencher,
s: &str,
expected: UNIT,
unescape: fn(&str, &mut dyn FnMut(Range<usize>, Result<UNIT, EscapeError>)),
) {
let input: String = test::black_box(repeat_n(s, LEN).collect());
assert_eq!(input.len(), LEN * s.len());
b.iter(|| {
let mut output = vec![];
unescape_unicode(&input, mode, &mut |range, res| output.push((range, res)));
unescape(&input, &mut |range, res| output.push((range, res)));
assert_eq!(output.len(), LEN);
assert_eq!(output[0], ((0..s.len()), Ok(expected)));
});
Expand All @@ -98,39 +111,39 @@ fn bench_unescape(b: &mut test::Bencher, s: &str, mode: Mode, expected: char) {

#[bench]
fn bench_unescape_str_trivial(b: &mut test::Bencher) {
bench_unescape(b, r"a", Mode::Str, 'a');
bench_unescape(b, r"a", 'a', |s, cb| unescape_str(s, cb));
}

#[bench]
fn bench_unescape_str_ascii(b: &mut test::Bencher) {
bench_unescape(b, r"\n", Mode::Str, '\n');
bench_unescape(b, r"\n", '\n', |s, cb| unescape_str(s, cb));
}

#[bench]
fn bench_unescape_str_hex(b: &mut test::Bencher) {
bench_unescape(b, r"\x22", Mode::Str, '"');
bench_unescape(b, r"\x22", '"', |s, cb| unescape_str(s, cb));
}

#[bench]
fn bench_unescape_str_unicode(b: &mut test::Bencher) {
bench_unescape(b, r"\u{1f980}", Mode::Str, '🦀');
bench_unescape(b, r"\u{1f980}", '🦀', |s, cb| unescape_str(s, cb));
}

// byte str

#[bench]
fn bench_unescape_byte_str_trivial(b: &mut test::Bencher) {
bench_unescape(b, r"a", Mode::ByteStr, 'a');
bench_unescape(b, r"a", b'a', |s, cb| unescape_byte_str(s, cb));
}

#[bench]
fn bench_unescape_byte_str_ascii(b: &mut test::Bencher) {
bench_unescape(b, r"\n", Mode::ByteStr, b'\n' as char);
bench_unescape(b, r"\n", b'\n', |s, cb| unescape_byte_str(s, cb));
}

#[bench]
fn bench_unescape_byte_str_hex(b: &mut test::Bencher) {
bench_unescape(b, r"\xff", Mode::ByteStr, b'\xff' as char);
bench_unescape(b, r"\xff", b'\xff', |s, cb| unescape_byte_str(s, cb));
}

// C str
Expand All @@ -140,9 +153,7 @@ fn bench_unescape_c_str(b: &mut test::Bencher, s: &str, expected: MixedUnit) {
assert_eq!(input.len(), LEN * s.len());
b.iter(|| {
let mut output = vec![];
unescape_mixed(&input, Mode::CStr, &mut |range, res| {
output.push((range, res))
});
unescape_c_str(&input, &mut |range, res| output.push((range, res)));
assert_eq!(output.len(), LEN);
assert_eq!(output[0], ((0..s.len()), Ok(expected)));
});
Expand Down
Loading