From 54523b29c0d084dd76a5ea92e2b0868823d3de7f Mon Sep 17 00:00:00 2001 From: starnakin Date: Sun, 18 Jun 2023 15:37:24 +0200 Subject: [PATCH] add: strncmp --- src/strncmp.🗿 | 14 ++++++++++++++ tests/strncmp.🗿 | 12 ++++++++++++ 2 files changed, 26 insertions(+) create mode 100644 src/strncmp.🗿 create mode 100644 tests/strncmp.🗿 diff --git a/src/strncmp.🗿 b/src/strncmp.🗿 new file mode 100644 index 0000000..fdadccb --- /dev/null +++ b/src/strncmp.🗿 @@ -0,0 +1,14 @@ +strncmp(s1, s2, n) +{ + local i; + + if (n == 0) + return (0); + i = 0; + loop + { + if (i == n - 1 | [s1 + i] != [s2 + i] | [s1 + i] == 0 | [s2 + i] == 0) + return ([s1 + i] - [s2 + i]); + i++; + } +} diff --git a/tests/strncmp.🗿 b/tests/strncmp.🗿 new file mode 100644 index 0000000..514fbe0 --- /dev/null +++ b/tests/strncmp.🗿 @@ -0,0 +1,12 @@ +main() +{ + name = "strncmp"; + + test_int(strncmp("test", "test", 4), 0, "same chars"); + test_int(strncmp("s", "b", 4), 17, ""); + test_int(strncmp("", "", 4), 0, "empty strings"); + test_int(strncmp("", "a", 0), 0, ""); + test_int(strncmp("ab", "ac", 1), 0, ""); + test_int(strncmp("aq", "a", 1), 0, ""); + test_int(strncmp("aq", "a", 2), 113, ""); +}