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

Inline the value of %r in T_FMT #12

Merged
merged 5 commits into from
Jan 30, 2024
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
1 change: 0 additions & 1 deletion .gitattributes

This file was deleted.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "pure-rust-locales"
version = "0.7.0"
version = "0.8.0"
authors = ["Cecile Tonglet <[email protected]>"]
description = "Pure Rust locales imported directly from the GNU C Library. `LC_COLLATE` and `LC_CTYPE` are not yet supported."
license = "MIT OR Apache-2.0"
Expand Down
23 changes: 19 additions & 4 deletions generate-api/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ fn validate_and_fix(objects: &mut [Object]) {
validate_and_fix_d_t_fmt(objects);
}

/// Add a `T_FMT_AMPM` item if it is missing (happens for 3 locales).
/// Add a `T_FMT_AMPM` item if it is missing or empty.
///
/// If the locale has non-empty values for `AM_PM` we assume the correct string to be the same as
/// for POSIX: `%l:%M:%S %p`.
Expand All @@ -97,17 +97,29 @@ fn validate_and_fix_t_fmt_ampm(objects: &mut [Object]) {
}
}
if !found_t_fmt_ampm {
// Insert `T_FMT_AMPM` if it is missing
let value = match am_pm_empty {
true => vec![Value::String(String::new())],
false => vec![Value::String("%l:%M:%S %p".to_string())],
};
object.values.push(("t_fmt_ampm".to_string(), value));
} else if !am_pm_empty {
// Update `T_FMT_AMPM` if it is empty and we do have a value for `AM_PM`.
for (key, value) in object.values.iter_mut() {
match (key.as_str(), value.as_slice()) {
("t_fmt_ampm", &[Value::String(ref t_fmt_ampm)]) if t_fmt_ampm.is_empty() => {
*value = vec![Value::String("%l:%M:%S %p".to_string())];
}
_ => {}
}
}
}
}
}

/// In some locales `D_T_FMT` refers to other items:
/// to `D_FMT` with `%x`, `T_FMT` with `%X`, and/or `T_FMT_AMPM` with `%r`.
/// In the same way `T_FMT` can refer `T_FMT_AMPM` with `%r`.
/// Inlining these strings simplifies the implementation of the strftime parser in chrono.
fn validate_and_fix_d_t_fmt(objects: &mut [Object]) {
for object in objects.iter_mut() {
Expand All @@ -125,14 +137,17 @@ fn validate_and_fix_d_t_fmt(objects: &mut [Object]) {
_ => {}
}
}
if t_fmt_ampm.is_empty() {
t_fmt_ampm = "%T".to_string();
}
for (key, ref mut value) in object.values.iter_mut() {
if let ("d_t_fmt", vec) = (key.as_str(), value) {
if let ("d_t_fmt", vec) | ("t_fmt", vec) = (key.as_str(), value) {
if let Value::String(ref val) = vec[0] {
let d_t_fmt = val
let fmt = val
.replace("%x", &d_fmt)
.replace("%X", &t_fmt)
.replace("%r", &t_fmt_ampm);
vec[0] = Value::String(d_t_fmt);
vec[0] = Value::String(fmt);
}
}
}
Expand Down
Loading