remove string.h dependency

This commit is contained in:
2025-05-21 20:34:11 +02:00
parent 7e26a39ec6
commit c3824e1dc7

View File

@ -1,8 +1,6 @@
#include "strops.h"
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <stdio.h>
/* Function template
@ -62,9 +60,35 @@ char* strops_copy_amount(const char* string, ull_t amount) {
return result;
}
ull_t strops_first_pos_of_char(const char* string, char char_to_search) {
ull_t i;
for (i = 0; string[i] != char_to_search; i++) {}
return i;
}
ull_t strops_first_pos_of_string(const char* string, const char* string_to_search) {
char contains_string;
ull_t sts_length = strops_length(string_to_search);
ull_t l = strops_length(string) - sts_length + 1;
ull_t i, j;
for (i = 0; i < l; i++) {
contains_string = 1;
for(j = 0; j < sts_length; j++) {
if (string[i + j] != string_to_search[j]) {
contains_string = 0;
break;
}
}
if (contains_string) {
break;
}
}
return i;
}
char strops_contains_char(const char* string, char char_to_search) {
ull_t i;
for (i = 0; i < strops_length(string); i++) {
for (i = 0; i < strops_length(string) + 1; i++) {
if (string[i] == char_to_search) {
return 1;
}
@ -173,7 +197,7 @@ char* strops_replace_at_pos_string(const char* string, const char* string_to_rem
char* strops_trim_right_whitespace(const char* string) {
char* result = strops_copy(string);
while (strchr("\t\n\v\f\r ", result[strops_length(result) - 1]) != NULL) {
while (strops_contains_char("\t\n\v\f\r ", result[strops_length(result) - 1])) {
result = strops_remove_at_pos_char(result, strops_length(result) - 1);
}
result = realloc(result, strops_length(result));
@ -182,7 +206,7 @@ char* strops_trim_right_whitespace(const char* string) {
char* strops_trim_left_whitespace(const char* string) {
char* result = strops_copy(string);
while (strchr("\t\n\v\f\r ", result[0]) != NULL) {
while (strops_contains_char("\t\n\v\f\r ", result[0])) {
result = strops_remove_at_pos_char(result, 0);
}
result = realloc(result, strops_length(result));
@ -200,7 +224,7 @@ char* strops_trim_both_whitespace(const char* string) {
char* strops_trim_right_chars(const char* string, const char* chars_to_remove) {
char* result = strops_copy(string);
while (strchr(chars_to_remove, result[strops_length(result) - 1]) != NULL) {
while (strops_contains_char(chars_to_remove, result[strops_length(result) - 1])) {
result = strops_remove_at_pos_char(result, strops_length(result) - 1);
}
result = realloc(result, strops_length(result));
@ -209,7 +233,7 @@ 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* result = strops_copy(string);
while (strchr(chars_to_remove, result[0]) != NULL) {
while (strops_contains_char(chars_to_remove, result[0])) {
result = strops_remove_at_pos_char(result, 0);
}
result = realloc(result, strops_length(result));
@ -272,10 +296,10 @@ 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* result = strops_copy(string);
ull_t i;
ull_t i, j;
for (i = 0; i < strops_length(chars_to_remove); i++) {
while (strchr(result, chars_to_remove[i]) != NULL) {
result = strops_remove_at_pos_char(result, strops_length(result) - strops_length(strchr(result, chars_to_remove[i])));
while (strops_contains_char(result, chars_to_remove[i])) {
result = strops_remove_at_pos_char(result, strops_first_pos_of_char(result, chars_to_remove[i]));
}
}
result = realloc(result, strops_length(result));
@ -286,8 +310,8 @@ char* strops_remove_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);
while (strstr(result, string_to_remove) != NULL) {
ull_t offset = strops_length(result) - strops_length(strstr(result, string_to_remove));
while (strops_contains_string(result, string_to_remove)) {
ull_t offset = strops_first_pos_of_string(result, string_to_remove);
ull_t i;
for (i = 0; i < strops_length(string_to_remove); i++) {
result = strops_remove_at_pos_char(result, offset);
@ -303,35 +327,32 @@ ull_t strops_word_count(const char* string) {
ull_t i = 0;
ull_t beginning_of_word = 0;
ull_t end_of_word = end_of_word;
while (i < strops_length(string) - 1) {
for (; i < strops_length(string); i++) {
for (; i < strops_length(tmp); i++) {
if ((tmp[i] >= 'A' && tmp[i] <= 'Z') || (tmp[i] >= 'a' && tmp[i] <= 'z')) {
beginning_of_word = i;
break;
}
}
if (strchr("\t\n\v\f\r ", tmp[beginning_of_word - 1]) == NULL) {
if (!strops_contains_char("\t\n\v\f\r ", tmp[beginning_of_word - 1])) {
continue;
}
for (i = beginning_of_word; i < strops_length(string); i++) {
for (i = beginning_of_word; i < strops_length(tmp); i++) {
if (!(tmp[i] >= 'A' && tmp[i] <= 'Z') && !(tmp[i] >= 'a' && tmp[i] <= 'z')) {
end_of_word = i;
break;
}
}
if (strchr("\t\n\v\f\r ", tmp[end_of_word + 1]) == NULL) {
if (!strops_contains_char("\t\n\v\f\r ", tmp[i + 1])) {
continue;
}
if (end_of_word - beginning_of_word < 2) {
if (i - beginning_of_word < 2) {
continue;
}
i = end_of_word;
word_count++;
}
free(tmp);
@ -340,7 +361,7 @@ ull_t strops_word_count(const char* string) {
/* TODO: verify that this is correct */
int strops_is_url_encoded(const char* string) {
if (strchr(string, '%') != NULL) {
if (strops_contains_char(string, '%')) {
return 1;
}
return 0;
@ -358,7 +379,7 @@ char* strops_url_encode(const char* string) {
ull_t i, j;
for (i = 0; i < strops_length(unsafe_chars); i++) {
j = 0;
while (strchr(result, unsafe_chars[i]) != NULL) {
while (strops_contains_char(result, unsafe_chars[i])) {
if (result[j] == unsafe_chars[i]) {
char bad = result[j];
sprintf(replacement + 1, "%02X", bad);