From fa5a7755e47b3c31493c4fd24368f8d2c14cb484 Mon Sep 17 00:00:00 2001 From: starnakin Date: Sun, 23 Jul 2023 21:08:37 +0200 Subject: [PATCH] add: replace --- src/replace.🗿 | 15 +++++++++++++++ tests/replace.🗿 | 9 +++++++++ 2 files changed, 24 insertions(+) create mode 100644 src/replace.🗿 create mode 100644 tests/replace.🗿 diff --git a/src/replace.🗿 b/src/replace.🗿 new file mode 100644 index 0000000..fa69170 --- /dev/null +++ b/src/replace.🗿 @@ -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); +} diff --git a/tests/replace.🗿 b/tests/replace.🗿 new file mode 100644 index 0000000..6651487 --- /dev/null +++ b/tests/replace.🗿 @@ -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", ""); +}