#io #iterator #character #source #read #chars #byte

read_chars

An iterator over characters read from some I/O source

3 releases (breaking)

0.3.0 Jan 3, 2024
0.2.0 Dec 24, 2023
0.1.0 Aug 22, 2023

#1501 in Text processing

MIT license

6KB
98 lines

read_chars

This is just a simple library I made for use in a new lexer for my programming language project Rouge. You are free to use it how you wish under the MIT license.

use read_chars::ReadChars;
use std::convert::From;
use std::io;
use std::fs::File;

fn main() -> io::Result<()> {
	let chars = ReadChars::from(File::open("test.txt")?);

	let max_nesting = chars.filter(|r| match r { Ok(c) if c == '(' || c == ')' => Some(c), _ => None })
		.scan(0, |acc, chr| if chr == '(' { acc + 1 } else { acc - 1 })
		.max()
		.ok_or_else(|| io::Error::from(io::ErrorKind::InvalidData))?;

	println!("Max parentheses nesting in file 'text,txt' is {max_nesting}.");

	Ok(())
}

lib.rs:

[ReadChars] is a simple iterator crate for turning the bytes of an I/O reader into characters for use by things like handwritten lexers.

No runtime deps