aboutsummaryrefslogtreecommitdiffstats
path: root/components/util/str.rs
diff options
context:
space:
mode:
authorJack Moffitt <jack@metajack.im>2015-07-31 12:23:13 -0600
committerJack Moffitt <jack@metajack.im>2015-07-31 12:23:13 -0600
commitdae1a398a43b92aa7390379417e00c7fddb14762 (patch)
tree5438167f21f59eef23bf0e833bc542c44c96f6e2 /components/util/str.rs
parentca9f9226b092f6f9349aecd53bf0b36fb5b6948e (diff)
downloadservo-dae1a398a43b92aa7390379417e00c7fddb14762.tar.gz
servo-dae1a398a43b92aa7390379417e00c7fddb14762.zip
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.
Diffstat (limited to 'components/util/str.rs')
-rw-r--r--components/util/str.rs24
1 files changed, 24 insertions, 0 deletions
diff --git a/components/util/str.rs b/components/util/str.rs
index fe6127e1106..af688a1103e 100644
--- a/components/util/str.rs
+++ b/components/util/str.rs
@@ -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) }
+ }
+}