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 joining-group sub-command #32

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
24 changes: 24 additions & 0 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ script-extension produces one table of Unicode codepoint ranges for each
possible Script_Extension value.
";

const ABOUT_JOINING_GROUP: &'static str = "\
joining-group produces one table of Unicode codepoint ranges for each
possible Joining_Group value.
";

const ABOUT_JOINING_TYPE: &'static str = "\
joining-type produces one table of Unicode codepoint ranges for each
possible Joining_Type value.
Expand Down Expand Up @@ -350,6 +355,24 @@ pub fn app() -> App<'static, 'static> {
"List the properties that can be generated with this \
command.",
));
let cmd_joining_group = SubCommand::with_name("joining-group")
.author(clap::crate_authors!())
.version(clap::crate_version!())
.template(TEMPLATE_SUB)
.about("Create the Joining_Group property tables.")
.before_help(ABOUT_JOINING_GROUP)
.arg(ucd_dir.clone())
.arg(flag_fst_dir.clone())
.arg(flag_name("JOINING_GROUP"))
.arg(flag_chars.clone())
.arg(flag_trie_set.clone())
.arg(Arg::with_name("enum").long("enum").help(
"Emit a single table that maps codepoints to joining group.",
))
.arg(Arg::with_name("rust-enum").long("rust-enum").help(
"Emit a Rust enum and a table that maps codepoints to \
joining group.",
));
let cmd_joining_type =
SubCommand::with_name("joining-type")
.author(clap::crate_authors!())
Expand Down Expand Up @@ -636,6 +659,7 @@ pub fn app() -> App<'static, 'static> {
.subcommand(cmd_general_category)
.subcommand(cmd_script)
.subcommand(cmd_script_extension)
.subcommand(cmd_joining_group)
.subcommand(cmd_joining_type)
.subcommand(cmd_age)
.subcommand(cmd_bidi_mirroring_glyph)
Expand Down
50 changes: 50 additions & 0 deletions src/joining_group.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
use std::collections::{BTreeMap, BTreeSet};

use ucd_parse::{self, ArabicShaping};

use crate::args::ArgMatches;
use crate::error::Result;
use crate::util::PropertyValues;

pub fn command(args: ArgMatches<'_>) -> Result<()> {
let dir = args.ucd_dir()?;
let propvals = PropertyValues::from_ucd_dir(&dir)?;
let rows: Vec<ArabicShaping> = ucd_parse::parse(&dir)?;

// Collect each joining group into an ordered set.
let mut by_group: BTreeMap<String, BTreeSet<u32>> = BTreeMap::new();
let mut assigned = BTreeSet::new();
for row in rows {
assigned.insert(row.codepoint.value());
let jg =
propvals.canonical("jg", row.joining_group.as_str())?.to_string();
by_group
.entry(jg)
.or_insert(BTreeSet::new())
.insert(row.codepoint.value());
}

// Process unassigned codepoints
let no_joining_group = propvals.canonical("jg", "no_joining_group")?;
for cp in 0..=0x10FFFF {
if assigned.contains(&cp) {
continue;
}
by_group.get_mut(&no_joining_group).unwrap().insert(cp);
}

let mut wtr = args.writer("joining_group")?;
if args.is_present("enum") {
wtr.ranges_to_enum(args.name(), &by_group)?;
} else if args.is_present("rust-enum") {
let variants = by_group.keys().map(String::as_str).collect::<Vec<_>>();
wtr.ranges_to_rust_enum(args.name(), &variants, &by_group)?;
} else {
wtr.names(by_group.keys())?;
for (name, set) in by_group {
wtr.ranges(&name, &set)?;
}
}

Ok(())
}
4 changes: 4 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ mod case_folding;
mod case_mapping;
mod general_category;
mod jamo_short_name;
mod joining_group;
mod joining_type;
mod names;
mod property_bool;
Expand Down Expand Up @@ -66,6 +67,9 @@ fn run() -> Result<()> {
("jamo-short-name", Some(m)) => {
jamo_short_name::command(ArgMatches::new(m))
}
("joining-group", Some(m)) => {
joining_group::command(ArgMatches::new(m))
}
("joining-type", Some(m)) => joining_type::command(ArgMatches::new(m)),
("names", Some(m)) => names::command(ArgMatches::new(m)),
("property-names", Some(m)) => cmd_property_names(ArgMatches::new(m)),
Expand Down
Loading