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 4 pub struct LineColIterator<I> { 7 /// Index of the current line. Characters in the first line of the input 8 /// (before the first newline character) are in line 1. 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. 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. 23 impl<I> LineColIterator<I> 25 I: Iterator<Item = io::Result<u8>>, 27 pub fn new(iter: I) -> LineColIterator<I> { 36 pub fn line(&self) -> usize { 40 pub fn col(&self) -> usize { 44 pub fn byte_offset(&self) -> usize { 45 self.start_of_line + self.col 49 impl<I> Iterator for LineColIterator<I> 51 I: Iterator<Item = io::Result<u8>>, 53 type Item = io::Result<u8>; 55 fn next(&mut self) -> Option<io::Result<u8>> { 56 match self.iter.next() { 59 self.start_of_line += self.col + 1; 68 Some(Err(e)) => Some(Err(e)),