Skip to content

Commit 2d4675b

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 2d4675b

File tree

5 files changed

+193
-174
lines changed

5 files changed

+193
-174
lines changed

crates/hir-expand/src/attrs.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -433,7 +433,7 @@ 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 (
436+
unescape::unescape_str(s, |char_range, unescaped_char| match (
437437
unescaped_char,
438438
buf.capacity() == 0,
439439
) {

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

Lines changed: 12 additions & 15 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

@@ -422,15 +422,12 @@ fn compile_error_expand(
422422
span: Span,
423423
) -> ExpandResult<tt::TopSubtree> {
424424
let err = match &*tt.0 {
425-
[
426-
_,
427-
tt::TokenTree::Leaf(tt::Leaf::Literal(tt::Literal {
428-
symbol: text,
429-
span: _,
430-
kind: tt::LitKind::Str | tt::LitKind::StrRaw(_),
431-
suffix: _,
432-
})),
433-
] => ExpandError::other(span, Box::from(unescape_str(text).as_str())),
425+
[_, tt::TokenTree::Leaf(tt::Leaf::Literal(tt::Literal {
426+
symbol: text,
427+
span: _,
428+
kind: tt::LitKind::Str | tt::LitKind::StrRaw(_),
429+
suffix: _,
430+
}))] => ExpandError::other(span, Box::from(unescape_symbol(text).as_str())),
434431
_ => ExpandError::other(span, "`compile_error!` argument must be a string"),
435432
};
436433

@@ -481,7 +478,7 @@ fn concat_expand(
481478
format_to!(text, "{}", it.symbol.as_str())
482479
}
483480
tt::LitKind::Str => {
484-
text.push_str(unescape_str(&it.symbol).as_str());
481+
text.push_str(unescape_symbol(&it.symbol).as_str());
485482
record_span(it.span);
486483
}
487484
tt::LitKind::StrRaw(_) => {
@@ -691,7 +688,7 @@ fn parse_string(tt: &tt::TopSubtree) -> Result<(Symbol, Span), ExpandError> {
691688
span,
692689
kind: tt::LitKind::Str,
693690
suffix: _,
694-
})) => Ok((unescape_str(text), *span)),
691+
})) => Ok((unescape_symbol(text), *span)),
695692
TtElement::Leaf(tt::Leaf::Literal(tt::Literal {
696693
symbol: text,
697694
span,
@@ -712,7 +709,7 @@ fn parse_string(tt: &tt::TopSubtree) -> Result<(Symbol, Span), ExpandError> {
712709
span,
713710
kind: tt::LitKind::Str,
714711
suffix: _,
715-
})) => Some((unescape_str(text), *span)),
712+
})) => Some((unescape_symbol(text), *span)),
716713
TtElement::Leaf(tt::Leaf::Literal(tt::Literal {
717714
symbol: text,
718715
span,
@@ -897,11 +894,11 @@ fn quote_expand(
897894
)
898895
}
899896

900-
fn unescape_str(s: &Symbol) -> Symbol {
897+
fn unescape_symbol(s: &Symbol) -> Symbol {
901898
if s.as_str().contains('\\') {
902899
let s = s.as_str();
903900
let mut buf = String::with_capacity(s.len());
904-
unescape_unicode(s, Mode::Str, &mut |_, c| {
901+
unescape_str(s, |_, c| {
905902
if let Ok(c) = c {
906903
buf.push(c)
907904
}

0 commit comments

Comments
 (0)