fix: Panic in unicode boundary checking code #578
1 changed files with 17 additions and 1 deletions
|
@ -34,9 +34,25 @@ impl StrExt for &'_ str {
|
|||
if index > len {
|
||||
len
|
||||
} else {
|
||||
(index..len)
|
||||
(index..=len)
|
||||
|
||||
.find(|&i| self.is_char_boundary(i))
|
||||
.expect("`i = len` must be a char boundary") // otherwise `self` wouldn't have been a valid `&str` to begin with
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use super::*;
|
||||
ada4a
commented
nit: replacing this with nit: replacing this with `use super::*` is probably more future-proof..
|
||||
|
||||
#[allow(unstable_name_collisions)]
|
||||
#[test]
|
||||
fn char_boundary_at_end() {
|
||||
ada4a
commented
thank you for actually adding a test for this:) thank you for actually adding a test for this:)
|
||||
let string = "❤️🧡💛";
|
||||
assert_eq!(string.ceil_char_boundary(0), 0);
|
||||
assert_eq!(string.ceil_char_boundary(1), 3);
|
||||
assert_eq!(string.ceil_char_boundary(3), 3);
|
||||
assert_eq!(string.ceil_char_boundary(4), 6);
|
||||
assert_eq!(string.ceil_char_boundary(13), string.len());
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue
nit:
index..=len
is imo a bit more readable