scanner_rust/lib.rs
1/*!
2# Scanner
3
4This crate provides Java-like Scanners which can parse primitive types and strings using UTF-8 or ASCII.
5
6### Scan a stream
7
8`Scanner` or `ScannerAscii` can be used for reading strings or raw data from a stream.
9
10```rust
11use std::io::{self, Write};
12
13use scanner_rust::ScannerAscii;
14
15print!("Please input two integers, a and b: ");
16io::stdout().flush().unwrap();
17
18let mut sc = ScannerAscii::new(io::stdin());
19
20let a = {
21 loop {
22 match sc.next_isize() {
23 Ok(i) => break i.unwrap_or(0),
24 Err(_) => {
25 print!("Re-input a: ");
26 io::stdout().flush().unwrap();
27 }
28 }
29 }
30};
31
32let b = {
33 loop {
34 match sc.next_isize() {
35 Ok(i) => break i.unwrap_or(0),
36 Err(_) => {
37 print!("Re-input b: ");
38 io::stdout().flush().unwrap();
39 }
40 }
41 }
42};
43
44println!("{} + {} = {}", a, b, a + b);
45```
46
47Besides, the `drop_next` and `drop_next_line` methods are useful when you want to skip some data.
48
49The default buffer size is 256 bytes. If you want to change that, you can use the `new2` associated function or the `scan_path2` associated function and define a length explicitly to create an instance of the above structs.
50
51For example, to change the buffer size to 64 bytes,
52
53```rust
54use scanner_rust::generic_array::typenum::U64;
55use scanner_rust::Scanner;
56
57let mut sc: Scanner<_, U64> = Scanner::scan_path2("Cargo.toml").unwrap();
58```
59
60### Scan a string slice (`&str`)
61
62`ScannerStr` can be used for reading strings from a string slice.
63
64```rust
65use std::io::{self, Write};
66
67use scanner_rust::ScannerStr;
68
69let mut sc = ScannerStr::new(" 123 456.7 \t\r\n\n c中文字\n\tHello world!");
70
71assert_eq!(Some(123), sc.next_u8().unwrap());
72assert_eq!(Some(456.7), sc.next_f64().unwrap());
73assert_eq!(Some(' '), sc.next_char().unwrap());
74assert_eq!(Some(' '), sc.next_char().unwrap());
75assert_eq!(true, sc.skip_whitespaces().unwrap());
76assert_eq!(Some('c'), sc.next_char().unwrap());
77assert_eq!(Some("中文字"), sc.next_line().unwrap());
78assert_eq!(Some("\tHello world!".into()), sc.next_line().unwrap());
79assert_eq!(None, sc.next_line().unwrap());
80```
81
82### Scan a u8 slice
83
84`ScannerU8Slice` or `ScannerU8SliceAscii` can be used for reading raw data from a `u8` slice.
85
86```rust
87use std::io::{self, Write};
88
89use scanner_rust::ScannerU8Slice;
90
91let mut sc = ScannerU8Slice::new(" 123 456.7 \t\r\n\n c中文字\n\tHello world!".as_bytes());
92
93assert_eq!(Some(123), sc.next_u8().unwrap());
94assert_eq!(Some(456.7), sc.next_f64().unwrap());
95assert_eq!(Some(' '), sc.next_char().unwrap());
96assert_eq!(Some(' '), sc.next_char().unwrap());
97assert_eq!(true, sc.skip_whitespaces().unwrap());
98assert_eq!(Some('c'), sc.next_char().unwrap());
99assert_eq!(Some("中文字".as_bytes()), sc.next_line().unwrap());
100assert_eq!(Some("\tHello world!".as_bytes()), sc.next_line().unwrap());
101assert_eq!(None, sc.next_line().unwrap());
102```
103
104*/
105
106pub extern crate generic_array;
107
108#[macro_use]
109extern crate educe;
110
111mod scanner;
112mod scanner_ascii;
113mod scanner_error;
114mod scanner_str;
115mod scanner_u8_slice;
116mod scanner_u8_slice_ascii;
117mod whitespaces;
118
119pub use scanner::*;
120pub use scanner_ascii::*;
121pub use scanner_error::*;
122pub use scanner_str::*;
123pub use scanner_u8_slice::*;
124pub use scanner_u8_slice_ascii::*;