Skip to content

Commit

Permalink
feat: support for multiple character escaping at the same time
Browse files Browse the repository at this point in the history
  • Loading branch information
joamag committed Jul 13, 2021
1 parent dff4f4e commit b9561d7
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
6 changes: 6 additions & 0 deletions src/appier/test/util.py
Expand Up @@ -688,13 +688,19 @@ def test_escape(self):
result = appier.escape("foo$,bar", ",", escape = "$")
self.assertEqual(result, "foo$$$,bar")

result = appier.escape("foo$,bar:", (",", ":"), escape = "$")
self.assertEqual(result, "foo$$$,bar$:")

def test_unescape(self):
result = appier.unescape("foo$,bar", escape = "$")
self.assertEqual(result, "foo,bar")

result = appier.unescape("foo$$,bar", escape = "$")
self.assertEqual(result, "foo$,bar")

result = appier.unescape("foo$$,bar$:", escape = "$")
self.assertEqual(result, "foo$,bar:")

result = appier.unescape("$$foo$,bar$$$$", escape = "$")
self.assertEqual(result, "$foo,bar$$")

Expand Down
15 changes: 10 additions & 5 deletions src/appier/util.py
Expand Up @@ -993,21 +993,26 @@ def unquote(value, *args, **kwargs):
def escape(value, char, escape = "\\"):
"""
Escapes the provided string value according to the requested
target character and escape value. Meaning that all the characters
target character(s) and escape value. Meaning that all the characters
are going to be replaced by the escape plus character sequence.
:type value: String
:param value: The string that is going to have the target characters
escaped according to the escape character.
:type char: String
:param char: The character that is going to be "target" of escaping.
:type char: String/List/Tuple
:param char: The character(s) that is going to be "target" of escaping
or a list of characters for escaping.
:type escape: String
:param escape: The character to be used for escaping (normally`\`).
:param escape: The character to be used for escaping (typically `\`).
:rtype: String
:return: The final string with the target character properly escaped.
"""

return value.replace(escape, escape + escape).replace(char, escape + char)
if not isinstance(char, (list, tuple)): char = (char,)
value = value.replace(escape, escape + escape)
for _char in char:
value = value.replace(_char, escape + _char)
return value

def unescape(value, escape = "\\"):
"""
Expand Down

0 comments on commit b9561d7

Please sign in to comment.