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>
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);
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);
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);
const unsigned char *repeating_key_xor_s(const char* s, const char* key);
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_s(const char* s, const char* key);
unsigned int hamming_distance_s(const char *a, const char *b);
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);
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

View File

@ -18,7 +18,7 @@ void vec_destroy(Vector *vec);
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);

View File

@ -45,7 +45,7 @@ static int resize_b64_buf(b64_buf *b, size_t sz) {
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;
b64_buf encbuf;
size_t size = 0;
@ -236,7 +236,7 @@ unsigned char *hex_decode(const char *orig, size_t *sz) {
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;
char *s = malloc(sizeof(char) * ssz);
char *pos = s;
@ -250,7 +250,7 @@ const char *hex_encode(const unsigned char *hex, size_t sz) {
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));
for (size_t i = 0; i < sz; ++i) {
s[i] = (char)hex[i];
@ -259,7 +259,7 @@ const char *hex_to_str(const unsigned char *hex, size_t sz) {
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);
for (size_t i = 0, j = 0; i < s_sz; ++i) {
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;
}
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));
}

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);
}
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) {
allocator->offset_cur = allocator->offset_prev + new_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;
}
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);
if (sz_str < len || idx + len > sz_str) {
return NULL;

View File

@ -81,7 +81,10 @@ int vec_insert(Vector *vec, void *data, size_t index) {
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));
}

View File

@ -70,9 +70,9 @@ void test_set() {
int j = 2;
int k = 2;
set_insert(set, (void *) &i);
set_insert(set, (void *) &j);
set_insert(set, (void *) &k);
set_insert(set, &i);
set_insert(set, &j);
set_insert(set, &k);
int i2 = 1;
int j2 = 4;
@ -80,8 +80,8 @@ void test_set() {
Set *set2 = malloc(sizeof(Set));
set_init(set2, int_match, NULL);
set_insert(set2, (void *) &i2);
set_insert(set2, (void *) &j2);
set_insert(set2, &i2);
set_insert(set2, &j2);
printf("Set 1:");
print_ll(set);
@ -219,13 +219,15 @@ void test_vector() {
int e3 = 3;
int e4 = 4;
vec_push(v, &e0);
size_t idx = 0;
vec_push(v, &e0, &idx);
assert(idx == 0);
assert(v->length == 1);
int *t = (int *) vec_at(v, 0);
assert(*t == 0);
vec_push(v, &e1);
vec_push(v, &e2);
vec_push(v, &e1, NULL);
vec_push(v, &e2, NULL);
assert(v->length == 3);
// test access outside bounds
@ -234,7 +236,7 @@ void test_vector() {
printf("Before insert: ");
print_vector(v);
vec_push(v, &e4);
vec_push(v, &e4, NULL);
vec_insert(v, &e3, 3);
printf("After insert: ");
print_vector(v);
@ -268,9 +270,11 @@ void test_vector() {
vec_destroy(v);
free(v);
}
void test_string() {
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 =
"Test one two one and also maybe two but not Gabe's least favorite number, which is not one.";
const char *needles[] = {
"one",
"two",
@ -286,7 +290,7 @@ void test_string() {
assert(subs[1] == 13);
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);
free(s);
@ -374,7 +378,7 @@ void test_crypto() {
free(s);
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);
free(enc);
free(s);