changelog shortlog graph tags branches changeset files revisions annotate raw help

Mercurial > core / rust/lib/sxp/src/iter.rs

changeset 8: 1227f932b628
author: ellis <ellis@rwest.io>
date: Tue, 17 Oct 2023 20:36:37 -0400
permissions: -rw-r--r--
description: rust import
1 //! iter.rs --- SXP Iterator primitives
2 use crate::io;
3 
4 pub struct LineColIterator<I> {
5  iter: I,
6 
7  /// Index of the current line. Characters in the first line of the input
8  /// (before the first newline character) are in line 1.
9  line: usize,
10 
11  /// Index of the current column. The first character in the input and any
12  /// characters immediately following a newline character are in column 1.
13  /// The column is 0 immediately after a newline character has been read.
14  col: usize,
15 
16  /// Byte offset of the start of the current line. This is the sum of lengths
17  /// of all previous lines. Keeping track of things this way allows efficient
18  /// computation of the current line, column, and byte offset while only
19  /// updating one of the counters in `next()` in the common case.
20  start_of_line: usize,
21 }
22 
23 impl<I> LineColIterator<I>
24 where
25  I: Iterator<Item = io::Result<u8>>,
26 {
27  pub fn new(iter: I) -> LineColIterator<I> {
28  LineColIterator {
29  iter,
30  line: 1,
31  col: 0,
32  start_of_line: 0,
33  }
34  }
35 
36  pub fn line(&self) -> usize {
37  self.line
38  }
39 
40  pub fn col(&self) -> usize {
41  self.col
42  }
43 
44  pub fn byte_offset(&self) -> usize {
45  self.start_of_line + self.col
46  }
47 }
48 
49 impl<I> Iterator for LineColIterator<I>
50 where
51  I: Iterator<Item = io::Result<u8>>,
52 {
53  type Item = io::Result<u8>;
54 
55  fn next(&mut self) -> Option<io::Result<u8>> {
56  match self.iter.next() {
57  None => None,
58  Some(Ok(b'\n')) => {
59  self.start_of_line += self.col + 1;
60  self.line += 1;
61  self.col = 0;
62  Some(Ok(b'\n'))
63  }
64  Some(Ok(c)) => {
65  self.col += 1;
66  Some(Ok(c))
67  }
68  Some(Err(e)) => Some(Err(e)),
69  }
70  }
71 }