mirror of
https://github.com/servo/servo.git
synced 2025-08-03 04:30:10 +01:00
Use local slice_chars
StrExt::slice_chars is deprecated and will be removed in Rust. This lifts the implementation from Rust libstd and puts it in util::str. This fixes a bunch of deprecation warnings in Servo.
This commit is contained in:
parent
ca9f9226b0
commit
dae1a398a4
7 changed files with 41 additions and 17 deletions
|
@ -335,3 +335,27 @@ pub fn str_join<T: AsRef<str>>(strs: &[T], join: &str) -> String {
|
|||
acc
|
||||
})
|
||||
}
|
||||
|
||||
// Lifted from Rust's StrExt implementation, which is being removed.
|
||||
pub fn slice_chars(s: &str, begin: usize, end: usize) -> &str {
|
||||
assert!(begin <= end);
|
||||
let mut count = 0;
|
||||
let mut begin_byte = None;
|
||||
let mut end_byte = None;
|
||||
|
||||
// This could be even more efficient by not decoding,
|
||||
// only finding the char boundaries
|
||||
for (idx, _) in s.char_indices() {
|
||||
if count == begin { begin_byte = Some(idx); }
|
||||
if count == end { end_byte = Some(idx); break; }
|
||||
count += 1;
|
||||
}
|
||||
if begin_byte.is_none() && count == begin { begin_byte = Some(s.len()) }
|
||||
if end_byte.is_none() && count == end { end_byte = Some(s.len()) }
|
||||
|
||||
match (begin_byte, end_byte) {
|
||||
(None, _) => panic!("slice_chars: `begin` is beyond end of string"),
|
||||
(_, None) => panic!("slice_chars: `end` is beyond end of string"),
|
||||
(Some(a), Some(b)) => unsafe { s.slice_unchecked(a, b) }
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue