summaryrefslogtreecommitdiff
path: root/userland/libc/tb/sv.c
blob: a4458d59929cb4b43bf409a8c347252bf74ecc2d (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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#include <tb/sv.h>

#define min(a, b) (((a) < (b)) ? (a) : (b))

struct sv sv_init(const char *s, size_t length) {
  return (struct sv){.s = s, .length = length};
}

char *SV_TO_C(struct sv s) {
  char *c_string = malloc(s.length + 1);
  memcpy(c_string, s.s, s.length);
  c_string[s.length] = '\0';
  return c_string;
}

size_t sv_to_cstring_buffer(struct sv s, char *buffer, size_t length) {
  if (0 == length || length - 1 < s.length) {
    return s.length;
  }
  memcpy(buffer, s.s, s.length);
  buffer[s.length] = '\0';
  return s.length;
}

struct sv sv_next(struct sv s) {
  if (0 == s.length) {
    return s;
  }
  s.length--;
  s.s++;
  return s;
}

struct sv sv_skip_chars(const struct sv input, const char *chars) {
  struct sv r = input;
  for (; r.length > 0;) {
    int found = 0;
    const char *p = chars;
    for (; *p; p++) {
      if (*p == r.s[0]) {
        r.s++;
        r.length--;
        found = 1;
        break;
      }
    }
    if (!found) {
      break;
    }
  }
  return r;
}

uint64_t sv_parse_unsigned_number(struct sv input, struct sv *rest,
                                  int *got_num) {
  if (got_num) {
    *got_num = 0;
  }
  uint64_t r = 0;
  size_t i = 0;
  for (; i < input.length; i++) {
    if (!isdigit(input.s[i])) {
      break;
    }
    r *= 10;
    r += input.s[i] - '0';
  }
  if (i > 0) {
    if (got_num) {
      *got_num = 1;
    }
  }
  input.length -= i;
  input.s += i;
  if (rest) {
    *rest = input;
  }
  return r;
}

struct sv sv_split_function(const struct sv input, struct sv *rest,
                            int (*function)(int)) {
  struct sv r = {
      .s = input.s,
  };
  for (size_t i = 0; i < input.length; i++) {
    if (function(input.s[i])) {
      r.length = i;
      if (rest) {
        rest->s += i;
        rest->length -= i;
      }
      return r;
    }
  }

  if (rest) {
    rest->s = NULL;
    rest->length = 0;
  }
  return input;
}

struct sv sv_split_space(const struct sv input, struct sv *rest) {
  return sv_split_function(input, rest, isspace);
}

struct sv sv_end_split_delim(const struct sv input, struct sv *rest,
                             char delim) {
  for (size_t i = input.length - 1; i > 0; i--) {
    if (delim == input.s[i]) {
      struct sv r = {
          .s = (input.s + i),
          .length = input.length - i,
      };
      if (rest) {
        rest->s = input.s;
        rest->length = i;
      }
      return r;
    }
  }

  if (rest) {
    rest->s = NULL;
    rest->length = 0;
  }
  return input;
}

struct sv sv_split_delim(const struct sv input, struct sv *rest, char delim) {
  struct sv r = {
      .s = input.s,
  };
  for (size_t i = 0; i < input.length; i++) {
    if (delim == input.s[i]) {
      r.length = i;
      if (rest) {
        rest->s += i + 1;
        rest->length -= (i + 1);
      }
      return r;
    }
  }

  if (rest) {
    rest->s = NULL;
    rest->length = 0;
  }
  return input;
}

int sv_isempty(struct sv s) {
  return (0 == s.length);
}

char sv_peek(struct sv s) {
  if (0 == s.length) {
    return '\0';
  }
  return s.s[0];
}

int sv_partial_eq(struct sv a, struct sv b) {
  if (a.length < b.length) {
    return 0;
  }
  for (size_t i = 0; i < b.length; i++) {
    if (a.s[i] != b.s[i]) {
      return 0;
    }
  }
  return 1;
}

int sv_eq(struct sv a, struct sv b) {
  if (a.length != b.length) {
    return 0;
  }
  for (size_t i = 0; i < a.length; i++) {
    if (a.s[i] != b.s[i]) {
      return 0;
    }
  }
  return 1;
}

struct sv sv_take(struct sv s, struct sv *rest, size_t n) {
  if (s.length < n) {
    if (rest) {
      rest->length = 0;
    }
    return s;
  }
  s.length = n;
  if (rest) {
    rest->length -= n;
    rest->s += n;
  }
  return s;
}

int sv_read(struct sv s, struct sv *rest, void *buf, size_t n) {
  if (s.length < n) {
    if (rest) {
      rest->length = 0;
    }
    return 0;
  }
  memcpy(buf, s.s, n);
  if (rest) {
    rest->length -= n;
    rest->s += n;
  }
  return 1;
}

struct sv sv_take_end(struct sv s, struct sv *rest, size_t n) {
  if (s.length < n) {
    if (rest) {
      rest->length = 0;
    }
    return s;
  }
  if (rest) {
    rest->length = s.length - n;
  }
  s.s += (s.length - n);
  s.length = n;
  return s;
}

struct sv sv_trim_left(struct sv s, size_t n) {
  if (s.length < n) {
    s.s += s.length;
    s.length = 0;
    return s;
  }
  s.s += n;
  s.length -= n;
  return s;
}

struct sv sv_clone(struct sv s) {
  struct sv new_sv;
  new_sv.length = s.length;
  char *new_string = malloc(s.length);
  memcpy(new_string, s.s, s.length);
  new_sv.s = new_string;
  return new_sv;
}

int sv_try_eat(struct sv input, struct sv *rest, struct sv b) {
  if (input.length < b.length) {
    return 0;
  }
  for (size_t i = 0; i < b.length; i++) {
    if (input.s[i] != b.s[i]) {
      return 0;
    }
  }
  input.s += b.length;
  input.length -= b.length;
  if (rest) {
    *rest = input;
  }
  return 1;
}

char *sv_copy_to_c(struct sv s, char *out, size_t buffer_length) {
  int copy_len = min(s.length + 1, buffer_length);
  if (0 == copy_len) {
    return NULL;
  }
  if (!out) {
    out = malloc(copy_len);
  }
  memcpy(out, s.s, copy_len - 1);
  out[copy_len - 1] = '\0';
  return out;
}

struct sv sv_clone_from_c(const char *s) {
  return sv_clone(C_TO_SV(s));
}