1
1
use {
2
- crate :: { TextSize , TextSized } ,
2
+ crate :: TextSize ,
3
3
std:: {
4
4
cmp,
5
5
convert:: { TryFrom , TryInto } ,
6
6
fmt,
7
- num:: TryFromIntError ,
8
- ops:: {
9
- Add , AddAssign , Bound , Index , IndexMut , Range , RangeBounds , RangeInclusive , RangeTo ,
10
- RangeToInclusive , Sub , SubAssign ,
11
- } ,
7
+ ops:: { Bound , Index , IndexMut , Range , RangeBounds } ,
12
8
} ,
13
9
} ;
14
10
@@ -19,17 +15,17 @@ use {
19
15
///
20
16
/// # Translation from `text_unit`
21
17
///
22
- /// - `TextRange::from_to(from, to)` ⟹ `TextRange::from(from..to)`
23
- /// - `TextRange::offset_len(offset, size)` ⟹ `TextRange::at (offset).with_len( size)`
24
- /// - `range.start()` ⟹ `range.start()`
25
- /// - `range.end()` ⟹ `range.end()`
26
- /// - `range.len()` ⟹ `range.len()`<sup>†</sup>
27
- /// - `range.is_empty()` ⟹ `range.is_empty()`
28
- /// - `a.is_subrange(b)` ⟹ `b.contains(a)`
29
- /// - `a.intersection(b)` ⟹ `TextRange::intersection(a, b)`
30
- /// - `a.extend_to(b)` ⟹ `TextRange::covering(a, b)`
31
- /// - `range.contains(offset)` ⟹ `range.contains_point (point)`
32
- /// - `range.contains_inclusive(offset)` ⟹ `range.contains_point_inclusive (point)`
18
+ /// - `TextRange::from_to(from, to)` ⟹ `TextRange::from(from..to)`
19
+ /// - `TextRange::offset_len(offset, size)` ⟹ `TextRange::from (offset..offset + size)`
20
+ /// - `range.start()` ⟹ `range.start()`
21
+ /// - `range.end()` ⟹ `range.end()`
22
+ /// - `range.len()` ⟹ `range.len()`<sup>†</sup>
23
+ /// - `range.is_empty()` ⟹ `range.is_empty()`
24
+ /// - `a.is_subrange(b)` ⟹ `b.contains(a)`
25
+ /// - `a.intersection(b)` ⟹ `TextRange::intersection(a, b)`
26
+ /// - `a.extend_to(b)` ⟹ `TextRange::covering(a, b)`
27
+ /// - `range.contains(offset)` ⟹ `range.contains_exclusive (point)`
28
+ /// - `range.contains_inclusive(offset)` ⟹ `range.contains_inclusive (point)`
33
29
///
34
30
/// † See the note on [`TextRange::len`] for differing behavior for incorrect reverse ranges.
35
31
#[ derive( Copy , Clone , Eq , PartialEq , Hash ) ]
@@ -44,12 +40,6 @@ pub(crate) const fn TextRange(start: TextSize, end: TextSize) -> TextRange {
44
40
}
45
41
46
42
impl fmt:: Debug for TextRange {
47
- fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
48
- fmt:: Display :: fmt ( self , f)
49
- }
50
- }
51
-
52
- impl fmt:: Display for TextRange {
53
43
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
54
44
write ! ( f, "[{}..{})" , self . start( ) , self . end( ) )
55
45
}
@@ -91,27 +81,6 @@ impl TextRange {
91
81
92
82
/// Manipulation methods.
93
83
impl TextRange {
94
- /// A range covering the text size of some text-like object.
95
- pub fn of ( size : impl TextSized ) -> TextRange {
96
- TextRange ( 0 . into ( ) , size. text_size ( ) )
97
- }
98
-
99
- /// An empty range at some text size offset.
100
- pub fn at ( size : impl Into < TextSize > ) -> TextRange {
101
- let size = size. into ( ) ;
102
- TextRange ( size, size)
103
- }
104
-
105
- /// Set the length without changing the starting offset.
106
- pub fn with_len ( self , len : impl Into < TextSize > ) -> TextRange {
107
- TextRange ( self . start ( ) , self . start ( ) + len. into ( ) )
108
- }
109
-
110
- /// Set the starting offset without changing the length.
111
- pub fn with_offset ( self , offset : impl Into < TextSize > ) -> TextRange {
112
- TextRange :: at ( offset) . with_len ( self . len ( ) )
113
- }
114
-
115
84
/// Check if this range completely contains another range.
116
85
pub fn contains ( self , other : TextRange ) -> bool {
117
86
self . start ( ) <= other. start ( ) && other. end ( ) <= self . end ( )
@@ -135,48 +104,35 @@ impl TextRange {
135
104
/// Check if this range contains a point.
136
105
///
137
106
/// The end index is considered excluded.
138
- pub fn contains_point ( self , point : impl Into < TextSize > ) -> bool {
107
+ pub fn contains_exclusive ( self , point : impl Into < TextSize > ) -> bool {
139
108
let point = point. into ( ) ;
140
109
self . start ( ) <= point && point < self . end ( )
141
110
}
142
111
143
112
/// Check if this range contains a point.
144
113
///
145
114
/// The end index is considered included.
146
- pub fn contains_point_inclusive ( self , point : impl Into < TextSize > ) -> bool {
115
+ pub fn contains_inclusive ( self , point : impl Into < TextSize > ) -> bool {
147
116
let point = point. into ( ) ;
148
117
self . start ( ) <= point && point <= self . end ( )
149
118
}
119
+ }
150
120
151
- /// Offset the entire range by some text size.
152
- pub fn checked_add ( self , rhs : impl TryInto < TextSize > ) -> Option < TextRange > {
153
- let rhs = rhs. try_into ( ) . ok ( ) ?;
154
- Some ( TextRange (
155
- self . start ( ) . checked_add ( rhs) ?,
156
- self . end ( ) . checked_add ( rhs) ?,
157
- ) )
158
- }
159
-
160
- /// Offset the entire range by some text size.
161
- pub fn checked_sub ( self , rhs : impl TryInto < TextSize > ) -> Option < TextRange > {
162
- let rhs = rhs. try_into ( ) . ok ( ) ?;
163
- Some ( TextRange (
164
- self . start ( ) . checked_sub ( rhs) ?,
165
- self . end ( ) . checked_sub ( rhs) ?,
166
- ) )
167
- }
121
+ fn ix ( size : TextSize ) -> usize {
122
+ size. try_into ( )
123
+ . unwrap_or_else ( |_| panic ! ( "overflow when converting TextSize to usize index" ) )
168
124
}
169
125
170
126
impl Index < TextRange > for str {
171
127
type Output = str ;
172
128
fn index ( & self , index : TextRange ) -> & Self :: Output {
173
- & self [ index. start ( ) . ix ( ) ..index. end ( ) . ix ( ) ]
129
+ & self [ ix ( index. start ( ) ) ..ix ( index. end ( ) ) ]
174
130
}
175
131
}
176
132
177
133
impl IndexMut < TextRange > for str {
178
134
fn index_mut ( & mut self , index : TextRange ) -> & mut Self :: Output {
179
- & mut self [ index. start ( ) . ix ( ) ..index. end ( ) . ix ( ) ]
135
+ & mut self [ ix ( index. start ( ) ) ..ix ( index. end ( ) ) ]
180
136
}
181
137
}
182
138
@@ -197,29 +153,7 @@ macro_rules! conversions {
197
153
TextRange ( value. start. into( ) , value. end. into( ) )
198
154
}
199
155
}
200
- impl TryFrom <RangeInclusive <$lte>> for TextRange {
201
- type Error = TryFromIntError ;
202
- fn try_from( value: RangeInclusive <$lte>) -> Result <TextRange , Self :: Error > {
203
- let ( start, end) = value. into_inner( ) ;
204
- let end: TextSize = end. into( ) ;
205
- // This is the only way to get a TryFromIntError currently.
206
- let end = end. checked_add( 1 ) . ok_or_else( || u8 :: try_from( -1 ) . unwrap_err( ) ) ?;
207
- Ok ( TextRange ( start. into( ) , end) )
208
- }
209
- }
210
- impl From <RangeTo <$lte>> for TextRange {
211
- fn from( value: RangeTo <$lte>) -> TextRange {
212
- TextRange ( 0 . into( ) , value. end. into( ) )
213
- }
214
- }
215
- impl TryFrom <RangeToInclusive <$lte>> for TextRange {
216
- type Error = TryFromIntError ;
217
- fn try_from( value: RangeToInclusive <$lte>) -> Result <TextRange , Self :: Error > {
218
- let start: TextSize = 0 . into( ) ;
219
- let end: TextSize = value. end. into( ) ;
220
- TextRange :: try_from( start..=end)
221
- }
222
- }
156
+ // Just support `start..end` for now, not `..end`, `start..=end`, `..=end`.
223
157
} ;
224
158
( TryFrom <$gt: ident> for TextRange ) => {
225
159
impl TryFrom <Range <$gt>> for TextRange {
@@ -228,30 +162,7 @@ macro_rules! conversions {
228
162
Ok ( TextRange ( value. start. try_into( ) ?, value. end. try_into( ) ?) )
229
163
}
230
164
}
231
- impl TryFrom <RangeInclusive <$gt>> for TextRange {
232
- type Error = TryFromIntError ;
233
- fn try_from( value: RangeInclusive <$gt>) -> Result <TextRange , Self :: Error > {
234
- let ( start, end) = value. into_inner( ) ;
235
- let end: TextSize = end. try_into( ) ?;
236
- // This is the only way to get a TryFromIntError currently.
237
- let end = end. checked_add( 1 ) . ok_or_else( || u8 :: try_from( -1 ) . unwrap_err( ) ) ?;
238
- Ok ( TextRange ( start. try_into( ) ?, end) )
239
- }
240
- }
241
- impl TryFrom <RangeTo <$gt>> for TextRange {
242
- type Error = TryFromIntError ;
243
- fn try_from( value: RangeTo <$gt>) -> Result <TextRange , Self :: Error > {
244
- Ok ( TextRange ( 0 . into( ) , value. end. try_into( ) ?) )
245
- }
246
- }
247
- impl TryFrom <RangeToInclusive <$gt>> for TextRange {
248
- type Error = TryFromIntError ;
249
- fn try_from( value: RangeToInclusive <$gt>) -> Result <TextRange , Self :: Error > {
250
- let start: TextSize = 0 . into( ) ;
251
- let end: TextSize = value. end. try_into( ) ?;
252
- TextRange :: try_from( start..=end)
253
- }
254
- }
165
+ // Just support `start..end` for now, not `..end`, `start..=end`, `..=end`.
255
166
} ;
256
167
{
257
168
lt TextSize [ $( $lt: ident) * ]
@@ -260,11 +171,8 @@ macro_rules! conversions {
260
171
varries [ $( $var: ident) * ]
261
172
} => {
262
173
$(
263
- // Not `From` yet because of integer type fallback. We want e.g.
264
- // `TextRange::from(0)` and `range + 1` to work, and more `From`
265
- // impls means that this will try (and fail) to use i32 rather
266
- // than one of the unsigned integer types that actually work.
267
- conversions!( TryFrom <$lt> for TextRange ) ;
174
+ conversions!( From <$lt> for TextRange ) ;
175
+ // unlike TextSize, we do not provide conversions in the "out" direction.
268
176
) *
269
177
270
178
$(
@@ -288,68 +196,3 @@ conversions! {
288
196
gt TextSize [ u64 ]
289
197
varries [ usize ]
290
198
}
291
-
292
- impl Into < TextRange > for & ' _ TextRange {
293
- fn into ( self ) -> TextRange {
294
- * self
295
- }
296
- }
297
-
298
- impl Into < TextRange > for & ' _ mut TextRange {
299
- fn into ( self ) -> TextRange {
300
- * self
301
- }
302
- }
303
-
304
- macro_rules! op {
305
- ( impl $Op: ident for TextRange by fn $f: ident = $op: tt) => {
306
- impl <IntoSize : Copy > $Op<IntoSize > for TextRange
307
- where
308
- TextSize : $Op<IntoSize , Output = TextSize >,
309
- {
310
- type Output = TextRange ;
311
- fn $f( self , rhs: IntoSize ) -> TextRange {
312
- TextRange ( self . start( ) $op rhs, self . end( ) $op rhs)
313
- }
314
- }
315
- impl <IntoSize > $Op<IntoSize > for & ' _ TextRange
316
- where
317
- TextRange : $Op<IntoSize , Output = TextRange >,
318
- {
319
- type Output = TextRange ;
320
- fn $f( self , rhs: IntoSize ) -> TextRange {
321
- * self $op rhs
322
- }
323
- }
324
- impl <IntoSize > $Op<IntoSize > for & ' _ mut TextRange
325
- where
326
- TextRange : $Op<IntoSize , Output = TextRange >,
327
- {
328
- type Output = TextRange ;
329
- fn $f( self , rhs: IntoSize ) -> TextRange {
330
- * self $op rhs
331
- }
332
- }
333
- } ;
334
- }
335
-
336
- op ! ( impl Add for TextRange by fn add = +) ;
337
- op ! ( impl Sub for TextRange by fn sub = -) ;
338
-
339
- impl < A > AddAssign < A > for TextRange
340
- where
341
- TextRange : Add < A , Output = TextRange > ,
342
- {
343
- fn add_assign ( & mut self , rhs : A ) {
344
- * self = * self + rhs
345
- }
346
- }
347
-
348
- impl < S > SubAssign < S > for TextRange
349
- where
350
- TextRange : Sub < S , Output = TextRange > ,
351
- {
352
- fn sub_assign ( & mut self , rhs : S ) {
353
- * self = * self - rhs
354
- }
355
- }
0 commit comments