add: replace
This commit is contained in:
parent
3c39d72bbe
commit
fa5a7755e4
15
src/replace.🗿
Normal file
15
src/replace.🗿
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
replace(str, fill, start, stop)
|
||||||
|
{
|
||||||
|
local out;
|
||||||
|
local sum;
|
||||||
|
|
||||||
|
out = galloc(strlen(str) + strlen(fill) - (stop - start) + 1);
|
||||||
|
if (out == NULL)
|
||||||
|
return (NULL);
|
||||||
|
strncpy(out, str, start);
|
||||||
|
strncpy(out + start, fill, strlen(fill));
|
||||||
|
sum = start + strlen(fill);
|
||||||
|
strncpy(out + sum, str + stop, strlen(str) - stop);
|
||||||
|
[out + sum + strlen(str) - stop] = 0;
|
||||||
|
return (out);
|
||||||
|
}
|
9
tests/replace.🗿
Normal file
9
tests/replace.🗿
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
main()
|
||||||
|
{
|
||||||
|
name = "replace";
|
||||||
|
|
||||||
|
test_str(replace("yo ca va ?", "t", 2, 3), "yotca va ?", "");
|
||||||
|
test_str(replace("yo ca va ?", "", 2, 3), "yoca va ?", "empty fill");
|
||||||
|
test_str(replace("yo ca va ?", "aaaaa", 2, 3), "yoaaaaaca va ?", "");
|
||||||
|
test_str(replace("", "aaaaa", 0, 0), "aaaaa", "");
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user