diff --git a/src/strops.c b/strops.c similarity index 86% rename from src/strops.c rename to strops.c index 4d126e7..77525ce 100644 --- a/src/strops.c +++ b/strops.c @@ -37,7 +37,27 @@ char* strops_to_uppercase(const char* string) { return result; } -char* strops_insert_at_pos(const char* string, const char* string_to_insert, size_t pos) { +int strops_is_lowercase(const char* string) { + size_t i; + for (i = 0; i < strlen(string); i++) { + if (!(string[i] >= 'a' && string[i] <= 'z')) { + return 0; + } + } + return 1; +} + +int strops_is_uppercase(const char* string) { + size_t i; + for (i = 0; i < strlen(string); i++) { + if (!(string[i] >= 'A' && string[i] <= 'Z')) { + return 0; + } + } + return 1; +} + +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); @@ -54,7 +74,7 @@ char* strops_insert_at_pos(const char* string, const char* string_to_insert, siz return result; } -char* strops_remove_at_pos(const char* string, size_t pos) { +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)); memcpy(result, string, strlen(string)); @@ -71,7 +91,7 @@ char* strops_trim_right_whitespace(const char* string) { 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(result, strlen(result) - 1); + result = strops_remove_at_pos_char(result, strlen(result) - 1); } result = realloc(result, strlen(result)); return result; @@ -81,7 +101,7 @@ char* strops_trim_left_whitespace(const char* string) { 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(result, 0); + result = strops_remove_at_pos_char(result, 0); } result = realloc(result, strlen(result)); return result; @@ -99,7 +119,7 @@ char* strops_trim_right_chars(const char* string, const char* chars_to_remove) { 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(result, strlen(result) - 1); + result = strops_remove_at_pos_char(result, strlen(result) - 1); } result = realloc(result, strlen(result)); return result; @@ -109,7 +129,7 @@ char* strops_trim_left_chars(const char* string, const char* chars_to_remove) { char *result = malloc(strlen(string) + 1); memcpy(result, string, strlen(string)); while (strchr(chars_to_remove, result[0]) != NULL) { - result = strops_remove_at_pos(result, 0); + result = strops_remove_at_pos_char(result, 0); } result = realloc(result, strlen(result)); return result; @@ -133,7 +153,7 @@ char* strops_trim_right_string(const char* string, const char* string_to_remove) while (strcmp(tmp, string_to_remove) == 0) { size_t i; for (i = 0; i < strlen(string_to_remove); i++) { - result = strops_remove_at_pos(result, offset); + result = strops_remove_at_pos_char(result, offset); } offset = strlen(result) - strlen(string_to_remove); tmp = result + offset; @@ -152,7 +172,7 @@ char* strops_trim_left_string(const char* string, const char* string_to_remove) while (strcmp(tmp, string_to_remove) == 0) { size_t i; for (i = 0; i < strlen(string_to_remove); i++) { - result = strops_remove_at_pos(result, 0); + result = strops_remove_at_pos_char(result, 0); } memcpy(tmp, result, strlen(string_to_remove)); } @@ -177,7 +197,7 @@ char* strops_remove_chars(const char* string, const char* chars_to_remove) { size_t i; for (i = 0; i < strlen(chars_to_remove); i++) { while (strchr(result, chars_to_remove[i]) != NULL) { - result = strops_remove_at_pos(result, strlen(result) - strlen(strchr(result, chars_to_remove[i]))); + result = strops_remove_at_pos_char(result, strlen(result) - strlen(strchr(result, chars_to_remove[i]))); } } result = realloc(result, strlen(result)); @@ -193,7 +213,7 @@ char* strops_remove_string(const char* string, const char* string_to_remove) { size_t offset = strlen(result) - strlen(strstr(result, string_to_remove)); size_t i; for (i = 0; i < strlen(string_to_remove); i++) { - result = strops_remove_at_pos(result, offset); + result = strops_remove_at_pos_char(result, offset); } } result = realloc(result, strlen(result)); @@ -242,6 +262,10 @@ size_t strops_word_count(const char* string) { return word_count; } +int strops_is_url_encoded(const char* string) { + return -1; +} + char* strops_url_encode(const char* string) { /* multiplied by 3, because example: ( => %28. Three times the chars */ char *result = malloc(strlen(string) * 3 + 1); diff --git a/src/strops.h b/strops.h similarity index 83% rename from src/strops.h rename to strops.h index 089d6a6..6ea535e 100644 --- a/src/strops.h +++ b/strops.h @@ -12,9 +12,11 @@ char* strops_to_lowercase(const char* string); char* strops_to_uppercase(const char* string); +int strops_is_lowercase(const char* string); +int strops_is_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_insert_at_pos_string(const char* string, const char* string_to_insert, size_t pos); +char* strops_remove_at_pos_char(const char* string, size_t pos); char* strops_trim_right_whitespace(const char* string); char* strops_trim_left_whitespace(const char* string); @@ -34,6 +36,7 @@ char* strops_remove_string(const char* string, const char* string_to_remove); size_t strops_word_count(const char* string); /* https://www.w3schools.com/tags/ref_urlencode.asp */ +int strops_is_url_encoded(const char* string); char* strops_url_encode(const char* string); char* strops_url_decode(const char* string); diff --git a/src/tests.c b/tests.c similarity index 62% rename from src/tests.c rename to tests.c index a388f66..b899be6 100644 --- a/src/tests.c +++ b/tests.c @@ -1,5 +1,26 @@ #include "strops.h" #include +#include + +/* https://gist.githubusercontent.com/rexim/b5b0c38f53157037923e7cdd77ce685d/raw/86c3db57f485f3b6f7958f308ba7126fa81282d8/da_append.c */ +#define da_append(xs, x) \ + do { \ + if ((xs)->count >= (xs)->capacity) { \ + if ((xs)->capacity == 0) (xs)->capacity = 256; \ + else (xs)->capacity *= 2; \ + (xs)->items = realloc((xs)->items, (xs)->capacity*sizeof(*(xs)->items)); \ + } \ + \ + (xs)->items[(xs)->count++] = (x); \ + } while (0) + +typedef int(*fnptr)(); +typedef struct { + fnptr* items; + size_t capacity; + size_t count; + size_t amount_successful; +} Tests; /* Test template (Maybe make a macro in the future?) int test_() { @@ -39,24 +60,48 @@ int test_to_uppercase() { return 0; } -int test_insert_at_pos() { +int test_is_lowercase() { + char* input = "string"; + int expected = 1; + int result = strops_is_lowercase(input); + if (result != expected) { + printf("test_is_lowercase failed\n"); + printf("Got = %d\nExpected = %d\n", result, expected); + return 1; + } + return 0; +} + +int test_is_uppercase() { + char* input = "sTrInG"; + int expected = 0; + int result = strops_is_uppercase(input); + if (result != expected) { + printf("test_is_uppercase failed\n"); + printf("Got = %d\nExpected = %d\n", result, expected); + return 1; + } + return 0; +} + +int test_insert_at_pos_string() { char* input = "Heo, World!"; char* expected = "Hello, World!"; - char* result = strops_insert_at_pos(input, "ll", 2); + char* result = strops_insert_at_pos_string(input, "ll", 2); if (strcmp(result, expected) != 0) { - printf("test_insert_at_pos failed\n"); + printf("test_insert_at_pos_string 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() { char* input = "Hello"; char* expected = "Hllo"; - char* result = strops_remove_at_pos(input, 1); + char* result = strops_remove_at_pos_char(input, 1); if (strcmp(result, expected) != 0) { - printf("test_remove_at_pos failed\n"); + printf("test_remove_at_pos_char failed\n"); printf("Got = '%s'\nExpected = '%s'\n", result, expected); return 1; } @@ -207,6 +252,33 @@ int test_word_count() { return 0; } +int test_is_url_encoded() { + int ret = 0; + char* input; + int expected; + int result; + + input = "(artist)"; + expected = 0; + result = strops_is_url_encoded(input); + if (result != expected) { + printf("test_is_url_encoded failed\n"); + printf("Got = %d\nExpected = %d\n", result, expected); + ret = 1; + } + + input = "%28artist%29"; + expected = 1; + result = strops_is_url_encoded(input); + if (result != expected) { + printf("test_is_url_encoded failed\n"); + printf("Got = %d\nExpected = %d\n", result, expected); + ret = 1; + } + + return ret; +} + int test_url_encode() { char* input = "(artist)"; char* expected = "%28artist%29"; @@ -232,112 +304,43 @@ int test_url_decode() { } int main() { - int amount_of_tests = 18; - int amount_of_successful_tests = 0; - int ret; - - /* Test template - ret = test_(); - if (ret == 0) { - amount_of_successful_tests++; + Tests tests = { 0 }; + da_append(&tests, test_to_lowercase); + da_append(&tests, test_to_lowercase); + da_append(&tests, test_to_uppercase); + da_append(&tests, test_is_lowercase); + da_append(&tests, test_is_uppercase); + da_append(&tests, test_insert_at_pos_string); + da_append(&tests, test_remove_at_pos_char); + da_append(&tests, test_trim_right_whitespace); + da_append(&tests, test_trim_left_whitespace); + da_append(&tests, test_trim_both_whitespace); + da_append(&tests, test_trim_right_chars); + da_append(&tests, test_trim_left_chars); + da_append(&tests, test_trim_both_chars); + da_append(&tests, test_trim_right_string); + da_append(&tests, test_trim_left_string); + da_append(&tests, test_trim_both_string); + da_append(&tests, test_remove_chars); + da_append(&tests, test_remove_string); + da_append(&tests, test_word_count); + da_append(&tests, test_is_url_encoded); + da_append(&tests, test_url_encode); + da_append(&tests, test_url_decode); + + size_t i; + for (i = 0; i < tests.count; i++) { + if (tests.items[i]() == 0) { + tests.amount_successful++; } - */ - - ret = test_to_lowercase(); - if (ret == 0) { - amount_of_successful_tests++; } - ret = test_to_uppercase(); - if (ret == 0) { - amount_of_successful_tests++; - } - - ret = test_insert_at_pos(); - if (ret == 0) { - amount_of_successful_tests++; - } - - ret = test_remove_at_pos(); - if (ret == 0) { - amount_of_successful_tests++; - } - - ret = test_trim_right_whitespace(); - if (ret == 0) { - amount_of_successful_tests++; - } - - ret = test_trim_left_whitespace(); - if (ret == 0) { - amount_of_successful_tests++; - } - - ret = test_trim_both_whitespace(); - if (ret == 0) { - amount_of_successful_tests++; - } - - ret = test_trim_right_chars(); - if (ret == 0) { - amount_of_successful_tests++; - } - - ret = test_trim_left_chars(); - if (ret == 0) { - amount_of_successful_tests++; - } - - ret = test_trim_both_chars(); - if (ret == 0) { - amount_of_successful_tests++; - } - - ret = test_trim_right_string(); - if (ret == 0) { - amount_of_successful_tests++; - } - - ret = test_trim_left_string(); - if (ret == 0) { - amount_of_successful_tests++; - } - - ret = test_trim_both_string(); - if (ret == 0) { - amount_of_successful_tests++; - } - - ret = test_remove_chars(); - if (ret == 0) { - amount_of_successful_tests++; - } - - ret = test_remove_string(); - if (ret == 0) { - amount_of_successful_tests++; - } - - ret = test_word_count(); - if (ret == 0) { - amount_of_successful_tests++; - } - - ret = test_url_encode(); - if (ret == 0) { - amount_of_successful_tests++; - } - - ret = test_url_decode(); - if (ret == 0) { - amount_of_successful_tests++; - } - - if (amount_of_successful_tests != amount_of_tests) { - printf("%d/%d tests passed successfully\n", amount_of_successful_tests, amount_of_tests); + if (tests.amount_successful != tests.count) { + printf("%d/%d tests passed successfully\n", tests.amount_successful, tests.count); return 1; } else { printf("All tests passed successfully\n"); return 0; } } +