diff options
author | Aqua-sama <aqua@iserlohn-fortress.net> | 2021-02-06 17:32:40 +0200 |
---|---|---|
committer | Aqua-sama <aqua@iserlohn-fortress.net> | 2021-02-06 18:31:10 +0200 |
commit | 13e1e4515746fe65b6c50cfbc42f00e29fd56599 (patch) | |
tree | 22722c729ad028dea612c5c07a16ec6f52ca63c0 /libk/string/string.cc | |
parent | Add IntegerView (diff) | |
download | kernel.cpp-13e1e4515746fe65b6c50cfbc42f00e29fd56599.tar.xz |
libk: add some tests
- merge types.h and type_traits.h
Diffstat (limited to 'libk/string/string.cc')
-rw-r--r-- | libk/string/string.cc | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/libk/string/string.cc b/libk/string/string.cc new file mode 100644 index 0000000..c75feb1 --- /dev/null +++ b/libk/string/string.cc @@ -0,0 +1,26 @@ +#include "string.h" + +// TODO + +// int strcpy(char *dst, const char *stc); +// void strcat(void *dst, const void *src); +// char* strncpy(char *dest, const char *src, int length); +// int strncmp(const char *s1, const char *s2, int c); + +/* strcmp - compare two C-strings */ +constexpr int strcmp(const char* s1, const char* s2) { + const auto s1_len = strlen(s1); + for (size_t i = 0; i < s1_len; ++i) { + if (s1[i] == s2[i]) + continue; + if (s1[i] > s2[i]) + return 1; + if (s1[i] < s2[i]) + return -1; + } + return 0; +} + +static_assert(strcmp("one", "one") == 0); +static_assert(strcmp("one", "two") < 0); +static_assert(strcmp("foo", "bar") > 0); |