Fix panic when parsing a format spec with multi-byte character

This commit is contained in:
messense
2023-01-13 14:40:20 +08:00
parent 91a897dd74
commit ee5a1c9002

View File

@@ -869,7 +869,7 @@ impl FormatString {
let mut left = String::new();
// There may be one layer nesting brackets in spec
for (idx, c) in text.chars().enumerate() {
for (idx, c) in text.char_indices() {
if idx == 0 {
if c != '{' {
return Err(FormatParseError::MissingStartBracket);
@@ -1076,6 +1076,11 @@ mod tests {
assert_eq!(FormatString::from_str("abcd{1}:{key}"), expected);
}
#[test]
fn test_format_parse_multi_byte_char() {
assert!(FormatString::from_str("{a:%ЫйЯЧ}").is_ok());
}
#[test]
fn test_format_parse_fail() {
assert_eq!(