Compare commits
2 Commits
28a235b680
...
9610831c9e
Author | SHA1 | Date | |
---|---|---|---|
9610831c9e
|
|||
1418c2843e
|
1
.gitignore
vendored
1
.gitignore
vendored
@ -1,3 +1,4 @@
|
|||||||
strops.o
|
strops.o
|
||||||
|
libstrops.so
|
||||||
libstrops.a
|
libstrops.a
|
||||||
tests
|
tests
|
||||||
|
9
Makefile
9
Makefile
@ -1,10 +1,13 @@
|
|||||||
tests : libstrops.a
|
tests : libstrops.a libstrops.so
|
||||||
gcc -ansi -I . -L. -o tests tests.c -Wl,-Bstatic -lstrops -Wl,-Bdynamic
|
gcc -ansi -I . -L. -o tests tests.c -Wl,-Bstatic -lstrops -Wl,-Bdynamic
|
||||||
|
# gcc -ansi -I . -L. -o tests tests.c -lstrops
|
||||||
|
|
||||||
|
libstrops.so : strops.o
|
||||||
|
gcc -shared -o libstrops.so strops.o
|
||||||
|
|
||||||
libstrops.a : strops.o
|
libstrops.a : strops.o
|
||||||
ar cr libstrops.a strops.o
|
ar cr libstrops.a strops.o
|
||||||
rm strops.o
|
|
||||||
|
|
||||||
strops.o : strops.c strops.h
|
strops.o : strops.c strops.h
|
||||||
gcc -c -ansi -ggdb -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 -O2 -pipe -o strops.o strops.c
|
||||||
|
272
strops.c
272
strops.c
@ -7,17 +7,76 @@
|
|||||||
|
|
||||||
/* Function template
|
/* Function template
|
||||||
char* strops_(const char* string) {
|
char* strops_(const char* string) {
|
||||||
char* result = malloc(strlen(string));
|
char* result = strops_copy(string);
|
||||||
memcpy(result, string, strlen(string));
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
ull_t strops_length(const char* string) {
|
||||||
|
if (!string) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
ull_t string_length = 0;
|
||||||
|
while(string[string_length] != '\0') {
|
||||||
|
string_length++;
|
||||||
|
}
|
||||||
|
|
||||||
|
return string_length;
|
||||||
|
}
|
||||||
|
|
||||||
|
char* strops_copy(const char* string) {
|
||||||
|
if (!string) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
ull_t length = strops_length(string);
|
||||||
|
if (length == 0) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
char* result = malloc(length);
|
||||||
|
|
||||||
|
ull_t i;
|
||||||
|
for(i = 0; i < length; i++) {
|
||||||
|
result[i] = string[i];
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
char strops_contains_char(const char* string, char char_to_search) {
|
||||||
|
ull_t i;
|
||||||
|
for (i = 0; i < strops_length(string); i++) {
|
||||||
|
if (string[i] == char_to_search) {
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
char strops_contains_string(const char* string, const char* string_to_search) {
|
||||||
|
char contains_string = 0;
|
||||||
|
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 contains_string;
|
||||||
|
}
|
||||||
|
|
||||||
char* strops_to_lowercase(const char* string) {
|
char* strops_to_lowercase(const char* string) {
|
||||||
char* result = malloc(strlen(string));
|
char* result = strops_copy(string);
|
||||||
memcpy(result, string, strlen(string));
|
ull_t i;
|
||||||
size_t i;
|
for (i = 0; i < strops_length(string); i++) {
|
||||||
for (i = 0; i < strlen(string); i++) {
|
|
||||||
if (result[i] >= 'A' && result[i] <= 'Z') {
|
if (result[i] >= 'A' && result[i] <= 'Z') {
|
||||||
result[i] += 32;
|
result[i] += 32;
|
||||||
}
|
}
|
||||||
@ -26,10 +85,9 @@ char* strops_to_lowercase(const char* string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
char* strops_to_uppercase(const char* string) {
|
char* strops_to_uppercase(const char* string) {
|
||||||
char* result = malloc(strlen(string));
|
char* result = strops_copy(string);
|
||||||
memcpy(result, string, strlen(string));
|
ull_t i;
|
||||||
size_t i;
|
for (i = 0; i < strops_length(string); i++) {
|
||||||
for (i = 0; i < strlen(string); i++) {
|
|
||||||
if (result[i] >= 'a' && result[i] <= 'z') {
|
if (result[i] >= 'a' && result[i] <= 'z') {
|
||||||
result[i] -= 32;
|
result[i] -= 32;
|
||||||
}
|
}
|
||||||
@ -38,8 +96,8 @@ char* strops_to_uppercase(const char* string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
int strops_is_lowercase(const char* string) {
|
int strops_is_lowercase(const char* string) {
|
||||||
size_t i;
|
ull_t i;
|
||||||
for (i = 0; i < strlen(string); i++) {
|
for (i = 0; i < strops_length(string); i++) {
|
||||||
if (!(string[i] >= 'a' && string[i] <= 'z')) {
|
if (!(string[i] >= 'a' && string[i] <= 'z')) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@ -48,8 +106,8 @@ int strops_is_lowercase(const char* string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
int strops_is_uppercase(const char* string) {
|
int strops_is_uppercase(const char* string) {
|
||||||
size_t i;
|
ull_t i;
|
||||||
for (i = 0; i < strlen(string); i++) {
|
for (i = 0; i < strops_length(string); i++) {
|
||||||
if (!(string[i] >= 'A' && string[i] <= 'Z')) {
|
if (!(string[i] >= 'A' && string[i] <= 'Z')) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@ -57,190 +115,178 @@ int strops_is_uppercase(const char* string) {
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
char* strops_insert_at_pos_string(const char* string, const char* string_to_insert, size_t pos) {
|
char* strops_insert_at_pos_string(const char* string, const char* string_to_insert, ull_t pos) {
|
||||||
assert(pos <= strlen(string) && "pos needs to be inside string");
|
assert(pos <= strops_length(string) && "pos needs to be inside string");
|
||||||
size_t string_length = strlen(string) + strlen(string_to_insert) + 1;
|
ull_t string_length = strops_length(string) + strops_length(string_to_insert) + 1;
|
||||||
char* result = malloc(string_length);
|
char* result = strops_copy(string);
|
||||||
memcpy(result, string, strlen(string));
|
ull_t i, j;
|
||||||
size_t i, j;
|
|
||||||
/* Make space for string_to_insert */
|
/* Make space for string_to_insert */
|
||||||
for (i = string_length - 1; i > pos; i--) {
|
for (i = string_length - 1; i > pos; i--) {
|
||||||
result[i] = result[i - strlen(string_to_insert)];
|
result[i] = result[i - strops_length(string_to_insert)];
|
||||||
}
|
}
|
||||||
/* Insert string into empty space */
|
/* Insert string into empty space */
|
||||||
for (j = 0; j < strlen(string_to_insert); j++) {
|
for (j = 0; j < strops_length(string_to_insert); j++) {
|
||||||
result[pos + j] = string_to_insert[j];
|
result[pos + j] = string_to_insert[j];
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
char* strops_remove_at_pos_char(const char* string, size_t pos) {
|
char* strops_remove_at_pos_char(const char* string, ull_t pos) {
|
||||||
assert(pos <= strlen(string) && "pos needs to be inside string");
|
assert(pos <= strops_length(string) && "pos needs to be inside string");
|
||||||
char* result = malloc(strlen(string) + 1);
|
char* result = strops_copy(string);
|
||||||
memcpy(result, string, strlen(string));
|
|
||||||
result[pos] = 0;
|
result[pos] = 0;
|
||||||
size_t i;
|
ull_t i;
|
||||||
for (i = pos; i < strlen(string); i++) {
|
for (i = pos; i < strops_length(string); i++) {
|
||||||
result[i] = result[i + 1];
|
result[i] = result[i + 1];
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
char* strops_remove_at_pos_string(const char* string, const char* string_to_remove, size_t pos) {
|
char* strops_remove_at_pos_string(const char* string, const char* string_to_remove, ull_t pos) {
|
||||||
char* result = malloc(strlen(string));
|
char* result = strops_copy(string);
|
||||||
memcpy(result, string, strlen(string));
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
char* strops_replace_at_pos_string(const char* string, const char* string_to_remove, const char* string_to_insert, size_t pos) {
|
char* strops_replace_at_pos_string(const char* string, const char* string_to_remove, const char* string_to_insert, ull_t pos) {
|
||||||
char* result = malloc(strlen(string));
|
char* result = strops_copy(string);
|
||||||
memcpy(result, string, strlen(string));
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
char* strops_trim_right_whitespace(const char* string) {
|
char* strops_trim_right_whitespace(const char* string) {
|
||||||
char* result = malloc(strlen(string) + 1);
|
char* result = strops_copy(string);
|
||||||
memcpy(result, string, strlen(string));
|
while (strchr("\t\n\v\f\r ", result[strops_length(result) - 1]) != NULL) {
|
||||||
while (strchr("\t\n\v\f\r ", result[strlen(result) - 1]) != NULL) {
|
result = strops_remove_at_pos_char(result, strops_length(result) - 1);
|
||||||
result = strops_remove_at_pos_char(result, strlen(result) - 1);
|
|
||||||
}
|
}
|
||||||
result = realloc(result, strlen(result));
|
result = realloc(result, strops_length(result));
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
char* strops_trim_left_whitespace(const char* string) {
|
char* strops_trim_left_whitespace(const char* string) {
|
||||||
char* result = malloc(strlen(string) + 1);
|
char* result = strops_copy(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_char(result, 0);
|
result = strops_remove_at_pos_char(result, 0);
|
||||||
}
|
}
|
||||||
result = realloc(result, strlen(result));
|
result = realloc(result, strops_length(result));
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
char* strops_trim_both_whitespace(const char* string) {
|
char* strops_trim_both_whitespace(const char* string) {
|
||||||
char* result = malloc(strlen(string) + 1);
|
char* result;
|
||||||
memcpy(result, string, strlen(string));
|
char* tmp;
|
||||||
result = strops_trim_right_whitespace(result);
|
tmp = strops_trim_right_whitespace(string);
|
||||||
result = strops_trim_left_whitespace(result);
|
result = strops_trim_left_whitespace(tmp);
|
||||||
|
free(tmp);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
char* strops_trim_right_chars(const char* string, const char* chars_to_remove) {
|
char* strops_trim_right_chars(const char* string, const char* chars_to_remove) {
|
||||||
char* result = malloc(strlen(string) + 1);
|
char* result = strops_copy(string);
|
||||||
memcpy(result, string, strlen(string));
|
while (strchr(chars_to_remove, result[strops_length(result) - 1]) != NULL) {
|
||||||
while (strchr(chars_to_remove, result[strlen(result) - 1]) != NULL) {
|
result = strops_remove_at_pos_char(result, strops_length(result) - 1);
|
||||||
result = strops_remove_at_pos_char(result, strlen(result) - 1);
|
|
||||||
}
|
}
|
||||||
result = realloc(result, strlen(result));
|
result = realloc(result, strops_length(result));
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
char* strops_trim_left_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) + 1);
|
char* result = strops_copy(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_char(result, 0);
|
result = strops_remove_at_pos_char(result, 0);
|
||||||
}
|
}
|
||||||
result = realloc(result, strlen(result));
|
result = realloc(result, strops_length(result));
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
char* strops_trim_both_chars(const char* string, const char* chars_to_remove) {
|
char* strops_trim_both_chars(const char* string, const char* chars_to_remove) {
|
||||||
char* result = malloc(strlen(string) + 1);
|
char* result;
|
||||||
memcpy(result, string, strlen(string));
|
char* tmp;
|
||||||
result = strops_trim_right_chars(result, chars_to_remove);
|
tmp = strops_trim_right_chars(string, chars_to_remove);
|
||||||
result = strops_trim_left_chars(result, chars_to_remove);
|
result = strops_trim_left_chars(tmp, chars_to_remove);
|
||||||
|
free(tmp);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
char* strops_trim_right_string(const char* string, const char* string_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");
|
assert(strops_length(string) >= strops_length(string_to_remove) && "string_to_remove cannot be bigger than string");
|
||||||
char* result = malloc(strlen(string) + 1);
|
char* result = strops_copy(string);
|
||||||
memcpy(result, string, strlen(string));
|
ull_t offset = strops_length(result) - strops_length(string_to_remove);
|
||||||
|
|
||||||
size_t offset = strlen(result) - strlen(string_to_remove);
|
|
||||||
char* tmp = result + offset;
|
char* tmp = result + offset;
|
||||||
|
|
||||||
while (strcmp(tmp, string_to_remove) == 0) {
|
while (strcmp(tmp, string_to_remove) == 0) {
|
||||||
size_t i;
|
ull_t i;
|
||||||
for (i = 0; i < strlen(string_to_remove); i++) {
|
for (i = 0; i < strops_length(string_to_remove); i++) {
|
||||||
result = strops_remove_at_pos_char(result, offset);
|
result = strops_remove_at_pos_char(result, offset);
|
||||||
}
|
}
|
||||||
offset = strlen(result) - strlen(string_to_remove);
|
offset = strops_length(result) - strops_length(string_to_remove);
|
||||||
tmp = result + offset;
|
tmp = result + offset;
|
||||||
}
|
}
|
||||||
result = realloc(result, strlen(result));
|
result = realloc(result, strops_length(result));
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
char* strops_trim_left_string(const char* string, const char* string_to_remove) {
|
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");
|
assert(strops_length(string) >= strops_length(string_to_remove) && "string_to_remove cannot be bigger than string");
|
||||||
char* result = malloc(strlen(string) + 1);
|
char* result = strops_copy(string);
|
||||||
memcpy(result, string, strlen(string));
|
char* tmp = strops_copy(string_to_remove);
|
||||||
|
|
||||||
char* tmp = malloc(strlen(string_to_remove) + 1);
|
|
||||||
memcpy(tmp, result, strlen(string_to_remove));
|
|
||||||
while (strcmp(tmp, string_to_remove) == 0) {
|
while (strcmp(tmp, string_to_remove) == 0) {
|
||||||
size_t i;
|
ull_t i;
|
||||||
for (i = 0; i < strlen(string_to_remove); i++) {
|
for (i = 0; i < strops_length(string_to_remove); i++) {
|
||||||
result = strops_remove_at_pos_char(result, 0);
|
result = strops_remove_at_pos_char(result, 0);
|
||||||
}
|
}
|
||||||
memcpy(tmp, result, strlen(string_to_remove));
|
memcpy(tmp, result, strops_length(string_to_remove));
|
||||||
}
|
}
|
||||||
free(tmp);
|
free(tmp);
|
||||||
result = realloc(result, strlen(result));
|
result = realloc(result, strops_length(result));
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
char* strops_trim_both_string(const char* string, const char* string_to_remove) {
|
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");
|
assert(strops_length(string) >= strops_length(string_to_remove) && "string_to_remove cannot be bigger than string");
|
||||||
char* result = malloc(strlen(string) + 1);
|
char* result;
|
||||||
memcpy(result, string, strlen(string));
|
char* tmp;
|
||||||
result = strops_trim_right_string(result, string_to_remove);
|
tmp = strops_trim_right_string(string, string_to_remove);
|
||||||
result = strops_trim_left_string(result, string_to_remove);
|
result = strops_trim_left_string(tmp, string_to_remove);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
char* strops_remove_chars(const char* string, const char* chars_to_remove) {
|
char* strops_remove_chars(const char* string, const char* chars_to_remove) {
|
||||||
char* result = malloc(strlen(string) + 1);
|
char* result = strops_copy(string);
|
||||||
memcpy(result, string, strlen(string));
|
|
||||||
|
|
||||||
size_t i;
|
ull_t i;
|
||||||
for (i = 0; i < strlen(chars_to_remove); i++) {
|
for (i = 0; i < strops_length(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_char(result, strlen(result) - strlen(strchr(result, chars_to_remove[i])));
|
result = strops_remove_at_pos_char(result, strops_length(result) - strops_length(strchr(result, chars_to_remove[i])));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
result = realloc(result, strlen(result));
|
result = realloc(result, strops_length(result));
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
char* strops_remove_string(const char* string, const char* string_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");
|
assert(strops_length(string) >= strops_length(string_to_remove) && "string_to_remove cannot be bigger than string");
|
||||||
char* result = malloc(strlen(string) + 1);
|
char* result = strops_copy(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));
|
ull_t offset = strops_length(result) - strops_length(strstr(result, string_to_remove));
|
||||||
size_t i;
|
ull_t i;
|
||||||
for (i = 0; i < strlen(string_to_remove); i++) {
|
for (i = 0; i < strops_length(string_to_remove); i++) {
|
||||||
result = strops_remove_at_pos_char(result, offset);
|
result = strops_remove_at_pos_char(result, offset);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
result = realloc(result, strlen(result));
|
result = realloc(result, strops_length(result));
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t strops_word_count(const char* string) {
|
ull_t strops_word_count(const char* string) {
|
||||||
size_t word_count = 0;
|
ull_t word_count = 0;
|
||||||
char* tmp = malloc(strlen(string) + 1);
|
char* tmp = strops_copy(string);
|
||||||
memcpy(tmp, string, strlen(string));char* strpos_remove_at_pos_string(const char* string, const char* string_to_remove, size_t pos);
|
|
||||||
|
|
||||||
size_t i = 0;
|
ull_t i = 0;
|
||||||
size_t beginning_of_word = 0;
|
ull_t beginning_of_word = 0;
|
||||||
size_t end_of_word = end_of_word;
|
ull_t end_of_word = end_of_word;
|
||||||
while (i < strlen(string) - 1) {
|
while (i < strops_length(string) - 1) {
|
||||||
for (; i < strlen(string); i++) {
|
for (; i < strops_length(string); i++) {
|
||||||
if ((tmp[i] >= 'A' && tmp[i] <= 'Z') || (tmp[i] >= 'a' && tmp[i] <= 'z')) {
|
if ((tmp[i] >= 'A' && tmp[i] <= 'Z') || (tmp[i] >= 'a' && tmp[i] <= 'z')) {
|
||||||
beginning_of_word = i;
|
beginning_of_word = i;
|
||||||
break;
|
break;
|
||||||
@ -251,7 +297,7 @@ size_t strops_word_count(const char* string) {
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (i = beginning_of_word; i < strlen(string); i++) {
|
for (i = beginning_of_word; i < strops_length(string); i++) {
|
||||||
if (!(tmp[i] >= 'A' && tmp[i] <= 'Z') && !(tmp[i] >= 'a' && tmp[i] <= 'z')) {
|
if (!(tmp[i] >= 'A' && tmp[i] <= 'Z') && !(tmp[i] >= 'a' && tmp[i] <= 'z')) {
|
||||||
end_of_word = i;
|
end_of_word = i;
|
||||||
break;
|
break;
|
||||||
@ -282,17 +328,16 @@ int strops_is_url_encoded(const char* string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
char* strops_url_encode(const char* string) {
|
char* strops_url_encode(const char* string) {
|
||||||
if (strops_is_url_encoded(string)) { return NULL; }
|
if (strops_is_url_encoded(string)) { return 0; }
|
||||||
/* multiplied by 3, because example: ( => %28. Three times the chars */
|
/* multiplied by 3, because example: ( => %28. Three times the chars */
|
||||||
char* result = malloc(strlen(string) + 1);
|
char* result = strops_copy(string);
|
||||||
memcpy(result, string, strlen(string));
|
|
||||||
char* unsafe_chars = "()";
|
char* unsafe_chars = "()";
|
||||||
char replacement[4];
|
char replacement[4];
|
||||||
replacement[0] = '%';
|
replacement[0] = '%';
|
||||||
replacement[3] = '\0';
|
replacement[3] = '\0';
|
||||||
|
|
||||||
size_t i, j;
|
ull_t i, j;
|
||||||
for (i = 0; i < strlen(unsafe_chars); i++) {
|
for (i = 0; i < strops_length(unsafe_chars); i++) {
|
||||||
j = 0;
|
j = 0;
|
||||||
while (strchr(result, unsafe_chars[i]) != NULL) {
|
while (strchr(result, unsafe_chars[i]) != NULL) {
|
||||||
if (result[j] == unsafe_chars[i]) {
|
if (result[j] == unsafe_chars[i]) {
|
||||||
@ -306,16 +351,15 @@ char* strops_url_encode(const char* string) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
result = realloc(result, strlen(result));
|
result = realloc(result, strops_length(result));
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
char* strops_url_decode(const char* string) {
|
char* strops_url_decode(const char* string) {
|
||||||
if (!strops_is_url_encoded(string)) { return NULL; }
|
if (!strops_is_url_encoded(string)) { return 0; }
|
||||||
char* result = malloc(strlen(string) + 1);
|
char* result = strops_copy(string);
|
||||||
memcpy(result, string, strlen(string));
|
|
||||||
|
|
||||||
result = realloc(result, strlen(result));
|
result = realloc(result, strops_length(result));
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
15
strops.h
15
strops.h
@ -11,15 +11,20 @@
|
|||||||
Only 7-Bit Ascii is supported.
|
Only 7-Bit Ascii is supported.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
typedef unsigned long long ull_t;
|
||||||
|
|
||||||
|
char contains_char(const char* string, char char_to_search);
|
||||||
|
char contains_string(const char* string, const char* string_to_search);
|
||||||
|
|
||||||
char* strops_to_lowercase(const char* string);
|
char* strops_to_lowercase(const char* string);
|
||||||
char* strops_to_uppercase(const char* string);
|
char* strops_to_uppercase(const char* string);
|
||||||
int strops_is_lowercase(const char* string);
|
int strops_is_lowercase(const char* string);
|
||||||
int strops_is_uppercase(const char* string);
|
int strops_is_uppercase(const char* string);
|
||||||
|
|
||||||
char* strops_insert_at_pos_string(const char* string, const char* string_to_insert, size_t pos);
|
char* strops_insert_at_pos_string(const char* string, const char* string_to_insert, ull_t pos);
|
||||||
char* strops_remove_at_pos_char(const char* string, size_t pos);
|
char* strops_remove_at_pos_char(const char* string, ull_t pos);
|
||||||
char* strops_remove_at_pos_string(const char* string, const char* string_to_remove, size_t pos);
|
char* strops_remove_at_pos_string(const char* string, const char* string_to_remove, ull_t pos);
|
||||||
char* strops_replace_at_pos_string(const char* string, const char* string_to_remove, const char* string_to_insert, size_t pos);
|
char* strops_replace_at_pos_string(const char* string, const char* string_to_remove, const char* string_to_insert, ull_t pos);
|
||||||
|
|
||||||
char* strops_trim_right_whitespace(const char* string);
|
char* strops_trim_right_whitespace(const char* string);
|
||||||
char* strops_trim_left_whitespace(const char* string);
|
char* strops_trim_left_whitespace(const char* string);
|
||||||
@ -36,7 +41,7 @@ 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_chars(const char* string, const char* chars_to_remove);
|
||||||
char* strops_remove_string(const char* string, const char* string_to_remove);
|
char* strops_remove_string(const char* string, const char* string_to_remove);
|
||||||
|
|
||||||
size_t strops_word_count(const char* string);
|
ull_t strops_word_count(const char* string);
|
||||||
|
|
||||||
int strops_is_url_encoded(const char* string);
|
int strops_is_url_encoded(const char* string);
|
||||||
|
|
||||||
|
35
tests.c
35
tests.c
@ -57,6 +57,38 @@ int test_() {
|
|||||||
}
|
}
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
int test_contains_char() {
|
||||||
|
int ret = 1;
|
||||||
|
char* input;
|
||||||
|
char expected;
|
||||||
|
char result;
|
||||||
|
input = "string";
|
||||||
|
expected = 1;
|
||||||
|
result = strops_contains_char(input, 'i');
|
||||||
|
if (result != expected) {
|
||||||
|
printf("test_ failed\n");
|
||||||
|
printf("Got = %d\nExpected = %d\n", result, expected);
|
||||||
|
ret = 0;
|
||||||
|
}
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
int test_contains_string() {
|
||||||
|
int ret = 1;
|
||||||
|
char* input;
|
||||||
|
char expected;
|
||||||
|
char result;
|
||||||
|
input = "I suck at C";
|
||||||
|
expected = 1;
|
||||||
|
result = strops_contains_string(input, "C");
|
||||||
|
if (result != expected) {
|
||||||
|
printf("test_ failed\n");
|
||||||
|
printf("Got = %d\nExpected = %d\n", result, expected);
|
||||||
|
ret = 0;
|
||||||
|
}
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
int test_to_lowercase() {
|
int test_to_lowercase() {
|
||||||
int ret = 1;
|
int ret = 1;
|
||||||
char* input;
|
char* input;
|
||||||
@ -507,6 +539,9 @@ int test_url_decode() {
|
|||||||
int main() {
|
int main() {
|
||||||
Tests tests = { 0 };
|
Tests tests = { 0 };
|
||||||
|
|
||||||
|
da_append(&tests, test_contains_char);
|
||||||
|
da_append(&tests, test_contains_string);
|
||||||
|
|
||||||
da_append(&tests, test_to_lowercase);
|
da_append(&tests, test_to_lowercase);
|
||||||
da_append(&tests, test_to_uppercase);
|
da_append(&tests, test_to_uppercase);
|
||||||
da_append(&tests, test_is_lowercase);
|
da_append(&tests, test_is_lowercase);
|
||||||
|
Reference in New Issue
Block a user