aboutsummaryrefslogtreecommitdiff
path: root/src/grapheme.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/grapheme.rs')
-rw-r--r--src/grapheme.rs30
1 files changed, 19 insertions, 11 deletions
diff --git a/src/grapheme.rs b/src/grapheme.rs
index e95d478..190b86e 100644
--- a/src/grapheme.rs
+++ b/src/grapheme.rs
@@ -10,7 +10,7 @@
use core::cmp;
-use tables::grapheme::GraphemeCat;
+use crate::tables::grapheme::GraphemeCat;
/// External iterator for grapheme clusters and byte offsets.
///
@@ -73,7 +73,7 @@ impl<'a> DoubleEndedIterator for GraphemeIndices<'a> {
///
/// [`graphemes`]: trait.UnicodeSegmentation.html#tymethod.graphemes
/// [`UnicodeSegmentation`]: trait.UnicodeSegmentation.html
-#[derive(Clone)]
+#[derive(Clone, Debug)]
pub struct Graphemes<'a> {
string: &'a str,
cursor: GraphemeCursor,
@@ -148,7 +148,7 @@ pub fn new_grapheme_indices<'b>(s: &'b str, is_extended: bool) -> GraphemeIndice
// maybe unify with PairResult?
// An enum describing information about a potential boundary.
-#[derive(PartialEq, Eq, Clone)]
+#[derive(PartialEq, Eq, Clone, Debug)]
enum GraphemeState {
// No information is known.
Unknown,
@@ -165,7 +165,7 @@ enum GraphemeState {
}
/// Cursor-based segmenter for grapheme clusters.
-#[derive(Clone)]
+#[derive(Clone, Debug)]
pub struct GraphemeCursor {
// Current cursor position.
offset: usize,
@@ -228,8 +228,9 @@ enum PairResult {
Emoji, // a break if preceded by emoji base and (Extend)*
}
+#[inline]
fn check_pair(before: GraphemeCat, after: GraphemeCat) -> PairResult {
- use tables::grapheme::GraphemeCat::*;
+ use crate::tables::grapheme::GraphemeCat::*;
use self::PairResult::*;
match (before, after) {
(GC_CR, GC_LF) => NotBreak, // GB3
@@ -295,8 +296,8 @@ impl GraphemeCursor {
}
fn grapheme_category(&mut self, ch: char) -> GraphemeCat {
- use tables::grapheme as gr;
- use tables::grapheme::GraphemeCat::*;
+ use crate::tables::grapheme as gr;
+ use crate::tables::grapheme::GraphemeCat::*;
if ch <= '\u{7e}' {
// Special-case optimization for ascii, except U+007F. This
@@ -387,7 +388,7 @@ impl GraphemeCursor {
/// assert_eq!(cursor.is_boundary(&flags[8..], 8), Ok(true));
/// ```
pub fn provide_context(&mut self, chunk: &str, chunk_start: usize) {
- use tables::grapheme as gr;
+ use crate::tables::grapheme as gr;
assert!(chunk_start + chunk.len() == self.pre_context_offset.unwrap());
self.pre_context_offset = None;
if self.is_extended && chunk_start + chunk.len() == self.offset {
@@ -407,6 +408,7 @@ impl GraphemeCursor {
}
}
+ #[inline]
fn decide(&mut self, is_break: bool) {
self.state = if is_break {
GraphemeState::Break
@@ -415,11 +417,13 @@ impl GraphemeCursor {
};
}
+ #[inline]
fn decision(&mut self, is_break: bool) -> Result<bool, GraphemeIncomplete> {
self.decide(is_break);
Ok(is_break)
}
+ #[inline]
fn is_boundary_result(&self) -> Result<bool, GraphemeIncomplete> {
if self.state == GraphemeState::Break {
Ok(true)
@@ -432,8 +436,9 @@ impl GraphemeCursor {
}
}
+ #[inline]
fn handle_regional(&mut self, chunk: &str, chunk_start: usize) {
- use tables::grapheme as gr;
+ use crate::tables::grapheme as gr;
let mut ris_count = self.ris_count.unwrap_or(0);
for ch in chunk.chars().rev() {
if self.grapheme_category(ch) != gr::GC_Regional_Indicator {
@@ -452,8 +457,9 @@ impl GraphemeCursor {
self.state = GraphemeState::Regional;
}
+ #[inline]
fn handle_emoji(&mut self, chunk: &str, chunk_start: usize) {
- use tables::grapheme as gr;
+ use crate::tables::grapheme as gr;
let mut iter = chunk.chars().rev();
if let Some(ch) = iter.next() {
if self.grapheme_category(ch) != gr::GC_ZWJ {
@@ -482,6 +488,7 @@ impl GraphemeCursor {
self.state = GraphemeState::Emoji;
}
+ #[inline]
/// Determine whether the current cursor location is a grapheme cluster boundary.
/// Only a part of the string need be supplied. If `chunk_start` is nonzero or
/// the length of `chunk` is not equal to `len` on creation, then this method
@@ -506,7 +513,7 @@ impl GraphemeCursor {
/// assert_eq!(cursor.is_boundary(flags, 0), Ok(false));
/// ```
pub fn is_boundary(&mut self, chunk: &str, chunk_start: usize) -> Result<bool, GraphemeIncomplete> {
- use tables::grapheme as gr;
+ use crate::tables::grapheme as gr;
if self.state == GraphemeState::Break {
return Ok(true)
}
@@ -563,6 +570,7 @@ impl GraphemeCursor {
}
}
+ #[inline]
/// Find the next boundary after the current cursor position. Only a part of
/// the string need be supplied. If the chunk is incomplete, then this
/// method might return `GraphemeIncomplete::PreContext` or