-
Notifications
You must be signed in to change notification settings - Fork 183
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
Bidi Data Adapter #1784
Merged
Merged
Bidi Data Adapter #1784
Changes from 20 commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
12ca497
Implement DataSource
younies 9c9ff4c
add tests
younies c062f8e
make unicode bidi crate optional
younies b93dc24
Fix comments
younies 9a0fee7
Finish the BidiDataSource trait implementation.
younies 883e9ad
make unicode-bidi optional
younies 685e705
fix most of the issues.
younies fefa84a
add unicode-bidi to features.
younies e239f5f
allow dead code.
younies f636b12
return other neutral as a default case.
younies cb708ae
add docs and fix comments.
younies 301dd4b
add no_std
younies 156ac37
std = false
younies 2bd29b4
default = false
younies 6c30b8a
fix default.
younies 0c4af58
fix
younies f2f092e
fix bidi version
younies b1694d9
Update components/properties/src/bidi.rs
younies 10f5e1e
update cargo.lock
younies 62b3f27
Update components/properties/src/bidi.rs
younies 8054628
remove not needed dependencies
younies 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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 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,163 @@ | ||
// This file is part of ICU4X. For terms of use, please see the file | ||
// called LICENSE at the top level of the ICU4X source tree | ||
// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ). | ||
|
||
//! This module exposes tooling for running the [unicode bidi algorithm](https://unicode.org/reports/tr9/) using ICU4X data. | ||
//! | ||
//! `BidiClassAdapter` enables ICU4X to provide data to [`unicode-bidi`]. | ||
//! | ||
//! # Examples | ||
//! | ||
//!``` | ||
//! use icu_properties::bidi::BidiClassAdapter; | ||
//! use icu_properties::{maps, BidiClass}; | ||
//! use icu_codepointtrie::CodePointTrie; | ||
//! use unicode_bidi::BidiClass as DataSourceBidiClass; | ||
//! use unicode_bidi::BidiDataSource; | ||
//! use unicode_bidi::BidiInfo; | ||
//! // This example text is defined using `concat!` because some browsers | ||
//! // and text editors have trouble displaying bidi strings. | ||
//! let text = concat![ | ||
younies marked this conversation as resolved.
Show resolved
Hide resolved
|
||
//! "א", | ||
//! "ב", | ||
//! "ג", | ||
//! "a", | ||
//! "b", | ||
//! "c", | ||
//! ]; | ||
//! | ||
//! // Create an adapter to provide the data to `BidiInfo`. | ||
//! let provider = icu_testdata::get_provider(); | ||
//! | ||
//! let payload = | ||
//! maps::get_bidi_class(&provider) | ||
//! .expect("The data should be valid"); | ||
//! let data_struct = payload.get(); | ||
//! let bc = &data_struct.code_point_trie; | ||
//! | ||
//! let adapter = BidiClassAdapter::new(&bc); | ||
//! // Resolve embedding levels within the text. Pass `None` to detect the | ||
//! // paragraph level automatically. | ||
//! | ||
//! let bidi_info = BidiInfo::new_with_data_source(&adapter, &text, None); | ||
//! | ||
//! // This paragraph has embedding level 1 because its first strong character is RTL. | ||
//! assert_eq!(bidi_info.paragraphs.len(), 1); | ||
//! let para = &bidi_info.paragraphs[0]; | ||
//! assert_eq!(para.level.number(), 1); | ||
//! assert_eq!(para.level.is_rtl(), true); | ||
//! | ||
//! // Re-ordering is done after wrapping each paragraph into a sequence of | ||
//! // lines. For this example, I'll just use a single line that spans the | ||
//! // entire paragraph. | ||
//! let line = para.range.clone(); | ||
//! | ||
//! let display = bidi_info.reorder_line(para, line); | ||
//! assert_eq!(display, concat![ | ||
//! "a", | ||
//! "b", | ||
//! "c", | ||
//! "ג", | ||
//! "ב", | ||
//! "א", | ||
//! ]); | ||
//! ``` | ||
|
||
use crate::props::BidiClass; | ||
use icu_codepointtrie::CodePointTrie; | ||
use unicode_bidi::data_source::BidiDataSource; | ||
use unicode_bidi::BidiClass as DataSourceBidiClass; | ||
|
||
/// An adapter to convert from icu4x `BidiClass` to `unicode_bidi::BidiClass`. | ||
/// | ||
/// # Example | ||
/// | ||
/// ``` | ||
/// use icu_properties::bidi::BidiClassAdapter; | ||
/// use icu_properties::{maps, BidiClass}; | ||
/// use icu_codepointtrie::CodePointTrie; | ||
/// use unicode_bidi::BidiClass as DataSourceBidiClass; | ||
/// use unicode_bidi::BidiDataSource; | ||
/// | ||
/// let provider = icu_testdata::get_provider(); | ||
/// | ||
/// let payload = | ||
/// maps::get_bidi_class(&provider) | ||
/// .expect("The data should be valid"); | ||
/// let data_struct = payload.get(); | ||
/// let bc = &data_struct.code_point_trie; | ||
/// | ||
/// let adapter = BidiClassAdapter::new(&bc); | ||
/// assert_eq!(adapter.bidi_class('a'), DataSourceBidiClass::L); | ||
/// assert_eq!(adapter.bidi_class('ع'), DataSourceBidiClass::AL); | ||
/// ``` | ||
pub struct BidiClassAdapter<'a> { | ||
sffc marked this conversation as resolved.
Show resolved
Hide resolved
|
||
bidi_trie: &'a CodePointTrie<'a, BidiClass>, | ||
} | ||
|
||
impl<'a> BidiClassAdapter<'a> { | ||
/// Creates new instance of `BidiClassAdapter`. | ||
pub fn new(bidi_trie: &'a CodePointTrie<'a, BidiClass>) -> BidiClassAdapter<'a> { | ||
sffc marked this conversation as resolved.
Show resolved
Hide resolved
|
||
BidiClassAdapter { bidi_trie } | ||
} | ||
} | ||
|
||
impl<'a> BidiDataSource for BidiClassAdapter<'a> { | ||
/// Returns a [`DataSourceBidiClass`] given a unicode character. | ||
/// | ||
/// # Example | ||
/// | ||
/// ``` | ||
/// use icu_properties::bidi::BidiClassAdapter; | ||
/// use icu_properties::{maps, BidiClass}; | ||
/// use icu_codepointtrie::CodePointTrie; | ||
/// use unicode_bidi::BidiClass as DataSourceBidiClass; | ||
/// use unicode_bidi::BidiDataSource; | ||
/// | ||
/// let provider = icu_testdata::get_provider(); | ||
/// | ||
/// let payload = | ||
/// maps::get_bidi_class(&provider) | ||
/// .expect("The data should be valid"); | ||
/// let data_struct = payload.get(); | ||
/// let bc = &data_struct.code_point_trie; | ||
/// | ||
/// let adapter = BidiClassAdapter::new(&bc); | ||
/// assert_eq!(adapter.bidi_class('a'), DataSourceBidiClass::L); | ||
/// ``` | ||
/// | ||
/// [`CodePointTrie`]: icu_codepointtrie::CodePointTrie | ||
fn bidi_class(&self, c: char) -> DataSourceBidiClass { | ||
let bidi_class = self.bidi_trie.get(c as u32); | ||
match bidi_class { | ||
BidiClass::LeftToRight => DataSourceBidiClass::L, | ||
BidiClass::RightToLeft => DataSourceBidiClass::R, | ||
BidiClass::EuropeanNumber => DataSourceBidiClass::EN, | ||
BidiClass::EuropeanSeparator => DataSourceBidiClass::ES, | ||
BidiClass::EuropeanTerminator => DataSourceBidiClass::ET, | ||
BidiClass::ArabicNumber => DataSourceBidiClass::AN, | ||
BidiClass::CommonSeparator => DataSourceBidiClass::CS, | ||
BidiClass::ParagraphSeparator => DataSourceBidiClass::B, | ||
BidiClass::SegmentSeparator => DataSourceBidiClass::S, | ||
BidiClass::WhiteSpace => DataSourceBidiClass::WS, | ||
BidiClass::OtherNeutral => DataSourceBidiClass::ON, | ||
BidiClass::LeftToRightEmbedding => DataSourceBidiClass::LRE, | ||
BidiClass::LeftToRightOverride => DataSourceBidiClass::LRO, | ||
BidiClass::ArabicLetter => DataSourceBidiClass::AL, | ||
BidiClass::RightToLeftEmbedding => DataSourceBidiClass::RLE, | ||
BidiClass::RightToLeftOverride => DataSourceBidiClass::RLO, | ||
BidiClass::PopDirectionalFormat => DataSourceBidiClass::PDF, | ||
BidiClass::NonspacingMark => DataSourceBidiClass::NSM, | ||
BidiClass::BoundaryNeutral => DataSourceBidiClass::BN, | ||
BidiClass::FirstStrongIsolate => DataSourceBidiClass::FSI, | ||
BidiClass::LeftToRightIsolate => DataSourceBidiClass::LRI, | ||
BidiClass::RightToLeftIsolate => DataSourceBidiClass::RLI, | ||
BidiClass::PopDirectionalIsolate => DataSourceBidiClass::PDI, | ||
_ => | ||
// This must not happen. | ||
{ | ||
DataSourceBidiClass::ON | ||
} | ||
} | ||
} | ||
} |
This file contains 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
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.
Nit: I do not think you are using this dependency, so you should remove it
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.
done.