commit e167b6dbf0e90d90a749f18c87a15e4718d52869
parent 0f8cfe28ec4a8d57188798b296a0817ad22ae52f
Author: robert <robertrussell.72001@gmail.com>
Date: Wed, 10 Aug 2022 14:25:08 -0700
Namespace all str functions
I don't like long names, but strxxx functions are technically reserved,
so we should probably do this.
Diffstat:
2 files changed, 28 insertions(+), 19 deletions(-)
diff --git a/inc/cext/str.h b/inc/cext/str.h
@@ -5,13 +5,14 @@
#include "cext/def.h"
-char *estrdup(char *s);
+char *cext_str_dup(char *s);
+char *cext_str_edup(char *s);
-bool strstartswith(char *s, char *sub);
-bool strendswith(char *s, char *sub);
+bool cext_str_starts_with(char *s, char *sub);
+bool cext_str_ends_with(char *s, char *sub);
/* Return the number of the nonoverlapping occurences of sub in s. */
-usize strcount(char *s, char *sub);
+usize cext_str_count(char *s, char *sub);
/* Split s into substrings separated by sep (which must be nonempty) and return
* the number of separated substrings found. If n > 0, then fields must point
@@ -21,7 +22,7 @@ usize strcount(char *s, char *sub);
* If there is an allocation failure, set *fields to NULL and return 0. In
* either case, the elements of *fields are pointers into s, and s will be
* modified in order to null-terminate the substrings. */
-usize strsplit(char ***fields, char *s, char *sep, usize n);
+usize cext_str_split(char ***fields, char *s, char *sep, usize n);
-int vaprintf(char **s, const char *fmt, va_list args);
-int aprintf(char **s, const char *fmt, ...);
+int cext_vaprintf(char **s, const char *fmt, va_list args);
+int cext_aprintf(char **s, const char *fmt, ...);
diff --git a/src/str.c b/src/str.c
@@ -1,4 +1,4 @@
-#define _XOPEN_SOURCE 500 /* strdup, vsnprintf */
+#define _ISOC99_SOURCE /* vsnprintf */
#include <assert.h>
#include <stdio.h>
#include <string.h>
@@ -8,29 +8,37 @@
#include "cext/log.h"
#include "cext/str.h"
-/* TODO: strtoTYPE converters */
+/* TODO: str to int converters */
char *
-estrdup(char *s) {
- char *dup = strdup(s);
+cext_str_dup(char *s) {
+ char *dup = alloc(strlen(s) + 1);
+ if (!dup) return 0;
+ strcpy(dup, s);
+ return dup;
+}
+
+char *
+cext_str_edup(char *s) {
+ char *dup = cext_str_dup(s);
if (!dup) fatalf("allocation failure");
return dup;
}
bool
-strstartswith(char *s, char *sub) {
+cext_str_starts_with(char *s, char *sub) {
return !strncmp(s, sub, strlen(sub));
}
bool
-strendswith(char *s, char *sub) {
+cext_str_ends_with(char *s, char *sub) {
usize slen = strlen(s);
usize sublen = strlen(sub);
return slen >= sublen && !strcmp(s + slen - sublen, sub);
}
usize
-strcount(char *s, char *sub) {
+cext_str_count(char *s, char *sub) {
usize n = 0;
usize sublen = strlen(sub);
while ((s = strstr(s, sub))) {
@@ -41,11 +49,11 @@ strcount(char *s, char *sub) {
}
usize
-strsplit(char ***fields, char *s, char *sep, usize n) {
+cext_str_split(char ***fields, char *s, char *sep, usize n) {
assert(strcmp(sep, ""));
if (n == 0) {
- n = strcount(s, sep) + 1;
+ n = cext_str_count(s, sep) + 1;
if (!(*fields = allocn(n, sizeof **fields)))
return 0;
}
@@ -65,7 +73,7 @@ strsplit(char ***fields, char *s, char *sep, usize n) {
}
int
-vaprintf(char **s, const char *fmt, va_list args) {
+cext_vaprintf(char **s, const char *fmt, va_list args) {
va_list args2;
va_copy(args2, args);
int len = vsnprintf(0, 0, fmt, args2);
@@ -87,10 +95,10 @@ vaprintf(char **s, const char *fmt, va_list args) {
}
int
-aprintf(char **s, const char *fmt, ...) {
+cext_aprintf(char **s, const char *fmt, ...) {
va_list args;
va_start(args, fmt);
- int ret = vaprintf(s, fmt, args);
+ int ret = cext_vaprintf(s, fmt, args);
va_end(args);
return ret;
}