Skip to content

Commit

Permalink
Use strip_suffix instead of slicing bytes
Browse files Browse the repository at this point in the history
String::len won't necessarily produce correct results on arbitrary UTF8 strings
  • Loading branch information
hansott committed Jan 12, 2025
1 parent e9cb408 commit dfcd131
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions src/tokenizer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1563,13 +1563,19 @@ impl<'a> Tokenizer<'a> {
temp.push(ch);

if temp.ends_with(&end_delimiter) {
s.push_str(&temp[..temp.len() - end_delimiter.len()]);
if let Some(temp) = temp.strip_suffix(&end_delimiter) {
s.push_str(temp);
break;
}
break;
}
}
None => {
if temp.ends_with(&end_delimiter) {
s.push_str(&temp[..temp.len() - end_delimiter.len()]);
if let Some(temp) = temp.strip_suffix(&end_delimiter) {
s.push_str(temp);
break;
}
break;
}

Expand Down Expand Up @@ -2579,6 +2585,15 @@ mod tests {
Token::Number("1".into(), false),
]
),
(
String::from("$function$abc$q$data$q$$function$"),
vec![
Token::DollarQuotedString(DollarQuotedString {
value: "abc$q$data$q$".into(),
tag: Some("function".into()),
}),
]
),
];

let dialect = GenericDialect {};
Expand Down

0 comments on commit dfcd131

Please sign in to comment.