fastparse 0.0.3

Miscellaneous general-purpose parsing utility components, useful in dependent projects
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
/// Fast-parsing constructs and operations
pub mod fastparse {

    /// Fast-parsing types.
    pub mod types {

        use std::cmp::{
            self as std_cmp,
            Ordering,
        };


        /// A slice representation of offset and length.
        #[derive(Clone, Copy)]
        #[derive(Debug)]
        #[derive(Eq)]
        pub struct PositionalSlice {
            /// The slice offset in the source sequence.
            pub offset : usize,
            /// The length of the slice.
            pub length : usize,
        }

        // API functions
        impl PositionalSlice {
            /// Creates an empty instance.
            pub fn empty() -> Self {
                let offset = 0;
                let length = 0;

                Self {
                    offset,
                    length,
                }
            }

            /// Creates an instance with the given `off`set and `len`gth.
            ///
            /// Parameters:
            /// - `off` - The offset of the slice;
            /// - `len` - The length of the slice;
            pub fn new(
                off : usize,
                len : usize,
            ) -> Self {
                Self {
                    offset : off,
                    length : len,
                }
            }
        }

        // Mutating methods
        impl PositionalSlice {
        }

        // Non-mutating methods
        impl PositionalSlice {
            /// Indicates whether the slice is empty.
            pub fn is_empty(&self) -> bool {
                0 == self.length
            }

            /// Indicates the length of the slice.
            pub fn len(&self) -> usize {
                self.length
            }

            /// Obtains unchecked a copy of the slice moved by the given
            /// `d`elta.
            ///
            /// # Parameters:
            /// - `d` - The delta;
            ///
            /// # Return:
            /// New instance of [`PositionalSlice`] adjusted appropriately.
            ///
            /// # Preconditions:
            /// * `isize <= self.offset` - will panic (in debug) if false
            pub fn offset_unchecked(
                &self,
                d : isize,
            ) -> Self {
                // TODO: determine the right Rust way of doing addition with
                let new_off : usize = if d < 0 {
                    self.offset - (-d) as usize
                } else {
                    self.offset + d as usize
                };

                Self {
                    length : self.length,
                    offset : new_off,
                }
            }

            /// Obtains checked a copy of the slice moved by the given
            /// `d`elta.
            ///
            /// # Parameters:
            /// - `d` - The delta;
            ///
            /// # Return:
            /// `Option<PositionalSlice>`, where, if `Some`, it contains
            /// appropriately adjusted slice.
            pub fn offset_checked(
                &self,
                d : isize,
            ) -> Option<Self> {
                // Possibilities for failure:
                //
                // 1. d is -ve and _would_ move offset below 0;
                // 2. d is +ve and _would_ move offset above usize::MAX; or
                // 3. d is +ve and _would_ move offset such that offset+length > usize::MAX

                if 0 == d {
                    return Some(*self);
                }

                if d < 0 {
                    let a = (-d) as usize;

                    if a > self.offset {
                        // case 1.
                        return None;
                    }

                    Some(Self::new(self.offset - a, self.length))
                } else {
                    debug_assert!(d > 0);

                    let a = d as usize;

                    if a > usize::MAX - self.offset {
                        // case 2.
                        return None;
                    }

                    if a + self.length > usize::MAX - self.offset {
                        // case 3.
                        return None;
                    }

                    Some(Self::new(self.offset + a, self.length))
                }
            }

            /// Applies this positional slice to a slice of arbitrary type,
            /// obtaining a relative slice as a result.
            ///
            /// # Parameters:
            /// - `slice` - The slice of which to provide a subslice;
            ///
            /// # Return:
            /// An instance of a slice of `slice` according to the `offset`
            /// and `length` of the receiving instance.
            pub fn subslice_of<'a, T>(
                &self,
                slice : &'a [T],
            ) -> &'a [T] {
                &slice[self.offset..self.offset + self.length]
            }

            /// Applies this positional slice to a slice of `'str`,
            /// obtaining a relative slice as a result.
            ///
            /// # Parameters:
            /// - `slice` - The slice of which to provide a subslice;
            ///
            /// # Return:
            /// An instance of a slice of `slice` according to the `offset`
            /// and `length` of the receiving instance.
            pub fn substring_of<'a>(
                &self,
                slice : &'a str,
            ) -> &'a str {
                &slice[self.offset..self.offset + self.length]
            }
        }

        // Trait implementations

        impl std_cmp::PartialEq for PositionalSlice {
            fn eq(
                &self,
                other : &Self,
            ) -> bool {
                if self.offset != other.offset {
                    return false;
                }

                if self.length != other.length {
                    return false;
                }

                true
            }
        }

        impl std_cmp::PartialOrd for PositionalSlice {
            fn partial_cmp(
                &self,
                other : &Self,
            ) -> Option<Ordering> {
                if self.offset < other.offset {
                    return Some(Ordering::Less);
                }

                if other.offset < self.offset {
                    return Some(Ordering::Greater);
                }

                if self.length < other.length {
                    return Some(Ordering::Less);
                }

                if other.length < self.length {
                    return Some(Ordering::Greater);
                }

                Some(Ordering::Equal)
            }
        }
    }
}


#[cfg(test)]
#[allow(non_snake_case)]
mod tests {
    use super::fastparse::types::PositionalSlice;


    #[test]
    fn PositionalSlice_empty() {
        // check empty() produces an empty slice
        {
            let ssi = PositionalSlice::empty();

            assert_eq!(0, ssi.offset);
            assert_eq!(0, ssi.length);

            assert!(ssi.is_empty());
        }
    }

