diff options
author | Christian Duerr <contact@christianduerr.com> | 2019-11-23 02:01:09 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-11-23 02:01:09 +0100 |
commit | ea940fcb74abce67b927788e4f9f64fc63073d37 (patch) | |
tree | cb0c9db4008e5fec07c9655dea29513614f5f7b9 /utf8parse/src/lib.rs | |
parent | a035f334a108f4a694cf022e063edb9c8ac349ad (diff) | |
download | r-alacritty-vte-ea940fcb74abce67b927788e4f9f64fc63073d37.tar.gz r-alacritty-vte-ea940fcb74abce67b927788e4f9f64fc63073d37.tar.bz2 r-alacritty-vte-ea940fcb74abce67b927788e4f9f64fc63073d37.zip |
Update to Rust 2018
This moves all crates in the workspace to the latest Rust standard and
resolves various style and formatting issues.
Fixes #32.
Diffstat (limited to 'utf8parse/src/lib.rs')
-rw-r--r-- | utf8parse/src/lib.rs | 64 |
1 files changed, 10 insertions, 54 deletions
diff --git a/utf8parse/src/lib.rs b/utf8parse/src/lib.rs index 3a8ca2f..8c866f5 100644 --- a/utf8parse/src/lib.rs +++ b/utf8parse/src/lib.rs @@ -7,16 +7,16 @@ use core::char; +mod table; mod types; -use self::types::{State, Action, unpack}; -mod table; -use self::table::TRANSITIONS; +use table::TRANSITIONS; +use types::{unpack, Action, State}; /// Handles codepoint and invalid sequence events from the parser. pub trait Receiver { /// Called whenever a codepoint is parsed successfully - fn codepoint(&mut self, char); + fn codepoint(&mut self, _: char); /// Called when an invalid_sequence is detected fn invalid_sequence(&mut self); @@ -25,6 +25,7 @@ pub trait Receiver { /// A parser for Utf8 Characters /// /// Repeatedly call `advance` with bytes to emit Utf8 characters +#[derive(Default)] pub struct Parser { point: u32, state: State, @@ -36,10 +37,7 @@ const CONTINUATION_MASK: u8 = 0b0011_1111; impl Parser { /// Create a new Parser pub fn new() -> Parser { - Parser { - point: 0, - state: State::Ground, - } + Parser { point: 0, state: State::Ground } } /// Advance the parser @@ -47,7 +45,8 @@ impl Parser { /// The provider receiver will be called whenever a codepoint is completed or an invalid /// sequence is detected. pub fn advance<R>(&mut self, receiver: &mut R, byte: u8) - where R: Receiver + where + R: Receiver, { let cur = self.state as usize; let change = TRANSITIONS[cur][byte as usize]; @@ -58,7 +57,8 @@ impl Parser { } fn perform_action<R>(&mut self, receiver: &mut R, byte: u8, action: Action) - where R: Receiver + where + R: Receiver, { match action { Action::InvalidSequence => { @@ -93,47 +93,3 @@ impl Parser { } } } - -#[cfg(test)] -#[macro_use] -extern crate std; - -#[cfg(test)] -mod tests { - use std::io::Read; - use std::fs::File; - use std::string::String; - use Receiver; - use Parser; - - impl Receiver for String { - fn codepoint(&mut self, c: char) { - self.push(c); - } - - fn invalid_sequence(&mut self) { - } - } - - #[test] - fn utf8parse_test() { - let mut buffer = String::new(); - let mut file = File::open("src/UTF-8-demo.txt").unwrap(); - let mut parser = Parser::new(); - - // read the file to a buffer - file.read_to_string(&mut buffer).expect("Reading file to string"); - - // standard library implementation - let expected = String::from_utf8(buffer.as_bytes().to_vec()).unwrap(); - - // utf8parse implementation - let mut actual = String::new(); - - for byte in buffer.as_bytes().to_vec() { - parser.advance(&mut actual, byte) - } - - assert_eq!(actual, expected); - } -} |