From 1fcefeacc9599bb7078380b26cb268ac7d528dd9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Krier?= Date: Fri, 16 Sep 2022 23:36:30 +0200 Subject: [PATCH] Strip only one wildcard issue11682 review443571003 --- CHANGELOG | 1 + trytond/tests/test_tools.py | 20 +++++++++++--------- trytond/tools/misc.py | 19 +++++++++---------- 3 files changed, 21 insertions(+), 19 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index 5a553b5694..4ad64ce257 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,3 +1,4 @@ +* Strip only one wildcard * Manage web extension origin as null * Remove same types restriction on PYSON If * Allow PYSON size of fields to be None diff --git a/trytond/tests/test_tools.py b/trytond/tests/test_tools.py index 09f0bc2bef..06b065eee5 100644 --- a/trytond/tests/test_tools.py +++ b/trytond/tests/test_tools.py @@ -167,26 +167,26 @@ class ToolsTestCase(unittest.TestCase): 'Test strip wildcard' for clause, result in [ ('%a%', 'a'), - ('%%%%a%%%', 'a'), + ('%%a%%', '%a%'), ('\\%a%', '\\%a'), ('\\%a\\%', '\\%a\\%'), ('a', 'a'), ('', ''), (None, None), ]: - self.assertEqual( - strip_wildcard(clause), result, msg=clause) + with self.subTest(clause=clause): + self.assertEqual(strip_wildcard(clause), result) def test_strip_wildcard_different_wildcard(self): 'Test strip wildcard with different wildcard' - self.assertEqual(strip_wildcard('___a___', '_'), 'a') + self.assertEqual(strip_wildcard('_a_', '_'), 'a') def test_lstrip_wildcard(self): 'Test lstrip wildcard' for clause, result in [ ('%a', 'a'), ('%a%', 'a%'), - ('%%%%a%', 'a%'), + ('%%a%', '%a%'), ('\\%a%', '\\%a%'), ('a', 'a'), ('', ''), @@ -197,14 +197,14 @@ class ToolsTestCase(unittest.TestCase): def test_lstrip_wildcard_different_wildcard(self): 'Test lstrip wildcard with different wildcard' - self.assertEqual(lstrip_wildcard('___a', '_'), 'a') + self.assertEqual(lstrip_wildcard('_a', '_'), 'a') def test_rstrip_wildcard(self): 'Test rstrip wildcard' for clause, result in [ ('a%', 'a'), ('%a%', '%a'), - ('%a%%%%%', '%a'), + ('%a%%', '%a%'), ('%a\\%', '%a\\%'), ('a', 'a'), ('', ''), @@ -213,8 +213,8 @@ class ToolsTestCase(unittest.TestCase): self.assertEqual( rstrip_wildcard(clause), result, msg=clause) - def test_rstrip_wildcard_diferent_wildcard(self): - self.assertEqual(rstrip_wildcard('a___', '_'), 'a') + def test_rstrip_wildcard_different_wildcard(self): + self.assertEqual(rstrip_wildcard('a_', '_'), 'a') def test_escape_wildcard(self): self.assertEqual( @@ -232,6 +232,8 @@ class ToolsTestCase(unittest.TestCase): for value, result in [ ('foo', True), ('%foo bar%', True), + ('%%foo bar%', False), + ('%foo bar%%', False), ('foo%', False), ('foo_bar', False), ('foo\\_bar', True), diff --git a/trytond/tools/misc.py b/trytond/tools/misc.py index 858ba23d1c..79900f058a 100644 --- a/trytond/tools/misc.py +++ b/trytond/tools/misc.py @@ -218,19 +218,18 @@ def strip_wildcard(string, wildcard='%', escape='\\'): def lstrip_wildcard(string, wildcard='%'): "Strip starting wildcard from string" - if not string: - return string - return string.lstrip(wildcard) + if string and string.startswith(wildcard): + string = string[1:] + return string def rstrip_wildcard(string, wildcard='%', escape='\\'): "Strip ending wildcard from string" - if not string: - return string - new_string = string.rstrip(wildcard) - if new_string[-1] == escape: - return string - return new_string + if (string + and string.endswith(wildcard) + and not string.endswith(escape + wildcard)): + string = string[:-1] + return string def escape_wildcard(string, wildcards='%_', escape='\\'): @@ -246,7 +245,7 @@ def unescape_wildcard(string, wildcards='%_', escape='\\'): def is_full_text(value, escape='\\'): - escaped = value.strip('%') + escaped = strip_wildcard(value, escape=escape) escaped = escaped.replace(escape + '%', '').replace(escape + '_', '') if '%' in escaped or '_' in escaped: return False