Crates.io | charwise |
lib.rs | charwise |
version | 1.0.1 |
source | src |
created_at | 2021-04-06 17:20:51.743165 |
updated_at | 2021-04-06 21:29:11.597193 |
description | This lightweight, dependency-free rust library provides a convenient way to read characters from different resources. |
homepage | https://github.com/ZeroBone/charwise |
repository | https://github.com/ZeroBone/charwise |
max_upload_size | |
id | 379896 |
size | 12,376 |
This lightweight, dependency-free rust library provides a convenient way to read characters from different resources.
For now, the following resources are supported:
std::fs::File
std::io::Stdin
This library is particularly useful when implementing handwritten lexers, because on one hand, we read characters one at a time in code and on the other hand, we may need the following features:
All these features are implemented in charwise
.
In order to use charwise
, simply add
charwise = "*"
to your Cargo.toml
file.
use std::fs::File;
use charwise::Charwise;
fn main() {
// file contains the following data: test contentโ๐
let file = File::open("test.txt").unwrap();
let mut cwf = Charwise::from_file(file);
assert_eq!('t', cwf.next().unwrap().unwrap());
assert_eq!('e', cwf.next().unwrap().unwrap());
assert_eq!('s', cwf.next().unwrap().unwrap());
assert_eq!('t', cwf.next().unwrap().unwrap());
assert_eq!(' ', cwf.next().unwrap().unwrap());
assert_eq!('c', cwf.next().unwrap().unwrap());
assert_eq!('o', cwf.next().unwrap().unwrap());
// peek the next character without reading it
assert_eq!('n', cwf.peek().unwrap().unwrap());
assert_eq!('n', cwf.next().unwrap().unwrap());
assert_eq!('t', cwf.next().unwrap().unwrap());
// peek 4 characters ahead
assert_eq!('โ', cwf.peek_nth(3).unwrap().unwrap());
assert_eq!('e', cwf.next().unwrap().unwrap());
assert_eq!('n', cwf.next().unwrap().unwrap());
assert_eq!('t', cwf.next().unwrap().unwrap());
assert_eq!('โ', cwf.next().unwrap().unwrap());
assert_eq!('๐', cwf.next().unwrap().unwrap());
// end of file
assert!(cwf.next().is_none());
}