Mercurial > libedl
annotate src/util.c @ 10:d6f8f73a0c50 v2.1
util: make strnchr an actual strnchr
author | Paper <paper@paper.us.eu.org> |
---|---|
date | Fri, 22 Mar 2024 20:51:40 -0400 |
parents | 0c98b46eaf73 |
children | 2d7c810a1ac2 |
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 } |