-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
64 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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:] |