only url stuff missing for now
This commit is contained in:
94
src/strops.c
94
src/strops.c
@ -3,6 +3,8 @@
|
||||
#include <stdlib.h>
|
||||
#include <assert.h>
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
/* Function template
|
||||
char* strops_(const char* string) {
|
||||
char *result = malloc(strlen(string));
|
||||
@ -15,7 +17,7 @@ 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++) {
|
||||
for (i = 0; i < strlen(string); i++) {
|
||||
if (result[i] >= 'A' && result[i] <= 'Z') {
|
||||
result[i] += 32;
|
||||
}
|
||||
@ -27,7 +29,7 @@ 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++) {
|
||||
for (i = 0; i < strlen(string); i++) {
|
||||
if (result[i] >= 'a' && result[i] <= 'z') {
|
||||
result[i] -= 32;
|
||||
}
|
||||
@ -40,7 +42,7 @@ char* strops_remove_at_pos(const char* string, size_t pos) {
|
||||
memcpy(result, string, strlen(string));
|
||||
result[pos] = 0;
|
||||
size_t i;
|
||||
for(i = pos; i < strlen(string); i++) {
|
||||
for (i = pos; i < strlen(string); i++) {
|
||||
result[i] = result[i + 1];
|
||||
}
|
||||
result = realloc(result, strlen(result));
|
||||
@ -50,7 +52,7 @@ char* strops_remove_at_pos(const char* string, size_t pos) {
|
||||
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) {
|
||||
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));
|
||||
@ -60,7 +62,7 @@ char* strops_trim_right_whitespace(const char* string) {
|
||||
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) {
|
||||
while (strchr("\t\n\v\f\r ", result[0]) != NULL) {
|
||||
result = strops_remove_at_pos(result, 0);
|
||||
}
|
||||
result = realloc(result, strlen(result));
|
||||
@ -78,7 +80,7 @@ char* strops_trim_both_whitespace(const char* string) {
|
||||
char* strops_trim_right_chars(const char* string, const char* chars_to_remove) {
|
||||
char *result = malloc(strlen(string));
|
||||
memcpy(result, string, strlen(string));
|
||||
while(strchr(chars_to_remove, result[strlen(result) - 1]) != NULL) {
|
||||
while (strchr(chars_to_remove, result[strlen(result) - 1]) != NULL) {
|
||||
result = strops_remove_at_pos(result, strlen(result) - 1);
|
||||
}
|
||||
result = realloc(result, strlen(result));
|
||||
@ -88,7 +90,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 = malloc(strlen(string));
|
||||
memcpy(result, string, strlen(string));
|
||||
while(strchr(chars_to_remove, result[0]) != NULL) {
|
||||
while (strchr(chars_to_remove, result[0]) != NULL) {
|
||||
result = strops_remove_at_pos(result, 0);
|
||||
}
|
||||
result = realloc(result, strlen(result));
|
||||
@ -104,18 +106,45 @@ 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) {
|
||||
assert(strlen(string) >= strlen(string_to_remove) && "string_to_remove cannot be bigger than string");
|
||||
char *result = malloc(strlen(string));
|
||||
memcpy(result, string, strlen(string));
|
||||
|
||||
size_t offset = strlen(result) - strlen(string_to_remove);
|
||||
char* tmp = result + offset;
|
||||
while (strcmp(tmp, string_to_remove) == 0) {
|
||||
size_t i;
|
||||
for (i = 0; i < strlen(string_to_remove); i++) {
|
||||
result = strops_remove_at_pos(result, offset);
|
||||
}
|
||||
offset = strlen(result) - strlen(string_to_remove);
|
||||
tmp = result + offset;
|
||||
}
|
||||
result = realloc(result, strlen(result));
|
||||
return result;
|
||||
}
|
||||
|
||||
char* strops_trim_left_string(const char* string, const char* string_to_remove) {
|
||||
assert(strlen(string) >= strlen(string_to_remove) && "string_to_remove cannot be bigger than string");
|
||||
char *result = malloc(strlen(string));
|
||||
memcpy(result, string, strlen(string));
|
||||
|
||||
char *tmp = malloc(strlen(string_to_remove) + 1);
|
||||
memcpy(tmp, result, strlen(string_to_remove));
|
||||
while (strcmp(tmp, string_to_remove) == 0) {
|
||||
size_t i;
|
||||
for (i = 0; i < strlen(string_to_remove); i++) {
|
||||
result = strops_remove_at_pos(result, 0);
|
||||
}
|
||||
memcpy(tmp, result, strlen(string_to_remove));
|
||||
}
|
||||
free(tmp);
|
||||
result = realloc(result, strlen(result));
|
||||
return result;
|
||||
}
|
||||
|
||||
char* strops_trim_both_string(const char* string, const char* string_to_remove) {
|
||||
assert(strlen(string) >= strlen(string_to_remove) && "string_to_remove cannot be bigger than string");
|
||||
char *result = malloc(strlen(string));
|
||||
memcpy(result, string, strlen(string));
|
||||
result = strops_trim_right_string(result, string_to_remove);
|
||||
@ -129,7 +158,7 @@ char* strops_remove_chars(const char* string, const char* chars_to_remove) {
|
||||
|
||||
size_t i;
|
||||
for (i = 0; i < strlen(chars_to_remove); i++) {
|
||||
while(strchr(result, chars_to_remove[i]) != NULL) {
|
||||
while (strchr(result, chars_to_remove[i]) != NULL) {
|
||||
result = strops_remove_at_pos(result, strlen(result) - strlen(strchr(result, chars_to_remove[i])));
|
||||
}
|
||||
}
|
||||
@ -138,10 +167,11 @@ char* strops_remove_chars(const char* string, const char* chars_to_remove) {
|
||||
}
|
||||
|
||||
char* strops_remove_string(const char* string, const char* string_to_remove) {
|
||||
assert(strlen(string) >= strlen(string_to_remove) && "string_to_remove cannot be bigger than string");
|
||||
char *result = malloc(strlen(string));
|
||||
memcpy(result, string, strlen(string));
|
||||
|
||||
while(strstr(result, string_to_remove) != NULL) {
|
||||
while (strstr(result, string_to_remove) != NULL) {
|
||||
size_t offset = strlen(result) - strlen(strstr(result, string_to_remove));
|
||||
size_t i;
|
||||
for (i = 0; i < strlen(string_to_remove); i++) {
|
||||
@ -153,20 +183,58 @@ char* strops_remove_string(const char* string, const char* string_to_remove) {
|
||||
}
|
||||
|
||||
size_t strops_word_count(const char* string) {
|
||||
char *result = malloc(strlen(string));
|
||||
memcpy(result, string, strlen(string));
|
||||
return 0;
|
||||
size_t word_count = 0;
|
||||
char *tmp = malloc(strlen(string));
|
||||
memcpy(tmp, string, strlen(string));
|
||||
|
||||
size_t i = 0;
|
||||
size_t beginning_of_word = 0;
|
||||
size_t end_of_word = end_of_word;
|
||||
while (i < strlen(string) - 1) {
|
||||
for (; i < strlen(string); 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) {
|
||||
continue;
|
||||
}
|
||||
|
||||
for (i = beginning_of_word; i < strlen(string); 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) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (end_of_word - beginning_of_word < 2) {
|
||||
continue;
|
||||
}
|
||||
|
||||
i = end_of_word;
|
||||
word_count++;
|
||||
}
|
||||
free(tmp);
|
||||
return word_count;
|
||||
}
|
||||
|
||||
char* strops_url_encode(const char* string) {
|
||||
char *result = malloc(strlen(string));
|
||||
char *result = malloc(strlen(string) * 3); /* multiplied by 3, because example: ( => %28. Three times the chars */
|
||||
memcpy(result, string, strlen(string));
|
||||
result = realloc(result, strlen(result));
|
||||
return result;
|
||||
}
|
||||
|
||||
char* strops_url_decode(const char* string) {
|
||||
char *result = malloc(strlen(string));
|
||||
memcpy(result, string, strlen(string));
|
||||
result = realloc(result, strlen(result));
|
||||
return result;
|
||||
}
|
||||
|
||||
|
@ -148,7 +148,7 @@ int test_trim_left_string() {
|
||||
}
|
||||
|
||||
int test_trim_both_string() {
|
||||
char* input = "NULL Bruh NULL";
|
||||
char* input = "NULLNULL Bruh NULLNULL";
|
||||
char* expected = " Bruh ";
|
||||
char* result = strops_trim_both_string(input, "NULL");
|
||||
if (strcmp(result, expected) != 0) {
|
||||
|
Reference in New Issue
Block a user