Skip to content

Commit 13a46ea

Browse files
hkBstlnicola
authored andcommitted
update to literal-escaper 0.0.4 for better API without unreachable and faster string parsing
1 parent e2c3647 commit 13a46ea

File tree

7 files changed

+137
-165
lines changed

7 files changed

+137
-165
lines changed

Cargo.lock

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ serde = { version = "1.0.219" }
143143
serde_derive = { version = "1.0.219" }
144144
serde_json = "1.0.140"
145145
rustc-hash = "2.1.1"
146-
rustc-literal-escaper = "0.0.3"
146+
rustc-literal-escaper = "0.0.4"
147147
smallvec = { version = "1.15.1", features = [
148148
"const_new",
149149
"union",

crates/hir-expand/src/attrs.rs

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -433,20 +433,19 @@ fn unescape(s: &str) -> Option<Cow<'_, str>> {
433433
let mut buf = String::new();
434434
let mut prev_end = 0;
435435
let mut has_error = false;
436-
unescape::unescape_unicode(s, unescape::Mode::Str, &mut |char_range, unescaped_char| match (
437-
unescaped_char,
438-
buf.capacity() == 0,
439-
) {
440-
(Ok(c), false) => buf.push(c),
441-
(Ok(_), true) if char_range.len() == 1 && char_range.start == prev_end => {
442-
prev_end = char_range.end
443-
}
444-
(Ok(c), true) => {
445-
buf.reserve_exact(s.len());
446-
buf.push_str(&s[..prev_end]);
447-
buf.push(c);
436+
unescape::unescape_str(s, |char_range, unescaped_char| {
437+
match (unescaped_char, buf.capacity() == 0) {
438+
(Ok(c), false) => buf.push(c),
439+
(Ok(_), true) if char_range.len() == 1 && char_range.start == prev_end => {
440+
prev_end = char_range.end
441+
}
442+
(Ok(c), true) => {
443+
buf.reserve_exact(s.len());
444+
buf.push_str(&s[..prev_end]);
445+
buf.push(c);
446+
}
447+
(Err(_), _) => has_error = true,
448448
}
449-
(Err(_), _) => has_error = true,
450449
});
451450

452451
match (has_error, buf.capacity() == 0) {

crates/hir-expand/src/builtin/fn_macro.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use span::{Edition, FileId, Span};
1212
use stdx::format_to;
1313
use syntax::{
1414
format_smolstr,
15-
unescape::{Mode, unescape_byte, unescape_char, unescape_unicode},
15+
unescape::{unescape_byte, unescape_char, unescape_str},
1616
};
1717
use syntax_bridge::syntax_node_to_token_tree;
1818

@@ -430,7 +430,7 @@ fn compile_error_expand(
430430
kind: tt::LitKind::Str | tt::LitKind::StrRaw(_),
431431
suffix: _,
432432
})),
433-
] => ExpandError::other(span, Box::from(unescape_str(text).as_str())),
433+
] => ExpandError::other(span, Box::from(unescape_symbol(text).as_str())),
434434
_ => ExpandError::other(span, "`compile_error!` argument must be a string"),
435435
};
436436

@@ -481,7 +481,7 @@ fn concat_expand(
481481
format_to!(text, "{}", it.symbol.as_str())
482482
}
483483
tt::LitKind::Str => {
484-
text.push_str(unescape_str(&it.symbol).as_str());
484+
text.push_str(unescape_symbol(&it.symbol).as_str());
485485
record_span(it.span);
486486
}
487487
tt::LitKind::StrRaw(_) => {
@@ -691,7 +691,7 @@ fn parse_string(tt: &tt::TopSubtree) -> Result<(Symbol, Span), ExpandError> {
691691
span,
692692
kind: tt::LitKind::Str,
693693
suffix: _,
694-
})) => Ok((unescape_str(text), *span)),
694+
})) => Ok((unescape_symbol(text), *span)),
695695
TtElement::Leaf(tt::Leaf::Literal(tt::Literal {
696696
symbol: text,
697697
span,
@@ -712,7 +712,7 @@ fn parse_string(tt: &tt::TopSubtree) -> Result<(Symbol, Span), ExpandError> {
712712
span,
713713
kind: tt::LitKind::Str,
714714
suffix: _,
715-
})) => Some((unescape_str(text), *span)),
715+
})) => Some((unescape_symbol(text), *span)),
716716
TtElement::Leaf(tt::Leaf::Literal(tt::Literal {
717717
symbol: text,
718718
span,
@@ -897,11 +897,11 @@ fn quote_expand(
897897
)
898898
}
899899

900-
fn unescape_str(s: &Symbol) -> Symbol {
900+
fn unescape_symbol(s: &Symbol) -> Symbol {
901901
if s.as_str().contains('\\') {
902902
let s = s.as_str();
903903
let mut buf = String::with_capacity(s.len());
904-
unescape_unicode(s, Mode::Str, &mut |_, c| {
904+
unescape_str(s, |_, c| {
905905
if let Ok(c) = c {
906906
buf.push(c)
907907
}

0 commit comments

Comments
 (0)