diff options
Diffstat (limited to 'src/text.rs')
-rw-r--r-- | src/text.rs | 59 |
1 files changed, 59 insertions, 0 deletions
diff --git a/src/text.rs b/src/text.rs index c3ef1ac8e..ee0dc8398 100644 --- a/src/text.rs +++ b/src/text.rs | |||
@@ -56,4 +56,63 @@ impl ops::SubAssign<TextUnit> for TextUnit { | |||
56 | fn sub_assign(&mut self, rhs: TextUnit) { | 56 | fn sub_assign(&mut self, rhs: TextUnit) { |
57 | self.0 -= rhs.0 | 57 | self.0 -= rhs.0 |
58 | } | 58 | } |
59 | } | ||
60 | |||
61 | |||
62 | #[derive(Clone, Copy, PartialEq, Eq)] | ||
63 | pub struct TextRange { | ||
64 | start: TextUnit, | ||
65 | end: TextUnit, | ||
66 | } | ||
67 | |||
68 | impl fmt::Debug for TextRange { | ||
69 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
70 | <Self as fmt::Display>::fmt(self, f) | ||
71 | } | ||
72 | } | ||
73 | |||
74 | impl fmt::Display for TextRange { | ||
75 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
76 | write!(f, "[{}; {})", self.start(), self.end()) | ||
77 | } | ||
78 | } | ||
79 | |||
80 | |||
81 | impl TextRange { | ||
82 | pub fn empty() -> TextRange { | ||
83 | TextRange::from_to(TextUnit::new(0), TextUnit::new(0)) | ||
84 | } | ||
85 | |||
86 | pub fn from_to(from: TextUnit, to: TextUnit) -> TextRange { | ||
87 | assert!(from <= to, "Invalid text range [{}; {})", from, to); | ||
88 | TextRange { start: from, end: to } | ||
89 | } | ||
90 | |||
91 | pub fn from_len(from: TextUnit, len: TextUnit) -> TextRange { | ||
92 | TextRange::from_to(from, from + len) | ||
93 | } | ||
94 | |||
95 | pub fn start(&self) -> TextUnit { | ||
96 | self.start | ||
97 | } | ||
98 | |||
99 | pub fn end(&self) -> TextUnit { | ||
100 | self.end | ||
101 | } | ||
102 | |||
103 | pub fn len(&self) -> TextUnit { | ||
104 | self.end - self.start | ||
105 | } | ||
106 | |||
107 | pub fn is_empty(&self) -> bool { | ||
108 | self.start() == self.end() | ||
109 | } | ||
110 | } | ||
111 | |||
112 | impl ops::Index<TextRange> for str { | ||
113 | type Output = str; | ||
114 | |||
115 | fn index(&self, index: TextRange) -> &str { | ||
116 | &self[index.start().0 as usize..index.end().0 as usize] | ||
117 | } | ||
59 | } \ No newline at end of file | 118 | } \ No newline at end of file |