diff options
author | Matt Brubeck <mbrubeck@limpet.net> | 2016-04-27 11:22:02 -0700 |
---|---|---|
committer | Matt Brubeck <mbrubeck@limpet.net> | 2016-04-28 14:32:14 -0700 |
commit | 659305fe0a8f94e950ca64fab5ccef9949abd295 (patch) | |
tree | ac836803ac3fba799a4b4f73c24b4d0d2d208d6f /components/util/str.rs | |
parent | dba878dfb278619bf2d808c0c21758a937ec6bb7 (diff) | |
download | servo-659305fe0a8f94e950ca64fab5ccef9949abd295.tar.gz servo-659305fe0a8f94e950ca64fab5ccef9949abd295.zip |
Use byte indices instead of char indices for text runs
Replace character indices with UTF-8 byte offsets throughout the code dealing
with text shaping and breaking. This eliminates a lot of complexity when
converting from one to the other, and interoperates better with the rest of
the Rust ecosystem.
Diffstat (limited to 'components/util/str.rs')
-rw-r--r-- | components/util/str.rs | 39 |
1 files changed, 1 insertions, 38 deletions
diff --git a/components/util/str.rs b/components/util/str.rs index 8d5a5e74bef..997aee8f53c 100644 --- a/components/util/str.rs +++ b/components/util/str.rs @@ -11,7 +11,7 @@ use std::ffi::CStr; use std::fmt; use std::iter::{Filter, Peekable}; use std::ops::{Deref, DerefMut}; -use std::str::{Bytes, CharIndices, Split, from_utf8}; +use std::str::{Bytes, Split, from_utf8}; use string_cache::Atom; #[derive(Clone, Debug, Deserialize, Eq, Hash, HeapSizeOf, Ord, PartialEq, PartialOrd, Serialize)] @@ -271,40 +271,3 @@ pub fn str_join<I, T>(strs: I, 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) } - } -} - -// searches a character index in CharIndices -// returns indices.count if not found -pub fn search_index(index: usize, indices: CharIndices) -> isize { - let mut character_count = 0; - for (character_index, _) in indices { - if character_index == index { - return character_count; - } - character_count += 1 - } - character_count -} |