cleaup: cleanup code.

This commit is contained in:
NunoSempere 2022-12-13 20:25:35 +00:00
parent 7c155525fd
commit 618262d2a2
3 changed files with 7 additions and 11 deletions

Binary file not shown.

View File

@ -4,8 +4,6 @@
/* Inspired by https://libredirect.github.io/, but in C. */
// int str_replace_start(const char* string, const char* target, const char* replacement, char* output){
void str_init(char* str, int n){
for(int i=0; i<n; i++){
str[i] = ' ';
@ -14,8 +12,6 @@ void str_init(char* str, int n){
} // could also use <https://manpages.ubuntu.com/manpages/impish/man3/strinit.3pub.html>
int libre_redirect(const char* uri, char* output){
/* inv.riverside.rocks */
// max length
int l1 = strlen(uri);
int l2 = strlen(output);
@ -23,7 +19,7 @@ int libre_redirect(const char* uri, char* output){
return 1; // not enough memory.
}else{
char tmp_uri[l2++];
strcpy(tmp_uri, uri); // includes terminating '\0'
strcpy(tmp_uri, uri); // strcpy also copies the terminating '\0'
char* sites[] = {
"https://youtube.com",

View File

@ -10,7 +10,7 @@ See also:
*/
int str_replace_start(const char* string, const char* target, const char* replacement, char* output){
/* Checks */
int l1 = strlen(string);
int l2 = strlen(target);
int l3 = strlen(replacement);
@ -19,16 +19,16 @@ int str_replace_start(const char* string, const char* target, const char* replac
if((l4 < (l1 - l2 + l3)) || l4 < l1 ){
// Not enough memory in output string.
if(DEBUG) printf("Case 1.\n");
if(DEBUG) printf("String not long enough.\n");
return 1;
}
else if(l1 < l2){
// Not even possible that there is a match.
if(DEBUG) printf("Case 2.\n");
if(DEBUG) printf("Target larger than string.\n");
strcpy(output, string);
}
else {
if(DEBUG) printf("Case 3.\n");
if(DEBUG) printf("Looking for a match.\n");
int match = true;
for(int i=0; i<l2; i++){
if(string[i] != target[i]){
@ -37,7 +37,7 @@ int str_replace_start(const char* string, const char* target, const char* replac
}
}
if(match){
if(DEBUG) printf("Case 3.a.\n");
if(DEBUG) printf("Found match.\n");
for(int i=0; i<l3; i++){
output[i] = replacement[i];
}
@ -49,7 +49,7 @@ int str_replace_start(const char* string, const char* target, const char* replac
output[counter] = '\0';
}
else {
if(DEBUG) printf("Case 3.b.\n");
if(DEBUG) printf("Did not find match.\n");
strcpy(output, string);
}
}