summaryrefslogtreecommitdiffstats
path: root/XUtils.c
diff options
context:
space:
mode:
authorChristian Göttsche <cgzones@googlemail.com>2020-10-03 21:20:43 +0200
committerChristian Göttsche <cgzones@googlemail.com>2020-10-19 15:38:45 +0200
commit4c66eb6d4cbdddc658e5f0274d8130155c6013f1 (patch)
tree8446343e944911b68b590a4bb5f834476f13230b /XUtils.c
parent577416d1a946382ab9f0c523e5fae755f9d71f69 (diff)
XUtils string related updates
- allow count out-parameter of String_split() to be NULL - introduce xStrndup() - do not allow NULL pointers passed to String_eq() it is not used in any code - implement String_startsWith(), String_contains_i() and String_eq() as inline header functions - adjust several conversion issues
Diffstat (limited to 'XUtils.c')
-rw-r--r--XUtils.c68
1 files changed, 32 insertions, 36 deletions
diff --git a/XUtils.c b/XUtils.c
index 3bdcba84..a444a7f8 100644
--- a/XUtils.c
+++ b/XUtils.c
@@ -54,11 +54,11 @@ void* xRealloc(void* ptr, size_t size) {
}
char* String_cat(const char* s1, const char* s2) {
- int l1 = strlen(s1);
- int l2 = strlen(s2);
+ const size_t l1 = strlen(s1);
+ const size_t l2 = strlen(s2);
char* out = xMalloc(l1 + l2 + 1);
memcpy(out, s1, l1);
- memcpy(out+l1, s2, l2+1);
+ memcpy(out+l1, s2, l2);
out[l1 + l2] = '\0';
return out;
}
@@ -67,39 +67,24 @@ char* String_trim(const char* in) {
while (in[0] == ' ' || in[0] == '\t' || in[0] == '\n') {
in++;
}
- int len = strlen(in);
+
+ size_t len = strlen(in);
while (len > 0 && (in[len-1] == ' ' || in[len-1] == '\t' || in[len-1] == '\n')) {
len--;
}
- char* out = xMalloc(len+1);
- strncpy(out, in, len);
- out[len] = '\0';
- return out;
-}
-inline int String_eq(const char* s1, const char* s2) {
- if (s1 == NULL || s2 == NULL) {
- if (s1 == NULL && s2 == NULL)
- return 1;
- else
- return 0;
- }
- return (strcmp(s1, s2) == 0);
+ return xStrndup(in, len);
}
-char** String_split(const char* s, char sep, int* n) {
- *n = 0;
- const int rate = 10;
+char** String_split(const char* s, char sep, size_t* n) {
+ const unsigned int rate = 10;
char** out = xCalloc(rate, sizeof(char*));
- int ctr = 0;
- int blocks = rate;
- char* where;
+ size_t ctr = 0;
+ unsigned int blocks = rate;
+ const char* where;
while ((where = strchr(s, sep)) != NULL) {
- int size = where - s;
- char* token = xMalloc(size + 1);
- strncpy(token, s, size);
- token[size] = '\0';
- out[ctr] = token;
+ size_t size = (size_t)(where - s);
+ out[ctr] = xStrndup(s, size);
ctr++;
if (ctr == blocks) {
blocks += rate;
@@ -113,7 +98,10 @@ char** String_split(const char* s, char sep, int* n) {
}
out = xRealloc(out, sizeof(char*) * (ctr + 1));
out[ctr] = NULL;
- *n = ctr;
+
+ if (n)
+ *n = ctr;
+
return out;
}
@@ -121,28 +109,28 @@ void String_freeArray(char** s) {
if (!s) {
return;
}
- for (int i = 0; s[i] != NULL; i++) {
+ for (size_t i = 0; s[i] != NULL; i++) {
free(s[i]);
}
free(s);
}
char* String_getToken(const char* line, const unsigned short int numMatch) {
- const unsigned short int len = strlen(line);
+ const size_t len = strlen(line);
char inWord = 0;
unsigned short int count = 0;
char match[50];
- unsigned short int foundCount = 0;
+ size_t foundCount = 0;
- for (unsigned short int i = 0; i < len; i++) {
+ for (size_t i = 0; i < len; i++) {
char lastState = inWord;
inWord = line[i] == ' ' ? 0:1;
if (lastState == 0 && inWord == 1)
count++;
- if(inWord == 1){
+ if (inWord == 1){
if (count == numMatch && line[i] != ' ' && line[i] != '\0' && line[i] != '\n' && line[i] != (char)EOF) {
match[foundCount] = line[i];
foundCount++;
@@ -155,8 +143,8 @@ char* String_getToken(const char* line, const unsigned short int numMatch) {
}
char* String_readLine(FILE* fd) {
- const int step = 1024;
- int bufSize = step;
+ const unsigned int step = 1024;
+ unsigned int bufSize = step;
char* buffer = xMalloc(step + 1);
char* at = buffer;
for (;;) {
@@ -213,3 +201,11 @@ char* xStrdup(const char* str) {
}
return data;
}
+
+char* xStrndup(const char* str, size_t len) {
+ char* data = strndup(str, len);
+ if (!data) {
+ fail();
+ }
+ return data;
+}

© 2014-2024 Faster IT GmbH | imprint | privacy policy