lineindex 0.1.1

Simple line-indexed string
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
/*!
A simple line-indexed string.

Rather than destructively breaking a string into lines, this structure will allow
create a vector of byte/character ranges each of which describes a line in the string
original string.

# Example

Given the following simple string,

```text
                    1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9
-----------------------------------------------------------
a a ␤ b b b ␤ c c c c ␤ d d
```

We get the following set of line index ranges.

| Row  | < Byte | < Char | > Byte | > Char | String  |
|======|========|========|========|========|=========|
| 0    | 0      | 0      | 2      | 2      | "aa␤"   |
| 1    | 3      | 3      | 6      | 6      | "bbb␤"  |
| 2    | 7      | 7      | 11     | 11     | "cccc␤" |
| 3    | 12     | 12     | 13     | 13     | "dd"    |

This set of ranges can be used to determine which line a character is on as well as
returning the indices for a line or even the text of a line.

```rust
use lineindex::IndexedString;

let indexed = IndexedString::from("aa\nbbb\ncccc\ndd");

assert_eq!(indexed.lines(), 4);

assert_eq!(indexed.line_for_byte(4), Some(1));
assert_eq!(indexed.line_for_character(5), Some(1));

assert_eq!(indexed.byte_range_for_line(1), Some(3..=6));
assert_eq!(indexed.character_range_for_line(2), Some(7..=11));

assert_eq!(indexed.line_str(0), Some("aa\n"));
```

*/

#![warn(
    unknown_lints,
    // ---------- Stylistic
    absolute_paths_not_starting_with_crate,
    elided_lifetimes_in_paths,
    explicit_outlives_requirements,
    macro_use_extern_crate,
    nonstandard_style, /* group */
    noop_method_call,
    rust_2018_idioms,
    single_use_lifetimes,
    trivial_casts,
    trivial_numeric_casts,
    // ---------- Future
    future_incompatible, /* group */
    rust_2021_compatibility, /* group */
    // ---------- Public
    missing_debug_implementations,
    // missing_docs,
    unreachable_pub,
    // ---------- Unsafe
    unsafe_code,
    unsafe_op_in_unsafe_fn,
    // ---------- Unused
    unused, /* group */
)]
#![deny(
    // ---------- Public
    exported_private_dependencies,
    private_in_public,
    // ---------- Deprecated
    anonymous_parameters,
    bare_trait_objects,
    ellipsis_inclusive_range_patterns,
    // ---------- Unsafe
    deref_nullptr,
    drop_bounds,
    dyn_drop,
)]

use std::{borrow::Cow, ops::RangeInclusive};

// ------------------------------------------------------------------------------------------------
// Public Types
// ------------------------------------------------------------------------------------------------

///
/// This type holds a reference to an string and an index of both the byte and character ranges for
/// lines within the string. Both of these values are immutable, a change to the original string
/// will require construction of a new indexed string.
///
/// The index itself is a vector of start/end indices; `start` is the 0-based index of the first
/// character in the line and `end` is the index of the last character in the line.
///
/// Note that an empty string will result in a zero-length vector of
///
#[derive(Clone, Debug)]
pub struct IndexedString<'a> {
    source: Cow<'a, str>,
    lines: Vec<Range>,
}

///
/// This is a simplified version of [`std::ops::RangeInclusive`] where each end of the range is an
/// [`Index`] structure.
///
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Range {
    start: Index,
    end: Index,
}

///
/// An index value is a tuple of the byte index and character index for a character in the string.
///
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Index {
    byte: usize,
    character: usize,
}

// ------------------------------------------------------------------------------------------------
// Public Functions
// ------------------------------------------------------------------------------------------------

// ------------------------------------------------------------------------------------------------
// Implementations
// ------------------------------------------------------------------------------------------------

impl<'a> From<&'a str> for IndexedString<'a> {
    fn from(s: &'a str) -> Self {
        let lines = Self::make_lines(s);
        Self {
            source: Cow::Borrowed(s),
            lines,
        }
    }
}

impl From<String> for IndexedString<'_> {
    fn from(s: String) -> Self {
        let lines = Self::make_lines(&s);
        Self {
            source: Cow::Owned(s),
            lines,
        }
    }
}

impl AsRef<str> for IndexedString<'_> {
    fn as_ref(&self) -> &str {
        &self.source
    }
}