    #[test]
    fn PositionalSlice_new() {
        // check new(0, 0) produces an empty slice
        {
            let ssi = PositionalSlice::new(0, 0);

            assert_eq!(0, ssi.offset);
            assert_eq!(0, ssi.length);

            assert!(ssi.is_empty());
        }

        // check new(1, 0) produces an empty slice
        {
            let ssi = PositionalSlice::new(1, 0);

            assert_eq!(1, ssi.offset);
            assert_eq!(0, ssi.length);

            assert!(ssi.is_empty());
        }

        // check new(0, 1) produces a non-empty slice
        {
            let ssi = PositionalSlice::new(0, 1);

            assert_eq!(0, ssi.offset);
            assert_eq!(1, ssi.length);

            assert!(!ssi.is_empty());
        }
    }

    #[test]
    fn PositionalSlice_clone() {
        let ssi1 = PositionalSlice::new(10, 13);
        let ssi2 = ssi1.clone();

        assert_eq!(ssi1, ssi2);
    }

    #[test]
    fn PositionalSlice_copy() {
        let ssi1 = PositionalSlice::new(10, 13);
        let ssi2 = ssi1;

        assert_eq!(ssi1, ssi2);
    }

    #[test]
    fn PositionalSlice_op_eq() {
        assert_eq!(PositionalSlice::new(0, 0), PositionalSlice::new(0, 0));

        assert_ne!(PositionalSlice::new(0, 0), PositionalSlice::new(1, 0));
        assert_ne!(PositionalSlice::new(0, 0), PositionalSlice::new(0, 1));
        assert_ne!(PositionalSlice::new(0, 0), PositionalSlice::new(1, 1));
    }

    #[test]
    fn PositionalSlice_op_lt() {
        assert!(!(PositionalSlice::new(0, 0) < PositionalSlice::new(0, 0)));
        assert!(!(PositionalSlice::new(0, 0) > PositionalSlice::new(0, 0)));

        assert!(PositionalSlice::new(0, 1) < PositionalSlice::new(1, 1));
        assert!(PositionalSlice::new(1, 1) > PositionalSlice::new(0, 1));

        assert!(PositionalSlice::new(0, 1) < PositionalSlice::new(0, 2));
        assert!(PositionalSlice::new(0, 2) > PositionalSlice::new(0, 1));
    }

    #[test]
    fn PositionalSlice_offset_unchecked() {
        {
            let ssi1 = PositionalSlice::new(0, 1);

            let ssi2 = ssi1.offset_unchecked(1);

            assert_eq!(PositionalSlice::new(1, 1), ssi2);
        }

        {
            let ssi1 = PositionalSlice::new(1, 1);

            let ssi2 = ssi1.offset_unchecked(-1);

            assert_eq!(PositionalSlice::new(0, 1), ssi2);
        }

        #[cfg(not(debug_assertions))]
        {
            let ssi1 = PositionalSlice::new(0, 1);

            let ssi2 = ssi1.offset_unchecked(-1);

            assert_eq!(PositionalSlice::new(std::usize::MAX, 1), ssi2);
        }
    }

    #[cfg(debug_assertions)]
    #[test]
    #[should_panic(expected = "attempt to subtract with overflow")]
    fn PositionalSlice_offset_unchecked_() {
        {
            let ssi1 = PositionalSlice::new(0, 1);

            let _ssi2 = ssi1.offset_unchecked(-1);

            panic!("should not get here");
        }
    }

    #[test]
    fn PositionalSlice_offset_checked() {
        {
            let ssi1 = PositionalSlice::new(0, 1);

            let ssi2 = ssi1.offset_checked(1);

            assert!(ssi2.is_some());
            assert_eq!(PositionalSlice::new(1, 1), ssi2.unwrap());
        }

        {
            let ssi1 = PositionalSlice::new(1, 1);

            let ssi2 = ssi1.offset_checked(-1);

            assert!(ssi2.is_some());
            assert_eq!(PositionalSlice::new(0, 1), ssi2.unwrap());
        }

        {
            let ssi1 = PositionalSlice::new(0, 1);

            let ssi2 = ssi1.offset_checked(-1);

            assert!(ssi2.is_none());
        }

        {
            let ssi1 = PositionalSlice::new(usize::MAX - 2, 1);

            let ssi2 = ssi1.offset_checked(1);

            assert!(ssi2.is_some());
            assert_eq!(PositionalSlice::new(usize::MAX - 1, 1), ssi2.unwrap());
        }

        {
            let ssi1 = PositionalSlice::new(usize::MAX - 2, 1);

            let ssi2 = ssi1.offset_checked(2);

            assert!(ssi2.is_none());
        }
    }

    #[test]
    fn PositionalSlice_subslice_of() {
        {
            let ps = PositionalSlice::new(2, 2);

            let source = vec![
                // insert list:
                0, 1, 2, 3, 4, 5, 6,
            ];

            let sub = ps.subslice_of(&source);

            assert_eq!(2, sub.len());
            assert_eq!(2, sub[0]);
            assert_eq!(3, sub[1]);
        }

        {
            let ps = PositionalSlice::new(2, 2);

            let source = vec![
                // insert list:
                0, 1, 2, 3, 4, 5, 6,
            ];

            let sub = ps.subslice_of(&source[1..]);

            assert_eq!(2, sub.len());
            assert_eq!(3, sub[0]);
            assert_eq!(4, sub[1]);
        }
    }

    #[test]
    fn PositionalSlice_substring_of() {
        {
            let ps = PositionalSlice::new(2, 2);

            let source = "abcdef".to_string();

            let sub = ps.substring_of(&source[..]);

            assert_eq!("cd", sub);
        }

        {
            let ps = PositionalSlice::new(2, 2);

            let source = "abcdef".to_string();

            let sub = ps.substring_of(&source[1..]);

            assert_eq!("de", sub);
        }
    }
}