Skip to content

Commit

Permalink
StringProcessing: Add unescape() function
Browse files Browse the repository at this point in the history
Function that removes all escape-sequences.
Includes a test.

Fixes #410.
  • Loading branch information
Makman2 committed Apr 25, 2015
1 parent e2d4a21 commit 4ff6ebc
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
14 changes: 14 additions & 0 deletions coalib/parsing/StringProcessing.py
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,20 @@ def unescaped_search_in_between(begin,
elem.group(begin_pattern_groups + 2))


def unescape(string):
"""
Trimms off all escape characters from the given string.
:param string: The string to unescape.
"""
regex = r"\\(.)"

def replacement_function(match):
return match.group(1)

return re.sub(regex, replacement_function, string, 0, re.DOTALL)


def position_is_escaped(string, position=None):
"""
Checks whether a char at a specific position of the string is preceded by
Expand Down
31 changes: 31 additions & 0 deletions coalib/tests/parsing/StringProcessingTest.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from coalib.parsing.StringProcessing import unescaped_split
from coalib.parsing.StringProcessing import search_in_between
from coalib.parsing.StringProcessing import unescaped_search_in_between
from coalib.parsing.StringProcessing import unescape
from coalib.parsing.StringProcessing import position_is_escaped


Expand Down Expand Up @@ -72,6 +73,7 @@ def setUp(self):
self.set_up_unescaped_split()
self.set_up_search_in_between()
self.set_up_unescaped_search_in_between()
self.set_up_unescape()

def set_up_search_for(self):
# Match either "out1" or "out2".
Expand Down Expand Up @@ -471,6 +473,27 @@ def set_up_unescaped_search_in_between(self):
self.test_unescaped_search_in_between_disabled_regex_expected = (
[[] for x in range(len(self.test_strings))])

def set_up_unescape(self):
self.test_unescape_expected_results = [
r"out1 'escaped-escape: \ ' out2",
r"out1 'escaped-quote: ' ' out2",
r"out1 'escaped-anything: X ' out2",
r"out1 'two escaped escapes: \\ ' out2",
r"out1 'escaped-quote at end: '' out2",
r"out1 'escaped-escape at end: \' out2",
r"out1 'str1' out2 'str2' out2",
r"out1 ' 'str1' out2 'str2' out2",
r"out1 \' 'str1' out2 'str2' out2",
r"out1 \ 'str1' out2 'str2' out2",
r"out1 \\ 'str1' out2 'str2' out2",
r"out1 \'str1' out2 'str2' out2",
r"out1 \\'str1' out2 'str2' out2",
r"out1 'str1''str2''str3' out2",
r"",
r"out1 out2 out3",
self.bs,
self.bs]

def assertSearchForResultEqual(self,
pattern,
test_strings,
Expand Down Expand Up @@ -1127,6 +1150,14 @@ def test_unescaped_search_in_between_disabled_regex(self):
True,
False)

# Test unescape() function.
def test_unescape(self):
results = [unescape(elem) for elem in self.test_strings]
compare = zip(self.test_unescape_expected_results, results)

for elem in compare:
self.assertEqual(elem[0], elem[1])

def test_position_is_escaped(self):
test_string = r"\\\\\abcabccba###\\13q4ujsabbc\+'**'ac###.#.####-ba"
result_dict = {
Expand Down

1 comment on commit 4ff6ebc

@sils
Copy link
Member

@sils sils commented on 4ff6ebc Apr 25, 2015

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thats better.

Please sign in to comment.