fix: Panic in unicode boundary checking code #578

Merged
wetneb merged 2 commits from wetneb/string_boundary_panic into main 2025-08-31 09:05:34 +02:00 AGit

View file

@ -34,9 +34,25 @@ impl StrExt for &'_ str {
if index > len {
len
} else {
(index..len)
(index..=len)

nit: index..=len is imo a bit more readable

nit: `index..=len` is imo a bit more readable
.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::*;

nit: replacing this with use super::* is probably more future-proof..

nit: replacing this with `use super::*` is probably more future-proof..
#[allow(unstable_name_collisions)]
#[test]
fn char_boundary_at_end() {

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());
}
}