refactor: use types from common.h

This commit is contained in:
thetek 2023-04-25 13:18:05 +02:00
parent ea058316be
commit 1baa0a33af
11 changed files with 51 additions and 59 deletions

View File

@ -8,7 +8,7 @@
* @param size: the number of bytes to allocate
*/
void *
smalloc (size_t size)
smalloc (usize size)
{
void *ptr;
@ -26,7 +26,7 @@ smalloc (size_t size)
* @param size: the new number of bytes
*/
void *
srealloc (void *ptr, size_t size)
srealloc (void *ptr, usize size)
{
#if __STDC_VERSION__ >= 202000L // C23 or above
if (ptr == NULL)

View File

@ -49,10 +49,10 @@ strtriml (char str[static 1])
*
* @return the new string length.
*/
size_t
usize
strtrimr (char str[static 1])
{
size_t len, i;
usize len, i;
i = len = strlen (str);
while (i <= len && isspace (str[i - 1]))
@ -70,10 +70,10 @@ strtrimr (char str[static 1])
*
* @return the amount of occurances of `c` in `str`
*/
size_t
usize
strcount (const char str[static 1], char c)
{
size_t count;
usize count;
count = 0;
while (*str)

View File

@ -19,12 +19,12 @@
* automatically inserts filename and line.
*/
void *
debug_malloc (size_t size, const char *file, size_t line)
debug_malloc (usize size, const char *file, usize line)
{
void *ptr;
ptr = malloc (size);
if (ptr == NULL) {
if (ptr == nullptr) {
log_print_fl (
LOG_LEVEL_DEBUG, file, line,
"malloc of size %zu \x1b[31mreturned null\x1b[0m\n", size
@ -55,14 +55,14 @@ debug_malloc (size_t size, const char *file, size_t line)
* automatically inserts filename and line.
*/
void *
debug_realloc (void *ptr, size_t size, const char *file, size_t line)
debug_realloc (void *ptr, usize size, const char *file, usize line)
{
void *new_ptr;
uintptr_t old_ptr;
old_ptr = (uintptr_t) ptr;
new_ptr = realloc (ptr, size);
if (new_ptr == NULL && size != 0) {
if (new_ptr == nullptr && size != 0) {
log_print_fl (
LOG_LEVEL_DEBUG, file, line,
"realloc of ptr %#" PRIxPTR " to size %zu \x1b[31mreturned null\x1b[0m\n",
@ -95,7 +95,7 @@ debug_realloc (void *ptr, size_t size, const char *file, size_t line)
* inserts filename and line.
*/
void
debug_free (void *ptr, const char *file, size_t line)
debug_free (void *ptr, const char *file, usize line)
{
log_print_fl (LOG_LEVEL_DEBUG, file, line, "free ptr %p\n", ptr);
free (ptr);
@ -115,7 +115,7 @@ debug_free (void *ptr, const char *file, size_t line)
* automatically inserts filename and line.
*/
void *
debug_smalloc (size_t size, const char *file, size_t line)
debug_smalloc (usize size, const char *file, usize line)
{
void *ptr;
@ -141,7 +141,7 @@ debug_smalloc (size_t size, const char *file, size_t line)
* automatically inserts filename and line.
*/
void *
debug_srealloc (void *ptr, size_t size, const char *file, size_t line)
debug_srealloc (void *ptr, usize size, const char *file, usize line)
{
void *new_ptr;
uintptr_t old_ptr;

View File

@ -1,14 +1,12 @@
#ifndef CUTILS_CSTR_H_
#define CUTILS_CSTR_H_
#include <stdbool.h>
#include <stddef.h>
#include <sys/types.h>
#include "common.h"
char *strtrim (char str[static 1]); /* trim whitespace of a cstring (both beginning and end) */
char *strtriml (char str[static 1]); /* trim whitespace of a cstring on the left (beginning of string) */
size_t strtrimr (char str[static 1]); /* trim whitespace of a cstring on the right (end of string) */
size_t strcount (const char str[static 1], char c); /* count number of occurances of a character within a string */
usize strtrimr (char str[static 1]); /* trim whitespace of a cstring on the right (end of string) */
usize strcount (const char str[static 1], char c); /* count number of occurances of a character within a string */
void strdowncase (char str[static 1]); /* transform all uppercase letters of a string into lowercase letters */
void strupcase (char str[static 1]); /* transform all lowercase letters of a string into uppercase letters */
bool strisblank (const char str[static 1]); /* check if a string only consists of whitespace characters */

View File

@ -1,7 +1,6 @@
#ifndef CUTILS_DEBUG_H_
#define CUTILS_DEBUG_H_
#include <stddef.h>
#include <stdlib.h>
#include "common.h"
@ -13,10 +12,10 @@
# define srealloc(ptr, size) debug_srealloc (ptr, size, __FILE__, __LINE__) /* macro wrapper for debug_realloc() */
#endif
void *debug_malloc (size_t size, const char *file, size_t line); /* allocate memory using malloc() and print a fitting debug message */
void *debug_realloc (void *ptr, size_t size, const char *file, size_t line); /* reallocate memory using realloc() and print a fitting debug message */
void debug_free (void *ptr, const char *file, size_t line); /* free memory using free() and print a fitting debug message */
void *debug_smalloc (size_t size, const char *file, size_t line); /* allocate memory using smalloc() and print a fitting debug message */
void *debug_srealloc (void *ptr, size_t size, const char *file, size_t line); /* reallocate memory using srealloc() and print a fitting debug message */
void *debug_malloc (size_t size, const char *file, usize line); /* allocate memory using malloc() and print a fitting debug message */
void *debug_realloc (void *ptr, usize size, const char *file, usize line); /* reallocate memory using realloc() and print a fitting debug message */
void debug_free (void *ptr, const char *file, usize line); /* free memory using free() and print a fitting debug message */
void *debug_smalloc (usize size, const char *file, usize line); /* allocate memory using smalloc() and print a fitting debug message */
void *debug_srealloc (void *ptr, usize size, const char *file, usize line); /* reallocate memory using srealloc() and print a fitting debug message */
#endif // CUTILS_DEBUG_H_

View File

@ -1,8 +1,8 @@
#ifndef CUTILS_LOG_H_
#define CUTILS_LOG_H_
#include <stddef.h>
#include <stdlib.h>
#include "common.h"
#define log_error(...) log_print (LOG_LEVEL_ERROR, __VA_ARGS__) /* print a log message with level error */
#define log_warn(...) log_print (LOG_LEVEL_WARN, __VA_ARGS__) /* print a log message with level warning */
@ -26,6 +26,6 @@ typedef enum {
} log_level_t;
__attribute__ ((format (printf, 2, 3))) void log_print (log_level_t type, const char *fmt, ...); /* print a log message with given log level and format */
__attribute__ ((format (printf, 4, 5))) void log_print_fl (log_level_t type, const char *file, size_t line, const char *fmt, ...); /* print a log message with given log level and format, and add the filename and line that it was called in */
__attribute__ ((format (printf, 4, 5))) void log_print_fl (log_level_t type, const char *file, usize line, const char *fmt, ...); /* print a log message with given log level and format, and add the filename and line that it was called in */
#endif // CUTILS_LOG_H_

View File

@ -1,36 +1,34 @@
#ifndef CUTILS_STR_H_
#define CUTILS_STR_H_
#include <stdbool.h>
#include <stddef.h>
#include <sys/types.h>
#include "common.h"
/* represents a string, where length and capacity are stored alongside the string data. */
typedef struct {
char *str; /* string data */
size_t len; /* string length */
size_t cap; /* capacity (allocated size) */
usize len; /* string length */
usize cap; /* capacity (allocated size) */
} str_t;
str_t str_new (void); /* initialize a str_t string. */
str_t str_new_cap (size_t want_cap); /* initialize a str_t string with a given capacity. */
str_t str_new_cap (usize want_cap); /* initialize a str_t string with a given capacity. */
str_t str_new_from (const char src[static 1]); /* initialize a str_t string with a given data. */
str_t str_new_from_len (size_t len, const char src[restrict len]); /* initialize a str_t string with a given data and length. */
str_t str_new_from_len (usize len, const char src[restrict len]); /* initialize a str_t string with a given data and length. */
str_t str_clone (const str_t orig[static 1]); /* clone the contents of a str_t string into a new str_t string. */
void str_free (str_t *str); /* free the contents of a str_t string. */
void str_append (str_t str[static 1], const char src[static 1]); /* append a string to the end of a str_t string. */
void str_append_len (str_t str[static 1], size_t len, const char src[restrict len]); /* append a string to the end of a str_t string with a given length. */
void str_append_len (str_t str[static 1], usize len, const char src[restrict len]); /* append a string to the end of a str_t string with a given length. */
int str_cmp (const str_t str[static 1], const char *s2); /* compare a str_t string with a cstring. */
bool str_eq (const str_t str[static 1], const char *s2); /* check if a str_t string equals a cstring. */
bool str_starts_with (const str_t str[static 1], const char find[static 1]); /* check if a str_t string starts with a cstring. */
bool str_starts_with_len (const str_t str[static 1], size_t len, const char find[restrict len]); /* check if a str_t string starts with a cstring of given length. */
bool str_starts_with_len (const str_t str[static 1], usize len, const char find[restrict len]); /* check if a str_t string starts with a cstring of given length. */
bool str_ends_with (const str_t str[static 1], const char find[static 1]); /* check if a str_t string ends with a cstring. */
bool str_ends_with_len (const str_t str[static 1], size_t len, const char find[restrict len]); /* check if a str_t string ends with a cstring of given length. */
bool str_ends_with_len (const str_t str[static 1], usize len, const char find[restrict len]); /* check if a str_t string ends with a cstring of given length. */
char *str_find (const str_t str[static 1], const char *find); /* find a substring in a str_t string as a pointer. */
ssize_t str_pos (const str_t str[static 1], const char *find); /* find the index of a substring in a str_t string. */
isize str_pos (const str_t str[static 1], const char *find); /* find the index of a substring in a str_t string. */
bool str_is_blank (const str_t str[static 1]); /* check if a string only consists of whitespace characters. */
void str_downcase (str_t str[static 1]); /* transform all uppercase characters in a str_t string to lowercase */
void str_upcase (str_t str[static 1]); /* transform all lowercase characters in a str_t string to uppercase */
void str_resize (str_t str[static 1], size_t cap); /* resize the allocated data of a str_t string. */
void str_resize (str_t str[static 1], usize cap); /* resize the allocated data of a str_t string. */
#endif // CUTILS_STR_H_

View File

@ -1,16 +1,15 @@
#ifndef CUTILS_TEST_H_
#define CUTILS_TEST_H_
#include <stdbool.h>
#include <stddef.h>
#include "common.h"
#define test_add(group, success_and_assertion, name) test_group_add (group, success_and_assertion, name, __FILE__, __LINE__) /* add a test to a test group. source file and line will be inserted automatically. to be used with the `test_assert()` macro. */
#define test_assert(a) ((bool) (a)), (#a) /* provides the `success` and `assertion` parameters for `test_add()` / `test_group_add()` */
/* results of a test group or suite */
typedef struct {
size_t success; /* number of successful tests */
size_t failure; /* number of failed tests */
usize success; /* number of successful tests */
usize failure; /* number of failed tests */
} test_results_t;
/* function signature for test group functions */
@ -19,8 +18,8 @@ typedef test_results_t (*test_entry_func_t) (void);
/* test suite. a suite contains multiple test groups. */
typedef struct {
test_entry_func_t *entries; /* test groups as executable function pointers */
size_t entries_len; /* number of functions */
size_t entries_cap; /* allocated space for functions */
usize entries_len; /* number of functions */
usize entries_cap; /* allocated space for functions */
} test_suite_t;
/* test group. contains multiple tests. */
@ -33,7 +32,7 @@ void test_suite_free (test_suite_t suite[static 1]); /* free dynamically allocat
void test_suite_add (test_suite_t suite[static 1], test_entry_func_t func); /* add a test group function to a test suite */
test_results_t test_suite_run (test_suite_t suite[static 1]); /* run the entries within a test suite and print an informational message */
test_group_t test_group_new (void); /* create a new test group */
void test_group_add (test_group_t group[static 1], bool success, const char *assertion, const char *name, const char *file, size_t line); /* add a test to a test group, print an error message if the test failed */
void test_group_add (test_group_t group[static 1], bool success, const char *assertion, const char *name, const char *file, usize line); /* add a test to a test group, print an error message if the test failed */
test_results_t test_group_get_results (test_group_t group[static 1]); /* get results of a test group. used for returning the status from a test group */
#endif // CUTILS_TEST_H_

View File

@ -69,7 +69,7 @@ log_print (log_level_t type, const char *restrict fmt, ...)
*/
__attribute__ ((format (printf, 4, 5)))
void
log_print_fl (log_level_t type, const char *file, size_t line,
log_print_fl (log_level_t type, const char *file, usize line,
const char *restrict fmt, ...)
{
va_list ap;

View File

@ -2,7 +2,6 @@
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include "common.h"
#include "cstr.h"
#define STR_MIN_ALLOC 64 /* minimum allocation size for string data */
@ -34,9 +33,9 @@ str_new (void)
* @return a new, empty str_t instance with given capacity.
*/
str_t
str_new_cap (size_t want_cap)
str_new_cap (usize want_cap)
{
size_t cap;
usize cap;
str_t str;
cap = max (next_pow_of_two (want_cap), STR_MIN_ALLOC);
@ -73,9 +72,9 @@ str_new_from (const char src[static 1])
* @return a new, empty str_t with given data.
*/
str_t
str_new_from_len (size_t len, const char src[restrict len])
str_new_from_len (usize len, const char src[restrict len])
{
size_t cap;
usize cap;
str_t str;
cap = max (next_pow_of_two (len + 1), STR_MIN_ALLOC);
@ -135,7 +134,7 @@ str_append (str_t str[static 1], const char src[static 1])
* strlen (src)`
*/
void
str_append_len (str_t str[static 1], size_t len, const char src[restrict len])
str_append_len (str_t str[static 1], usize len, const char src[restrict len])
{
str_resize (str, str->len + len + 1);
strncpy (str->str + str->len, src, len);
@ -197,7 +196,7 @@ str_starts_with (const str_t str[static 1], const char find[static 1])
* @return true if `str` starts with `find`, else false
*/
inline bool
str_starts_with_len (const str_t str[static 1], size_t len,
str_starts_with_len (const str_t str[static 1], usize len,
const char find[restrict len])
{
return strncmp (str->str, find, len) == 0;
@ -228,7 +227,7 @@ str_ends_with (const str_t str[static 1], const char find[static 1])
* @return true if `str` ends with `find`, else false
*/
inline bool
str_ends_with_len (const str_t str[static 1], size_t len,
str_ends_with_len (const str_t str[static 1], usize len,
const char find[restrict len])
{
return strcmp (str->str + str->len - len, find) == 0;
@ -258,7 +257,7 @@ str_find (const str_t str[static 1], const char *find)
*
* @return beginning of found substring as string index, or -1 if not found
*/
ssize_t
isize
str_pos (const str_t str[static 1], const char *find)
{
char *pos;
@ -315,7 +314,7 @@ str_upcase (str_t str[static 1])
* @param cap: the minimum amount of capacity to resize to
*/
void
str_resize (str_t str[static 1], size_t cap)
str_resize (str_t str[static 1], usize cap)
{
cap = max (next_pow_of_two (cap), STR_MIN_ALLOC);
if (cap > str->len && cap != str->cap) {

View File

@ -1,7 +1,6 @@
#include "test.h"
#include <stdio.h>
#include <stdlib.h>
#include "common.h"
#include "log.h"
#define TEST_GROUP_ENTRIES_ALLOC 16
@ -67,7 +66,7 @@ test_results_t
test_suite_run (test_suite_t suite[static 1])
{
test_results_t total_result, result;
size_t i;
usize i;
total_result = (test_results_t) {0};
@ -123,7 +122,7 @@ test_group_new (void)
void
test_group_add (test_group_t group[static 1], bool success,
const char *assertion, const char *name, const char *file,
size_t line)
usize line)
{
if (success) {
group->results.success += 1;