summaryrefslogtreecommitdiff
path: root/src/str.c
blob: 5ce8331c82ec3311423bb72fe8eb92e7f1a2dc85 (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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#include "str.h"

#include "stralloc.h"

/**
  @file str.c
  @author djb
  @source qmail
  @brief string handling functions
*/

unsigned int str_copy(char *s, const char *t)
{
  int len = 0;

  for (;;) {
    // clang-format off
    if (!(*s = *t)) { return len; } ++s; ++t; ++len;
    if (!(*s = *t)) { return len; } ++s; ++t; ++len;
    if (!(*s = *t)) { return len; } ++s; ++t; ++len;
    if (!(*s = *t)) { return len; } ++s; ++t; ++len;
    // clang-format on
  }
}

unsigned int str_copyb(char *s, const char *t, unsigned int max)
{
  int len = 0;

  while (max-- > 0) {
    // clang-format off
    if (!(*s = *t)) { return len; } ++s; ++t; ++len;
    if (!(*s = *t)) { return len; } ++s; ++t; ++len;
    if (!(*s = *t)) { return len; } ++s; ++t; ++len;
    if (!(*s = *t)) { return len; } ++s; ++t; ++len;
    // clang-format on
  }

  return len;
}

int str_diff(const char *s, const char *t)
{
  char x;

  for (;;) {
    // clang-format off
    x = *s; if (x != *t) { break; } if (!x) { break; } ++s; ++t;
    x = *s; if (x != *t) { break; } if (!x) { break; } ++s; ++t;
    x = *s; if (x != *t) { break; } if (!x) { break; } ++s; ++t;
    x = *s; if (x != *t) { break; } if (!x) { break; } ++s; ++t;
    // clang-format on
  }
  return ((int)(unsigned int)(unsigned char)x) - ((int)(unsigned int)(unsigned char)*t);
}

int str_diffn(const char *s, const char *t, unsigned int len)
{
  char x;

  for (;;) {
    // clang-format off
    if (!len--) { return 0; } x = *s; if (x != *t) { break; } if (!x) { break; } ++s; ++t;
    if (!len--) { return 0; } x = *s; if (x != *t) { break; } if (!x) { break; } ++s; ++t;
    if (!len--) { return 0; } x = *s; if (x != *t) { break; } if (!x) { break; } ++s; ++t;
    if (!len--) { return 0; } x = *s; if (x != *t) { break; } if (!x) { break; } ++s; ++t;
    // clang-format on
  }
  return ((int)(unsigned int)(unsigned char)x) - ((int)(unsigned int)(unsigned char)*t);
}

unsigned int str_len(const char *s)
{
  const char *t = s;

  for (;;) {
    // clang-format off
    if (!*t) { return t - s; } ++t;
    if (!*t) { return t - s; } ++t;
    if (!*t) { return t - s; } ++t;
    if (!*t) { return t - s; } ++t;
    // clang-format on
  }
}

unsigned int str_chr(const char *s, int c)
{
  char ch = c;
  const char *t = s;

  for (;;) {
    // clang-format off
    if (!*t) { break; } if (*t == ch) { break; } ++t;
    if (!*t) { break; } if (*t == ch) { break; } ++t;
    if (!*t) { break; } if (*t == ch) { break; } ++t;
    if (!*t) { break; } if (*t == ch) { break; } ++t;
    // clang-format on
  }
  return t - s;
}

unsigned int str_rchr(const char *s, int c)
{
  char ch = c;
  const char *t = s;
  const char *u = 0;

  for (;;) {
    // clang-format off
    if (!*t) { break; } if (*t == ch) { u = t; } ++t;
    if (!*t) { break; } if (*t == ch) { u = t; } ++t;
    if (!*t) { break; } if (*t == ch) { u = t; } ++t;
    if (!*t) { break; } if (*t == ch) { u = t; } ++t;
    // clang-format on
  }
  if (!u) u = t;
  return u - s;
}

int str_start(const char *s, const char *t)
{
  char x;

  for (;;) {
    // clang-format off
    x = *t++; if (!x) return 1; if (x != *s++) return 0;
    x = *t++; if (!x) return 1; if (x != *s++) return 0;
    x = *t++; if (!x) return 1; if (x != *s++) return 0;
    x = *t++; if (!x) return 1; if (x != *s++) return 0;
    // clang-format on
  }
}

char *str_append(char *dest, const char *s)
{
  static stralloc sa = {0};

  stralloc_copys(&sa, dest);
  stralloc_catb(&sa, s, sizeof(s));
  return sa.s;
}