Repository navigation
New cursor-based implementation of grapheme clusters - #23
Merged
Merged
Conversation
Very much work in progress. See unicode-rs#21
Implemented next_boundary and prev_boundary functions in terms of is_boundary (plus fixups to the internal state when moving the cursor). Fixed various problems in previous commit. Still work in progress, not tested yet.
Also, additional state machine work, mostly resume logic in next and prev grapheme cluster boundary methods. Includes some documentation, and also a bit of renaming from the earlier development drafts.
This adds a test case for unicode-rs#19 (which was a mismatch between forward and reverse iterators in the original codebase).
Contributor
Author
|
Note: this fixes #19 (which, I believe, had an incorrect forward iterator in the old codebase, because it was left in the Emoji state after the first fitzpatrick skin tone modifier). |
Manishearth
approved these changes
Mar 13, 2017
Manishearth
left a comment
Member
There was a problem hiding this comment.
Overall looks good. Would like more docs, especially on API usage for the cursor stuff.
| Extended, // a break if not in extended mode | ||
| CheckCrlf, // a break unless it's a CR LF pair | ||
| Regional, // a break if preceded by an even number of RIS | ||
| Emoji, // a break if preceded by emoji base and extend |
Contributor
Author
There was a problem hiding this comment.
I changed this to read "a break iff not in extended mode" which I think is more precise. What you suggest is the converse of what I originally said, but both are true.
| state: GraphemeState, | ||
| cat_before: Option<GraphemeCat>, // category of codepoint immediately preceding cursor | ||
| cat_after: Option<GraphemeCat>, // category of codepoint immediately after cursor | ||
| pre_context_offset: Option<usize>, |
Member
There was a problem hiding this comment.
nit: needs more comments on how this works
| idx = idx + self.string[idx..].chars().next().unwrap().len_utf8(); | ||
| None | ||
| impl GraphemeCursor { | ||
| /// Create a new cursor. The string and initial offset are given at creation |
Member
There was a problem hiding this comment.
nit: Examples on how the whole cursor API is supposed to be used?
Member
Some of the doc tests also encouraged me to tweak the implementation.
The existing code treated CR and LF as special cases of the Control grapheme category, for reasons that weren't very good. This patch gets rid of that and just handles GB3 in the pair lookup. That should improve performance in the rope case, as it will cut down on the amount of pre-context requested when a chunk begins with LF.
Member
|
Merged! Thanks so much for working on this! |
This was referenced Jan 11, 2019
Open
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This is a draft of the implementation for #21. It should be useful both for random access (determining the previous or next boundary from any starting offset) and for dealing with noncontiguous string representations such as ropes.
I'd probably want to add doc examples. Also, testing of the cursor-based API is inadequate, it's only tested through the existing iterators (which are implemented in terms of cursors). But I think it's complete enough to take a look at.
In its current form, this patch is not suitable for purely streaming use cases (as described in #7), as incomplete chunk input may cause a
PreContextreturn even when enough input has already been provided. It would be possible to add more state to the cursor, at a cost of some additional complexity. However, this implementation should at least avoid quadratic behavior when moving the cursor through a sequence of flags.