Skip to content

Commit

Permalink
Parameter field (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
cecton authored Oct 11, 2023
1 parent 2af5295 commit 60355ad
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 7 deletions.
22 changes: 15 additions & 7 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ use std::path::Path;
// regex that tries to parse printf's format placeholders
// see: https://docs.microsoft.com/en-us/cpp/c-runtime-library/format-specification-syntax-printf-and-wprintf-functions?view=msvc-160
static RE_PRINTF: Lazy<Regex> = Lazy::new(|| {
Regex::new(r#"%([-+#])?(\d+)?(\.\d+)?([dis@xXf])|\\u([0-9a-fA-F]{4})|\\.|%%|%$|"|[^%"\\]+"#)
Regex::new(r#"%((?P<parameter>\d+)\$)?(?P<flags>[-+#])?(?P<width>\d+)?(?P<precision>\.\d+)?(?P<type>[dis@xXf])|\\u(?P<unicode>[0-9a-fA-F]{4})|\\.|%%|%$|"|[^%"\\]+"#)
.unwrap()
});
static RE_LANG: Lazy<Regex> = Lazy::new(|| Regex::new(r"(\w+)(-(\w+))?").unwrap());
Expand Down Expand Up @@ -434,15 +434,23 @@ impl TwineFormatter {
// transform all printf's format placeholder to Rust's format
let mut out = String::new();
for caps in RE_PRINTF.captures_iter(text.as_str()) {
if let Some(type_) = caps.get(4) {
out.push_str("{:");
if let Some(flag) = caps.get(1) {
if let Some(type_) = caps.name("type") {
out.push_str("{");
if let Some(parameter) = caps.name("parameter") {
let parameter: usize = parameter
.as_str()
.parse()
.expect("could not parse parameter index");
write!(out, "{}", parameter.saturating_sub(1))?;
}
out.push_str(":");
if let Some(flag) = caps.name("flags") {
out.push_str(flag.as_str());
}
if let Some(width) = caps.get(2) {
if let Some(width) = caps.name("width") {
out.push_str(width.as_str());
}
if let Some(precision) = caps.get(3) {
if let Some(precision) = caps.name("precision") {
out.push_str(precision.as_str());
}
match type_.as_str() {
Expand All @@ -454,7 +462,7 @@ impl TwineFormatter {
out.push_str("%");
} else if &caps[0] == "\"" {
out.push_str("\\\"");
} else if let Some(unicode) = caps.get(5) {
} else if let Some(unicode) = caps.name("unicode") {
out.push_str(r"\u{");
out.push_str(unicode.as_str());
out.push_str(r"}");
Expand Down
1 change: 1 addition & 0 deletions tests/test-crate/src/my_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ pub fn basic() {
println!("{}", t!(fallback_to_default_lang => lang));
println!("{}", t!(name_with_dot_in_it => lang));
println!("{}", t!(stuff_with_escaped_sequences_and_double_quotes => lang));
println!("{}", t!(format_parameter_posix_extension, 1, 2 => lang));
}

assert_eq!(
Expand Down
3 changes: 3 additions & 0 deletions tests/test-crate/translations.ini
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,6 @@
en = Name with a dot
[stuff_with_escaped_sequences_and_double_quotes]
en = Stuff with\nescaped\tsequences and "double quotes"
[format_parameter_posix_extension]
en = %1$s %2$4s
fr = %2$s %1$4s
3 changes: 3 additions & 0 deletions tests/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Hello, World !
Hello
Name with a dot
Stuff with\nescaped\tsequences and \"double quotes\"
2 1
Ruin a band name by translating it in French
Rage Against the Machine
Hello, World!
Expand All @@ -15,6 +16,7 @@ badcafe
Hello
Name with a dot
Stuff with\nescaped\tsequences and \"double quotes\"
1 2
Ruin a band name by translating it in French
Wrath Against the Machine
Hello, World!
Expand All @@ -23,6 +25,7 @@ badcafe
Hello
Name with a dot
Stuff with\nescaped\tsequences and \"double quotes\"
1 2
";

#[test]
Expand Down

0 comments on commit 60355ad

Please sign in to comment.