aboutsummaryrefslogtreecommitdiff
path: root/libk/string/string.cc
blob: c75feb1490a1517165027666d241cdf10fbf1bbe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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);