Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed comment typo. #29

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
target
Cargo.lock
**/*.rs.bk
scripts/tmp
*.pyc
*.txt
/.idea/
/unicode-segmentation.iml
64 changes: 45 additions & 19 deletions src/grapheme.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ impl<'a> Iterator for GraphemeIndices<'a> {

#[inline]
fn next(&mut self) -> Option<(usize, &'a str)> {
self.iter.next().map(|s| (s.as_ptr() as usize - self.start_offset, s))
self.iter
.next()
.map(|s| (s.as_ptr() as usize - self.start_offset, s))
}

#[inline]
Expand All @@ -55,7 +57,9 @@ impl<'a> Iterator for GraphemeIndices<'a> {
impl<'a> DoubleEndedIterator for GraphemeIndices<'a> {
#[inline]
fn next_back(&mut self) -> Option<(usize, &'a str)> {
self.iter.next_back().map(|s| (s.as_ptr() as usize - self.start_offset, s))
self.iter
.next_back()
.map(|s| (s.as_ptr() as usize - self.start_offset, s))
}
}

Expand Down Expand Up @@ -114,7 +118,10 @@ impl<'a> DoubleEndedIterator for Graphemes<'a> {
if end == self.cursor.cur_cursor() {
return None;
}
let prev = self.cursor_back.prev_boundary(self.string, 0).unwrap().unwrap();
let prev = self.cursor_back
.prev_boundary(self.string, 0)
.unwrap()
.unwrap();
Some(&self.string[prev..end])
}
}
Expand All @@ -131,7 +138,10 @@ pub fn new_graphemes<'b>(s: &'b str, is_extended: bool) -> Graphemes<'b> {

#[inline]
pub fn new_grapheme_indices<'b>(s: &'b str, is_extended: bool) -> GraphemeIndices<'b> {
GraphemeIndices { start_offset: s.as_ptr() as usize, iter: new_graphemes(s, is_extended) }
GraphemeIndices {
start_offset: s.as_ptr() as usize,
iter: new_graphemes(s, is_extended),
}
}

// maybe unify with PairResult?
Expand Down Expand Up @@ -198,7 +208,7 @@ pub enum GraphemeIncomplete {
/// current chunk, so the chunk after that is requested. This will only be
/// returned if the chunk ends before the `len` parameter provided on
/// creation of the cursor.
NextChunk, // requesting chunk following the one given
NextChunk, // requesting chunk following the one given

/// An error returned when the chunk given does not contain the cursor position.
InvalidOffset,
Expand All @@ -207,11 +217,11 @@ pub enum GraphemeIncomplete {
// An enum describing the result from lookup of a pair of categories.
#[derive(PartialEq, Eq)]
enum PairResult {
NotBreak, // definitely not a break
Break, // definitely a break
Extended, // a break iff not in extended mode
Regional, // a break if preceded by an even number of RIS
Emoji, // a break if preceded by emoji base and (Extend)*
NotBreak, // definitely not a break
Break, // definitely a break
Extended, // a break if not in extended mode
Regional, // a break if preceded by an even number of RIS
Emoji, // a break if preceded by emoji base and (Extend)*
}

fn check_pair(before: GraphemeCat, after: GraphemeCat) -> PairResult {
Expand Down Expand Up @@ -353,14 +363,14 @@ impl GraphemeCursor {
if self.is_extended && chunk_start + chunk.len() == self.offset {
let ch = chunk.chars().rev().next().unwrap();
if gr::grapheme_category(ch) == gr::GC_Prepend {
self.decide(false); // GB9b
self.decide(false); // GB9b
return;
}
}
match self.state {
GraphemeState::Regional => self.handle_regional(chunk, chunk_start),
GraphemeState::Emoji => self.handle_emoji(chunk, chunk_start),
_ => panic!("invalid state")
_ => panic!("invalid state"),
}
}

Expand Down Expand Up @@ -453,17 +463,21 @@ impl GraphemeCursor {
/// cursor.set_cursor(12);
/// assert_eq!(cursor.is_boundary(flags, 0), Ok(false));
/// ```
pub fn is_boundary(&mut self, chunk: &str, chunk_start: usize) -> Result<bool, GraphemeIncomplete> {
pub fn is_boundary(
&mut self,
chunk: &str,
chunk_start: usize,
) -> Result<bool, GraphemeIncomplete> {
use tables::grapheme as gr;
if self.state == GraphemeState::Break {
return Ok(true)
return Ok(true);
}
if self.state == GraphemeState::NotBreak {
return Ok(false)
return Ok(false);
}
if self.offset < chunk_start || self.offset >= chunk_start + chunk.len() {
if self.offset > chunk_start + chunk.len() || self.cat_after.is_none() {
return Err(GraphemeIncomplete::InvalidOffset)
return Err(GraphemeIncomplete::InvalidOffset);
}
}
if let Some(pre_context_offset) = self.pre_context_offset {
Expand Down Expand Up @@ -543,7 +557,11 @@ impl GraphemeCursor {
/// assert_eq!(cursor.next_boundary(&s[2..4], 2), Ok(Some(4)));
/// assert_eq!(cursor.next_boundary(&s[2..4], 2), Ok(None));
/// ```
pub fn next_boundary(&mut self, chunk: &str, chunk_start: usize) -> Result<Option<usize>, GraphemeIncomplete> {
pub fn next_boundary(
&mut self,
chunk: &str,
chunk_start: usize,
) -> Result<Option<usize>, GraphemeIncomplete> {
use tables::grapheme as gr;
if self.offset == self.len {
return Ok(None);
Expand Down Expand Up @@ -619,7 +637,11 @@ impl GraphemeCursor {
/// assert_eq!(cursor.prev_boundary(&s[0..2], 0), Ok(Some(0)));
/// assert_eq!(cursor.prev_boundary(&s[0..2], 0), Ok(None));
/// ```
pub fn prev_boundary(&mut self, chunk: &str, chunk_start: usize) -> Result<Option<usize>, GraphemeIncomplete> {
pub fn prev_boundary(
&mut self,
chunk: &str,
chunk_start: usize,
) -> Result<Option<usize>, GraphemeIncomplete> {
use tables::grapheme as gr;
if self.offset == 0 {
return Ok(None);
Expand All @@ -638,7 +660,11 @@ impl GraphemeCursor {
self.cat_after = self.cat_before.take();
self.state = GraphemeState::Unknown;
if let Some(ris_count) = self.ris_count {
self.ris_count = if ris_count > 0 { Some(ris_count - 1) } else { None };
self.ris_count = if ris_count > 0 {
Some(ris_count - 1)
} else {
None
};
}
if let Some(prev_ch) = iter.next() {
ch = prev_ch;
Expand Down