aboutsummaryrefslogtreecommitdiff
path: root/utf8parse/src
diff options
context:
space:
mode:
Diffstat (limited to 'utf8parse/src')
-rw-r--r--utf8parse/src/lib.rs50
-rw-r--r--utf8parse/src/table.rs166
-rw-r--r--utf8parse/src/table.rs.in60
-rw-r--r--utf8parse/src/types.rs121
4 files changed, 115 insertions, 282 deletions
diff --git a/utf8parse/src/lib.rs b/utf8parse/src/lib.rs
index 8c866f5..c092647 100644
--- a/utf8parse/src/lib.rs
+++ b/utf8parse/src/lib.rs
@@ -3,15 +3,14 @@
//! This module implements a table-driven UTF-8 parser which should
//! theoretically contain the minimal number of branches (1). The only branch is
//! on the `Action` returned from unpacking a transition.
+#![cfg_attr(all(feature = "nightly", test), feature(test))]
#![no_std]
use core::char;
-mod table;
mod types;
-use table::TRANSITIONS;
-use types::{unpack, Action, State};
+use types::{Action, State};
/// Handles codepoint and invalid sequence events from the parser.
pub trait Receiver {
@@ -48,10 +47,7 @@ impl Parser {
where
R: Receiver,
{
- let cur = self.state as usize;
- let change = TRANSITIONS[cur][byte as usize];
- let (state, action) = unsafe { unpack(change) };
-
+ let (state, action) = self.state.advance(byte);
self.perform_action(receiver, byte, action);
self.state = state;
}
@@ -93,3 +89,43 @@ impl Parser {
}
}
}
+
+#[cfg(all(feature = "nightly", test))]
+mod benches {
+ extern crate std;
+ extern crate test;
+
+ use super::{Parser, Receiver};
+
+ use self::test::{black_box, Bencher};
+
+ static UTF8_DEMO: &[u8] = include_bytes!("../tests/UTF-8-demo.txt");
+
+ impl Receiver for () {
+ fn codepoint(&mut self, c: char) {
+ black_box(c);
+ }
+
+ fn invalid_sequence(&mut self) {}
+ }
+
+ #[bench]
+ fn parse_bench_utf8_demo(b: &mut Bencher) {
+ let mut parser = Parser::new();
+
+ b.iter(|| {
+ for byte in UTF8_DEMO {
+ parser.advance(&mut (), *byte);
+ }
+ })
+ }
+
+ #[bench]
+ fn std_string_parse_utf8(b: &mut Bencher) {
+ b.iter(|| {
+ for c in std::str::from_utf8(UTF8_DEMO).unwrap().chars() {
+ black_box(c);
+ }
+ });
+ }
+}
diff --git a/utf8parse/src/table.rs b/utf8parse/src/table.rs
deleted file mode 100644
index 04c4d34..0000000
--- a/utf8parse/src/table.rs
+++ /dev/null
@@ -1,166 +0,0 @@
-//! UTF-8 Parse Transition Table
-
-/// Transition table for parsing UTF-8. This is built from the grammar described
-/// at https://tools.ietf.org/html/rfc3629#section-4 which I have copied and
-/// formatted below.
-///
-/// # UTF-8 Grammar
-///
-/// ```notrust
-/// UTF8-octets = *( UTF8-char )
-/// UTF8-char = UTF8-1 / UTF8-2 / UTF8-3 / UTF8-4
-/// UTF8-1 = %x00-7F
-/// UTF8-2 = %xC2-DF UTF8-tail
-/// UTF8-3 = %xE0 %xA0-BF UTF8-tail /
-/// %xE1-EC 2( UTF8-tail ) /
-/// %xED %x80-9F UTF8-tail /
-/// %xEE-EF 2( UTF8-tail )
-/// UTF8-4 = %xF0 %x90-BF 2( UTF8-tail ) /
-/// %xF1-F3 3( UTF8-tail ) /
-/// %xF4 %x80-8F 2( UTF8-tail )
-/// UTF8-tail = %x80-BF
-/// ```
-///
-/// Not specifying an action in this table is equivalent to specifying
-/// Action::InvalidSequence. Not specifying a state is equivalent to specifying
-/// state::ground.
-pub static TRANSITIONS: [[u8; 256]; 8] = [
- [
- 16u8, 16u8, 16u8, 16u8, 16u8, 16u8, 16u8, 16u8, 16u8, 16u8, 16u8, 16u8, 16u8, 16u8, 16u8,
- 16u8, 16u8, 16u8, 16u8, 16u8, 16u8, 16u8, 16u8, 16u8, 16u8, 16u8, 16u8, 16u8, 16u8, 16u8,
- 16u8, 16u8, 16u8, 16u8, 16u8, 16u8, 16u8, 16u8, 16u8, 16u8, 16u8, 16u8, 16u8, 16u8, 16u8,
- 16u8, 16u8, 16u8, 16u8, 16u8, 16u8, 16u8, 16u8, 16u8, 16u8, 16u8, 16u8, 16u8, 16u8, 16u8,
- 16u8, 16u8, 16u8, 16u8, 16u8, 16u8, 16u8, 16u8, 16u8, 16u8, 16u8, 16u8, 16u8, 16u8, 16u8,
- 16u8, 16u8, 16u8, 16u8, 16u8, 16u8, 16u8, 16u8, 16u8, 16u8, 16u8, 16u8, 16u8, 16u8, 16u8,
- 16u8, 16u8, 16u8, 16u8, 16u8, 16u8, 16u8, 16u8, 16u8, 16u8, 16u8, 16u8, 16u8, 16u8, 16u8,
- 16u8, 16u8, 16u8, 16u8, 16u8, 16u8, 16u8, 16u8, 16u8, 16u8, 16u8, 16u8, 16u8, 16u8, 16u8,
- 16u8, 16u8, 16u8, 16u8, 16u8, 16u8, 16u8, 16u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8,
- 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8,
- 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8,
- 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8,
- 0u8, 0u8, 0u8, 0u8, 67u8, 67u8, 67u8, 67u8, 67u8, 67u8, 67u8, 67u8, 67u8, 67u8, 67u8, 67u8,
- 67u8, 67u8, 67u8, 67u8, 67u8, 67u8, 67u8, 67u8, 67u8, 67u8, 67u8, 67u8, 67u8, 67u8, 67u8,
- 67u8, 67u8, 67u8, 100u8, 98u8, 98u8, 98u8, 98u8, 98u8, 98u8, 98u8, 98u8, 98u8, 98u8, 98u8,
- 98u8, 101u8, 98u8, 98u8, 118u8, 113u8, 113u8, 113u8, 119u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8,
- 0u8, 0u8, 0u8, 0u8, 0u8,
- ],
- [
- 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8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e7..0000000
--- a/utf8parse/src/table.rs.in
+++ /dev/null
@@ -1,60 +0,0 @@
-//! UTF-8 Parse Transition Table
-
-/// Transition table for parsing UTF-8. This is built from the grammar described
-/// at https://tools.ietf.org/html/rfc3629#section-4 which I have copied and
-/// formatted below.
-///
-/// # UTF-8 Grammar
-///
-/// ```ignore
-/// UTF8-octets = *( UTF8-char )
-/// UTF8-char = UTF8-1 / UTF8-2 / UTF8-3 / UTF8-4
-/// UTF8-1 = %x00-7F
-/// UTF8-2 = %xC2-DF UTF8-tail
-/// UTF8-3 = %xE0 %xA0-BF UTF8-tail /
-/// %xE1-EC 2( UTF8-tail ) /
-/// %xED %x80-9F UTF8-tail /
-/// %xEE-EF 2( UTF8-tail )
-/// UTF8-4 = %xF0 %x90-BF 2( UTF8-tail ) /
-/// %xF1-F3 3( UTF8-tail ) /
-/// %xF4 %x80-8F 2( UTF8-tail )
-/// UTF8-tail = %x80-BF
-/// ```
-///
-/// Not specifying an action in this table is equivalent to specifying
-/// Action::InvalidSequence. Not specifying a state is equivalent to specifying
-/// state::ground.
-pub static TRANSITIONS: [[u8; 256]; 8] = utf8_state_table! {
- State::Ground => {
- 0x00...0x7f => (State::Ground, Action::EmitByte),
- 0xc2...0xdf => (State::Tail1, Action::SetByte2Top),
- 0xe0 => (State::U3_2_e0, Action::SetByte3Top),
- 0xe1...0xec => (State::Tail2, Action::SetByte3Top),
- 0xed => (State::U3_2_ed, Action::SetByte3Top),
- 0xee...0xef => (State::Tail2, Action::SetByte3Top),
- 0xf0 => (State::Utf8_4_3_f0, Action::SetByte4),
- 0xf1...0xf3 => (State::Tail3, Action::SetByte4),
- 0xf4 => (State::Utf8_4_3_f4, Action::SetByte4),
- },
- State::U3_2_e0 => {
- 0xa0...0xbf => (State::Tail1, Action::SetByte2),
- },
- State::U3_2_ed => {
- 0x80...0x9f => (State::Tail1, Action::SetByte2),
- },
- State::Utf8_4_3_f0 => {
- 0x90...0xbf => (State::Tail2, Action::SetByte3),
- },
- State::Utf8_4_3_f4 => {
- 0x80...0x8f => (State::Tail2, Action::SetByte3),
- },
- State::Tail3 => {
- 0x80...0xbf => (State::Tail2, Action::SetByte3),
- },
- State::Tail2 => {
- 0x80...0xbf => (State::Tail1, Action::SetByte2),
- },
- State::Tail1 => {
- 0x80...0xbf => (State::Ground, Action::SetByte1),
- },
-};
diff --git a/utf8parse/src/types.rs b/utf8parse/src/types.rs
index 93607fb..5a70b3c 100644
--- a/utf8parse/src/types.rs
+++ b/utf8parse/src/types.rs
@@ -1,12 +1,31 @@
//! Types supporting the UTF-8 parser
-#![allow(non_camel_case_types)]
-use core::mem;
+
+/// Action to take when receiving a byte
+#[derive(Debug, Copy, Clone)]
+pub enum Action {
+ /// Unexpected byte; sequence is invalid
+ InvalidSequence = 0,
+ /// Received valid 7-bit ASCII byte which can be directly emitted.
+ EmitByte = 1,
+ /// Set the bottom continuation byte
+ SetByte1 = 2,
+ /// Set the 2nd-from-last continuation byte
+ SetByte2 = 3,
+ /// Set the 2nd-from-last byte which is part of a two byte sequence
+ SetByte2Top = 4,
+ /// Set the 3rd-from-last continuation byte
+ SetByte3 = 5,
+ /// Set the 3rd-from-last byte which is part of a three byte sequence
+ SetByte3Top = 6,
+ /// Set the top byte of a four byte sequence.
+ SetByte4 = 7,
+}
/// States the parser can be in.
///
/// There is a state for each initial input of the 3 and 4 byte sequences since
/// the following bytes are subject to different conditions than a tail byte.
-#[allow(dead_code)]
+#[allow(non_camel_case_types)]
#[derive(Debug, Copy, Clone)]
pub enum State {
/// Ground state; expect anything
@@ -33,50 +52,54 @@ impl Default for State {
}
}
-/// Action to take when receiving a byte
-#[allow(dead_code)]
-#[derive(Debug, Copy, Clone)]
-pub enum Action {
- /// Unexpected byte; sequence is invalid
- InvalidSequence = 0,
- /// Received valid 7-bit ASCII byte which can be directly emitted.
- EmitByte = 1,
- /// Set the bottom continuation byte
- SetByte1 = 2,
- /// Set the 2nd-from-last continuation byte
- SetByte2 = 3,
- /// Set the 2nd-from-last byte which is part of a two byte sequence
- SetByte2Top = 4,
- /// Set the 3rd-from-last continuation byte
- SetByte3 = 5,
- /// Set the 3rd-from-last byte which is part of a three byte sequence
- SetByte3Top = 6,
- /// Set the top byte of a four byte sequence.
- SetByte4 = 7,
-}
-
-/// Convert a state and action to a u8
-///
-/// State will be the bottom 4 bits and action the top 4
-#[inline]
-#[allow(dead_code)]
-pub fn pack(state: State, action: Action) -> u8 {
- ((action as u8) << 4) | (state as u8)
-}
-
-/// Convert a u8 to a state and action
-///
-/// # Unsafety
-///
-/// If this function is called with a byte that wasn't encoded with the `pack`
-/// function in this module, there is no guarantee that a valid state and action
-/// can be produced.
-#[inline]
-pub unsafe fn unpack(val: u8) -> (State, Action) {
- (
- // State is stored in bottom 4 bits
- mem::transmute(val & 0x0f),
- // Action is stored in top 4 bits
- mem::transmute(val >> 4),
- )
+impl State {
+ /// Advance the parser state.
+ ///
+ /// This takes the current state and input byte into consideration, to determine the next state
+ /// and any action that should be taken.
+ #[inline]
+ pub fn advance(&self, byte: u8) -> (State, Action) {
+ match self {
+ State::Ground => match byte {
+ 0x00..=0x7f => (State::Ground, Action::EmitByte),
+ 0xc2..=0xdf => (State::Tail1, Action::SetByte2Top),
+ 0xe0 => (State::U3_2_e0, Action::SetByte3Top),
+ 0xe1..=0xec => (State::Tail2, Action::SetByte3Top),
+ 0xed => (State::U3_2_ed, Action::SetByte3Top),
+ 0xee..=0xef => (State::Tail2, Action::SetByte3Top),
+ 0xf0 => (State::Utf8_4_3_f0, Action::SetByte4),
+ 0xf1..=0xf3 => (State::Tail3, Action::SetByte4),
+ 0xf4 => (State::Utf8_4_3_f4, Action::SetByte4),
+ _ => (State::Ground, Action::InvalidSequence),
+ },
+ State::U3_2_e0 => match byte {
+ 0xa0..=0xbf => (State::Tail1, Action::SetByte2),
+ _ => (State::Ground, Action::InvalidSequence),
+ },
+ State::U3_2_ed => match byte {
+ 0x80..=0x9f => (State::Tail1, Action::SetByte2),
+ _ => (State::Ground, Action::InvalidSequence),
+ },
+ State::Utf8_4_3_f0 => match byte {
+ 0x90..=0xbf => (State::Tail2, Action::SetByte3),
+ _ => (State::Ground, Action::InvalidSequence),
+ },
+ State::Utf8_4_3_f4 => match byte {
+ 0x80..=0x8f => (State::Tail2, Action::SetByte3),
+ _ => (State::Ground, Action::InvalidSequence),
+ },
+ State::Tail3 => match byte {
+ 0x80..=0xbf => (State::Tail2, Action::SetByte3),
+ _ => (State::Ground, Action::InvalidSequence),
+ },
+ State::Tail2 => match byte {
+ 0x80..=0xbf => (State::Tail1, Action::SetByte2),
+ _ => (State::Ground, Action::InvalidSequence),
+ },
+ State::Tail1 => match byte {
+ 0x80..=0xbf => (State::Ground, Action::SetByte1),
+ _ => (State::Ground, Action::InvalidSequence),
+ },
+ }
+ }
}