progress on url and new stuff

This commit is contained in:
2025-05-09 00:10:40 +02:00
parent fd30b35d10
commit 28a235b680
3 changed files with 397 additions and 141 deletions

View File

@ -7,14 +7,14 @@
/* Function template
char* strops_(const char* string) {
char *result = malloc(strlen(string));
char* result = malloc(strlen(string));
memcpy(result, string, strlen(string));
return result;
}
*/
char* strops_to_lowercase(const char* string) {
char *result = malloc(strlen(string));
char* result = malloc(strlen(string));
memcpy(result, string, strlen(string));
size_t i;
for (i = 0; i < strlen(string); i++) {
@ -26,7 +26,7 @@ char* strops_to_lowercase(const char* string) {
}
char* strops_to_uppercase(const char* string) {
char *result = malloc(strlen(string));
char* result = malloc(strlen(string));
memcpy(result, string, strlen(string));
size_t i;
for (i = 0; i < strlen(string); i++) {
@ -60,7 +60,7 @@ int strops_is_uppercase(const char* string) {
char* strops_insert_at_pos_string(const char* string, const char* string_to_insert, size_t pos) {
assert(pos <= strlen(string) && "pos needs to be inside string");
size_t string_length = strlen(string) + strlen(string_to_insert) + 1;
char *result = malloc(string_length);
char* result = malloc(string_length);
memcpy(result, string, strlen(string));
size_t i, j;
/* Make space for string_to_insert */
@ -76,19 +76,30 @@ char* strops_insert_at_pos_string(const char* string, const char* string_to_inse
char* strops_remove_at_pos_char(const char* string, size_t pos) {
assert(pos <= strlen(string) && "pos needs to be inside string");
char *result = malloc(strlen(string));
char* result = malloc(strlen(string) + 1);
memcpy(result, string, strlen(string));
result[pos] = 0;
size_t i;
for (i = pos; i < strlen(string); i++) {
result[i] = result[i + 1];
}
result = realloc(result, strlen(result));
return result;
}
char* strops_remove_at_pos_string(const char* string, const char* string_to_remove, size_t pos) {
char* result = malloc(strlen(string));
memcpy(result, string, strlen(string));
return result;
}
char* strops_replace_at_pos_string(const char* string, const char* string_to_remove, const char* string_to_insert, size_t pos) {
char* result = malloc(strlen(string));
memcpy(result, string, strlen(string));
return result;
}
char* strops_trim_right_whitespace(const char* string) {
char *result = malloc(strlen(string) + 1);
char* result = malloc(strlen(string) + 1);
memcpy(result, string, strlen(string));
while (strchr("\t\n\v\f\r ", result[strlen(result) - 1]) != NULL) {
result = strops_remove_at_pos_char(result, strlen(result) - 1);
@ -98,7 +109,7 @@ char* strops_trim_right_whitespace(const char* string) {
}
char* strops_trim_left_whitespace(const char* string) {
char *result = malloc(strlen(string) + 1);
char* result = malloc(strlen(string) + 1);
memcpy(result, string, strlen(string));
while (strchr("\t\n\v\f\r ", result[0]) != NULL) {
result = strops_remove_at_pos_char(result, 0);
@ -108,7 +119,7 @@ char* strops_trim_left_whitespace(const char* string) {
}
char* strops_trim_both_whitespace(const char* string) {
char *result = malloc(strlen(string) + 1);
char* result = malloc(strlen(string) + 1);
memcpy(result, string, strlen(string));
result = strops_trim_right_whitespace(result);
result = strops_trim_left_whitespace(result);
@ -116,7 +127,7 @@ char* strops_trim_both_whitespace(const char* string) {
}
char* strops_trim_right_chars(const char* string, const char* chars_to_remove) {
char *result = malloc(strlen(string) + 1);
char* result = malloc(strlen(string) + 1);
memcpy(result, string, strlen(string));
while (strchr(chars_to_remove, result[strlen(result) - 1]) != NULL) {
result = strops_remove_at_pos_char(result, strlen(result) - 1);
@ -126,7 +137,7 @@ char* strops_trim_right_chars(const char* string, const char* chars_to_remove) {
}
char* strops_trim_left_chars(const char* string, const char* chars_to_remove) {
char *result = malloc(strlen(string) + 1);
char* result = malloc(strlen(string) + 1);
memcpy(result, string, strlen(string));
while (strchr(chars_to_remove, result[0]) != NULL) {
result = strops_remove_at_pos_char(result, 0);
@ -136,7 +147,7 @@ char* strops_trim_left_chars(const char* string, const char* chars_to_remove) {
}
char* strops_trim_both_chars(const char* string, const char* chars_to_remove) {
char *result = malloc(strlen(string) + 1);
char* result = malloc(strlen(string) + 1);
memcpy(result, string, strlen(string));
result = strops_trim_right_chars(result, chars_to_remove);
result = strops_trim_left_chars(result, chars_to_remove);
@ -145,7 +156,7 @@ char* strops_trim_both_chars(const char* string, const char* chars_to_remove) {
char* strops_trim_right_string(const char* string, const char* string_to_remove) {
assert(strlen(string) >= strlen(string_to_remove) && "string_to_remove cannot be bigger than string");
char *result = malloc(strlen(string) + 1);
char* result = malloc(strlen(string) + 1);
memcpy(result, string, strlen(string));
size_t offset = strlen(result) - strlen(string_to_remove);
@ -164,10 +175,10 @@ char* strops_trim_right_string(const char* string, const char* string_to_remove)
char* strops_trim_left_string(const char* string, const char* string_to_remove) {
assert(strlen(string) >= strlen(string_to_remove) && "string_to_remove cannot be bigger than string");
char *result = malloc(strlen(string) + 1);
char* result = malloc(strlen(string) + 1);
memcpy(result, string, strlen(string));
char *tmp = malloc(strlen(string_to_remove) + 1);
char* tmp = malloc(strlen(string_to_remove) + 1);
memcpy(tmp, result, strlen(string_to_remove));
while (strcmp(tmp, string_to_remove) == 0) {
size_t i;
@ -183,7 +194,7 @@ char* strops_trim_left_string(const char* string, const char* string_to_remove)
char* strops_trim_both_string(const char* string, const char* string_to_remove) {
assert(strlen(string) >= strlen(string_to_remove) && "string_to_remove cannot be bigger than string");
char *result = malloc(strlen(string) + 1);
char* result = malloc(strlen(string) + 1);
memcpy(result, string, strlen(string));
result = strops_trim_right_string(result, string_to_remove);
result = strops_trim_left_string(result, string_to_remove);
@ -191,7 +202,7 @@ char* strops_trim_both_string(const char* string, const char* string_to_remove)
}
char* strops_remove_chars(const char* string, const char* chars_to_remove) {
char *result = malloc(strlen(string) + 1);
char* result = malloc(strlen(string) + 1);
memcpy(result, string, strlen(string));
size_t i;
@ -206,7 +217,7 @@ char* strops_remove_chars(const char* string, const char* chars_to_remove) {
char* strops_remove_string(const char* string, const char* string_to_remove) {
assert(strlen(string) >= strlen(string_to_remove) && "string_to_remove cannot be bigger than string");
char *result = malloc(strlen(string) + 1);
char* result = malloc(strlen(string) + 1);
memcpy(result, string, strlen(string));
while (strstr(result, string_to_remove) != NULL) {
@ -222,8 +233,8 @@ char* strops_remove_string(const char* string, const char* string_to_remove) {
size_t strops_word_count(const char* string) {
size_t word_count = 0;
char *tmp = malloc(strlen(string) + 1);
memcpy(tmp, string, strlen(string));
char* tmp = malloc(strlen(string) + 1);
memcpy(tmp, string, strlen(string));char* strpos_remove_at_pos_string(const char* string, const char* string_to_remove, size_t pos);
size_t i = 0;
size_t beginning_of_word = 0;
@ -262,23 +273,48 @@ size_t strops_word_count(const char* string) {
return word_count;
}
/* TODO: verify that this is correct */
int strops_is_url_encoded(const char* string) {
return -1;
if (strchr(string, '%') != NULL) {
return 1;
}
return 0;
}
char* strops_url_encode(const char* string) {
if (strops_is_url_encoded(string)) { return NULL; }
/* multiplied by 3, because example: ( => %28. Three times the chars */
char *result = malloc(strlen(string) * 3 + 1);
char* result = malloc(strlen(string) + 1);
memcpy(result, string, strlen(string));
char *unsafe_chars = "";
char* unsafe_chars = "()";
char replacement[4];
replacement[0] = '%';
replacement[3] = '\0';
size_t i, j;
for (i = 0; i < strlen(unsafe_chars); i++) {
j = 0;
while (strchr(result, unsafe_chars[i]) != NULL) {
if (result[j] == unsafe_chars[i]) {
char bad = result[j];
sprintf(replacement + 1, "%02X", bad);
result = strops_remove_at_pos_char(result, j);
result = strops_insert_at_pos_string(result, replacement, j);
j += 2;
} else {
j += 1;
}
}
}
result = realloc(result, strlen(result));
return result;
}
char* strops_url_decode(const char* string) {
char *result = malloc(strlen(string) + 1);
if (!strops_is_url_encoded(string)) { return NULL; }
char* result = malloc(strlen(string) + 1);
memcpy(result, string, strlen(string));
result = realloc(result, strlen(result));
return result;
}