Skip to content

Implement a special-case lookup for ascii grapheme categories. #79

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

Merged
merged 2 commits into from
Feb 14, 2020
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 23 additions & 5 deletions src/grapheme.rs
Original file line number Diff line number Diff line change
Expand Up @@ -284,12 +284,30 @@ impl GraphemeCursor {

fn grapheme_category(&mut self, ch: char) -> GraphemeCat {
use tables::grapheme as gr;
// If this char isn't within the cached range, update the cache to the
// range that includes it.
if (ch as u32) < self.grapheme_cat_cache.0 || (ch as u32) > self.grapheme_cat_cache.1 {
self.grapheme_cat_cache = gr::grapheme_category(ch);
use tables::grapheme::GraphemeCat::*;

if ch <= '\u{7e}' {
// Special-case optimization for ascii, except U+007F. This
// improves performance even for many primarily non-ascii texts,
// due to use of punctuation and white space characters from the
// ascii range.
if ch >= '\u{20}' {
GC_Any
} else if ch == '\u{a}' {
GC_LF
} else if ch == '\u{d}' {
GC_CR
} else {
GC_Control
}
} else {
// If this char isn't within the cached range, update the cache to the
// range that includes it.
if (ch as u32) < self.grapheme_cat_cache.0 || (ch as u32) > self.grapheme_cat_cache.1 {
self.grapheme_cat_cache = gr::grapheme_category(ch);
}
self.grapheme_cat_cache.2
}
self.grapheme_cat_cache.2
}

// Not sure I'm gonna keep this, the advantage over new() seems thin.
Expand Down