text stringlengths 0 357 |
|---|
void dc_trim(char* buf) |
{ |
dc_ltrim(buf); |
dc_rtrim(buf); |
} |
void dc_strlower_in_place(char* in) |
{ |
char* p = in; |
for ( ; *p; p++) { |
*p = tolower(*p); |
} |
} |
char* dc_strlower(const char* in) /* the result must be free()'d */ |
{ |
char* out = dc_strdup(in); |
char* p = out; |
for ( ; *p; p++) { |
*p = tolower(*p); |
} |
return out; |
} |
/* |
* haystack may be realloc()'d, returns the number of replacements. |
*/ |
int dc_str_replace(char** haystack, const char* needle, const char* replacement) |
{ |
int replacements = 0; |
int start_search_pos = 0; |
int needle_len = 0; |
int replacement_len = 0; |
if (haystack==NULL || *haystack==NULL || needle==NULL || needle[0]==0) { |
return 0; |
} |
needle_len = strlen(needle); |
replacement_len = replacement? strlen(replacement) : 0; |
while (1) |
{ |
char* p2 = strstr((*haystack)+start_search_pos, needle); |
if (p2==NULL) { break; } |
start_search_pos = (p2-(*haystack))+replacement_len; /* avoid recursion and skip the replaced part */ |
*p2 = 0; |
p2 += needle_len; |
char* new_string = dc_mprintf("%s%s%s", *haystack, replacement? replacement : "", p2); |
free(*haystack); |
*haystack = new_string; |
replacements++; |
} |
return replacements; |
} |
int dc_str_contains(const char* haystack, const char* needle) |
{ |
/* case-insensitive search of needle in haystack, return 1 if found, 0 if not */ |
if (haystack==NULL || needle==NULL) { |
return 0; |
} |
if (strstr(haystack, needle)!=NULL) { |
return 1; |
} |
char* haystack_lower = dc_strlower(haystack); |
char* needle_lower = dc_strlower(needle); |
int ret = strstr(haystack_lower, needle_lower)? 1 : 0; |
free(haystack_lower); |
free(needle_lower); |
return ret; |
} |
/** |
* Creates a null-terminated string from a buffer. |
* Similar to strndup() but allows bad parameters and just halts the program |
* on memory allocation errors. |
* |
* @param in The start of the string. |
* @param bytes The number of bytes to take from the string. |
* @return The null-terminates string, must be free()'d by the caller. |
* On memory-allocation errors, the program halts. |
* On other errors, an empty string is returned. |
*/ |
char* dc_null_terminate(const char* in, int bytes) /* the result must be free()'d */ |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.