summaryrefslogtreecommitdiff
path: root/euler/source/std/cstring.cpp
blob: 3e5a56ccfed517b7ed7a694c4982abe5f70c5a49 (plain) (blame)
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
27
28
29
30
31
32
33
34
35
36
#include <cstring>

extern "C" void *memset(void *dest, int ch, size_t count) {
  unsigned char c = static_cast<unsigned char>(ch);
  unsigned char *d = (unsigned char *)dest;
  for (size_t i = 0; i < count; ++i)
    d[i] = c;
  return dest;
}

extern "C" void *memcpy(void *dest, const void *src, size_t count) {
  unsigned char *d = (unsigned char *)dest;
  const unsigned char *s = (const unsigned char *)src;
  for (size_t i = 0; i < count; ++i)
    d[i] = s[i];
  return dest;
}

extern "C" int strcmp(const char *lhs, const char *rhs) {
  const unsigned char *l = (const unsigned char *)lhs;
  const unsigned char *r = (const unsigned char *)rhs;
  while (*l == *r) {
    if (*l == 0)
      return 0;
    ++l;
    ++r;
  }
  return *l < *r ? -1 : 1;
}

extern "C" size_t strlen(const char *str) {
  size_t len = 0;
  while (str[len] != '\0')
    ++len;
  return len;
}