diff options
Diffstat (limited to 'src/util/slice.rs')
-rw-r--r-- | src/util/slice.rs | 44 |
1 files changed, 18 insertions, 26 deletions
diff --git a/src/util/slice.rs b/src/util/slice.rs index 14fd527..cd3641e 100644 --- a/src/util/slice.rs +++ b/src/util/slice.rs @@ -48,17 +48,21 @@ impl<'a> Position<'a> { /// Includes information on virtual spaces before and after the chars. #[derive(Debug)] pub struct Slice<'a> { - pub chars: &'a [char], + pub bytes: &'a [u8], pub before: usize, pub after: usize, } impl<'a> Slice<'a> { /// Get the slice belonging to a position. - pub fn from_point(list: &'a [char], point: &Point) -> Slice<'a> { + pub fn from_point(bytes: &'a [u8], point: &Point) -> Slice<'a> { let mut before = point.vs; let mut start = point.index; - let end = if start < list.len() { start + 1 } else { start }; + let end = if start < bytes.len() { + start + 1 + } else { + start + }; // If we have virtual spaces before, it means we are past the actual // character at that index, and those virtual spaces. @@ -68,14 +72,14 @@ impl<'a> Slice<'a> { }; Slice { - chars: if start < end { &list[start..end] } else { &[] }, + bytes: if start < end { &bytes[start..end] } else { &[] }, before, after: 0, } } /// Get the slice belonging to a position. - pub fn from_position(list: &'a [char], position: &Position) -> Slice<'a> { + pub fn from_position(bytes: &'a [u8], position: &Position) -> Slice<'a> { let mut before = position.start.vs; let mut after = position.end.vs; let mut start = position.start.index; @@ -96,15 +100,16 @@ impl<'a> Slice<'a> { } Slice { - chars: &list[start..end], + bytes: &bytes[start..end], before, after, } } /// To do. + // To do: rename to `len`? pub fn size(&self) -> usize { - self.chars.len() + self.before + self.after + self.bytes.len() + self.before + self.after } // To do: @@ -112,27 +117,13 @@ impl<'a> Slice<'a> { // to implement an `as_str`. /// To do. - pub fn head(&self) -> Option<char> { + pub fn head(&self) -> Option<u8> { if self.before > 0 { - Some(' ') - } else if self.chars.is_empty() { + Some(b' ') + } else if self.bytes.is_empty() { None } else { - Some(self.chars[0]) - } - } - - /// To do. - pub fn tail(&self) -> Option<char> { - if self.after > 0 { - Some(' ') - } else { - let index = self.chars.len(); - if index > 0 { - Some(self.chars[index - 1]) - } else { - None - } + Some(self.bytes[0]) } } @@ -144,7 +135,8 @@ impl<'a> Slice<'a> { string.push(' '); index -= 1; } - string.push_str(&self.chars.iter().collect::<String>()); + // To do: invalid UTF8? + string.push_str(std::str::from_utf8(self.bytes).unwrap()); index = self.after; while index > 0 { string.push(' '); |