annotate src/util.c @ 14:2d7c810a1ac2 default tip

*: misc. cleanup
author Paper <paper@paper.us.eu.org>
date Fri, 03 May 2024 22:40:28 -0400
parents d6f8f73a0c50
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
8
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1 #include "util.h"
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
3 #include <stdlib.h>
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
4 #include <string.h>
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
5
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
6 /* reimplementations of non-portable stdlib functions */
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
7
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
8 size_t EDL_internal_strnlen(const char* s, size_t maxlen) {
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
9 size_t len;
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
10
14
2d7c810a1ac2 *: misc. cleanup
Paper <paper@paper.us.eu.org>
parents: 10
diff changeset
11 for (len = 0; len < maxlen; len++, s++)
8
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
12 if (!*s)
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
13 break;
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
14
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
15 return len;
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
16 }
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
17
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
18 char* EDL_internal_strnchr(const char* haystack, char needle, size_t length) {
10
d6f8f73a0c50 util: make strnchr an actual strnchr
Paper <paper@paper.us.eu.org>
parents: 8
diff changeset
19 for (; length && *haystack && *haystack != needle; haystack++, length--);
8
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
20
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
21 return length ? haystack : NULL;
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
22 }
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
23
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
24 char* EDL_internal_strdup(const char* s) {
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
25 const size_t len = strlen(s);
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
26
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
27 char* d = malloc((len + 1) * sizeof(char));
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
28 memcpy(d, s, len * sizeof(char));
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
29 d[len] = '\0';
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
30
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
31 return d;
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
32 }