8
8
9
9
/// A range in text, represented as a pair of [`TextSize`][struct@TextSize].
10
10
///
11
+ /// It is a logic error for `start` to be greater than `end`.
12
+ ///
11
13
/// # Translation from `text_unit`
12
14
///
13
15
/// - `TextRange::from_to(from, to)` ⟹ `TextRange::new(from, to)`
@@ -40,19 +42,57 @@ impl TextRange {
40
42
/// # Panics
41
43
///
42
44
/// Panics if `end < start`.
45
+ ///
46
+ /// # Examples
47
+ ///
48
+ /// ```rust
49
+ /// # use text_size::*;
50
+ /// let start = TextSize::from(5);
51
+ /// let end = TextSize::from(10);
52
+ /// let range = TextRange::new(start, end);
53
+ ///
54
+ /// assert_eq!(range.start(), start);
55
+ /// assert_eq!(range.end(), end);
56
+ /// assert_eq!(range.len(), end - start);
57
+ /// ```
43
58
#[ inline]
44
59
pub fn new ( start : TextSize , end : TextSize ) -> TextRange {
45
60
assert ! ( start <= end) ;
46
61
TextRange { start, end }
47
62
}
48
63
49
64
/// Create a new `TextRange` with the given `start` and `len` (`start..start + len`).
65
+ ///
66
+ /// # Examples
67
+ ///
68
+ /// ```rust
69
+ /// # use text_size::*;
70
+ /// let text = "0123456789";
71
+ ///
72
+ /// let start = TextSize::from(2);
73
+ /// let length = TextSize::from(5);
74
+ /// let range = TextRange::from_len(start, length);
75
+ ///
76
+ /// assert_eq!(range, TextRange::new(start, start + length));
77
+ /// assert_eq!(&text[range], "23456")
78
+ /// ```
50
79
#[ inline]
51
80
pub fn from_len ( start : TextSize , len : TextSize ) -> TextRange {
52
81
TextRange :: new ( start, start + len)
53
82
}
54
83
55
84
/// Create a zero-length range at the specified offset (`offset..offset`).
85
+ ///
86
+ /// # Examples
87
+ ///
88
+ /// ```rust
89
+ /// # use text_size::*;
90
+ /// let point: TextSize;
91
+ /// # point = TextSize::from(3);
92
+ /// let range = TextRange::empty(point);
93
+ /// assert!(range.is_empty());
94
+ /// assert_eq!(range, TextRange::new(point, point));
95
+ /// ```
56
96
#[ inline]
57
97
pub const fn empty ( offset : TextSize ) -> TextRange {
58
98
TextRange {
@@ -62,6 +102,19 @@ impl TextRange {
62
102
}
63
103
64
104
/// Create a range up to the given end (`..end`).
105
+ ///
106
+ /// # Examples
107
+ ///
108
+ /// ```rust
109
+ /// # use text_size::*;
110
+ /// let point: TextSize;
111
+ /// # point = TextSize::from(12);
112
+ /// let range = TextRange::up_to(point);
113
+ ///
114
+ /// assert_eq!(range.len(), point);
115
+ /// assert_eq!(range, TextRange::new(TextSize::zero(), point));
116
+ /// assert_eq!(range, TextRange::from_len(TextSize::zero(), point));
117
+ /// ```
65
118
#[ inline]
66
119
pub const fn up_to ( end : TextSize ) -> TextRange {
67
120
let start = TextSize :: zero ( ) ;
@@ -105,24 +158,73 @@ impl TextRange {
105
158
/// Check if this range contains an offset.
106
159
///
107
160
/// The end index is considered excluded.
161
+ ///
162
+ /// # Examples
163
+ ///
164
+ /// ```rust
165
+ /// # use text_size::*;
166
+ /// let (start, end): (TextSize, TextSize);
167
+ /// # start = 10.into(); end = 20.into();
168
+ /// let range = TextRange::new(start, end);
169
+ /// assert!(range.contains(start));
170
+ /// assert!(!range.contains(end));
171
+ /// ```
108
172
pub fn contains ( self , offset : TextSize ) -> bool {
109
173
self . start ( ) <= offset && offset < self . end ( )
110
174
}
111
175
112
176
/// Check if this range contains an offset.
113
177
///
114
178
/// The end index is considered included.
179
+ ///
180
+ /// # Examples
181
+ ///
182
+ /// ```rust
183
+ /// # use text_size::*;
184
+ /// let (start, end): (TextSize, TextSize);
185
+ /// # start = 10.into(); end = 20.into();
186
+ /// let range = TextRange::new(start, end);
187
+ /// assert!(range.contains_inclusive(start));
188
+ /// assert!(range.contains_inclusive(end));
189
+ /// ```
115
190
pub fn contains_inclusive ( self , offset : TextSize ) -> bool {
116
191
self . start ( ) <= offset && offset <= self . end ( )
117
192
}
118
193
119
194
/// Check if this range completely contains another range.
195
+ ///
196
+ /// # Examples
197
+ ///
198
+ /// ```rust
199
+ /// # use text_size::*;
200
+ /// let larger = TextRange::new(0.into(), 20.into());
201
+ /// let smaller = TextRange::new(5.into(), 15.into());
202
+ /// assert!(larger.contains_range(smaller));
203
+ /// assert!(!smaller.contains_range(larger));
204
+ ///
205
+ /// // a range always contains itself
206
+ /// assert!(larger.contains_range(larger));
207
+ /// assert!(smaller.contains_range(smaller));
208
+ /// ```
120
209
pub fn contains_range ( self , other : TextRange ) -> bool {
121
210
self . start ( ) <= other. start ( ) && other. end ( ) <= self . end ( )
122
211
}
123
212
124
213
/// The range covered by both ranges, if it exists.
125
214
/// If the ranges touch but do not overlap, the output range is empty.
215
+ ///
216
+ /// # Examples
217
+ ///
218
+ /// ```rust
219
+ /// # use text_size::*;
220
+ /// assert_eq!(
221
+ /// TextRange::intersect(
222
+ /// TextRange::new(0.into(), 10.into()),
223
+ /// TextRange::new(5.into(), 15.into()),
224
+ /// ),
225
+ /// Some(TextRange::new(5.into(), 10.into())),
226
+ /// );
227
+ /// ```
126
228
pub fn intersect ( self , other : TextRange ) -> Option < TextRange > {
127
229
let start = cmp:: max ( self . start ( ) , other. start ( ) ) ;
128
230
let end = cmp:: min ( self . end ( ) , other. end ( ) ) ;
@@ -133,13 +235,36 @@ impl TextRange {
133
235
}
134
236
135
237
/// Extends the range to cover `other` as well.
238
+ ///
239
+ /// # Examples
240
+ ///
241
+ /// ```rust
242
+ /// # use text_size::*;
243
+ /// assert_eq!(
244
+ /// TextRange::cover(
245
+ /// TextRange::new(0.into(), 5.into()),
246
+ /// TextRange::new(15.into(), 20.into()),
247
+ /// ),
248
+ /// TextRange::new(0.into(), 20.into()),
249
+ /// );
250
+ /// ```
136
251
pub fn cover ( self , other : TextRange ) -> TextRange {
137
252
let start = cmp:: min ( self . start ( ) , other. start ( ) ) ;
138
253
let end = cmp:: max ( self . end ( ) , other. end ( ) ) ;
139
254
TextRange :: new ( start, end)
140
255
}
141
256
142
257
/// Extends the range to cover `other` offsets as well.
258
+ ///
259
+ /// # Examples
260
+ ///
261
+ /// ```rust
262
+ /// # use text_size::*;
263
+ /// assert_eq!(
264
+ /// TextRange::empty(TextSize::zero()).cover_offset(20.into()),
265
+ /// TextRange::new(0.into(), 20.into()),
266
+ /// )
267
+ /// ```
143
268
pub fn cover_offset ( self , offset : TextSize ) -> TextRange {
144
269
self . cover ( TextRange :: empty ( offset) )
145
270
}
0 commit comments