From a9cb36e2f6908e55912076eb5cc8d0faa4b58ba3 Mon Sep 17 00:00:00 2001 From: AustrianToast Date: Wed, 7 May 2025 18:15:24 +0200 Subject: [PATCH] already started, but not done --- .gitignore | 2 + Makefile | 11 +++++ src/strops.c | 78 ++++++++++++++++++++++++++++++++ src/strops.h | 38 ++++++++++++++++ src/tests.c | 122 +++++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 251 insertions(+) create mode 100644 .gitignore create mode 100644 Makefile create mode 100644 src/strops.c create mode 100644 src/strops.h create mode 100644 src/tests.c diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..f8dd6e2 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +# executables +build diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..09d3a07 --- /dev/null +++ b/Makefile @@ -0,0 +1,11 @@ +test: debug + gcc -ansi -L./build -o build/tests src/tests.c -lstrops + +debug: + gcc -c -ansi -ggdb -o build/strops.o src/strops.c + gcc -shared -o build/libstrops.so build/strops.o + +#release: +# gcc -ansi -O2 -pipe -fno-semantic-interposition -o build/hdb src/main.c src/db.c -lsqlite3 +# strip build/hdb + diff --git a/src/strops.c b/src/strops.c new file mode 100644 index 0000000..13092dc --- /dev/null +++ b/src/strops.c @@ -0,0 +1,78 @@ +#include "strops.h" +#include +#include +#include + +#include + +/* Function template +char* strops_(const char* 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)); + memcpy(result, string, strlen(string)); + size_t i; + for(i = 0; i < strlen(string); i++) { + if (result[i] >= 'A' && result[i] <= 'Z') { + result[i] += 32; + } + } + return result; +} + +char* strops_to_uppercase(const char* string) { + char *result = malloc(strlen(string)); + memcpy(result, string, strlen(string)); + size_t i; + for(i = 0; i < strlen(string); i++) { + if (result[i] >= 'a' && result[i] <= 'z') { + result[i] -= 32; + } + } + return result; +} + +char* strops_remove_at_pos(const char* string, size_t pos) { + char *result = malloc(strlen(string)); + 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_trim_right_whitespace(const char* string) { + char *result = malloc(strlen(string)); + 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 = realloc(result, strlen(result)); + return result; +} + +char* strops_trim_left_whitespace(const char* string) { + char *result = malloc(strlen(string)); + memcpy(result, string, strlen(string)); + while(strchr("\t\n\v\f\r ", result[0]) != NULL) { + result = strops_remove_at_pos(result, 0); + } + result = realloc(result, strlen(result)); + return result; +} + +char* strops_trim_both_whitespace(const char* string) { + char *result = malloc(strlen(string)); + memcpy(result, string, strlen(string)); + result = strops_trim_right_whitespace(result); + result = strops_trim_left_whitespace(result); + return result; +} diff --git a/src/strops.h b/src/strops.h new file mode 100644 index 0000000..ccac289 --- /dev/null +++ b/src/strops.h @@ -0,0 +1,38 @@ +#ifndef STROPS_H +#define STROPS_H + +#include + +/* + All functions return a new heap-allocated string to which the requested operation was applied to. + The user of this library is responsible for freeing the memory of the result. + No function modifies the input string. The input string is copied at the beginning of a function. + Only 7-Bit Ascii is supported. +*/ + +char* strops_to_lowercase(const char* string); +char* strops_to_uppercase(const char* string); + +char* strops_remove_at_pos(const char* string, size_t pos); + +char* strops_trim_right_whitespace(const char* string); +char* strops_trim_left_whitespace(const char* string); +char* strops_trim_both_whitespace(const char* string); + +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_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_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_remove_chars(const char* string, const char* chars_to_remove); +char* strops_remove_string(const char* string, const char* string_to_remove); + +size_t word_count(const char* string); + +char* strops_url_encode(const char* string); +char* strops_url_decode(const char* string); + +#endif /* STROPS_H */ diff --git a/src/tests.c b/src/tests.c new file mode 100644 index 0000000..c65161f --- /dev/null +++ b/src/tests.c @@ -0,0 +1,122 @@ +#include "strops.h" +#include + +/* Test template (Maybe make a macro in the future?) +int test_() { + char* input = ""; + char* expected = ""; + char* result = strops_(input); + if (strcmp(result, expected) != 0) { + printf("test_ failed\n"); + printf("Got = '%s'\nExpected = '%s'\n", result, expected); + return 1; + } + return 0; +} +*/ + +int test_to_lowercase() { + char* input = "sTrInG"; + char* expected = "string"; + char* result = strops_to_lowercase(input); + if (strcmp(result, expected) != 0) { + printf("test_to_lowercase failed\n"); + printf("Got = '%s'\nExpected = '%s'\n", result, expected); + return 1; + } + return 0; +} + +int test_to_uppercase() { + char* input = "sTrInG"; + char* expected = "STRING"; + char* result = strops_to_uppercase(input); + if (strcmp(result, expected) != 0) { + printf("test_to_uppercase failed\n"); + printf("Got = '%s'\nExpected = '%s'\n", result, expected); + return 1; + } + return 0; +} + +int test_trim_right() { + char* input = "String \t\f\v\r\n "; + char* expected = "String"; + char* result = strops_trim_right_whitespace(input); + if (strcmp(result, expected) != 0) { + printf("test_trim_right failed\n"); + printf("Got = '%s'\nExpected = '%s'\n", result, expected); + return 1; + } + return 0; +} + +int test_trim_left() { + char* input = " \t\f\v\r\n String"; + char* expected = "String"; + char* result = strops_trim_left_whitespace(input); + if (strcmp(result, expected) != 0) { + printf("test_trim_left failed\n"); + printf("Got = '%s'\nExpected = '%s'\n", result, expected); + return 1; + } + return 0; +} + +int test_trim_both() { + char* input = " \t\f\v String \r\n "; + char* expected = "String"; + char* result = strops_trim_both_whitespace(input); + if (strcmp(result, expected) != 0) { + printf("test_trim_both failed\n"); + printf("Got = '%s'\nExpected = '%s'\n", result, expected); + return 1; + } + return 0; +} + +int main() { + int amount_of_tests = 5; + int amount_of_successful_tests = 0; + int ret; + + /* Test template + ret = test_(); + if (ret == 0) { + amount_of_successful_tests++; + } + */ + + ret = test_to_lowercase(); + if (ret == 0) { + amount_of_successful_tests++; + } + + ret = test_to_uppercase(); + if (ret == 0) { + amount_of_successful_tests++; + } + + ret = test_trim_right(); + if (ret == 0) { + amount_of_successful_tests++; + } + + ret = test_trim_left(); + if (ret == 0) { + amount_of_successful_tests++; + } + + ret = test_trim_both(); + 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); + return 1; + } else { + printf("All tests passed successfully\n"); + return 0; + } +}