impl IndexedString<'_> {
    fn make_lines(s: &str) -> Vec<Range> {
        let mut lines: Vec<Range> = Default::default();
        if !s.is_empty() {
            let mut start = Index {
                byte: 0,
                character: 0,
            };
            let mut next = false;
            let end = s.len() - 1;
            for (c_i, (b_i, c)) in s.char_indices().enumerate() {
                if next {
                    let here = Index {
                        byte: b_i,
                        character: c_i,
                    };
                    start = here;
                    next = false;
                }
                if c == '\n' || c_i == end {
                    let here = Index {
                        byte: b_i,
                        character: c_i,
                    };
                    lines.push(Range { start, end: here });
                    next = true;
                }
            }
        }
        lines
    }

    ///
    /// Returns the number of rows calculated within the source string.
    ///
    pub fn lines(&self) -> usize {
        self.lines.len()
    }

    ///
    /// Return a reference to the source, as a string.
    ///
    pub fn as_str(&self) -> &str {
        &self.source
    }

    ///
    /// Return a reference to the source, as a byte array.
    ///
    pub fn as_bytes(&self) -> &[u8] {
        self.source.as_bytes()
    }

    ///
    /// Return the line containing the provided byte index. If the index is
    /// outside the range of the string return `None`.
    ///
    pub fn line_for_byte(&self, byte: usize) -> Option<usize> {
        self.line_for(true, byte)
    }

    ///
    /// Return the line containing the provided character index. If the index is
    /// outside the range of the string return `None`.
    ///
    pub fn line_for_character(&self, character: usize) -> Option<usize> {
        self.line_for(false, character)
    }

    fn line_for(&self, byte: bool, index: usize) -> Option<usize> {
        let start = 0;
        let end = self.lines.len();
        self.inner_line_for(byte, index, start, end)
    }

    fn inner_line_for(&self, byte: bool, index: usize, start: usize, end: usize) -> Option<usize> {
        let mid_index = start + ((end - start) / 2);
        let mid_range = self.lines.get(mid_index).unwrap();
        let mid_range = if byte {
            mid_range.bytes()
        } else {
            mid_range.characters()
        };
        if mid_range.contains(&index) {
            Some(mid_index)
        } else if mid_index > start && index < *mid_range.start() {
            self.inner_line_for(byte, index, start, mid_index - 1)
        } else if mid_index < end && index > *mid_range.end() {
            self.inner_line_for(byte, index, mid_index + 1, end)
        } else {
            None
        }
    }

    ///
    /// Return the byte range (including any terminating newline) for the provided
    /// line number. If the line number is outside the range of the string return `None`.
    ///
    pub fn byte_range_for_line(&self, line: usize) -> Option<RangeInclusive<usize>> {
        self.lines.get(line).map(|range| range.bytes())
    }

    ///
    /// Return the character range (including any terminating newline) for the provided
    /// line number. If the line number is outside the range of the string return `None`.
    ///
    pub fn character_range_for_line(&self, line: usize) -> Option<RangeInclusive<usize>> {
        self.lines.get(line).map(|range| range.characters())
    }

    ///
    /// Return the line, as a string, (including any terminating newline) for the provided
    /// line number. If the line number is outside the range of the string return `None`.
    ///
    pub fn line_str(&self, line: usize) -> Option<&str> {
        if let Some(range) = self.byte_range_for_line(line) {
            Some(&self.source[range])
        } else {
            None
        }
    }
}

// ------------------------------------------------------------------------------------------------

impl Range {
    ///
    /// Construct a new range from the start and end indices.
    ///
    pub fn new(start: Index, end: Index) -> Self {
        Self { start, end }
    }

    ///
    /// Return the start index of this range.
    ///
    pub fn start(&self) -> Index {
        self.start
    }

    ///
    /// Return the end index of this range.
    ///
    pub fn end(&self) -> Index {
        self.end
    }

    ///
    /// Return a standard library range for just the byte indices.
    ///
    pub fn bytes(&self) -> RangeInclusive<usize> {
        self.start.byte..=self.end.byte
    }

    ///
    /// Return a standard library range for just the character indices.
    ///
    pub fn characters(&self) -> RangeInclusive<usize> {
        self.start.character..=self.end.character
    }
}

// ------------------------------------------------------------------------------------------------

impl Index {
    ///
    /// Construct a new index with byte and character indices.
    ///
    pub fn new(byte: usize, character: usize) -> Self {
        Self { byte, character }
    }

    ///
    /// Return the byte part of this index.
    ///
    pub fn byte(&self) -> usize {
        self.byte
    }

    ///
    /// Return the character part of this index.
    ///
    pub fn character(&self) -> usize {
        self.character
    }
}

// ------------------------------------------------------------------------------------------------
// Modules
// ------------------------------------------------------------------------------------------------

// ------------------------------------------------------------------------------------------------
// Unit Tests
// ------------------------------------------------------------------------------------------------

#[cfg(test)]
mod tests {
    use super::*;
    use pretty_assertions::assert_eq;

    #[test]
    fn test_empty_string() {
        let indexed = IndexedString::from("");

        println!("{:#?}", indexed);
        assert_eq!(indexed.lines(), 0);
    }

    #[test]
    fn test_simple_lines() {
        let lines = "aa\nbbb\ncccc\ndd";
        let indexed = IndexedString::from(lines);

        println!("{:#?}", indexed);
        assert_eq!(indexed.lines(), 4);

        [
            (0, 0),
            (1, 0),
            (2, 0),
            (3, 1),
            (4, 1),
            (5, 1),
            (6, 1),
            (7, 2),
            (8, 2),
            (9, 2),
            (10, 2),
            (11, 2),
            (12, 3),
            (13, 3),
        ]
        .into_iter()
        .for_each(|(byte, line)| assert_eq!(indexed.line_for_byte(byte).unwrap(), line));

        [(0, "aa\n"), (1, "bbb\n"), (2, "cccc\n"), (3, "dd")]
            .into_iter()
            .for_each(|(line, string)| assert_eq!(indexed.line_str(line), Some(string)));
    }
}