cleanup const qualifiers. Add index argument to vec_push
Test and Deploy / test (push) Successful in 15s Details
Test and Deploy / docs (push) Successful in 17s Details

This commit is contained in:
Evan Burkey 2024-07-31 14:40:16 -07:00
parent c2fe01f04c
commit 34b21c494b
8 changed files with 54 additions and 47 deletions

View File

@ -3,15 +3,15 @@
#include <stddef.h> #include <stddef.h>
const char *b64_encode(const unsigned char *s, size_t sz); char *b64_encode(const unsigned char *s, size_t sz);
unsigned char *b64_decode(const char *s, size_t sz, size_t *decode_sz); unsigned char *b64_decode(const char *s, size_t sz, size_t *decode_sz);
const char *hex_encode(const unsigned char *hex, size_t sz); char *hex_encode(const unsigned char *hex, size_t sz);
unsigned char *hex_decode(const char *orig, size_t *sz); unsigned char *hex_decode(const char *orig, size_t *sz);
const char *hex_to_str(const unsigned char *hex, size_t sz); char *hex_to_str(const unsigned char *hex, size_t sz);
const unsigned char* repeating_key_xor(const unsigned char* s, size_t s_sz, const unsigned char* key, size_t k_sz); unsigned char* repeating_key_xor(const unsigned char* s, size_t s_sz, const unsigned char* key, size_t k_sz);
const unsigned char *repeating_key_xor_s(const char* s, const char* key); unsigned char *repeating_key_xor_s(const char* s, const char* key);
unsigned int hamming_distance_s(const char *a, const char *b); unsigned int hamming_distance_s(const char *a, const char *b);
unsigned int hamming_distance(unsigned char *a, unsigned char *b, size_t sz); unsigned int hamming_distance(unsigned char *a, unsigned char *b, size_t sz);

View File

@ -5,6 +5,6 @@
int find_substrings(const char* haystack, const char* needle, size_t *num_substrings, size_t **substrings); int find_substrings(const char* haystack, const char* needle, size_t *num_substrings, size_t **substrings);
const char* substr(const char* str, size_t idx, size_t len); char* substr(const char* str, size_t idx, size_t len);
#endif // LIBFLINT_H_STRING #endif // LIBFLINT_H_STRING

View File

@ -18,7 +18,7 @@ void vec_destroy(Vector *vec);
int vec_insert(Vector *vec, void *data, size_t index); int vec_insert(Vector *vec, void *data, size_t index);
int vec_push(Vector *vec, void *data); int vec_push(Vector *vec, void *data, size_t *index);
void *vec_safe_at(Vector *vec, size_t index); void *vec_safe_at(Vector *vec, size_t index);

View File

