4 #[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
5 pub struct TextUnit(u32);
8 pub fn len_of_char(c: char) -> TextUnit {
9 TextUnit(c.len_utf8() as u32)
12 pub fn new(val: u32) -> TextUnit {
17 impl fmt::Debug for TextUnit {
18 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
19 <Self as fmt::Display>::fmt(self, f)
23 impl fmt::Display for TextUnit {
24 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
29 impl From<TextUnit> for u32 {
30 fn from(tu: TextUnit) -> u32 {
35 impl From<u32> for TextUnit {
36 fn from(tu: u32) -> TextUnit {
41 impl ops::Add<TextUnit> for TextUnit {
42 type Output = TextUnit;
43 fn add(self, rhs: TextUnit) -> TextUnit {
44 TextUnit(self.0 + rhs.0)
48 impl ops::AddAssign<TextUnit> for TextUnit {
49 fn add_assign(&mut self, rhs: TextUnit) {
54 impl ops::Sub<TextUnit> for TextUnit {
55 type Output = TextUnit;
56 fn sub(self, rhs: TextUnit) -> TextUnit {
57 TextUnit(self.0 - rhs.0)
61 impl ops::SubAssign<TextUnit> for TextUnit {
62 fn sub_assign(&mut self, rhs: TextUnit) {
67 #[derive(Clone, Copy, PartialEq, Eq)]
68 pub struct TextRange {
73 impl fmt::Debug for TextRange {
74 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
75 <Self as fmt::Display>::fmt(self, f)
79 impl fmt::Display for TextRange {
80 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
81 write!(f, "[{}; {})", self.start(), self.end())
86 pub fn empty() -> TextRange {
87 TextRange::from_to(TextUnit::new(0), TextUnit::new(0))
90 pub fn from_to(from: TextUnit, to: TextUnit) -> TextRange {
91 assert!(from <= to, "Invalid text range [{}; {})", from, to);
98 pub fn from_len(from: TextUnit, len: TextUnit) -> TextRange {
99 TextRange::from_to(from, from + len)
102 pub fn start(&self) -> TextUnit {
106 pub fn end(&self) -> TextUnit {
110 pub fn len(&self) -> TextUnit {
111 self.end - self.start
114 pub fn is_empty(&self) -> bool {
115 self.start() == self.end()
119 impl ops::Index<TextRange> for str {
122 fn index(&self, index: TextRange) -> &str {
123 &self[index.start().0 as usize..index.end().0 as usize]