Compare commits
11 Commits
28a235b680
...
main
Author | SHA1 | Date | |
---|---|---|---|
db208bd912
|
|||
e25b8236e4
|
|||
77ccd59ce3
|
|||
fec9f2633d
|
|||
61a398f402
|
|||
d31bc73884
|
|||
5b4a3f84e5
|
|||
c3824e1dc7
|
|||
7e26a39ec6
|
|||
9610831c9e
|
|||
1418c2843e
|
1
.gitignore
vendored
1
.gitignore
vendored
@ -1,3 +1,4 @@
|
|||||||
strops.o
|
strops.o
|
||||||
|
libstrops.so
|
||||||
libstrops.a
|
libstrops.a
|
||||||
tests
|
tests
|
||||||
|
27
Makefile
27
Makefile
@ -1,10 +1,27 @@
|
|||||||
tests : libstrops.a
|
.POSIX :
|
||||||
|
|
||||||
|
tests : libstrops.a libstrops.so strops.h
|
||||||
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
|
||||||
gcc -c -ansi -ggdb -o strops.o strops.c
|
# gcc -c -ansi -fPIC -ggdb -o strops.o strops.c
|
||||||
# gcc -c -ansi -O2 -pipe -o strops.o strops.c
|
gcc -c -ansi -fpic -O2 -pipe -o strops.o strops.c
|
||||||
|
|
||||||
|
.PHONY : install uninstall
|
||||||
|
|
||||||
|
install: libstrops.a libstrops.so strops.h
|
||||||
|
sudo mv libstrops.a /usr/local/lib
|
||||||
|
sudo mv libstrops.so /usr/local/lib
|
||||||
|
sudo cp strops.h /usr/local/include
|
||||||
|
|
||||||
|
uninstall:
|
||||||
|
sudo rm /usr/local/lib/libstrops.a
|
||||||
|
sudo rm /usr/local/lib/libstrops.so
|
||||||
|
sudo rm /usr/local/include/strops.h
|
||||||
|
418
strops.c
418
strops.c
@ -1,23 +1,155 @@
|
|||||||
#include "strops.h"
|
#include "strops.h"
|
||||||
#include <string.h>
|
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
/* 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];
|
||||||
|
}
|
||||||
|
result[i] = '\0';
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
char* strops_copy_amount(const char* string, ull_t amount) {
|
||||||
|
if (!string) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
if (amount > strops_length(string)) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
char* result = malloc(amount);
|
||||||
|
|
||||||
|
ull_t i;
|
||||||
|
for(i = 0; i < amount; i++) {
|
||||||
|
result[i] = string[i];
|
||||||
|
}
|
||||||
|
result[i] = '\0';
|
||||||
|
|
||||||
|
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) {
|
||||||
|
bool_t 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool_t strops_contains_char(const char* string, char char_to_search) {
|
||||||
|
ull_t i;
|
||||||
|
for (i = 0; i < strops_length(string) + 1; i++) {
|
||||||
|
if (string[i] == char_to_search) {
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool_t strops_contains_string(const char* string, const char* string_to_search) {
|
||||||
|
bool_t 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool_t strops_equals(const char* string1, const char* string2) {
|
||||||
|
if (strops_length(string1) != strops_length(string2)) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
bool_t equal = 1;
|
||||||
|
ull_t i;
|
||||||
|
for (i = 0; i < strops_length(string1); i++) {
|
||||||
|
if (string1[i] != string2[i]) {
|
||||||
|
equal = 0;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return equal;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool_t strops_starts_with(const char* string1, const char* string2) {
|
||||||
|
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++) {
|
||||||
|
if (string1[i] != string2[i]) {
|
||||||
|
starts_with = 0;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return starts_with;
|
||||||
|
}
|
||||||
|
|
||||||
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 +158,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;
|
||||||
}
|
}
|
||||||
@ -37,9 +168,9 @@ char* strops_to_uppercase(const char* string) {
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
int strops_is_lowercase(const char* string) {
|
bool_t 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;
|
||||||
}
|
}
|
||||||
@ -47,9 +178,9 @@ int strops_is_lowercase(const char* string) {
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
int strops_is_uppercase(const char* string) {
|
bool_t 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,265 +188,202 @@ 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) {
|
void strops_remove_at_pos_char_inplace(char* string, ull_t pos) {
|
||||||
char* result = malloc(strlen(string));
|
assert(pos <= strops_length(string) && "pos needs to be inside string");
|
||||||
memcpy(result, string, strlen(string));
|
ull_t length = strops_length(string);
|
||||||
return result;
|
string[pos] = 0;
|
||||||
}
|
ull_t i;
|
||||||
|
for (i = pos; i < length; i++) {
|
||||||
char* strops_replace_at_pos_string(const char* string, const char* string_to_remove, const char* string_to_insert, size_t pos) {
|
string[i] = string[i + 1];
|
||||||
char* result = malloc(strlen(string));
|
}
|
||||||
memcpy(result, string, strlen(string));
|
|
||||||
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));
|
char* tmp;
|
||||||
while (strchr("\t\n\v\f\r ", result[strlen(result) - 1]) != NULL) {
|
while (strops_contains_char("\t\n\v\f\r ", result[strops_length(result) - 1])) {
|
||||||
result = strops_remove_at_pos_char(result, strlen(result) - 1);
|
strops_remove_at_pos_char_inplace(result, strops_length(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 (strops_contains_char("\t\n\v\f\r ", result[0])) {
|
||||||
while (strchr("\t\n\v\f\r ", result[0]) != NULL) {
|
strops_remove_at_pos_char_inplace(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 (strops_contains_char(chars_to_remove, result[strops_length(result) - 1])) {
|
||||||
while (strchr(chars_to_remove, result[strlen(result) - 1]) != NULL) {
|
strops_remove_at_pos_char_inplace(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 (strops_contains_char(chars_to_remove, result[0])) {
|
||||||
while (strchr(chars_to_remove, result[0]) != NULL) {
|
strops_remove_at_pos_char_inplace(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) {
|
|
||||||
size_t i;
|
while (strops_equals(tmp, string_to_remove)) {
|
||||||
for (i = 0; i < strlen(string_to_remove); i++) {
|
ull_t i;
|
||||||
result = strops_remove_at_pos_char(result, offset);
|
for (i = 0; i < strops_length(string_to_remove); i++) {
|
||||||
|
strops_remove_at_pos_char_inplace(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 = malloc(strlen(string_to_remove) + 1);
|
while (strops_first_pos_of_string(result, string_to_remove) == 0) {
|
||||||
memcpy(tmp, result, strlen(string_to_remove));
|
ull_t i;
|
||||||
while (strcmp(tmp, string_to_remove) == 0) {
|
for (i = 0; i < strops_length(string_to_remove); i++) {
|
||||||
size_t i;
|
strops_remove_at_pos_char_inplace(result, 0);
|
||||||
for (i = 0; i < strlen(string_to_remove); i++) {
|
|
||||||
result = strops_remove_at_pos_char(result, 0);
|
|
||||||
}
|
}
|
||||||
memcpy(tmp, result, strlen(string_to_remove));
|
|
||||||
}
|
}
|
||||||
free(tmp);
|
result = realloc(result, strops_length(result));
|
||||||
result = realloc(result, strlen(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, j;
|
||||||
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 (strops_contains_char(result, chars_to_remove[i])) {
|
||||||
result = strops_remove_at_pos_char(result, strlen(result) - strlen(strchr(result, chars_to_remove[i])));
|
strops_remove_at_pos_char_inplace(result, strops_first_pos_of_char(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 (strops_contains_string(result, string_to_remove)) {
|
||||||
size_t offset = strlen(result) - strlen(strstr(result, string_to_remove));
|
ull_t offset = strops_first_pos_of_string(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);
|
strops_remove_at_pos_char_inplace(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;
|
while (i < strops_length(string) - 1) {
|
||||||
while (i < strlen(string) - 1) {
|
for (; i < strops_length(tmp); i++) {
|
||||||
for (; i < strlen(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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (i = beginning_of_word; i < strlen(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')) {
|
if (!(tmp[i] >= 'A' && tmp[i] <= 'Z') && !(tmp[i] >= 'a' && tmp[i] <= 'z')) {
|
||||||
end_of_word = i;
|
|
||||||
break;
|
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;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (end_of_word - beginning_of_word < 2) {
|
if (i - beginning_of_word < 2) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
i = end_of_word;
|
|
||||||
word_count++;
|
word_count++;
|
||||||
}
|
}
|
||||||
free(tmp);
|
free(tmp);
|
||||||
return word_count;
|
return word_count;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* TODO: verify that this is correct */
|
|
||||||
int strops_is_url_encoded(const char* string) {
|
|
||||||
if (strchr(string, '%') != NULL) {
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
char* strops_url_encode(const char* string) {
|
|
||||||
if (strops_is_url_encoded(string)) { return NULL; }
|
|
||||||
/* multiplied by 3, because example: ( => %28. Three times the chars */
|
|
||||||
char* result = malloc(strlen(string) + 1);
|
|
||||||
memcpy(result, string, strlen(string));
|
|
||||||
char* unsafe_chars = "()";
|
|
||||||
char replacement[4];
|
|
||||||
replacement[0] = '%';
|
|
||||||
replacement[3] = '\0';
|
|
||||||
|
|
||||||
size_t i, j;
|
|
||||||
for (i = 0; i < strlen(unsafe_chars); i++) {
|
|
||||||
j = 0;
|
|
||||||
while (strchr(result, unsafe_chars[i]) != NULL) {
|
|
||||||
if (result[j] == unsafe_chars[i]) {
|
|
||||||
char bad = result[j];
|
|
||||||
sprintf(replacement + 1, "%02X", bad);
|
|
||||||
result = strops_remove_at_pos_char(result, j);
|
|
||||||
result = strops_insert_at_pos_string(result, replacement, j);
|
|
||||||
j += 2;
|
|
||||||
} else {
|
|
||||||
j += 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
result = realloc(result, strlen(result));
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
char* strops_url_decode(const char* string) {
|
|
||||||
if (!strops_is_url_encoded(string)) { return NULL; }
|
|
||||||
char* result = malloc(strlen(string) + 1);
|
|
||||||
memcpy(result, string, strlen(string));
|
|
||||||
|
|
||||||
result = realloc(result, strlen(result));
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
43
strops.h
43
strops.h
@ -4,22 +4,34 @@
|
|||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
|
|
||||||
/*
|
/*
|
||||||
ATTENTION! THIS LIBRARY CURRENTLY LEAKS MEMORY!
|
|
||||||
Functions that return char*, return a new heap-allocated string.
|
Functions that return char*, return a new heap-allocated string.
|
||||||
The user of this library is responsible for freeing the memory of the result.
|
The user of this library is responsible for freeing the memory of the result.
|
||||||
No function modifies the input string. The input string is copied at the beginning of a function.
|
|
||||||
Only 7-Bit Ascii is supported.
|
Only 7-Bit Ascii is supported.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
typedef unsigned long long ull_t;
|
||||||
|
typedef unsigned char bool_t;
|
||||||
|
|
||||||
|
ull_t strops_length(const char* string);
|
||||||
|
char* strops_copy(const char* string);
|
||||||
|
char* strops_copy_amount(const char* string, ull_t amount);
|
||||||
|
|
||||||
|
ull_t strops_first_pos_of_char(const char* string, char char_to_search);
|
||||||
|
ull_t strops_first_pos_of_string(const char* string, const char* string_to_search);
|
||||||
|
|
||||||
|
bool_t strops_contains_char(const char* string, char char_to_search);
|
||||||
|
bool_t strops_contains_string(const char* string, const char* string_to_search);
|
||||||
|
bool_t strops_equals(const char* string1, const char* string2);
|
||||||
|
bool_t strops_starts_with(const char* string1, const char* string2);
|
||||||
|
|
||||||
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);
|
bool_t strops_is_lowercase(const char* string);
|
||||||
int strops_is_uppercase(const char* string);
|
bool_t 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);
|
void strops_remove_at_pos_char_inplace(char* string, 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_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,20 +48,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);
|
|
||||||
|
|
||||||
/*
|
|
||||||
https://www.w3schools.com/tags/ref_urlencode.asp
|
|
||||||
PLEASE NEVER USE THEM WITHOUT UNDERSTANDING THEIR ISSUES!
|
|
||||||
They will fully remove the following '%20 %21 %22 %23 %24 %26 %27 %28 %29 %2A %2C %2E %2F \
|
|
||||||
%3B %3C %3E %3F %5B %5C %5D %5E %60 %7B %7C %7D %7E'
|
|
||||||
These will get turned into their ascii counterpart '%25 %2B %2D %2E %2F %3A %3D %40 %5F'
|
|
||||||
They will return NULL on failure
|
|
||||||
*/
|
|
||||||
char* strops_url_encode(const char* string);
|
|
||||||
char* strops_url_decode(const char* string);
|
|
||||||
|
|
||||||
#endif /* STROPS_H */
|
#endif /* STROPS_H */
|
||||||
|
|
||||||
|
169
tests.c
169
tests.c
@ -1,7 +1,6 @@
|
|||||||
#include "strops.h"
|
#include "strops.h"
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <execinfo.h>
|
|
||||||
|
|
||||||
/* https://gist.githubusercontent.com/rexim/b5b0c38f53157037923e7cdd77ce685d/raw/86c3db57f485f3b6f7958f308ba7126fa81282d8/da_append.c */
|
/* https://gist.githubusercontent.com/rexim/b5b0c38f53157037923e7cdd77ce685d/raw/86c3db57f485f3b6f7958f308ba7126fa81282d8/da_append.c */
|
||||||
#define da_append(xs, x) \
|
#define da_append(xs, x) \
|
||||||
@ -57,6 +56,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, "suck");
|
||||||
|
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;
|
||||||
@ -185,38 +216,6 @@ int test_remove_at_pos_char() {
|
|||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
int test_remove_at_pos_string() {
|
|
||||||
int ret = 1;
|
|
||||||
char* input;
|
|
||||||
char* expected;
|
|
||||||
char* result;
|
|
||||||
input = "NULL BRUH NULL";
|
|
||||||
expected = "NULL NULL";
|
|
||||||
result = strops_remove_at_pos_string(input, " BRUH", 4);
|
|
||||||
if (strcmp(result, expected) != 0) {
|
|
||||||
printf("test_remove_at_pos_string failed\n");
|
|
||||||
printf("Got = '%s'\nExpected = '%s'\n", result, expected);
|
|
||||||
ret = 0;
|
|
||||||
}
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
int test_replace_at_pos_string() {
|
|
||||||
int ret = 1;
|
|
||||||
char* input;
|
|
||||||
char* expected;
|
|
||||||
char* result;
|
|
||||||
input = "What happened in Tiananmen Square in 1989";
|
|
||||||
expected = "Nothing happened in Tiananmen Square in 1989";
|
|
||||||
result = strops_replace_at_pos_string(input, "What", "Nothing", 0);
|
|
||||||
if (strcmp(result, expected) != 0) {
|
|
||||||
printf("test_replace_at_pos_string failed\n");
|
|
||||||
printf("Got = '%s'\nExpected = '%s'\n", result, expected);
|
|
||||||
ret = 0;
|
|
||||||
}
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
int test_trim_right_whitespace() {
|
int test_trim_right_whitespace() {
|
||||||
int ret = 1;
|
int ret = 1;
|
||||||
char* input;
|
char* input;
|
||||||
@ -409,104 +408,12 @@ int test_word_count() {
|
|||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
int test_is_url_encoded() {
|
|
||||||
int ret = 1;
|
|
||||||
char* input;
|
|
||||||
int expected;
|
|
||||||
int result;
|
|
||||||
|
|
||||||
input = "(artist)";
|
|
||||||
expected = 0;
|
|
||||||
result = strops_is_url_encoded(input);
|
|
||||||
if (result != expected) {
|
|
||||||
printf("test_is_url_encoded failed\n");
|
|
||||||
printf("Got = %d\nExpected = %d\n", result, expected);
|
|
||||||
ret = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
input = "%28artist%29";
|
|
||||||
expected = 1;
|
|
||||||
result = strops_is_url_encoded(input);
|
|
||||||
if (result != expected) {
|
|
||||||
printf("test_is_url_encoded failed\n");
|
|
||||||
printf("Got = %d\nExpected = %d\n", result, expected);
|
|
||||||
ret = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
int test_url_encode() {
|
|
||||||
int ret = 1;
|
|
||||||
char* input;
|
|
||||||
char* expected;
|
|
||||||
char* result;
|
|
||||||
input = "(artist)";
|
|
||||||
expected = "%28artist%29";
|
|
||||||
result = strops_url_encode(input);
|
|
||||||
if (result == NULL) {
|
|
||||||
printf("test_url_encode failed\n");
|
|
||||||
printf("Got = NULL\nExpected = '%s'\n", expected);
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
if (strcmp(result, expected) != 0) {
|
|
||||||
printf("test_url_encode failed\n");
|
|
||||||
printf("Got = '%s'\nExpected = '%s'\n", result, expected);
|
|
||||||
ret = 0;
|
|
||||||
}
|
|
||||||
input = "()";
|
|
||||||
expected = "%28%29";
|
|
||||||
result = strops_url_encode(input);
|
|
||||||
if (result == NULL) {
|
|
||||||
printf("test_url_encode failed\n");
|
|
||||||
printf("Got = NULL\nExpected = '%s'\n", expected);
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
if (strcmp(result, expected) != 0) {
|
|
||||||
printf("test_url_encode failed\n");
|
|
||||||
printf("Got = '%s'\nExpected = '%s'\n", result, expected);
|
|
||||||
ret = 0;
|
|
||||||
}
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
int test_url_decode() {
|
|
||||||
int ret = 1;
|
|
||||||
char* input;
|
|
||||||
char* expected;
|
|
||||||
char* result;
|
|
||||||
input = "%28artist%29";
|
|
||||||
expected = "(artist)";
|
|
||||||
result = strops_url_decode(input);
|
|
||||||
if (result == NULL) {
|
|
||||||
printf("test_url_decode failed\n");
|
|
||||||
printf("Got = NULL\nExpected = '%s'\n", expected);
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
if (strcmp(result, expected) != 0) {
|
|
||||||
printf("test_url_decode failed\n");
|
|
||||||
printf("Got = '%s'\nExpected = '%s'\n", result, expected);
|
|
||||||
ret = 0;
|
|
||||||
}
|
|
||||||
input = "%28%29";
|
|
||||||
expected = "()";
|
|
||||||
result = strops_url_decode(input);
|
|
||||||
if (result == NULL) {
|
|
||||||
printf("test_url_decode failed\n");
|
|
||||||
printf("Got = NULL\nExpected = '%s'\n", expected);
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
if (strcmp(result, expected) != 0) {
|
|
||||||
printf("test_url_decode failed\n");
|
|
||||||
printf("Got = '%s'\nExpected = '%s'\n", result, expected);
|
|
||||||
ret = 0;
|
|
||||||
}
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
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);
|
||||||
@ -514,8 +421,6 @@ int main() {
|
|||||||
|
|
||||||
da_append(&tests, test_insert_at_pos_string);
|
da_append(&tests, test_insert_at_pos_string);
|
||||||
da_append(&tests, test_remove_at_pos_char);
|
da_append(&tests, test_remove_at_pos_char);
|
||||||
da_append(&tests, test_remove_at_pos_string);
|
|
||||||
da_append(&tests, test_replace_at_pos_string);
|
|
||||||
|
|
||||||
da_append(&tests, test_trim_right_whitespace);
|
da_append(&tests, test_trim_right_whitespace);
|
||||||
da_append(&tests, test_trim_left_whitespace);
|
da_append(&tests, test_trim_left_whitespace);
|
||||||
@ -534,10 +439,6 @@ int main() {
|
|||||||
|
|
||||||
da_append(&tests, test_word_count);
|
da_append(&tests, test_word_count);
|
||||||
|
|
||||||
da_append(&tests, test_is_url_encoded);
|
|
||||||
da_append(&tests, test_url_encode);
|
|
||||||
da_append(&tests, test_url_decode);
|
|
||||||
|
|
||||||
size_t i;
|
size_t i;
|
||||||
for (i = 0; i < tests.count; i++) {
|
for (i = 0; i < tests.count; i++) {
|
||||||
tests.amount_successful += tests.items[i]();
|
tests.amount_successful += tests.items[i]();
|
||||||
|
Reference in New Issue
Block a user