@ -45,7 +45,7 @@ static int resize_b64_buf(b64_buf *b, size_t sz) {
return 0; return 0;
} }
const char *b64_encode(const unsigned char *s, size_t sz) { char *b64_encode(const unsigned char *s, size_t sz) {
int i = 0; int i = 0;
b64_buf encbuf; b64_buf encbuf;
size_t size = 0; size_t size = 0;
@ -236,7 +236,7 @@ unsigned char *hex_decode(const char *orig, size_t *sz) {
return hex; return hex;
} }
const char *hex_encode(const unsigned char *hex, size_t sz) { char *hex_encode(const unsigned char *hex, size_t sz) {
size_t ssz = sz * 2 + 1; size_t ssz = sz * 2 + 1;
char *s = malloc(sizeof(char) * ssz); char *s = malloc(sizeof(char) * ssz);
char *pos = s; char *pos = s;
@ -250,7 +250,7 @@ const char *hex_encode(const unsigned char *hex, size_t sz) {
return s; return s;
} }
const char *hex_to_str(const unsigned char *hex, size_t sz) { char *hex_to_str(const unsigned char *hex, size_t sz) {
char *s = malloc(sizeof(char) * (sz + 1)); char *s = malloc(sizeof(char) * (sz + 1));
for (size_t i = 0; i < sz; ++i) { for (size_t i = 0; i < sz; ++i) {
s[i] = (char)hex[i]; s[i] = (char)hex[i];
@ -259,7 +259,7 @@ const char *hex_to_str(const unsigned char *hex, size_t sz) {
return s; return s;
} }
const unsigned char* repeating_key_xor(const unsigned char* s, size_t s_sz, const unsigned char* key, size_t k_sz) { unsigned char* repeating_key_xor(const unsigned char* s, size_t s_sz, const unsigned char* key, size_t k_sz) {
unsigned char* r = malloc(sizeof(unsigned char) * s_sz); unsigned char* r = malloc(sizeof(unsigned char) * s_sz);
for (size_t i = 0, j = 0; i < s_sz; ++i) { for (size_t i = 0, j = 0; i < s_sz; ++i) {
r[i] = s[i] ^ key[j]; r[i] = s[i] ^ key[j];
@ -268,7 +268,7 @@ const unsigned char* repeating_key_xor(const unsigned char* s, size_t s_sz, cons
return r; return r;
} }
const unsigned char *repeating_key_xor_s(const char* s, const char* key) { unsigned char *repeating_key_xor_s(const char* s, const char* key) {
return repeating_key_xor((unsigned char*)s, strlen(s), (unsigned char*)key, strlen(key)); return repeating_key_xor((unsigned char*)s, strlen(s), (unsigned char*)key, strlen(key));
} }

View File

@ -83,7 +83,7 @@ static void *arena_resize_align(ArenaAllocator *allocator, void *mem, const size
return arena_malloc_align(allocator, new_sz, align); return arena_malloc_align(allocator, new_sz, align);
} }
if (allocator->buf <= mem && mem < allocator->buf + allocator->buf_sz) { if (allocator->buf <= (unsigned char*)mem && (unsigned char*)mem < allocator->buf + allocator->buf_sz) {
if (allocator->buf + allocator->offset_prev == old_mem) { if (allocator->buf + allocator->offset_prev == old_mem) {
allocator->offset_cur = allocator->offset_prev + new_sz; allocator->offset_cur = allocator->offset_prev + new_sz;
if (new_sz > old_sz) { if (new_sz > old_sz) {

View File

@ -40,7 +40,7 @@ int find_substrings(const char* haystack, const char* needle, size_t *num_substr
return 0; return 0;
} }
const char* substr(const char* str, size_t idx, size_t len) { char* substr(const char* str, size_t idx, size_t len) {
size_t sz_str = strlen(str); size_t sz_str = strlen(str);
if (sz_str < len || idx + len > sz_str) { if (sz_str < len || idx + len > sz_str) {
return NULL; return NULL;

View File

@ -81,7 +81,10 @@ int vec_insert(Vector *vec, void *data, size_t index) {
return 0; return 0;
} }
int vec_push(Vector *vec, void *data) { int vec_push(Vector *vec, void *data, size_t *index) {
if (index != NULL) {
*index = vec_len(vec);
}
return vec_insert(vec, data, vec_len(vec)); return vec_insert(vec, data, vec_len(vec));
} }

View File

@ -70,9 +70,9 @@ void test_set() {
int j = 2; int j = 2;
int k = 2; int k = 2;
set_insert(set, (void *) &i); set_insert(set, &i);
set_insert(set, (void *) &j); set_insert(set, &j);
set_insert(set, (void *) &k); set_insert(set, &k);
int i2 = 1; int i2 = 1;
int j2 = 4; int j2 = 4;
@ -80,8 +80,8 @@ void test_set() {
Set *set2 = malloc(sizeof(Set)); Set *set2 = malloc(sizeof(Set));
set_init(set2, int_match, NULL); set_init(set2, int_match, NULL);
set_insert(set2, (void *) &i2); set_insert(set2, &i2);
set_insert(set2, (void *) &j2); set_insert(set2, &j2);
printf("Set 1:"); printf("Set 1:");
print_ll(set); print_ll(set);
@ -202,7 +202,7 @@ void test_math() {
void print_vector(Vector *vec) { void print_vector(Vector *vec) {
for (size_t i = 0; i < vec->length; ++i) { for (size_t i = 0; i < vec->length; ++i) {
int t = *(int*)vec_at(vec, i); int t = *(int *) vec_at(vec, i);
printf("%d ", t); printf("%d ", t);
} }
printf("\n"); printf("\n");
@ -219,29 +219,31 @@ void test_vector() {
int e3 = 3; int e3 = 3;
int e4 = 4; int e4 = 4;
vec_push(v, &e0); size_t idx = 0;
vec_push(v, &e0, &idx);
assert(idx == 0);
assert(v->length == 1); assert(v->length == 1);
int *t = (int*)vec_at(v, 0); int *t = (int *) vec_at(v, 0);
assert(*t == 0); assert(*t == 0);
vec_push(v, &e1); vec_push(v, &e1, NULL);
vec_push(v, &e2); vec_push(v, &e2, NULL);
assert(v->length == 3); assert(v->length == 3);
// test access outside bounds // test access outside bounds
t = (int*)vec_safe_at(v, 3); t = (int *) vec_safe_at(v, 3);
assert(t == NULL); assert(t == NULL);
printf("Before insert: "); printf("Before insert: ");
print_vector(v); print_vector(v);
vec_push(v, &e4); vec_push(v, &e4, NULL);
vec_insert(v, &e3, 3); vec_insert(v, &e3, 3);
printf("After insert: "); printf("After insert: ");
print_vector(v); print_vector(v);
t = (int*)vec_at(v, 3); t = (int *) vec_at(v, 3);
assert(*t == e3); assert(*t == e3);
t = (int*)vec_at(v, 4); t = (int *) vec_at(v, 4);
assert(*t == e4); assert(*t == e4);
const int *min = vec_min(v, vec_cmp_int); const int *min = vec_min(v, vec_cmp_int);
@ -251,13 +253,13 @@ void test_vector() {
assert(*min == e0); assert(*min == e0);
assert(*max == e4); assert(*max == e4);
t = (int*)vec_remove(v, 1); t = (int *) vec_remove(v, 1);
assert(t != NULL); assert(t != NULL);
assert(*t == 1); assert(*t == 1);
printf("After removal: "); printf("After removal: ");
print_vector(v); print_vector(v);
t = (int*)vec_remove(v, 10); t = (int *) vec_remove(v, 10);
assert(t == NULL); assert(t == NULL);
printf("\ncap before shrink: %zu\n", vec_cap(v)); printf("\ncap before shrink: %zu\n", vec_cap(v));
@ -268,13 +270,15 @@ void test_vector() {
vec_destroy(v); vec_destroy(v);
free(v); free(v);
} }
void test_string() { void test_string() {
printf("\n--- STRING TEST ---\n"); printf("\n--- STRING TEST ---\n");
const char* haystack = "Test one two one and also maybe two but not Gabe's least favorite number, which is not one."; const char *haystack =
const char* needles[] = { "Test one two one and also maybe two but not Gabe's least favorite number, which is not one.";
"one", const char *needles[] = {
"two", "one",
"Gabe" "two",
"Gabe"
}; };
size_t sub_sz = 0; size_t sub_sz = 0;
@ -286,7 +290,7 @@ void test_string() {
assert(subs[1] == 13); assert(subs[1] == 13);
assert(subs[2] == 87); assert(subs[2] == 87);
const char *s = substr(haystack, subs[0], strlen(needles[0])); char *s = substr(haystack, subs[0], strlen(needles[0]));
assert(strcmp(s, needles[0]) == 0); assert(strcmp(s, needles[0]) == 0);
free(s); free(s);
@ -330,14 +334,14 @@ void test_crypto() {
char *out2 = "YSBsb25nZXIgYmFzZTY0IHRlc3QsIGFwcGFyZW50bHk="; char *out2 = "YSBsb25nZXIgYmFzZTY0IHRlc3QsIGFwcGFyZW50bHk=";
size_t s_sz = 0; size_t s_sz = 0;
s = (char *)b64_decode(out2, strlen(out2), &s_sz); s = (char *) b64_decode(out2, strlen(out2), &s_sz);
assert(strcmp(s, "a longer base64 test, apparently") == 0); assert(strcmp(s, "a longer base64 test, apparently") == 0);
assert(strlen(s) == s_sz); assert(strlen(s) == s_sz);
free(s); free(s);
s = hex_decode("DEADBEEF", &s_sz); s = hex_decode("DEADBEEF", &s_sz);
unsigned char h[4] = { unsigned char h[4] = {
0xDE, 0xAD, 0xBE, 0xEF 0xDE, 0xAD, 0xBE, 0xEF
}; };
for (size_t i = 0; i < 4; ++i) { for (size_t i = 0; i < 4; ++i) {
assert(s[i] == h[i]); assert(s[i] == h[i]);
@ -347,7 +351,7 @@ void test_crypto() {
// Odd number of characters // Odd number of characters
s = hex_decode("f00f5", &s_sz); s = hex_decode("f00f5", &s_sz);
unsigned char h2[4] = { unsigned char h2[4] = {
0x0F, 0x00, 0xF5 0x0F, 0x00, 0xF5
}; };
for (size_t i = 0; i < 3; ++i) { for (size_t i = 0; i < 3; ++i) {
assert(s[i] == h2[i]); assert(s[i] == h2[i]);
@ -367,20 +371,20 @@ void test_crypto() {
// "Sup?" // "Sup?"
unsigned char hexsup[4] = { unsigned char hexsup[4] = {
0x53, 0x75, 0x70, 0x3F 0x53, 0x75, 0x70, 0x3F
}; };
s = hex_to_str(hexsup, 4); s = hex_to_str(hexsup, 4);
assert(strcmp(s, "Sup?") == 0); assert(strcmp(s, "Sup?") == 0);
free(s); free(s);
s = repeating_key_xor_s("TEST", "HI"); s = repeating_key_xor_s("TEST", "HI");
const char *enc = hex_encode(s, 4); char *enc = hex_encode(s, 4);
assert(strcmp(enc, "1c0c1b1d") == 0); assert(strcmp(enc, "1c0c1b1d") == 0);
free(enc); free(enc);
free(s); free(s);
unsigned char ua[2] = { 0x2, 0xF }; unsigned char ua[2] = {0x2, 0xF};
unsigned char ub[2] = { 0x4, 0xE }; unsigned char ub[2] = {0x4, 0xE};
unsigned int hamming = hamming_distance(ua, ub, 2); unsigned int hamming = hamming_distance(ua, ub, 2);
assert(hamming == 3); assert(hamming == 3);
@ -405,7 +409,7 @@ void test_parsing() {
void tcp_test_handler(Server *s) { void tcp_test_handler(Server *s) {
struct sockaddr_storage client_addr; struct sockaddr_storage client_addr;
socklen_t client_addr_sz = sizeof(client_addr); socklen_t client_addr_sz = sizeof(client_addr);
int new_fd = accept(s->fd, (struct sockaddr *)&client_addr, &client_addr_sz); int new_fd = accept(s->fd, (struct sockaddr *) &client_addr, &client_addr_sz);
assert(new_fd != -1); assert(new_fd != -1);
assert(send(new_fd, NET_MSG, 10, 0) != -1); assert(send(new_fd, NET_MSG, 10, 0) != -1);
close(new_fd); close(new_fd);
@ -422,7 +426,7 @@ void udp_test_handler(Server *s) {
socklen_t client_addr_sz = sizeof(client_addr); socklen_t client_addr_sz = sizeof(client_addr);
char recv_buf[128]; char recv_buf[128];
int r = (int)recvfrom(s->fd, recv_buf, 128, 0, (struct sockaddr*)&client_addr, &client_addr_sz); int r = (int) recvfrom(s->fd, recv_buf, 128, 0, (struct sockaddr *) &client_addr, &client_addr_sz);
assert(r > 0); assert(r > 0);
assert(strcmp(recv_buf, NET_MSG) == 0); assert(strcmp(recv_buf, NET_MSG) == 0);
} }
@ -441,7 +445,7 @@ void test_network() {
sleep(1); sleep(1);
const char *s = capture_system("echo hello | nc localhost 18632", 0); const char *s = capture_system("echo hello | nc localhost 18632", 0);
assert(strcmp(s, NET_MSG) == 0); assert(strcmp(s, NET_MSG) == 0);
free((char *)s); free((char *) s);
pthread_join(srv_tid, NULL); pthread_join(srv_tid, NULL);
printf("Passed TCP test\n"); printf("Passed TCP test\n");