Scanner

Build Status

This crate provides Java-like Scanners which can parse primitive types and strings using UTF-8 or ASCII.

Scan a stream

Scanner or ScannerAscii can be used for reading strings or raw data from a stream.

```rust extern crate scanner_rust;

use std::io::{self, Write};

use scanner_rust::ScannerAscii;

print!("Please input two integers, a and b: "); io::stdout().flush().unwrap();

let mut sc = ScannerAscii::new(io::stdin());

let a = { loop { match sc.nextisize() { Ok(i) => break i.unwrapor(0), Err(_) => { print!("Re-input a: "); io::stdout().flush().unwrap(); } } } };

let b = { loop { match sc.nextisize() { Ok(i) => break i.unwrapor(0), Err(_) => { print!("Re-input b: "); io::stdout().flush().unwrap(); } } } };

println!("{} + {} = {}", a, b, a + b); ```

Besides, the drop_next and drop_next_line methods are useful when you want to skip some data.

Scan a string slice (&str)

ScannerStr can be used for reading strings from a string slice.

```rust extern crate scanner_rust;

use std::io::{self, Write};

use scanner_rust::ScannerStr;

let mut sc = ScannerStr::new(" 123 456.7 \t\r\n\n c中文字\n\tHello world!");

asserteq!(Some(123), sc.nextu8().unwrap()); asserteq!(Some(456.7), sc.nextf64().unwrap()); asserteq!(Some(' '), sc.nextchar().unwrap()); asserteq!(Some(' '), sc.nextchar().unwrap()); asserteq!(true, sc.skipwhitespaces().unwrap()); asserteq!(Some('c'), sc.nextchar().unwrap()); asserteq!(Some("中文字"), sc.nextline().unwrap()); asserteq!(Some("\tHello world!".into()), sc.nextline().unwrap()); asserteq!(None, sc.nextline().unwrap()); ```

Scan a u8 slice

ScannerU8Slice or ScannerU8SliceAscii can be used for reading raw data from a u8 slice.

```rust extern crate scanner_rust;

use std::io::{self, Write};

use scanner_rust::ScannerU8Slice;

let mut sc = ScannerU8Slice::new(" 123 456.7 \t\r\n\n c中文字\n\tHello world!".as_bytes());

asserteq!(Some(123), sc.nextu8().unwrap()); asserteq!(Some(456.7), sc.nextf64().unwrap()); asserteq!(Some(' '), sc.nextchar().unwrap()); asserteq!(Some(' '), sc.nextchar().unwrap()); asserteq!(true, sc.skipwhitespaces().unwrap()); asserteq!(Some('c'), sc.nextchar().unwrap()); asserteq!(Some("中文字".asbytes()), sc.nextline().unwrap()); asserteq!(Some("\tHello world!".asbytes()), sc.nextline().unwrap()); asserteq!(None, sc.nextline().unwrap()); ```

Crates.io

https://crates.io/crates/scanner-rust

Documentation

https://docs.rs/scanner-rust

License

MIT