Skip to content

Commit

Permalink
Implement, test splice
Browse files Browse the repository at this point in the history
  • Loading branch information
mmore500 committed Nov 5, 2023
1 parent c7e475e commit a3ef533
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 0 deletions.
29 changes: 29 additions & 0 deletions conduitpylib/test/test_utils/test_splice.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
from conduitpylib.utils import splice


def test_splice_removal_only():
assert splice("Hello world", (5, 11)) == "Hello"


def test_splice_insertion_only():
assert splice("123456789", (3, 3), "ABC") == "123ABC456789"


def test_splice_removal_and_insertion():
assert splice("Hello world", (0, 5), "Goodbye") == "Goodbye world"


def test_splice_empty_string():
assert splice("", (0, 0), "Hello") == "Hello"


def test_splice_full_replacement():
assert splice("Hi", (0, 2), "Hello") == "Hello"


def test_splice_out_of_bounds():
assert splice("Hello", (0, 15)) == ""


def test_splice_negative_indices():
splice("Hello", (-3, -1))
2 changes: 2 additions & 0 deletions conduitpylib/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
seaborn_monkeypatched_kdecache,
seaborn_unmonkeypatch_kdecache,
)
from .splice import splice
from .strip_end import strip_end
from .UnequalSentinel import UnequalSentinel

Expand All @@ -30,6 +31,7 @@
'seaborn_monkeypatch_kdecache',
'seaborn_monkeypatched_kdecache',
'seaborn_unmonkeypatch_kdecache',
'splice',
'strip_end',
'UnequalSentinel',
]
33 changes: 33 additions & 0 deletions conduitpylib/utils/splice.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import typing


def splice(string: str, span: typing.Tuple[int, int], insert: str = "") -> str:
"""Remove index span from string and optionally insert new content.
Parameters
----------
string : str
The original string to be spliced.
span : tuple[int, int]
A tuple indicating the start and end indices for the splice operation.
The start index is inclusive and the end index is exclusive.
insert : str, default ""
The string to be inserted in place of the removed span.
Defaults to an empty string, i.e., simple removal.
Returns
-------
str
The spliced string after removal and insert operation.
Examples
--------
>>> splice("Hello world", (1, 6))
'Hworld'
>>> splice("Hello world", (0, 5), "Goodbye")
'Goodbye world'
"""
start, end = span
return string[:start] + insert + string[end:]

0 comments on commit a3ef533

Please sign in to comment.