Skip to content

Commit a3ef533

Browse files
committed
Implement, test splice
1 parent c7e475e commit a3ef533

File tree

3 files changed

+64
-0
lines changed

3 files changed

+64
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
from conduitpylib.utils import splice
2+
3+
4+
def test_splice_removal_only():
5+
assert splice("Hello world", (5, 11)) == "Hello"
6+
7+
8+
def test_splice_insertion_only():
9+
assert splice("123456789", (3, 3), "ABC") == "123ABC456789"
10+
11+
12+
def test_splice_removal_and_insertion():
13+
assert splice("Hello world", (0, 5), "Goodbye") == "Goodbye world"
14+
15+
16+
def test_splice_empty_string():
17+
assert splice("", (0, 0), "Hello") == "Hello"
18+
19+
20+
def test_splice_full_replacement():
21+
assert splice("Hi", (0, 2), "Hello") == "Hello"
22+
23+
24+
def test_splice_out_of_bounds():
25+
assert splice("Hello", (0, 15)) == ""
26+
27+
28+
def test_splice_negative_indices():
29+
splice("Hello", (-3, -1))

conduitpylib/utils/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
seaborn_monkeypatched_kdecache,
1515
seaborn_unmonkeypatch_kdecache,
1616
)
17+
from .splice import splice
1718
from .strip_end import strip_end
1819
from .UnequalSentinel import UnequalSentinel
1920

@@ -30,6 +31,7 @@
3031
'seaborn_monkeypatch_kdecache',
3132
'seaborn_monkeypatched_kdecache',
3233
'seaborn_unmonkeypatch_kdecache',
34+
'splice',
3335
'strip_end',
3436
'UnequalSentinel',
3537
]

conduitpylib/utils/splice.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import typing
2+
3+
4+
def splice(string: str, span: typing.Tuple[int, int], insert: str = "") -> str:
5+
"""Remove index span from string and optionally insert new content.
6+
7+
Parameters
8+
----------
9+
string : str
10+
The original string to be spliced.
11+
span : tuple[int, int]
12+
A tuple indicating the start and end indices for the splice operation.
13+
14+
The start index is inclusive and the end index is exclusive.
15+
insert : str, default ""
16+
The string to be inserted in place of the removed span.
17+
18+
Defaults to an empty string, i.e., simple removal.
19+
20+
Returns
21+
-------
22+
str
23+
The spliced string after removal and insert operation.
24+
25+
Examples
26+
--------
27+
>>> splice("Hello world", (1, 6))
28+
'Hworld'
29+
>>> splice("Hello world", (0, 5), "Goodbye")
30+
'Goodbye world'
31+
"""
32+
start, end = span
33+
return string[:start] + insert + string[end:]

0 commit comments

Comments
 (0)