Skip to content
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

Add --combined option #37

Merged
merged 5 commits into from
Nov 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
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
10 changes: 10 additions & 0 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,12 @@ pub fn app() -> App<'static, 'static> {
let ucd_dir = Arg::with_name("ucd-dir")
.required(true)
.help("Directory containing the Unicode character database files.");
let flag_combined = Arg::with_name("combined").long("combined").help(
"Emit a single table with all included codepoint ranges. You might \
want to use this option when checking if characters belong to a \
subset of categories, since only one table will need to be checked. \
Searching the combined table should be simpler and more efficient.",
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Excellent. Thank you!

);

// Subcommands.
let cmd_bidi_class = SubCommand::with_name("bidi-class")
Expand All @@ -213,6 +219,7 @@ pub fn app() -> App<'static, 'static> {
.arg(flag_chars.clone())
.arg(flag_trie_set.clone())
.arg(flag_short_names.clone())
.arg(flag_combined.clone())
.arg(
Arg::with_name("enum").long("enum").help(
"Emit a single table that maps codepoints to bidi class.",
Expand Down Expand Up @@ -252,6 +259,7 @@ pub fn app() -> App<'static, 'static> {
.arg(flag_name("GENERAL_CATEGORY"))
.arg(flag_chars.clone())
.arg(flag_trie_set.clone())
.arg(flag_combined.clone())
.arg(
Arg::with_name("enum").long("enum").help(
"Emit a single table that maps codepoints to categories.",
Expand Down Expand Up @@ -285,6 +293,7 @@ pub fn app() -> App<'static, 'static> {
.arg(flag_name("SCRIPT"))
.arg(flag_chars.clone())
.arg(flag_trie_set.clone())
.arg(flag_combined.clone())
.arg(
Arg::with_name("enum")
.long("enum")
Expand Down Expand Up @@ -362,6 +371,7 @@ pub fn app() -> App<'static, 'static> {
.arg(flag_name("JOINING_TYPE"))
.arg(flag_chars.clone())
.arg(flag_trie_set.clone())
.arg(flag_combined.clone())
.arg(Arg::with_name("enum").long("enum").help(
"Emit a single table that maps codepoints to joining type.",
))
Expand Down
2 changes: 2 additions & 0 deletions src/bidi_class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,8 @@ pub fn command(args: ArgMatches<'_>) -> Result<()> {
} else if args.is_present("rust-enum") {
let variants = by_type.keys().map(String::as_str).collect::<Vec<_>>();
wtr.ranges_to_rust_enum(args.name(), &variants, &by_type)?;
} else if args.is_present("combined") {
wtr.ranges_to_combined(args.name(), &by_type)?;
} else {
wtr.names(by_type.keys())?;
for (name, set) in by_type {
Expand Down
2 changes: 2 additions & 0 deletions src/general_category.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ pub fn command(args: ArgMatches<'_>) -> Result<()> {
} else if args.is_present("rust-enum") {
let variants = bycat.keys().map(String::as_str).collect::<Vec<_>>();
wtr.ranges_to_rust_enum(args.name(), &variants, &bycat)?;
} else if args.is_present("combined") {
wtr.ranges_to_combined(args.name(), &bycat)?;
} else {
wtr.names(bycat.keys().filter(|n| filter.contains(n)))?;
for (name, set) in bycat {
Expand Down
2 changes: 2 additions & 0 deletions src/joining_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ pub fn command(args: ArgMatches<'_>) -> Result<()> {
} else if args.is_present("rust-enum") {
let variants = by_type.keys().map(String::as_str).collect::<Vec<_>>();
wtr.ranges_to_rust_enum(args.name(), &variants, &by_type)?;
} else if args.is_present("combined") {
wtr.ranges_to_combined(args.name(), &by_type)?;
} else {
wtr.names(by_type.keys())?;
for (name, set) in by_type {
Expand Down
2 changes: 2 additions & 0 deletions src/script.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ pub fn command_script(args: ArgMatches<'_>) -> Result<()> {
let mut variants = vec!["Unknown"];
variants.extend(by_name.keys().map(String::as_str));
wtr.ranges_to_rust_enum(args.name(), &variants, &by_name)?;
} else if args.is_present("combined") {
wtr.ranges_to_combined(args.name(), &by_name)?;
} else {
wtr.names(by_name.keys().filter(|n| filter.contains(n)))?;
for (name, set) in by_name {
Expand Down
18 changes: 18 additions & 0 deletions src/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,24 @@ impl Writer {
Ok(())
}

/// Write a map that combines codepoint ranges into a single table.
///
/// The given map should be a map from the variant value to the set of
/// codepoints that have that value.
pub fn ranges_to_combined(
&mut self,
name: &str,
enum_map: &BTreeMap<String, BTreeSet<u32>>,
) -> Result<()> {
let mut set = BTreeSet::new();
for other_set in enum_map.values() {
set.extend(other_set.iter().cloned());
}
self.ranges(name, &set)?;
self.wtr.flush()?;
Ok(())
}

fn ranges_to_enum_slice<S>(
&mut self,
name: &str,
Expand Down