Compare commits

..

2 Commits

Author SHA1 Message Date
db208bd912 remove strcmp and fix hidden bugs 2025-06-02 23:50:20 +02:00
e25b8236e4 switch from assert to if + return 2025-05-28 20:21:24 +02:00
3 changed files with 8 additions and 9 deletions

View File

@ -11,8 +11,8 @@ libstrops.a : strops.o
ar cr libstrops.a strops.o
strops.o : strops.c
# gcc -c -fPIC -ansi -ggdb -o strops.o strops.c
gcc -c -ansi -O2 -pipe -o strops.o strops.c
# gcc -c -ansi -fPIC -ggdb -o strops.o strops.c
gcc -c -ansi -fpic -O2 -pipe -o strops.o strops.c
.PHONY : install uninstall

View File

@ -132,7 +132,9 @@ bool_t strops_equals(const char* string1, const char* string2) {
}
bool_t strops_starts_with(const char* string1, const char* string2) {
assert(strops_length(string1) >= strops_length(string2) && "string2 cannot be longer than string1");
if (strops_length(string1) < strops_length(string2)) {
return 0;
}
bool_t starts_with = 1;
ull_t i;
for (i = 0; i < strops_length(string2); i++) {
@ -284,7 +286,7 @@ char* strops_trim_right_string(const char* string, const char* string_to_remove)
ull_t offset = strops_length(result) - strops_length(string_to_remove);
char* tmp = result + offset;
while (strcmp(tmp, string_to_remove) == 0) {
while (strops_equals(tmp, string_to_remove)) {
ull_t i;
for (i = 0; i < strops_length(string_to_remove); i++) {
strops_remove_at_pos_char_inplace(result, offset);
@ -299,16 +301,13 @@ 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) {
assert(strops_length(string) >= strops_length(string_to_remove) && "string_to_remove cannot be bigger than string");
char* result = strops_copy(string);
char* tmp = strops_copy(string_to_remove);
while (strcmp(tmp, string_to_remove) == 0) {
while (strops_first_pos_of_string(result, string_to_remove) == 0) {
ull_t i;
for (i = 0; i < strops_length(string_to_remove); i++) {
strops_remove_at_pos_char_inplace(result, 0);
}
tmp = strops_copy_amount(result, strops_length(string_to_remove));
}
free(tmp);
result = realloc(result, strops_length(result));
return result;
}

View File

@ -12,7 +12,7 @@
typedef unsigned long long ull_t;
typedef unsigned char bool_t;
char* strops_lenght(const char* string);
ull_t strops_length(const char* string);
char* strops_copy(const char* string);
char* strops_copy_amount(const char* string, ull_t amount);