text-reader

Build Status

Rust string character reader.

Usage

toml [dependencies] text-reader = "0.1"

Examples

```rust use text_reader::TextReader;

[test]

fn testwhile() { let mut reader = TextReader::new("華文\ndef"); while reader.hasnext() { let position = reader.position(); match reader.next() { Some(ch) => match position { 0 => asserteq!('華', ch), 1 => asserteq!('文', ch), 2 => asserteq!('\n', ch), 3 => asserteq!('d', ch), 4 => asserteq!('e', ch), 5 => asserteq!('f', ch), _ => {} }, None => panic!("None") } } } ```

more

Analysis

```rust use text_reader::TextReader;

[test]

fn teststat() { let mut reader = TextReader::new("abc\ndef"); println!("{:?}", reader); reader.next(); println!("{:?}", reader); reader.back(); println!("{:?}", reader); let linetext = reader.thisline(); println!("{:?}", linetext); let position = reader.position(); println!("{:?}", position); println!("{:?}", reader);

reader.next(); reader.next(); reader.next(); let line = reader.line(); // 1 asserteq!(1, line); println!("{:?}", reader); reader.next(); let line = reader.line(); // 2 asserteq!(2, line); println!("{:?}", reader); } ```

When create a TextReader TextReader::new("abc\ndef"), TextReader status is:

TextReader { len: 7, text: ['a', 'b', 'c', '\n', 'd', 'e', 'f'], position: 0, line: 1, cursor: 0 }

next

And then, read next character let ch = reader.next(), ch will return Some('a'); TextReader status:

TextReader { len: 7, text: ['a', 'b', 'c', '\n', 'd', 'e', 'f'], position: 1, line: 1, cursor: 1 }

peek

peek function not change status, only get current character let ch = reader.peek()

TextReader { len: 7, text: ['a', 'b', 'c', '\n', 'd', 'e', 'f'], position: 1, line: 1, cursor: 1 }

back

Back will change TextReader to previous status. return TextReader reference. reader.back().

TextReader { len: 7, text: ['a', 'b', 'c', '\n', 'd', 'e', 'f'], position: 0, line: 1, cursor: 0 }

this_line

this line return current line text, not change TextReader status. let line_text = reader.this_line().

TextReader { len: 7, text: ['a', 'b', 'c', '\n', 'd', 'e', 'f'], position: 0, line: 1, cursor: 0 }

line_text is Some("abc")

position

position function return TextReader position value. the num of character position. let position = reader.position()

position is 0

TextReader { len: 7, text: ['a', 'b', 'c', '\n', 'd', 'e', 'f'], position: 0, line: 1, cursor: 0 }

line

return current line number, split of \n.

rust reader.next(); reader.next(); reader.next(); let line = reader.line(); // 1 // TextReader { len: 7, text: ['a', 'b', 'c', '\n', 'd', 'e', 'f'], position: 3, line: 1, cursor: 3 } reader.next(); let line = reader.line(); // 2 // TextReader { len: 7, text: ['a', 'b', 'c', '\n', 'd', 'e', 'f'], position: 4, line: 2, cursor: 0 }

cursor

position of line. starting from 0. and change to 0 when it encounters \n

has_next

has next character. can be used with while. or determine if the last character