annotate src/util.c @ 12:0cc2555db371

*: make our string functions not stupid strncat() made everything slow! new addition internally: an EDL_header structure to make things not stupid. if we were using C++ it wouldn't really be necessary, but alas :)
author Paper <paper@paper.us.eu.org>
date Wed, 27 Mar 2024 13:38:30 -0400
parents d6f8f73a0c50
children 2d7c810a1ac2
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
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
11 for (len = 0; len < maxlen; len++, s++) {
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
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
16 return len;
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
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
19 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
20 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
21
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
22 return length ? haystack : NULL;
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
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
25 char* EDL_internal_strdup(const char* s) {
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
26 const size_t len = strlen(s);
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
27
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
28 char* d = malloc((len + 1) * sizeof(char));
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
29 memcpy(d, s, len * sizeof(char));
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
30 d[len] = '\0';
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
31
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
32 return d;
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents:
diff changeset
33 }