Skip to main content

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 and b: ");
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::Scanner;
55
56let mut sc: Scanner<_, 64> = Scanner::scan_path2("Cargo.toml").unwrap();
57```
58
59### Scan a string slice (`&str`)
60
61`ScannerStr` can be used for reading strings from a string slice.
62
63```rust
64use std::io::{self, Write};
65
66use scanner_rust::ScannerStr;
67
68let mut sc = ScannerStr::new(" 123   456.7    \t\r\n\n c中文字\n\tHello world!");
69
70assert_eq!(Some(123), sc.next_u8().unwrap());
71assert_eq!(Some(456.7), sc.next_f64().unwrap());
72assert_eq!(Some(' '), sc.next_char().unwrap());
73assert_eq!(Some(' '), sc.next_char().unwrap());
74assert_eq!(true, sc.skip_whitespaces().unwrap());
75assert_eq!(Some('c'), sc.next_char().unwrap());
76assert_eq!(Some("中文字"), sc.next_line().unwrap());
77assert_eq!(Some("\tHello world!"), sc.next_line().unwrap());
78assert_eq!(None, sc.next_line().unwrap());
79```
80
81### Scan a u8 slice
82
83`ScannerU8Slice` or `ScannerU8SliceAscii` can be used for reading raw data from a `u8` slice.
84
85```rust
86use std::io::{self, Write};
87
88use scanner_rust::ScannerU8Slice;
89
90let mut sc = ScannerU8Slice::new(" 123   456.7    \t\r\n\n c中文字\n\tHello world!".as_bytes());
91
92assert_eq!(Some(123), sc.next_u8().unwrap());
93assert_eq!(Some(456.7), sc.next_f64().unwrap());
94assert_eq!(Some(' '), sc.next_char().unwrap());
95assert_eq!(Some(' '), sc.next_char().unwrap());
96assert_eq!(true, sc.skip_whitespaces().unwrap());
97assert_eq!(Some('c'), sc.next_char().unwrap());
98assert_eq!(Some("中文字".as_bytes()), sc.next_line().unwrap());
99assert_eq!(Some("\tHello world!".as_bytes()), sc.next_line().unwrap());
100assert_eq!(None, sc.next_line().unwrap());
101```
102
103*/
104
105mod kmp;
106mod scanner;
107mod scanner_ascii;
108mod scanner_error;
109mod scanner_str;
110mod scanner_u8_slice;
111mod scanner_u8_slice_ascii;
112mod whitespaces;
113
114pub use scanner::*;
115pub use scanner_ascii::*;
116pub use scanner_error::*;
117pub use scanner_str::*;
118pub use scanner_u8_slice::*;
119pub use scanner_u8_slice_ascii::*;