Check character boundaries before comparing string slices
This commit is contained in:
parent
7e05d35ba6
commit
1e5525c9cf
|
@ -490,28 +490,30 @@ pub(crate) fn decode(input: &str) -> Cow<'_, str> {
|
||||||
|
|
||||||
let mut last_end = 0;
|
let mut last_end = 0;
|
||||||
while input_len - last_end >= 4 {
|
while input_len - last_end >= 4 {
|
||||||
match &input[last_end..(last_end + 4)] {
|
if input.is_char_boundary(last_end + 4) {
|
||||||
"<" => {
|
match &input[last_end..(last_end + 4)] {
|
||||||
result.push('<');
|
"<" => {
|
||||||
last_end += 4;
|
result.push('<');
|
||||||
continue;
|
last_end += 4;
|
||||||
}
|
continue;
|
||||||
">" => {
|
}
|
||||||
result.push('>');
|
">" => {
|
||||||
last_end += 4;
|
result.push('>');
|
||||||
continue;
|
last_end += 4;
|
||||||
}
|
continue;
|
||||||
_ => (),
|
}
|
||||||
};
|
_ => (),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
if input_len - last_end >= 5 {
|
if input_len - last_end >= 5 && input.is_char_boundary(last_end + 5) {
|
||||||
if &input[last_end..(last_end + 5)] == "&" {
|
if &input[last_end..(last_end + 5)] == "&" {
|
||||||
result.push('&');
|
result.push('&');
|
||||||
last_end += 5;
|
last_end += 5;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
if input_len - last_end >= 6 {
|
if input_len - last_end >= 6 && input.is_char_boundary(last_end + 6) {
|
||||||
match &input[last_end..(last_end + 6)] {
|
match &input[last_end..(last_end + 6)] {
|
||||||
"'" => {
|
"'" => {
|
||||||
result.push('\'');
|
result.push('\'');
|
||||||
|
|
|
@ -14,9 +14,9 @@ struct Foo {
|
||||||
fn direct() {
|
fn direct() {
|
||||||
let v = Foo {
|
let v = Foo {
|
||||||
flag: true,
|
flag: true,
|
||||||
inner: "hello".to_string(),
|
inner: "cbdté".to_string(),
|
||||||
};
|
};
|
||||||
let xml = "<Foo flag=\"true\">hello</Foo>";
|
let xml = "<Foo flag=\"true\">cbdté</Foo>";
|
||||||
|
|
||||||
assert_eq!(to_string(&v).unwrap(), xml);
|
assert_eq!(to_string(&v).unwrap(), xml);
|
||||||
assert_eq!(from_str::<Foo>(xml), Ok(v));
|
assert_eq!(from_str::<Foo>(xml), Ok(v));
|
||||||
|
|
Loading…
Reference in New Issue