1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/*!
# Scanner

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.next_isize() {
            Ok(i) => break i.unwrap_or(0),
            Err(_) => {
                print!("Re-input a: ");
                io::stdout().flush().unwrap();
            }
        }
    }
};

let b = {
    loop {
        match sc.next_isize() {
            Ok(i) => break i.unwrap_or(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!");

assert_eq!(Some(123), sc.next_u8().unwrap());
assert_eq!(Some(456.7), sc.next_f64().unwrap());
assert_eq!(Some(' '), sc.next_char().unwrap());
assert_eq!(Some(' '), sc.next_char().unwrap());
assert_eq!(true, sc.skip_whitespaces().unwrap());
assert_eq!(Some('c'), sc.next_char().unwrap());
assert_eq!(Some("中文字"), sc.next_line().unwrap());
assert_eq!(Some("\tHello world!".into()), sc.next_line().unwrap());
assert_eq!(None, sc.next_line().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());

assert_eq!(Some(123), sc.next_u8().unwrap());
assert_eq!(Some(456.7), sc.next_f64().unwrap());
assert_eq!(Some(' '), sc.next_char().unwrap());
assert_eq!(Some(' '), sc.next_char().unwrap());
assert_eq!(true, sc.skip_whitespaces().unwrap());
assert_eq!(Some('c'), sc.next_char().unwrap());
assert_eq!(Some("中文字".as_bytes()), sc.next_line().unwrap());
assert_eq!(Some("\tHello world!".as_bytes()), sc.next_line().unwrap());
assert_eq!(None, sc.next_line().unwrap());
```

*/

#[macro_use]
extern crate educe;

mod scanner;
mod scanner_ascii;
mod scanner_error;
mod scanner_str;
mod scanner_u8_slice;
mod scanner_u8_slice_ascii;
mod utf8;
mod whitespaces;

pub use scanner::*;
pub use scanner_ascii::*;
pub use scanner_error::*;
pub use scanner_str::*;
pub use scanner_u8_slice::*;
pub use scanner_u8_slice_ascii::*;

const BUFFER_SIZE: usize = 256; // must be bigger than or equal to 4