charwise 1.0.1

This lightweight, dependency-free rust library provides a convenient way to read characters from different resources.
Documentation
  • Coverage
  • 55.56%
    5 out of 9 items documented0 out of 8 items with examples
  • Size
  • Source code size: 17.34 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 2.08 MB This is the summed size of all files generated by rustdoc for all configured targets
  • ร˜ build duration
  • this release: 9s Average build duration of successful builds.
  • all releases: 9s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • Homepage
  • ZeroBone/charwise
    1 0 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • ZeroBone

charwise

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:

  • Buffering of characters.
  • Peeking next characters without consuming them.

All these features are implemented in charwise.

Installation

In order to use charwise, simply add

charwise = "*"

to your Cargo.toml file.

Example

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());

}