-
Notifications
You must be signed in to change notification settings - Fork 42
Add new normalization algorithms using Standardized Variants #70
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
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
d1ad2ac
Add new normalization algorithms using Standardized Variants
sunfishcode d0c3706
Switch to a more explicit API.
sunfishcode 41dc717
Don't decompose Hangul in the `svar` iterator.
sunfishcode 5aca91b
Avoid saying "non-standard" in a comment.
sunfishcode fea4f13
Rename `svar` to `cjk_compat_variants`.
sunfishcode 485e9e7
Use `ArrayVec` to panic instead of resizing on overflow.
sunfishcode 0d31e1e
Fix the CJK Compat Variants decomp stats string.
sunfishcode File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
// Copyright 2012-2015 The Rust Project Developers. See the COPYRIGHT | ||
// file at the top-level directory of this distribution and at | ||
// http://rust-lang.org/COPYRIGHT. | ||
// | ||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or | ||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | ||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your | ||
// option. This file may not be copied, modified, or distributed | ||
// except according to those terms. | ||
use core::fmt::{self, Write}; | ||
use tinyvec::ArrayVec; | ||
|
||
/// External iterator for replacements for a string's characters. | ||
#[derive(Clone)] | ||
pub struct Replacements<I> { | ||
iter: I, | ||
// At this time, the longest replacement sequence has length 2, so we just | ||
// need buffer space for 1 codepoint. | ||
buffer: Option<char>, | ||
} | ||
|
||
#[inline] | ||
pub fn new_cjk_compat_variants<I: Iterator<Item = char>>(iter: I) -> Replacements<I> { | ||
Replacements { iter, buffer: None } | ||
} | ||
|
||
impl<I: Iterator<Item = char>> Iterator for Replacements<I> { | ||
type Item = char; | ||
|
||
#[inline] | ||
fn next(&mut self) -> Option<char> { | ||
if let Some(c) = self.buffer.take() { | ||
return Some(c); | ||
} | ||
|
||
match self.iter.next() { | ||
Some(ch) => { | ||
// At this time, the longest replacement sequence has length 2. | ||
let mut buffer = ArrayVec::<[char; 2]>::new(); | ||
super::char::decompose_cjk_compat_variants(ch, |d| buffer.push(d)); | ||
self.buffer = buffer.get(1).copied(); | ||
Some(buffer[0]) | ||
} | ||
None => None, | ||
} | ||
} | ||
|
||
fn size_hint(&self) -> (usize, Option<usize>) { | ||
let (lower, _) = self.iter.size_hint(); | ||
(lower, None) | ||
} | ||
} | ||
|
||
impl<I: Iterator<Item = char> + Clone> fmt::Display for Replacements<I> { | ||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
for c in self.clone() { | ||
f.write_char(c)?; | ||
} | ||
Ok(()) | ||
} | ||
} |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can we assert this or codegen a constant from the python file?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It effectively is asserted by the
TinyVec::<[char; 2]>
, which panics if too many elements are appended. I could codegen the constant if you want, but the code is simpler this way.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I could be misreading it, but I thought
TinyVec
allocates if it exceeds the inline array size?https://docs.rs/tinyvec/1.1.0/tinyvec/enum.TinyVec.html
but yeah, just asserting within the python file should be fine, agreed that codegenerating a constant is probably overkill :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ah yeah, you can use
ArrayVec
which will have the panic on overflow behavior we want here.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oops, looks like I misread the
TinyVec
docs. Updated to useArrayVec
, and I added an assert to the python file.