Skip to content

Commit

Permalink
Fix surround substring to surround all substrings (#212)
Browse files Browse the repository at this point in the history
* surround_substring surround **all** substrings

* added unit tests

* ruff lint

---------

Co-authored-by: Jimmy Jensen <[email protected]>
  • Loading branch information
mrjsj and Jimmy Jensen authored Feb 26, 2024
1 parent 6bb7fcd commit a661769
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 8 deletions.
11 changes: 4 additions & 7 deletions quinn/keyword_finder.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,7 @@ def surround_substring(input: str, substring: str, surround_start: str, surround
:rtype: str
"""
index = input.find(substring)
res = ""
if index == -1:
res = input
else:
res = input[:index] + surround_start + substring + surround_end + input[(index + len(substring)) :]
return res
return input.replace(
substring,
surround_start + substring + surround_end,
)
7 changes: 6 additions & 1 deletion tests/test_keyword_finder.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,9 @@ def test_keyword_format():


def test_surround_substring():
print(surround_substring("spark rdd stuff", "rdd", "**", "||"))

assert "spark **rdd|| stuff" == surround_substring("spark rdd stuff", "rdd", "**", "||")
assert "spark **rdd|| stuff with **rdd||" == surround_substring("spark rdd stuff with rdd", "rdd", "**", "||")
assert "spark **rdd||dd stuff" == surround_substring("spark rdddd stuff", "rdd", "**", "||")


0 comments on commit a661769

Please sign in to comment.