Compare commits
3 Commits
b12577a0e9
...
fd30b35d10
Author | SHA1 | Date | |
---|---|---|---|
fd30b35d10
|
|||
6505add9f5
|
|||
4b4ce3d36f
|
5
.gitignore
vendored
5
.gitignore
vendored
@ -1,2 +1,3 @@
|
|||||||
# executables
|
strops.o
|
||||||
build
|
libstrops.a
|
||||||
|
tests
|
||||||
|
17
Makefile
17
Makefile
@ -1,11 +1,10 @@
|
|||||||
test: debug
|
tests : libstrops.a
|
||||||
gcc -ansi -L./build -o build/tests src/tests.c -lstrops
|
gcc -ansi -I . -L. -o tests tests.c -Wl,-Bstatic -lstrops -Wl,-Bdynamic
|
||||||
|
|
||||||
debug:
|
libstrops.a : strops.o
|
||||||
gcc -c -ansi -ggdb -o build/strops.o src/strops.c
|
ar cr libstrops.a strops.o
|
||||||
gcc -shared -o build/libstrops.so build/strops.o
|
rm strops.o
|
||||||
|
|
||||||
#release:
|
|
||||||
# gcc -ansi -O2 -pipe -fno-semantic-interposition -o build/hdb src/main.c src/db.c -lsqlite3
|
|
||||||
# strip build/hdb
|
|
||||||
|
|
||||||
|
strops.o : strops.c strops.h
|
||||||
|
gcc -c -ansi -ggdb -o strops.o strops.c
|
||||||
|
# gcc -c -ansi -O2 -pipe -o strops.o strops.c
|
||||||
|
@ -37,7 +37,27 @@ char* strops_to_uppercase(const char* string) {
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
char* strops_insert_at_pos(const char* string, const char* string_to_insert, size_t pos) {
|
int strops_is_lowercase(const char* string) {
|
||||||
|
size_t i;
|
||||||
|
for (i = 0; i < strlen(string); i++) {
|
||||||
|
if (!(string[i] >= 'a' && string[i] <= 'z')) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
int strops_is_uppercase(const char* string) {
|
||||||
|
size_t i;
|
||||||
|
for (i = 0; i < strlen(string); i++) {
|
||||||
|
if (!(string[i] >= 'A' && string[i] <= 'Z')) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
char* strops_insert_at_pos_string(const char* string, const char* string_to_insert, size_t pos) {
|
||||||
assert(pos <= strlen(string) && "pos needs to be inside string");
|
assert(pos <= strlen(string) && "pos needs to be inside string");
|
||||||
size_t string_length = strlen(string) + strlen(string_to_insert) + 1;
|
size_t string_length = strlen(string) + strlen(string_to_insert) + 1;
|
||||||
char *result = malloc(string_length);
|
char *result = malloc(string_length);
|
||||||
@ -54,7 +74,7 @@ char* strops_insert_at_pos(const char* string, const char* string_to_insert, siz
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
char* strops_remove_at_pos(const char* string, size_t pos) {
|
char* strops_remove_at_pos_char(const char* string, size_t pos) {
|
||||||
assert(pos <= strlen(string) && "pos needs to be inside string");
|
assert(pos <= strlen(string) && "pos needs to be inside string");
|
||||||
char *result = malloc(strlen(string));
|
char *result = malloc(strlen(string));
|
||||||
memcpy(result, string, strlen(string));
|
memcpy(result, string, strlen(string));
|
||||||
@ -71,7 +91,7 @@ char* strops_trim_right_whitespace(const char* string) {
|
|||||||
char *result = malloc(strlen(string) + 1);
|
char *result = malloc(strlen(string) + 1);
|
||||||
memcpy(result, string, 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 = strops_remove_at_pos_char(result, strlen(result) - 1);
|
||||||
}
|
}
|
||||||
result = realloc(result, strlen(result));
|
result = realloc(result, strlen(result));
|
||||||
return result;
|
return result;
|
||||||
@ -81,7 +101,7 @@ char* strops_trim_left_whitespace(const char* string) {
|
|||||||
char *result = malloc(strlen(string) + 1);
|
char *result = malloc(strlen(string) + 1);
|
||||||
memcpy(result, string, 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 = strops_remove_at_pos_char(result, 0);
|
||||||
}
|
}
|
||||||
result = realloc(result, strlen(result));
|
result = realloc(result, strlen(result));
|
||||||
return result;
|
return result;
|
||||||
@ -99,7 +119,7 @@ char* strops_trim_right_chars(const char* string, const char* chars_to_remove) {
|
|||||||
char *result = malloc(strlen(string) + 1);
|
char *result = malloc(strlen(string) + 1);
|
||||||
memcpy(result, string, 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 = strops_remove_at_pos_char(result, strlen(result) - 1);
|
||||||
}
|
}
|
||||||
result = realloc(result, strlen(result));
|
result = realloc(result, strlen(result));
|
||||||
return result;
|
return result;
|
||||||
@ -109,7 +129,7 @@ char* strops_trim_left_chars(const char* string, const char* chars_to_remove) {
|
|||||||
char *result = malloc(strlen(string) + 1);
|
char *result = malloc(strlen(string) + 1);
|
||||||
memcpy(result, string, 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 = strops_remove_at_pos_char(result, 0);
|
||||||
}
|
}
|
||||||
result = realloc(result, strlen(result));
|
result = realloc(result, strlen(result));
|
||||||
return result;
|
return result;
|
||||||
@ -133,7 +153,7 @@ char* strops_trim_right_string(const char* string, const char* string_to_remove)
|
|||||||
while (strcmp(tmp, string_to_remove) == 0) {
|
while (strcmp(tmp, string_to_remove) == 0) {
|
||||||
size_t i;
|
size_t i;
|
||||||
for (i = 0; i < strlen(string_to_remove); i++) {
|
for (i = 0; i < strlen(string_to_remove); i++) {
|
||||||
result = strops_remove_at_pos(result, offset);
|
result = strops_remove_at_pos_char(result, offset);
|
||||||
}
|
}
|
||||||
offset = strlen(result) - strlen(string_to_remove);
|
offset = strlen(result) - strlen(string_to_remove);
|
||||||
tmp = result + offset;
|
tmp = result + offset;
|
||||||
@ -152,7 +172,7 @@ char* strops_trim_left_string(const char* string, const char* string_to_remove)
|
|||||||
while (strcmp(tmp, string_to_remove) == 0) {
|
while (strcmp(tmp, string_to_remove) == 0) {
|
||||||
size_t i;
|
size_t i;
|
||||||
for (i = 0; i < strlen(string_to_remove); i++) {
|
for (i = 0; i < strlen(string_to_remove); i++) {
|
||||||
result = strops_remove_at_pos(result, 0);
|
result = strops_remove_at_pos_char(result, 0);
|
||||||
}
|
}
|
||||||
memcpy(tmp, result, strlen(string_to_remove));
|
memcpy(tmp, result, strlen(string_to_remove));
|
||||||
}
|
}
|
||||||
@ -177,7 +197,7 @@ char* strops_remove_chars(const char* string, const char* chars_to_remove) {
|
|||||||
size_t i;
|
size_t i;
|
||||||
for (i = 0; i < strlen(chars_to_remove); 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])));
|
result = strops_remove_at_pos_char(result, strlen(result) - strlen(strchr(result, chars_to_remove[i])));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
result = realloc(result, strlen(result));
|
result = realloc(result, strlen(result));
|
||||||
@ -193,7 +213,7 @@ char* strops_remove_string(const char* string, const char* string_to_remove) {
|
|||||||
size_t offset = strlen(result) - strlen(strstr(result, string_to_remove));
|
size_t offset = strlen(result) - strlen(strstr(result, string_to_remove));
|
||||||
size_t i;
|
size_t i;
|
||||||
for (i = 0; i < strlen(string_to_remove); i++) {
|
for (i = 0; i < strlen(string_to_remove); i++) {
|
||||||
result = strops_remove_at_pos(result, offset);
|
result = strops_remove_at_pos_char(result, offset);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
result = realloc(result, strlen(result));
|
result = realloc(result, strlen(result));
|
||||||
@ -242,6 +262,10 @@ size_t strops_word_count(const char* string) {
|
|||||||
return word_count;
|
return word_count;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int strops_is_url_encoded(const char* string) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
char* strops_url_encode(const char* string) {
|
char* strops_url_encode(const char* string) {
|
||||||
/* 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) * 3 + 1);
|
char *result = malloc(strlen(string) * 3 + 1);
|
@ -12,9 +12,11 @@
|
|||||||
|
|
||||||
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_uppercase(const char* string);
|
||||||
|
|
||||||
char* strops_insert_at_pos(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, size_t pos);
|
||||||
char* strops_remove_at_pos(const char* string, size_t pos);
|
char* strops_remove_at_pos_char(const char* string, 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);
|
||||||
@ -34,6 +36,7 @@ char* strops_remove_string(const char* string, const char* string_to_remove);
|
|||||||
size_t strops_word_count(const char* string);
|
size_t strops_word_count(const char* string);
|
||||||
|
|
||||||
/* https://www.w3schools.com/tags/ref_urlencode.asp */
|
/* https://www.w3schools.com/tags/ref_urlencode.asp */
|
||||||
|
int strops_is_url_encoded(const char* string);
|
||||||
char* strops_url_encode(const char* string);
|
char* strops_url_encode(const char* string);
|
||||||
char* strops_url_decode(const char* string);
|
char* strops_url_decode(const char* string);
|
||||||
|
|
@ -1,5 +1,26 @@
|
|||||||
#include "strops.h"
|
#include "strops.h"
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
/* https://gist.githubusercontent.com/rexim/b5b0c38f53157037923e7cdd77ce685d/raw/86c3db57f485f3b6f7958f308ba7126fa81282d8/da_append.c */
|
||||||
|
#define da_append(xs, x) \
|
||||||
|
do { \
|
||||||
|
if ((xs)->count >= (xs)->capacity) { \
|
||||||
|
if ((xs)->capacity == 0) (xs)->capacity = 256; \
|
||||||
|
else (xs)->capacity *= 2; \
|
||||||
|
(xs)->items = realloc((xs)->items, (xs)->capacity*sizeof(*(xs)->items)); \
|
||||||
|
} \
|
||||||
|
\
|
||||||
|
(xs)->items[(xs)->count++] = (x); \
|
||||||
|
} while (0)
|
||||||
|
|
||||||
|
typedef int(*fnptr)();
|
||||||
|
typedef struct {
|
||||||
|
fnptr* items;
|
||||||
|
size_t capacity;
|
||||||
|
size_t count;
|
||||||
|
size_t amount_successful;
|
||||||
|
} Tests;
|
||||||
|
|
||||||
/* Test template (Maybe make a macro in the future?)
|
/* Test template (Maybe make a macro in the future?)
|
||||||
int test_() {
|
int test_() {
|
||||||
@ -39,24 +60,48 @@ int test_to_uppercase() {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int test_insert_at_pos() {
|
int test_is_lowercase() {
|
||||||
|
char* input = "string";
|
||||||
|
int expected = 1;
|
||||||
|
int result = strops_is_lowercase(input);
|
||||||
|
if (result != expected) {
|
||||||
|
printf("test_is_lowercase failed\n");
|
||||||
|
printf("Got = %d\nExpected = %d\n", result, expected);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int test_is_uppercase() {
|
||||||
|
char* input = "sTrInG";
|
||||||
|
int expected = 0;
|
||||||
|
int result = strops_is_uppercase(input);
|
||||||
|
if (result != expected) {
|
||||||
|
printf("test_is_uppercase failed\n");
|
||||||
|
printf("Got = %d\nExpected = %d\n", result, expected);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int test_insert_at_pos_string() {
|
||||||
char* input = "Heo, World!";
|
char* input = "Heo, World!";
|
||||||
char* expected = "Hello, World!";
|
char* expected = "Hello, World!";
|
||||||
char* result = strops_insert_at_pos(input, "ll", 2);
|
char* result = strops_insert_at_pos_string(input, "ll", 2);
|
||||||
if (strcmp(result, expected) != 0) {
|
if (strcmp(result, expected) != 0) {
|
||||||
printf("test_insert_at_pos failed\n");
|
printf("test_insert_at_pos_string failed\n");
|
||||||
printf("Got = '%s'\nExpected = '%s'\n", result, expected);
|
printf("Got = '%s'\nExpected = '%s'\n", result, expected);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int test_remove_at_pos() {
|
int test_remove_at_pos_char() {
|
||||||
char* input = "Hello";
|
char* input = "Hello";
|
||||||
char* expected = "Hllo";
|
char* expected = "Hllo";
|
||||||
char* result = strops_remove_at_pos(input, 1);
|
char* result = strops_remove_at_pos_char(input, 1);
|
||||||
if (strcmp(result, expected) != 0) {
|
if (strcmp(result, expected) != 0) {
|
||||||
printf("test_remove_at_pos failed\n");
|
printf("test_remove_at_pos_char failed\n");
|
||||||
printf("Got = '%s'\nExpected = '%s'\n", result, expected);
|
printf("Got = '%s'\nExpected = '%s'\n", result, expected);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
@ -207,6 +252,33 @@ int test_word_count() {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int test_is_url_encoded() {
|
||||||
|
int ret = 0;
|
||||||
|
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 = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
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 = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
int test_url_encode() {
|
int test_url_encode() {
|
||||||
char* input = "(artist)";
|
char* input = "(artist)";
|
||||||
char* expected = "%28artist%29";
|
char* expected = "%28artist%29";
|
||||||
@ -232,112 +304,43 @@ int test_url_decode() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
int amount_of_tests = 18;
|
Tests tests = { 0 };
|
||||||
int amount_of_successful_tests = 0;
|
da_append(&tests, test_to_lowercase);
|
||||||
int ret;
|
da_append(&tests, test_to_lowercase);
|
||||||
|
da_append(&tests, test_to_uppercase);
|
||||||
|
da_append(&tests, test_is_lowercase);
|
||||||
|
da_append(&tests, test_is_uppercase);
|
||||||
|
da_append(&tests, test_insert_at_pos_string);
|
||||||
|
da_append(&tests, test_remove_at_pos_char);
|
||||||
|
da_append(&tests, test_trim_right_whitespace);
|
||||||
|
da_append(&tests, test_trim_left_whitespace);
|
||||||
|
da_append(&tests, test_trim_both_whitespace);
|
||||||
|
da_append(&tests, test_trim_right_chars);
|
||||||
|
da_append(&tests, test_trim_left_chars);
|
||||||
|
da_append(&tests, test_trim_both_chars);
|
||||||
|
da_append(&tests, test_trim_right_string);
|
||||||
|
da_append(&tests, test_trim_left_string);
|
||||||
|
da_append(&tests, test_trim_both_string);
|
||||||
|
da_append(&tests, test_remove_chars);
|
||||||
|
da_append(&tests, test_remove_string);
|
||||||
|
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);
|
||||||
|
|
||||||
/* Test template
|
size_t i;
|
||||||
ret = test_();
|
for (i = 0; i < tests.count; i++) {
|
||||||
if (ret == 0) {
|
if (tests.items[i]() == 0) {
|
||||||
amount_of_successful_tests++;
|
tests.amount_successful++;
|
||||||
}
|
}
|
||||||
*/
|
|
||||||
|
|
||||||
ret = test_to_lowercase();
|
|
||||||
if (ret == 0) {
|
|
||||||
amount_of_successful_tests++;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ret = test_to_uppercase();
|
if (tests.amount_successful != tests.count) {
|
||||||
if (ret == 0) {
|
printf("%d/%d tests passed successfully\n", tests.amount_successful, tests.count);
|
||||||
amount_of_successful_tests++;
|
|
||||||
}
|
|
||||||
|
|
||||||
ret = test_insert_at_pos();
|
|
||||||
if (ret == 0) {
|
|
||||||
amount_of_successful_tests++;
|
|
||||||
}
|
|
||||||
|
|
||||||
ret = test_remove_at_pos();
|
|
||||||
if (ret == 0) {
|
|
||||||
amount_of_successful_tests++;
|
|
||||||
}
|
|
||||||
|
|
||||||
ret = test_trim_right_whitespace();
|
|
||||||
if (ret == 0) {
|
|
||||||
amount_of_successful_tests++;
|
|
||||||
}
|
|
||||||
|
|
||||||
ret = test_trim_left_whitespace();
|
|
||||||
if (ret == 0) {
|
|
||||||
amount_of_successful_tests++;
|
|
||||||
}
|
|
||||||
|
|
||||||
ret = test_trim_both_whitespace();
|
|
||||||
if (ret == 0) {
|
|
||||||
amount_of_successful_tests++;
|
|
||||||
}
|
|
||||||
|
|
||||||
ret = test_trim_right_chars();
|
|
||||||
if (ret == 0) {
|
|
||||||
amount_of_successful_tests++;
|
|
||||||
}
|
|
||||||
|
|
||||||
ret = test_trim_left_chars();
|
|
||||||
if (ret == 0) {
|
|
||||||
amount_of_successful_tests++;
|
|
||||||
}
|
|
||||||
|
|
||||||
ret = test_trim_both_chars();
|
|
||||||
if (ret == 0) {
|
|
||||||
amount_of_successful_tests++;
|
|
||||||
}
|
|
||||||
|
|
||||||
ret = test_trim_right_string();
|
|
||||||
if (ret == 0) {
|
|
||||||
amount_of_successful_tests++;
|
|
||||||
}
|
|
||||||
|
|
||||||
ret = test_trim_left_string();
|
|
||||||
if (ret == 0) {
|
|
||||||
amount_of_successful_tests++;
|
|
||||||
}
|
|
||||||
|
|
||||||
ret = test_trim_both_string();
|
|
||||||
if (ret == 0) {
|
|
||||||
amount_of_successful_tests++;
|
|
||||||
}
|
|
||||||
|
|
||||||
ret = test_remove_chars();
|
|
||||||
if (ret == 0) {
|
|
||||||
amount_of_successful_tests++;
|
|
||||||
}
|
|
||||||
|
|
||||||
ret = test_remove_string();
|
|
||||||
if (ret == 0) {
|
|
||||||
amount_of_successful_tests++;
|
|
||||||
}
|
|
||||||
|
|
||||||
ret = test_word_count();
|
|
||||||
if (ret == 0) {
|
|
||||||
amount_of_successful_tests++;
|
|
||||||
}
|
|
||||||
|
|
||||||
ret = test_url_encode();
|
|
||||||
if (ret == 0) {
|
|
||||||
amount_of_successful_tests++;
|
|
||||||
}
|
|
||||||
|
|
||||||
ret = test_url_decode();
|
|
||||||
if (ret == 0) {
|
|
||||||
amount_of_successful_tests++;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (amount_of_successful_tests != amount_of_tests) {
|
|
||||||
printf("%d/%d tests passed successfully\n", amount_of_successful_tests, amount_of_tests);
|
|
||||||
return 1;
|
return 1;
|
||||||
} else {
|
} else {
|
||||||
printf("All tests passed successfully\n");
|
printf("All tests passed successfully\n");
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Reference in New Issue
Block a user