add strops_insert_at_pos
This commit is contained in:
49
src/strops.c
49
src/strops.c
@ -37,7 +37,25 @@ char* strops_to_uppercase(const char* string) {
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
char* strops_insert_at_pos(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);
|
||||||
|
memcpy(result, string, strlen(string));
|
||||||
|
size_t i, j;
|
||||||
|
/* Make space for string_to_insert */
|
||||||
|
for (i = string_length - 1; i > pos; i--) {
|
||||||
|
result[i] = result[i - strlen(string_to_insert)];
|
||||||
|
}
|
||||||
|
/* Insert string into empty space */
|
||||||
|
for (j = 0; j < strlen(string_to_insert); j++) {
|
||||||
|
result[pos + j] = string_to_insert[j];
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
char* strops_remove_at_pos(const char* string, size_t pos) {
|
char* strops_remove_at_pos(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));
|
||||||
memcpy(result, string, strlen(string));
|
memcpy(result, string, strlen(string));
|
||||||
result[pos] = 0;
|
result[pos] = 0;
|
||||||
@ -50,7 +68,7 @@ char* strops_remove_at_pos(const char* string, size_t pos) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
char* strops_trim_right_whitespace(const char* string) {
|
char* strops_trim_right_whitespace(const char* string) {
|
||||||
char *result = malloc(strlen(string));
|
char *result = malloc(strlen(string) + 1);
|
||||||
memcpy(result, string, strlen(string));
|
memcpy(result, string, strlen(string));
|
||||||
while (strchr("\t\n\v\f\r ", result[strlen(result) - 1]) != NULL) {
|
while (strchr("\t\n\v\f\r ", result[strlen(result) - 1]) != NULL) {
|
||||||
result = strops_remove_at_pos(result, strlen(result) - 1);
|
result = strops_remove_at_pos(result, strlen(result) - 1);
|
||||||
@ -60,7 +78,7 @@ char* strops_trim_right_whitespace(const char* string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
char* strops_trim_left_whitespace(const char* string) {
|
char* strops_trim_left_whitespace(const char* string) {
|
||||||
char *result = malloc(strlen(string));
|
char *result = malloc(strlen(string) + 1);
|
||||||
memcpy(result, string, strlen(string));
|
memcpy(result, string, strlen(string));
|
||||||
while (strchr("\t\n\v\f\r ", result[0]) != NULL) {
|
while (strchr("\t\n\v\f\r ", result[0]) != NULL) {
|
||||||
result = strops_remove_at_pos(result, 0);
|
result = strops_remove_at_pos(result, 0);
|
||||||
@ -70,7 +88,7 @@ char* strops_trim_left_whitespace(const char* string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
char* strops_trim_both_whitespace(const char* string) {
|
char* strops_trim_both_whitespace(const char* string) {
|
||||||
char *result = malloc(strlen(string));
|
char *result = malloc(strlen(string) + 1);
|
||||||
memcpy(result, string, strlen(string));
|
memcpy(result, string, strlen(string));
|
||||||
result = strops_trim_right_whitespace(result);
|
result = strops_trim_right_whitespace(result);
|
||||||
result = strops_trim_left_whitespace(result);
|
result = strops_trim_left_whitespace(result);
|
||||||
@ -78,7 +96,7 @@ char* strops_trim_both_whitespace(const char* string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
char* strops_trim_right_chars(const char* string, const char* chars_to_remove) {
|
char* strops_trim_right_chars(const char* string, const char* chars_to_remove) {
|
||||||
char *result = malloc(strlen(string));
|
char *result = malloc(strlen(string) + 1);
|
||||||
memcpy(result, string, strlen(string));
|
memcpy(result, string, strlen(string));
|
||||||
while (strchr(chars_to_remove, result[strlen(result) - 1]) != NULL) {
|
while (strchr(chars_to_remove, result[strlen(result) - 1]) != NULL) {
|
||||||
result = strops_remove_at_pos(result, strlen(result) - 1);
|
result = strops_remove_at_pos(result, strlen(result) - 1);
|
||||||
@ -88,7 +106,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* strops_trim_left_chars(const char* string, const char* chars_to_remove) {
|
||||||
char *result = malloc(strlen(string));
|
char *result = malloc(strlen(string) + 1);
|
||||||
memcpy(result, string, strlen(string));
|
memcpy(result, string, strlen(string));
|
||||||
while (strchr(chars_to_remove, result[0]) != NULL) {
|
while (strchr(chars_to_remove, result[0]) != NULL) {
|
||||||
result = strops_remove_at_pos(result, 0);
|
result = strops_remove_at_pos(result, 0);
|
||||||
@ -98,7 +116,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* strops_trim_both_chars(const char* string, const char* chars_to_remove) {
|
||||||
char *result = malloc(strlen(string));
|
char *result = malloc(strlen(string) + 1);
|
||||||
memcpy(result, string, strlen(string));
|
memcpy(result, string, strlen(string));
|
||||||
result = strops_trim_right_chars(result, chars_to_remove);
|
result = strops_trim_right_chars(result, chars_to_remove);
|
||||||
result = strops_trim_left_chars(result, chars_to_remove);
|
result = strops_trim_left_chars(result, chars_to_remove);
|
||||||
@ -107,7 +125,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) {
|
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");
|
assert(strlen(string) >= strlen(string_to_remove) && "string_to_remove cannot be bigger than string");
|
||||||
char *result = malloc(strlen(string));
|
char *result = malloc(strlen(string) + 1);
|
||||||
memcpy(result, string, strlen(string));
|
memcpy(result, string, strlen(string));
|
||||||
|
|
||||||
size_t offset = strlen(result) - strlen(string_to_remove);
|
size_t offset = strlen(result) - strlen(string_to_remove);
|
||||||
@ -126,7 +144,7 @@ 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) {
|
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");
|
assert(strlen(string) >= strlen(string_to_remove) && "string_to_remove cannot be bigger than string");
|
||||||
char *result = malloc(strlen(string));
|
char *result = malloc(strlen(string) + 1);
|
||||||
memcpy(result, string, strlen(string));
|
memcpy(result, string, strlen(string));
|
||||||
|
|
||||||
char *tmp = malloc(strlen(string_to_remove) + 1);
|
char *tmp = malloc(strlen(string_to_remove) + 1);
|
||||||
@ -145,7 +163,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) {
|
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");
|
assert(strlen(string) >= strlen(string_to_remove) && "string_to_remove cannot be bigger than string");
|
||||||
char *result = malloc(strlen(string));
|
char *result = malloc(strlen(string) + 1);
|
||||||
memcpy(result, string, strlen(string));
|
memcpy(result, string, strlen(string));
|
||||||
result = strops_trim_right_string(result, string_to_remove);
|
result = strops_trim_right_string(result, string_to_remove);
|
||||||
result = strops_trim_left_string(result, string_to_remove);
|
result = strops_trim_left_string(result, string_to_remove);
|
||||||
@ -153,7 +171,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* strops_remove_chars(const char* string, const char* chars_to_remove) {
|
||||||
char *result = malloc(strlen(string));
|
char *result = malloc(strlen(string) + 1);
|
||||||
memcpy(result, string, strlen(string));
|
memcpy(result, string, strlen(string));
|
||||||
|
|
||||||
size_t i;
|
size_t i;
|
||||||
@ -168,7 +186,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) {
|
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");
|
assert(strlen(string) >= strlen(string_to_remove) && "string_to_remove cannot be bigger than string");
|
||||||
char *result = malloc(strlen(string));
|
char *result = malloc(strlen(string) + 1);
|
||||||
memcpy(result, string, strlen(string));
|
memcpy(result, string, strlen(string));
|
||||||
|
|
||||||
while (strstr(result, string_to_remove) != NULL) {
|
while (strstr(result, string_to_remove) != NULL) {
|
||||||
@ -184,7 +202,7 @@ char* strops_remove_string(const char* string, const char* string_to_remove) {
|
|||||||
|
|
||||||
size_t strops_word_count(const char* string) {
|
size_t strops_word_count(const char* string) {
|
||||||
size_t word_count = 0;
|
size_t word_count = 0;
|
||||||
char *tmp = malloc(strlen(string));
|
char *tmp = malloc(strlen(string) + 1);
|
||||||
memcpy(tmp, string, strlen(string));
|
memcpy(tmp, string, strlen(string));
|
||||||
|
|
||||||
size_t i = 0;
|
size_t i = 0;
|
||||||
@ -225,14 +243,17 @@ size_t strops_word_count(const char* string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
char* strops_url_encode(const char* string) {
|
char* strops_url_encode(const char* string) {
|
||||||
char *result = malloc(strlen(string) * 3); /* multiplied by 3, because example: ( => %28. Three times the chars */
|
/* multiplied by 3, because example: ( => %28. Three times the chars */
|
||||||
|
char *result = malloc(strlen(string) * 3 + 1);
|
||||||
memcpy(result, string, strlen(string));
|
memcpy(result, string, strlen(string));
|
||||||
|
char *unsafe_chars = "";
|
||||||
|
|
||||||
result = realloc(result, strlen(result));
|
result = realloc(result, strlen(result));
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
char* strops_url_decode(const char* string) {
|
char* strops_url_decode(const char* string) {
|
||||||
char *result = malloc(strlen(string));
|
char *result = malloc(strlen(string) + 1);
|
||||||
memcpy(result, string, strlen(string));
|
memcpy(result, string, strlen(string));
|
||||||
result = realloc(result, strlen(result));
|
result = realloc(result, strlen(result));
|
||||||
return result;
|
return result;
|
||||||
|
@ -13,6 +13,7 @@
|
|||||||
char* strops_to_lowercase(const char* string);
|
char* strops_to_lowercase(const char* string);
|
||||||
char* strops_to_uppercase(const char* string);
|
char* strops_to_uppercase(const char* string);
|
||||||
|
|
||||||
|
char* strops_insert_at_pos(const char* string, const char* string_to_insert, size_t pos);
|
||||||
char* strops_remove_at_pos(const char* string, size_t pos);
|
char* strops_remove_at_pos(const char* string, size_t pos);
|
||||||
|
|
||||||
char* strops_trim_right_whitespace(const char* string);
|
char* strops_trim_right_whitespace(const char* string);
|
||||||
@ -32,6 +33,7 @@ char* strops_remove_string(const char* string, const char* string_to_remove);
|
|||||||
|
|
||||||
size_t strops_word_count(const char* string);
|
size_t strops_word_count(const char* string);
|
||||||
|
|
||||||
|
/* https://www.w3schools.com/tags/ref_urlencode.asp */
|
||||||
char* strops_url_encode(const char* string);
|
char* strops_url_encode(const char* string);
|
||||||
char* strops_url_decode(const char* string);
|
char* strops_url_decode(const char* string);
|
||||||
|
|
||||||
|
19
src/tests.c
19
src/tests.c
@ -39,6 +39,18 @@ int test_to_uppercase() {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int test_insert_at_pos() {
|
||||||
|
char* input = "Heo, World!";
|
||||||
|
char* expected = "Hello, World!";
|
||||||
|
char* result = strops_insert_at_pos(input, "ll", 2);
|
||||||
|
if (strcmp(result, expected) != 0) {
|
||||||
|
printf("test_insert_at_pos failed\n");
|
||||||
|
printf("Got = '%s'\nExpected = '%s'\n", result, expected);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
int test_remove_at_pos() {
|
int test_remove_at_pos() {
|
||||||
char* input = "Hello";
|
char* input = "Hello";
|
||||||
char* expected = "Hllo";
|
char* expected = "Hllo";
|
||||||
@ -220,7 +232,7 @@ int test_url_decode() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
int amount_of_tests = 17;
|
int amount_of_tests = 18;
|
||||||
int amount_of_successful_tests = 0;
|
int amount_of_successful_tests = 0;
|
||||||
int ret;
|
int ret;
|
||||||
|
|
||||||
@ -241,6 +253,11 @@ int main() {
|
|||||||
amount_of_successful_tests++;
|
amount_of_successful_tests++;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ret = test_insert_at_pos();
|
||||||
|
if (ret == 0) {
|
||||||
|
amount_of_successful_tests++;
|
||||||
|
}
|
||||||
|
|
||||||
ret = test_remove_at_pos();
|
ret = test_remove_at_pos();
|
||||||
if (ret == 0) {
|
if (ret == 0) {
|
||||||
amount_of_successful_tests++;
|
amount_of_successful_tests++;
|
||||||
|
Reference in New Issue
Block a user