already started, but not done

This commit is contained in:
2025-05-07 18:15:24 +02:00
parent 0c3fdc912b
commit a9cb36e2f6
5 changed files with 251 additions and 0 deletions

78
src/strops.c Normal file
View File

@ -0,0 +1,78 @@
#include "strops.h"
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <stdio.h>
/* 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;
}

38
src/strops.h Normal file
View File

@ -0,0 +1,38 @@
#ifndef STROPS_H
#define STROPS_H
#include <stddef.h>
/*
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 */

122
src/tests.c Normal file
View File

@ -0,0 +1,122 @@
#include "strops.h"
#include <stdio.h>
/* 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;
}
}