#white-space #input #read-input #read #text-input #text-parser #scanf

bin+lib whiteread

Crate for easily reading whitespace-separated values from strings or input

7 releases (4 breaking)

Uses old Rust 2015

0.5.0 Nov 29, 2019
0.4.4 Nov 19, 2017
0.4.3 May 23, 2017
0.3.0 Jun 10, 2016
0.1.0 Jun 3, 2015

#6 in #read-input

Download history 154/week @ 2023-12-17 122/week @ 2023-12-24 60/week @ 2023-12-31 143/week @ 2024-01-07 195/week @ 2024-01-14 132/week @ 2024-01-21 78/week @ 2024-01-28 89/week @ 2024-02-04 131/week @ 2024-02-11 157/week @ 2024-02-18 170/week @ 2024-02-25 161/week @ 2024-03-03 144/week @ 2024-03-10 196/week @ 2024-03-17 267/week @ 2024-03-24 363/week @ 2024-03-31

1,009 downloads per month
Used in 3 crates

MIT license

49KB
772 lines

whiteread

Build Status

Yet another crate for easily reading values from strings or input.

It was made to mimic cin >> functionality and to be usable for parsing text input in format used in algorithmic contests.

Documentation (0.5.0)

Crate

Changelog

Features:

  • Function-based interface (as opposed to macro-based one).
  • Simple: only whitespace can separate values, hence name. (so it's not a general solution for parsing arbitrary data).
  • Parsing in newline-agnostic mode (just like cin >>). (Line-aware mode is also supported)
  • Easy detection of end of input.
  • Proper (Result, not panics) handling of errors.
  • No unnecessary allocs and locks.
  • "One-file" copy-pastable implementation with no dependencies. This crate uses modules, but you can use cargo run to generate a concatenated single-file template (see below).

Examples

Reading an integer from stdin:

let x: i32 = parse_line()?;

Tuples and vectors (nest everything as you like)!

let tup: (i32, f64) = parse_string("  5  3.14 ")?;
let v: Vec<(String, u8)> = parse_string("one 1 two 2 three 3")?;

Wrapping StdinLock for non-line-based parsing...

let mut i = Reader::from_stdin_naive();

// (almost) equivalent to scanf("%d%d", &a, &b) or cin >> a >> b
let (a, b): (i32, i32) = i.parse()?;

...or just for speed (the line-buffer will be allocated just once):

while let Some((x, y)) = i.line::<Option<(usize, f32)>>()? {
	println!("{} {}", y, x);
}

Reading a file (can also use Reader for more control):

let number: i32 = parse_file("number.txt")?;

On failure, a rendered error will be provided by default, even when unwrapping, eg.:

Error: excessive input provided at
6 | hello world 1 2 3
                ^

Installation

cargo add whiteread or add this to your Cargo.toml:

[dependencies]
whiteread = "0.5.0"

Minimal supported Rust version is 1.18.

Using in non-Cargo environment

If you want to use this crate where cargo is unavailable, whiteread can be squished into single file. Here's how to generate a template containing a whiteread module:

$ cargo install whiteread
$ whiteread-template > my_file.rs

Alternatively, you can clone this repository and just cargo run.

No runtime deps