improved index_of_with_escape
Some checks are pending
CI / build (push) Waiting to run

This commit is contained in:
2024-11-18 09:00:23 +08:00
parent 36f7031fea
commit 79246f70c4
2 changed files with 9 additions and 5 deletions

View File

@@ -266,13 +266,15 @@ def index_of_with_escape(haystack: str, needle: str, escape: str, begin: int, en
if escape_count > 0:
escape_count -= 1
if c == escape:
if c[0] == escape:
result = -1
elif escape_count == 0:
if c == escape:
if c[0] == escape:
escape_count += 1
if c == needle:
result = cursor
if cursor + len(needle) <= len(haystack):
test = haystack[cursor:cursor + len(needle)]
if test == needle:
result = cursor
if result >= 0 and escape_count == 0:
break

View File

@@ -104,7 +104,7 @@ class TestIndexOfWithEscape(unittest.TestCase):
break
solution.append(i)
i += 1
self.assertEqual(solution, expected_solution)
self.assertEqual(expected_solution, solution)
def test_simple(self):
self.run_test_case(" dsds $sdsa \\$dfivbdsf \\\\$sdgsga", '$', '\\', [6, 25])
@@ -124,4 +124,6 @@ class TestIndexOfWithEscape(unittest.TestCase):
def test_special_case(self):
self.run_test_case("\n${sys:user.home}${env:HOME}", ':', '\\', [6, 22])
def test_wide_needle(self):
self.run_test_case("asdasd\\${#vdfv|${#sdfs}", '${', '\\', [15])