char_stream

Crates.iochar_stream
lib.rschar_stream
version0.1.8
sourcesrc
created_at2018-01-01 05:20:40.718846
updated_at2018-01-02 12:52:44.824337
descriptionUnified character reading interface to str, String, bytes, File and Stdin for Rust language.
homepage
repositoryhttps://github.com/JunSuzukiJapan/char_stream
max_upload_size
id45095
size31,637
(JunSuzukiJapan)

documentation

README

char_stream

Unified character reading interface to str, String, bytes, File and Stdin for Rust language.

Installation

In Cargo.toml:

[dependencies]
char_stream = "*"

Examples

for str

use char_stream::CharStream;

let mut stream = CharStream::from("Hello 世界❤");

assert_eq!('H', stream.next().unwrap());
assert_eq!('e', stream.next().unwrap());
assert_eq!('l', stream.next().unwrap());
assert_eq!('l', stream.next().unwrap());
assert_eq!('o', stream.next().unwrap());
assert_eq!(' ', stream.next().unwrap());
assert_eq!('世', stream.next().unwrap());
assert_eq!('界', stream.next().unwrap());
assert_eq!('❤', stream.next().unwrap());
assert_eq!(None, stream.next());

for String

use char_stream::CharStream;

let s = String::from("Hello 世界❤");
let mut stream = CharStream::from_string(s);

assert_eq!('H', stream.next().unwrap());
assert_eq!('e', stream.next().unwrap());
assert_eq!('l', stream.next().unwrap());
assert_eq!('l', stream.next().unwrap());
assert_eq!('o', stream.next().unwrap());
assert_eq!(' ', stream.next().unwrap());
assert_eq!('世', stream.next().unwrap());
assert_eq!('界', stream.next().unwrap());
assert_eq!('❤', stream.next().unwrap());
assert_eq!(None, stream.next());

for bytes

use char_stream::CharStream;

let bytes: [u8; 15] = [72, 101, 108, 108, 111, 32, 228, 184, 150, 231, 149, 140, 226, 157, 164];
if let Ok(mut stream) = CharStream::from_bytes(&bytes) {
    assert_eq!('H', stream.next().unwrap());
    assert_eq!('e', stream.next().unwrap());
    assert_eq!('l', stream.next().unwrap());
    assert_eq!('l', stream.next().unwrap());
    assert_eq!('o', stream.next().unwrap());
    assert_eq!(' ', stream.next().unwrap());
    assert_eq!('世', stream.next().unwrap());
    assert_eq!('界', stream.next().unwrap());
    assert_eq!('❤', stream.next().unwrap());
    assert_eq!(None, stream.next());
}

for File

extern crate tempfile;
extern crate char_stream;

use std::io::prelude::*;
use std::io::{Seek, SeekFrom};
use std::fs::File;
use char_stream::CharStream;

fn main(){
    let test_data = "Hello\n 世界❤";

    // write test data to tempfile
    let mut tmpfile: File = tempfile::tempfile().unwrap();
    tmpfile.write_all(test_data.as_bytes()).unwrap();

    // Seek to start
    tmpfile.seek(SeekFrom::Start(0)).unwrap();

    // read test data from tempfile
    let mut stream = CharStream::from_file(tmpfile);

    assert_eq!('H', stream.next().unwrap());
    assert_eq!('e', stream.next().unwrap());
    assert_eq!('l', stream.next().unwrap());
    assert_eq!('l', stream.next().unwrap());
    assert_eq!('o', stream.next().unwrap());
    assert_eq!('\n', stream.next().unwrap());
    assert_eq!(' ', stream.next().unwrap());
    assert_eq!('世', stream.next().unwrap());
    assert_eq!('界', stream.next().unwrap());
    assert_eq!('❤', stream.next().unwrap());
    assert_eq!(None, stream.next());
}

for Stdin

extern crate char_stream;

use char_stream::CharStream;

fn main() {
     let mut stream = CharStream::from_stdin();
     while let Some(ch) = stream.next() {
         println!("ch: {}", ch);
     }
}

reverse str

extern crate char_stream;
use char_stream::CharStream;

fn main() {
    let input = "stressed";
    let stream = CharStream::from(input);
    let rev_stream = stream.wend_iter().rev();
    let result: String = rev_stream.collect();
    println!("'{}' reverse to '{}'", input , result);
}
Commit count: 29

cargo fmt