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;
}