Skip to content

Commit d41975c

Browse files
TrueBrainfrosch123
authored andcommitted
fix: trailing UTF-8 character breaks remove_trailing_blanks
1 parent e19c942 commit d41975c

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

src/validate.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -190,9 +190,7 @@ fn remove_ascii_ctrl(t: &mut String) {
190190
}
191191

192192
fn remove_trailing_blanks(t: &mut String) {
193-
if let Some(last) = t.rfind(|c| c != ' ') {
194-
t.truncate(last + 1);
195-
}
193+
t.truncate(t.trim_end().len());
196194
}
197195

198196
/// Replace all ASCII control codes with blank.
@@ -775,18 +773,23 @@ mod tests {
775773
let mut s1 = String::from("");
776774
let mut s2 = String::from(" a b c ");
777775
let mut s3 = String::from("\0a\tb\rc\r\n");
776+
let mut s4 = String::from("abc\u{b3}");
778777
remove_ascii_ctrl(&mut s1);
779778
remove_ascii_ctrl(&mut s2);
780779
remove_ascii_ctrl(&mut s3);
780+
remove_ascii_ctrl(&mut s4);
781781
assert_eq!(s1, String::from(""));
782782
assert_eq!(s2, String::from(" a b c "));
783783
assert_eq!(s3, String::from(" a b c "));
784+
assert_eq!(s4, String::from("abc\u{b3}"));
784785
remove_trailing_blanks(&mut s1);
785786
remove_trailing_blanks(&mut s2);
786787
remove_trailing_blanks(&mut s3);
788+
remove_trailing_blanks(&mut s4);
787789
assert_eq!(s1, String::from(""));
788790
assert_eq!(s2, String::from(" a b c"));
789791
assert_eq!(s3, String::from(" a b c"));
792+
assert_eq!(s4, String::from("abc\u{b3}"));
790793
}
791794

792795
#[test]

0 commit comments

Comments
 (0)