-
Notifications
You must be signed in to change notification settings - Fork 185
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
Explicit LocaleFallbacker
argument for DatagenDriver
#5114
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
82597de
fallback
robertbastian cac0b6f
doc
robertbastian 653c36a
fix
robertbastian e5bdb0b
Update provider/datagen/src/driver.rs
robertbastian 1ec3e2d
stuf
robertbastian 2004108
!
robertbastian bf6437d
features
robertbastian 6fcb145
.
robertbastian 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,106 @@ | ||
// 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 ). | ||
|
||
use icu_provider::prelude::*; | ||
use std::collections::HashSet; | ||
use std::path::Path; | ||
|
||
macro_rules! cb { | ||
($($marker:path = $path:literal,)+ #[experimental] $($emarker:path = $epath:literal,)+) => { | ||
fn markers_for_bin_inner(bytes: &[u8]) -> Result<HashSet<DataMarkerInfo>, DataError> { | ||
use std::sync::OnceLock; | ||
use crate as icu; | ||
static LOOKUP: OnceLock<std::collections::HashMap<&'static str, Option<DataMarkerInfo>>> = OnceLock::new(); | ||
let lookup = LOOKUP.get_or_init(|| { | ||
[ | ||
("core/helloworld@1", Some(icu_provider::hello_world::HelloWorldV1Marker::INFO)), | ||
$( | ||
($path, Some(<$marker>::INFO)), | ||
)+ | ||
$( | ||
#[cfg(feature = "experimental")] | ||
($epath, Some(<$emarker>::INFO)), | ||
#[cfg(not(feature = "experimental"))] | ||
($epath, None), | ||
)+ | ||
|
||
] | ||
.into_iter() | ||
.collect() | ||
}); | ||
|
||
use memchr::memmem::*; | ||
|
||
const LEADING_TAG: &[u8] = icu_provider::leading_tag!().as_bytes(); | ||
const TRAILING_TAG: &[u8] = icu_provider::trailing_tag!().as_bytes(); | ||
|
||
let trailing_tag = Finder::new(TRAILING_TAG); | ||
|
||
find_iter(bytes, LEADING_TAG) | ||
.map(|tag_position| tag_position + LEADING_TAG.len()) | ||
.filter_map(|marker_start| bytes.get(marker_start..)) | ||
.filter_map(move |marker_fragment| { | ||
trailing_tag | ||
.find(marker_fragment) | ||
.and_then(|end| marker_fragment.get(..end)) | ||
}) | ||
.map(std::str::from_utf8) | ||
.filter_map(Result::ok) | ||
.filter_map(|p| { | ||
match lookup.get(p) { | ||
Some(Some(marker)) => Some(Ok(*marker)), | ||
Some(None) => Some(Err(DataError::custom("This marker requires the `experimental` Cargo feature").with_display_context(p))), | ||
None => None, | ||
} | ||
}) | ||
.collect::<Result<_, _>>() | ||
} | ||
} | ||
} | ||
icu_registry::registry!(cb); | ||
|
||
/// Parses a compiled binary and returns a list of [`DataMarkerInfo`]s that it uses *at runtime*. | ||
/// | ||
/// This function is intended to be used for binaries that use `AnyProvider` or `BufferProvider`, | ||
/// for which the compiler cannot remove unused data. Using this function on a binary that only | ||
/// uses compiled data (through the `compiled_data` feature or manual baked data) will not return | ||
/// any markers, as the markers only exist in the type system. | ||
/// | ||
/// # Example | ||
/// | ||
/// ```no_run | ||
/// # use icu_provider::DataMarker; | ||
/// # fn main() -> Result<(), Box<dyn std::error::Error>> { | ||
/// assert_eq!( | ||
/// icu::markers_for_bin("target/release/my-app")?, | ||
/// std::collections::HashSet::from_iter([ | ||
/// icu::list::provider::AndListV1Marker::INFO, | ||
/// icu::list::provider::OrListV1Marker::INFO, | ||
/// ]), | ||
/// ); | ||
/// # Ok(()) | ||
/// # } | ||
/// ``` | ||
pub fn markers_for_bin<P: AsRef<Path>>(path: P) -> Result<HashSet<DataMarkerInfo>, DataError> { | ||
let file = std::fs::read(path.as_ref())?; | ||
let file = file.as_slice(); | ||
|
||
markers_for_bin_inner(file) | ||
} | ||
|
||
#[test] | ||
fn test_markers_for_bin() { | ||
assert_eq!( | ||
markers_for_bin_inner(include_bytes!("../tests/data/tutorial_buffer.wasm")), | ||
Ok(HashSet::from_iter([ | ||
crate::datetime::provider::calendar::GregorianDateLengthsV1Marker::INFO, | ||
crate::datetime::provider::calendar::GregorianDateSymbolsV1Marker::INFO, | ||
crate::datetime::provider::calendar::TimeLengthsV1Marker::INFO, | ||
crate::datetime::provider::calendar::TimeSymbolsV1Marker::INFO, | ||
crate::calendar::provider::WeekDataV1Marker::INFO, | ||
crate::decimal::provider::DecimalSymbolsV1Marker::INFO, | ||
crate::plurals::provider::OrdinalV1Marker::INFO, | ||
])) | ||
); | ||
} |
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
File renamed without changes.
File renamed without changes.
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
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
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.
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.
Nit: Make sure we bikeshed whether this function is in a module or not with the rest of the WG