summaryrefslogtreecommitdiff
path: root/src/dged/s8.c
blob: 71b6c6d0fa223bc62e96194283624bec311a1b69 (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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include "s8.h"

#include <stdlib.h>
#include <string.h>

bool s8eq(struct s8 s1, struct s8 s2) {
  return s1.l == s2.l && memcmp(s1.s, s2.s, s1.l) == 0;
}

int s8cmp(struct s8 s1, struct s8 s2) {
  if (s1.l < s2.l) {
    return memcmp(s1.s, s2.s, s1.l);
  } else if (s2.l < s1.l) {
    return memcmp(s1.s, s2.s, s2.l);
  }

  return memcmp(s1.s, s2.s, s1.l);
}

char *s8tocstr(struct s8 s) {
  char *cstr = (char *)malloc(s.l + 1);
  memcpy(cstr, s.s, s.l);
  cstr[s.l] = '\0';
  return cstr;
}

bool s8startswith(struct s8 s, struct s8 prefix) {
  if (prefix.l > s.l) {
    return false;
  }

  return memcmp(s.s, prefix.s, prefix.l) == 0;
}

struct s8 s8dup(struct s8 s) {
  struct s8 new = {0};
  new.l = s.l;

  new.s = (uint8_t *)malloc(s.l);
  memcpy(new.s, s.s, s.l);

  return new;
}