Skip to content

Commit 0392a7d

Browse files
committed
Add string trimming functions
This patch is submitted under X11
1 parent 770298e commit 0392a7d

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

utils.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,3 +104,14 @@ int dsock_random(uint8_t *buf, size_t len, int64_t deadline) {
104104
return 0;
105105
}
106106

107+
const char *dsock_lstrip(const char *string, char delim) {
108+
const char *pos = string;
109+
while(*pos && *pos == delim) ++pos;
110+
return pos;
111+
}
112+
113+
const char *dsock_rstrip(const char *string, char delim) {
114+
const char *end = string + strlen(string) - 1;
115+
while(end > string && *end == delim) --end;
116+
return ++end;
117+
}

utils.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include <stdlib.h>
3030
#include <stdint.h>
3131
#include <stdio.h>
32+
#include <string.h>
3233

3334
#define dsock_concat(x,y) x##y
3435

@@ -79,5 +80,11 @@ void dsock_putll(uint8_t *buf, uint64_t val);
7980

8081
int dsock_random(uint8_t *buf, size_t len, int64_t deadline);
8182

83+
/* Returns a pointer to the first character in string that is not delim */
84+
const char *dsock_lstrip(const char *string, char delim);
85+
86+
/* Returns a pointer after the last character in string that is not delim */
87+
const char *dsock_rstrip(const char *string, char delim);
88+
8289
#endif
8390

0 commit comments

Comments
 (0)