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
This commit is contained in:
Christian Göttsche
2020-10-03 21:20:43 +02:00
parent 577416d1a9
commit 4c66eb6d4c
13 changed files with 77 additions and 65 deletions

View File

@ -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;
return xStrndup(in, len);
}
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);
}
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;
}