summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorJanito Vaqueiro Ferreira Filho <janito@mullvad.net>2021-05-21 10:41:22 -0300
committerJanito Vaqueiro Ferreira Filho <janito@mullvad.net>2021-05-21 10:41:22 -0300
commit3f38c0d6af9fe85e349bb8abe07546bae063c7b6 (patch)
tree7bd7246259a943dedb097d5d0d794510d1730fc6
parentda1fa41fe29a466de434e575cfbca9256037eb41 (diff)
parentd2dcd09574927d3e1d1c7299310c153ae4118467 (diff)
downloadmullvadvpn-3f38c0d6af9fe85e349bb8abe07546bae063c7b6.tar.xz
mullvadvpn-3f38c0d6af9fe85e349bb8abe07546bae063c7b6.zip
Merge branch 'improve-gettext-message-escaping'
-rw-r--r--android/translations-converter/src/gettext/msg_string.rs26
-rw-r--r--android/translations-converter/src/normalize.rs13
2 files changed, 31 insertions, 8 deletions
diff --git a/android/translations-converter/src/gettext/msg_string.rs b/android/translations-converter/src/gettext/msg_string.rs
index d693c4ee04..b6ae21aadf 100644
--- a/android/translations-converter/src/gettext/msg_string.rs
+++ b/android/translations-converter/src/gettext/msg_string.rs
@@ -17,9 +17,17 @@ impl MsgString {
/// Create a new `MsgString` from string without any escaped characters.
///
- /// This will ensure that the string has the double quotes characters properly escaped.
+ /// This will ensure that the string has common C escape sequences properly created for special
+ /// characters. It will not attempt to escape non-ASCII characters and will just keep them as
+ /// UTF-8 characters.
pub fn from_unescaped(string: &str) -> Self {
- MsgString(string.replace(r#"""#, r#"\""#))
+ let string = string.replace(r"\", r"\\");
+ let string = string.replace("\n", r"\n");
+ let string = string.replace("\r", r"\r");
+ let string = string.replace("\t", r"\t");
+ let string = string.replace(r#"""#, r#"\""#);
+
+ MsgString(string)
}
/// Create a new `MsgString` from string that already has proper escaping.
@@ -56,9 +64,19 @@ mod tests {
#[test]
fn escaping() {
- let input = MsgString::from_unescaped(r#""Inside double quotes""#);
+ let input = MsgString::from_unescaped(concat!(
+ r#""Inside double quotes""#,
+ r"'Inside single quotes'",
+ r"Back-slash character: \",
+ "Whitespace characters: \n\r\t",
+ ));
- let expected = r#"\"Inside double quotes\""#;
+ let expected = concat!(
+ r#"\"Inside double quotes\""#,
+ "'Inside single quotes'",
+ r"Back-slash character: \\",
+ r"Whitespace characters: \n\r\t",
+ );
assert_eq!(input.to_string(), expected);
}
diff --git a/android/translations-converter/src/normalize.rs b/android/translations-converter/src/normalize.rs
index 9e8af385f4..ffeddbc6f7 100644
--- a/android/translations-converter/src/normalize.rs
+++ b/android/translations-converter/src/normalize.rs
@@ -39,6 +39,7 @@ mod gettext {
lazy_static! {
static ref APOSTROPHE_VARIATION: Regex = Regex::new("’").unwrap();
+ static ref ESCAPED_SINGLE_QUOTES: Regex = Regex::new(r"\\'").unwrap();
static ref ESCAPED_DOUBLE_QUOTES: Regex = Regex::new(r#"\\""#).unwrap();
static ref PARAMETERS: Regex = Regex::new(r"%\([^)]*\)").unwrap();
}
@@ -49,6 +50,8 @@ mod gettext {
let string = APOSTROPHE_VARIATION.replace_all(&*self, "'");
// Mark where parameters are positioned, removing the parameter name
let string = PARAMETERS.replace_all(&string, "%");
+ // Remove escaped single-quotes
+ let string = ESCAPED_SINGLE_QUOTES.replace_all(&string, r"'");
// Remove escaped double-quotes
let string = ESCAPED_DOUBLE_QUOTES.replace_all(&string, r#"""#);
@@ -72,7 +75,7 @@ mod tests {
));
let expected = concat!(
- "\'Inside single quotes\'",
+ "'Inside single quotes'",
r#""Inside double quotes""#,
"With parameters: %d, %s",
);
@@ -84,14 +87,16 @@ mod tests {
fn normalize_gettext_msg_string() {
use crate::gettext::MsgString;
- let input = MsgString::from_unescaped(concat!(
+ let input = MsgString::from_escaped(concat!(
"'Inside single quotes'",
- r#""Inside double quotes""#,
+ r"\'Inside escaped single quotes\'",
+ r#"\"Inside double quotes\""#,
"With parameters: %(number)d, %(string)s",
));
let expected = concat!(
- "\'Inside single quotes\'",
+ "'Inside single quotes'",
+ "'Inside escaped single quotes'",
r#""Inside double quotes""#,
"With parameters: %d, %s",
);