Skip to content

Commit

Permalink
quote: handle all control chars equally
Browse files Browse the repository at this point in the history
This make the behavior consistent for all control characters, not just
to these few defined.

Fixes #5199
  • Loading branch information
nijel committed Mar 11, 2024
1 parent 6b015ba commit 6f56225
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 10 deletions.
11 changes: 10 additions & 1 deletion tests/translate/misc/test_quote.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ def test_mozilla_control_escapes():
prefix, suffix = "bling", "blang"
for control in ("\u0005", "\u0006", "\u0007", "\u0011"):
string = prefix + control + suffix
assert quote.escapecontrols(string) == string
assert quote.escapecontrols(string) != string

@staticmethod
def test_propertiesdecode():
Expand All @@ -149,6 +149,15 @@ def test_propertiesdecode():
assert quote.propertiesdecode("abc\\") == "abc\\"
assert quote.propertiesdecode("abc\\") == "abc\\"

@staticmethod
def test_controlchars():
assert quote.javapropertiesencode(quote.propertiesdecode("\u0001")) == r"\u0001"
assert quote.javapropertiesencode(quote.propertiesdecode("\\u01")) == r"\u0001"
assert quote.javapropertiesencode("\\") == "\\\\"
assert quote.javapropertiesencode("\x01") == "\\u0001"
assert quote.propertiesdecode("\x01") == "\x01"
assert quote.propertiesdecode("\\u0001") == "\x01"

@staticmethod
def test_properties_decode_slashu():
# The real input strings don't have double backslashes, but we have to
Expand Down
45 changes: 36 additions & 9 deletions translate/misc/quote.py
Original file line number Diff line number Diff line change
Expand Up @@ -429,20 +429,47 @@ def mozillaescapemarginspaces(source: str) -> str:
}

controlchars = {
# the reverse of the above...
"\\": "\\\\",
"\f": "\\f",
"\n": "\\n",
"\r": "\\r",
"\t": "\\t",
# complement of the above with all control chars
"\\": r"\\",
"\x00": r"\u0000",
"\x01": r"\u0001",
"\x02": r"\u0002",
"\x03": r"\u0003",
"\x04": r"\u0004",
"\x05": r"\u0005",
"\x06": r"\u0006",
"\x07": r"\u0007",
"\x08": r"\u0008",
"\t": r"\t",
"\n": r"\n",
"\x0b": r"\u000b",
"\f": r"\f",
"\r": r"\r",
"\x0e": r"\u000e",
"\x0f": r"\u000f",
"\x10": r"\u0010",
"\x11": r"\u0011",
"\x12": r"\u0012",
"\x13": r"\u0013",
"\x14": r"\u0014",
"\x15": r"\u0015",
"\x16": r"\u0016",
"\x17": r"\u0017",
"\x18": r"\u0018",
"\x19": r"\u0019",
"\x1a": r"\u001a",
"\x1b": r"\u001b",
"\x1c": r"\u001c",
"\x1d": r"\u001d",
"\x1e": r"\u001e",
"\x1f": r"\u001f",
}
controlchars_trans = str.maketrans(controlchars)


def escapecontrols(source: str) -> str:
"""Escape control characters in the given string."""
for key, value in controlchars.items():
source = source.replace(key, value)
return source
return source.translate(controlchars_trans)


def propertiesdecode(source: str) -> str:
Expand Down

0 comments on commit 6f56225

Please sign in to comment.