remove url stuff
I recommend using libcurl or similar for that
This commit is contained in:
44
strops.c
44
strops.c
@ -359,47 +359,3 @@ ull_t strops_word_count(const char* string) {
|
||||
return word_count;
|
||||
}
|
||||
|
||||
/* TODO: verify that this is correct */
|
||||
int strops_is_url_encoded(const char* string) {
|
||||
if (strops_contains_char(string, '%')) {
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
char* strops_url_encode(const char* string) {
|
||||
if (strops_is_url_encoded(string)) { return 0; }
|
||||
/* multiplied by 3, because example: ( => %28. Three times the chars */
|
||||
char* result = strops_copy(string);
|
||||
char* unsafe_chars = "()";
|
||||
char replacement[4];
|
||||
replacement[0] = '%';
|
||||
replacement[3] = '\0';
|
||||
|
||||
ull_t i, j;
|
||||
for (i = 0; i < strops_length(unsafe_chars); i++) {
|
||||
j = 0;
|
||||
while (strops_contains_char(result, unsafe_chars[i])) {
|
||||
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, strops_length(result));
|
||||
return result;
|
||||
}
|
||||
|
||||
char* strops_url_decode(const char* string) {
|
||||
if (!strops_is_url_encoded(string)) { return 0; }
|
||||
char* result = strops_copy(string);
|
||||
|
||||
result = realloc(result, strops_length(result));
|
||||
return result;
|
||||
}
|
||||
|
||||
|
13
strops.h
13
strops.h
@ -43,18 +43,5 @@ char* strops_remove_string(const char* string, const char* string_to_remove);
|
||||
|
||||
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 */
|
||||
|
||||
|
101
tests.c
101
tests.c
@ -441,107 +441,12 @@ int test_word_count() {
|
||||
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() {
|
||||
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_uppercase);
|
||||
da_append(&tests, test_is_lowercase);
|
||||
@ -569,10 +474,6 @@ int main() {
|
||||
|
||||
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;
|
||||
for (i = 0; i < tests.count; i++) {
|
||||
tests.amount_successful += tests.items[i]();
|
||||
|
Reference in New Issue
Block a user