Create: sc_str_find_replace()

Find and replace all matches in a string
This commit is contained in:
brunoais 2022-04-16 14:45:51 +01:00
parent 2056a21538
commit 3e69f696c1
2 changed files with 91 additions and 0 deletions

View file

@ -48,6 +48,76 @@ truncated:
return n;
}
int64_t
sc_str_find_replace(char* input, char* find, char* replace, char** output) {
uint64_t inputLen;
int64_t findLen = strlen(find);
int64_t replaceLen = strlen(replace);
if(findLen < 1){
return 0;
}
uint64_t first_match = -1;
uint32_t matchCount = 0;
for(inputLen = 0; input[inputLen] != '\0'; inputLen++){
// strncmp and not memcmp because it needs to detect null terminating string. Otherwise, it may overrrun.
if(input[inputLen] == find[0] && strncmp(&(input[inputLen]), find, findLen) == 0){
if(first_match == (uint64_t) -1){
first_match = inputLen;
}
matchCount++;
}
}
printf("matches: %u; \n", matchCount);
inputLen++;
if(matchCount == 0){
return 0;
}
int64_t output_size = inputLen - ( matchCount * (findLen - replaceLen));
if(output_size <= 0){
return -2;
}
// Caller is responsible for freeing this
char* output_str = malloc(output_size);
if(output_str == NULL){
printf("inputi: %p\n", output_str);
return -2;
}
*output = output_str;
memcpy(output_str, input, first_match);
uint64_t inputi = first_match;
uint64_t desti = first_match;
while (input[inputi] != '\0') {
printf("inputi: %lu; %i; %lu; %lu; %s\n", inputi, input[inputi] == find[0], output_size, inputLen, &(input[inputi]));
if(input[inputi] == find[0] && strncmp(&(input[inputi]), find, findLen) == 0){
memcpy(&(output_str[desti]), replace, replaceLen);
// printf("replaced: %s\n", output_str);
desti += replaceLen;
inputi += findLen;
} else {
// printf("outputstr: %s\n", output_str);
output_str[desti++] = input[inputi++];
}
}
output_str[output_size - 1] = '\0';
return output_size;
}
char *
sc_str_quote(const char *src) {
size_t len = strlen(src);

View file

@ -26,6 +26,27 @@ sc_strncpy(char *dest, const char *src, size_t n);
size_t
sc_str_join(char *dst, const char *const tokens[], char sep, size_t n);
/**
* String find and replace all occurrences.
* Compatible with different sizes of find and the replacement
* Only creates output string if changes exist
* WARNING: You are responsible for freeing the output if it's not NULL
*
* @param input The string to find on and replace with matches found
* @param find What to find in the input
* @param replace What to replace with for each found instance from find
* @param output Null or a pointer to the char* which contains the replaced char*
* @return int64_t
* if > 0: The size of the string in output
* if < 1: output should be ignored and:
* if == 0: Nothing changed from the original string
* if == -2: Overflow when trying to create the replaced string or OOM
*
*
*/
int64_t
sc_str_find_replace(char* input, char* find, char* replace, char** output);
/**
* Quote a string
*