refactor: function parameters, pedantic warnings

This commit is contained in:
thetek 2023-04-25 12:22:35 +02:00
parent f8300ed3a2
commit 389e5f1d69
9 changed files with 78 additions and 75 deletions

1
.ccls
View File

@ -2,4 +2,5 @@ gcc
-Wall
-Wextra
-Werror=implicit
-pedantic
-Ilib/inc

View File

@ -15,7 +15,7 @@
* original string.
*/
char *
strtrim (char *str)
strtrim (char str[static 1])
{
strtrimr (str);
return strtriml (str);
@ -32,7 +32,7 @@ strtrim (char *str)
* original string.
*/
char *
strtriml (char *str)
strtriml (char str[static 1])
{
while (*str && isspace (*str))
str++;
@ -50,7 +50,7 @@ strtriml (char *str)
* @return the new string length.
*/
size_t
strtrimr (char *str)
strtrimr (char str[static 1])
{
size_t len, i;
@ -71,7 +71,7 @@ strtrimr (char *str)
* @return the amount of occurances of `c` in `str`
*/
size_t
strcount (const char *str, char c)
strcount (const char str[static 1], char c)
{
size_t count;
@ -89,7 +89,7 @@ strcount (const char *str, char c)
* @param str: the string to convert
*/
void
strdowncase (char *str)
strdowncase (char str[static 1])
{
while (*str)
{
@ -105,7 +105,7 @@ strdowncase (char *str)
* @param str: the string to convert
*/
void
strupcase (char *str)
strupcase (char str[static 1])
{
while (*str)
{
@ -125,7 +125,7 @@ strupcase (char *str)
* if other characters are present.
*/
bool
strisblank (const char *str)
strisblank (const char str[static 1])
{
while (*str)
if (!isspace (*str++))

View File

@ -25,7 +25,7 @@ debug_malloc (size_t size, const char *file, size_t line)
ptr = malloc (size);
if (ptr == NULL)
log_print_fl (LOG_LEVEL_DEBUG, file, line, "malloc of size %zu \e[31mreturned null\e[0m\n", size);
log_print_fl (LOG_LEVEL_DEBUG, file, line, "malloc of size %zu \x1b[31mreturned null\x1b[0m\n", size);
else
log_print_fl (LOG_LEVEL_DEBUG, file, line, "malloc of size %zu to ptr %p\n", size, ptr);
@ -56,7 +56,7 @@ debug_realloc (void *ptr, size_t size, const char *file, size_t line)
old_ptr = (uintptr_t) ptr;
new_ptr = realloc (ptr, size);
if (new_ptr == NULL && size != 0)
log_print_fl (LOG_LEVEL_DEBUG, file, line, "realloc of ptr %#" PRIxPTR " to size %zu \e[31mreturned null\e[0m\n", old_ptr, size);
log_print_fl (LOG_LEVEL_DEBUG, file, line, "realloc of ptr %#" PRIxPTR " to size %zu \x1b[31mreturned null\x1b[0m\n", old_ptr, size);
else if (new_ptr == ptr)
log_print_fl (LOG_LEVEL_DEBUG, file, line, "realloc of ptr %p to size %zu\n", new_ptr, size);
else

View File

@ -5,12 +5,12 @@
#include <stddef.h>
#include <sys/types.h>
char *strtrim (char *str); /* trim whitespace of a cstring (both beginning and end) */
char *strtriml (char *str); /* trim whitespace of a cstring on the left (beginning of string) */
size_t strtrimr (char *str); /* trim whitespace of a cstring on the right (end of string) */
size_t strcount (const char *str, char c); /* count number of occurances of a character within a string */
void strdowncase (char *str); /* transform all uppercase letters of a string into lowercase letters */
void strupcase (char *str); /* transform all lowercase letters of a string into uppercase letters */
bool strisblank (const char *str); /* check if a string only consists of whitespace characters */
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 */
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 */
#endif // CUTILS_CSTR_H_

View File

@ -13,25 +13,25 @@ typedef struct
size_t cap; /* capacity (allocated size) */
} str_t;
str_t str_new (); /* initialize a str_t string. */
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_from (const char *src); /* initialize a str_t string with a given data. */
str_t str_new_from_len (const char *src, size_t len); /* initialize a str_t string with a given data and length. */
str_t str_clone (const str_t *orig); /* clone the contents of a str_t string into a new str_t string. */
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_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, const char *src); /* append a string to the end of a str_t string. */
void str_append_len (str_t *str, const char *src, size_t len); /* append a string to the end of a str_t string with a given length. */
int str_cmp (const str_t *str, const char *s2); /* compare a str_t string with a cstring. */
bool str_eq (const str_t *str, const char *s2); /* check if a str_t string equals a cstring. */
bool str_starts_with (const str_t *str, const char *find); /* check if a str_t string starts with a cstring. */
bool str_starts_with_len (const str_t *str, const char *find, size_t len); /* check if a str_t string starts with a cstring of given length. */
bool str_ends_with (const str_t *str, const char *find); /* check if a str_t string ends with a cstring. */
bool str_ends_with_len (const str_t *str, const char *find, size_t len); /* check if a str_t string ends with a cstring of given length. */
char *str_find (const str_t *str, const char *find); /* find a substring in a str_t string as a pointer. */
ssize_t str_pos (const str_t *str, const char *find); /* find the index of a substring in a str_t string. */
bool str_is_blank (const str_t *str); /* check if a string only consists of whitespace characters. */
void str_downcase (str_t *str); /* transform all uppercase characters in a str_t string to lowercase */
void str_upcase (str_t *str); /* transform all lowercase characters in a str_t string to uppercase */
void str_resize (str_t *str, size_t cap); /* resize the allocated data 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. */
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_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. */
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. */
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. */
#endif // CUTILS_STR_H_

View File

@ -32,11 +32,11 @@ typedef struct
} test_group_t;
test_suite_t test_suite_new (void); /* create a new test suite */
void test_suite_free (test_suite_t *suite); /* free dynamically allocated memory occupied by a test suite */
void test_suite_add (test_suite_t *suite, test_entry_func_t func); /* add a test group function to a test suite */
test_results_t test_suite_run (test_suite_t *suite); /* run the entries within a test suite and print an informational message */
void test_suite_free (test_suite_t suite[static 1]); /* free dynamically allocated memory occupied by a test suite */
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, 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 */
test_results_t test_group_get_results (test_group_t *group); /* get results of a test group. used for returning the status from a 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 */
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

@ -9,19 +9,19 @@ _log_print_type (log_level_t type)
switch (type)
{
case LOG_LEVEL_ERROR:
printf ("[\e[31merror\e[0m] ");
printf ("[\x1b[31merror\x1b[0m] ");
break;
case LOG_LEVEL_WARNING:
printf ("[\e[33mwarn\e[0m] ");
printf ("[\x1b[33mwarn\x1b[0m] ");
break;
case LOG_LEVEL_INFO:
printf ("[\e[34minfo\e[0m] ");
printf ("[\x1b[34minfo\x1b[0m] ");
break;
case LOG_LEVEL_OK:
printf ("[\e[32mok\e[0m] ");
printf ("[\x1b[32mok\x1b[0m] ");
break;
case LOG_LEVEL_DEBUG:
printf ("[\e[35mdebug\e[0m] ");
printf ("[\x1b[35mdebug\x1b[0m] ");
break;
}
}
@ -39,7 +39,7 @@ _log_print_type (log_level_t type)
*/
__attribute__ ((format (printf, 2, 3)))
void
log_print (log_level_t type, const char *fmt, ...)
log_print (log_level_t type, const char *restrict fmt, ...)
{
va_list ap;
@ -65,13 +65,13 @@ log_print (log_level_t type, const char *fmt, ...)
*/
__attribute__ ((format (printf, 4, 5)))
void
log_print_fl (log_level_t type, const char *file, size_t line, const char *fmt, ...)
log_print_fl (log_level_t type, const char *file, size_t line, const char *restrict fmt, ...)
{
va_list ap;
va_start (ap, fmt);
_log_print_type (type);
printf ("(\e[36m%s:%zu\e[0m) ", file, line);
printf ("(\x1b[36m%s:%zu\x1b[0m) ", file, line);
vprintf (fmt, ap);
va_end (ap);
}

View File

@ -13,7 +13,7 @@
* @return a new, empty str_t instance
*/
str_t
str_new ()
str_new (void)
{
str_t str;
@ -57,9 +57,9 @@ str_new_cap (size_t want_cap)
* @return a new, empty str_t with given data.
*/
inline str_t
str_new_from (const char *src)
str_new_from (const char src[static 1])
{
return str_new_from_len (src, strlen (src));
return str_new_from_len (strlen (src), src);
}
/**
@ -73,7 +73,7 @@ str_new_from (const char *src)
* @return a new, empty str_t with given data.
*/
str_t
str_new_from_len (const char *src, size_t len)
str_new_from_len (size_t len, const char src[restrict len])
{
size_t cap;
str_t str;
@ -96,9 +96,9 @@ str_new_from_len (const char *src, size_t len)
* @return a new str_t instance with the same data as `str`
*/
inline str_t
str_clone (const str_t *orig)
str_clone (const str_t orig[static 1])
{
return str_new_from_len (orig->str, orig->len);
return str_new_from_len (orig->len, orig->str);
}
/**
@ -120,9 +120,9 @@ str_free (str_t *str)
* @param src: the cstring to append
*/
inline void
str_append (str_t *str, const char *src)
str_append (str_t str[static 1], const char src[static 1])
{
str_append_len (str, src, strlen (src));
str_append_len (str, strlen (src), src);
}
/**
@ -135,7 +135,7 @@ str_append (str_t *str, const char *src)
* strlen (src)`
*/
void
str_append_len (str_t *str, const char *src, size_t len)
str_append_len (str_t str[static 1], size_t len, const char src[restrict len])
{
str_resize (str, str->len + len + 1);
strncpy (str->str + str->len, src, len);
@ -153,7 +153,7 @@ str_append_len (str_t *str, const char *src, size_t len)
* to be less than, to match, or to be greater than s2
*/
inline int
str_cmp (const str_t *str, const char *s2)
str_cmp (const str_t str[static 1], const char *s2)
{
return strcmp (str->str, s2);
}
@ -167,7 +167,7 @@ str_cmp (const str_t *str, const char *s2)
* @return true if str equals s2, else false
*/
inline bool
str_eq (const str_t *str, const char *s2)
str_eq (const str_t str[static 1], const char *s2)
{
return str_cmp (str, s2) == 0;
}
@ -181,9 +181,9 @@ str_eq (const str_t *str, const char *s2)
* @return true if `str` starts with `find`, else false
*/
inline bool
str_starts_with (const str_t *str, const char *find)
str_starts_with (const str_t str[static 1], const char find[static 1])
{
return str_starts_with_len (str, find, strlen (find));
return str_starts_with_len (str, strlen (find), find);
}
/**
@ -197,7 +197,8 @@ str_starts_with (const str_t *str, const char *find)
* @return true if `str` starts with `find`, else false
*/
inline bool
str_starts_with_len (const str_t *str, const char *find, size_t len)
str_starts_with_len (const str_t str[static 1], size_t len,
const char find[restrict len])
{
return strncmp (str->str, find, len) == 0;
}
@ -211,9 +212,9 @@ str_starts_with_len (const str_t *str, const char *find, size_t len)
* @return true if `str` ends with `find`, else false
*/
inline bool
str_ends_with (const str_t *str, const char *find)
str_ends_with (const str_t str[static 1], const char find[static 1])
{
return str_ends_with_len (str, find, strlen (find));
return str_ends_with_len (str, strlen (find), find);
}
/**
@ -227,7 +228,8 @@ str_ends_with (const str_t *str, const char *find)
* @return true if `str` ends with `find`, else false
*/
inline bool
str_ends_with_len (const str_t *str, const char *find, size_t len)
str_ends_with_len (const str_t str[static 1], size_t len,
const char find[restrict len])
{
return strcmp (str->str + str->len - len, find) == 0;
}
@ -242,7 +244,7 @@ str_ends_with_len (const str_t *str, const char *find, size_t len)
* @return beginning of found substring as pointer, or NULL if not found
*/
inline char *
str_find (const str_t *str, const char *find)
str_find (const str_t str[static 1], const char *find)
{
return strstr (str->str, find);
}
@ -257,7 +259,7 @@ str_find (const str_t *str, const char *find)
* @return beginning of found substring as string index, or -1 if not found
*/
ssize_t
str_pos (const str_t *str, const char *find)
str_pos (const str_t str[static 1], const char *find)
{
char *pos;
@ -277,7 +279,7 @@ str_pos (const str_t *str, const char *find)
* if other characters are present. false in case of error.
*/
inline bool
str_is_blank (const str_t *str)
str_is_blank (const str_t str[static 1])
{
return strisblank (str->str);
}
@ -289,7 +291,7 @@ str_is_blank (const str_t *str)
* @param str: the string to modify
*/
inline void
str_downcase (str_t *str)
str_downcase (str_t str[static 1])
{
strdowncase (str->str);
}
@ -301,7 +303,7 @@ str_downcase (str_t *str)
* @param str: the string to modify
*/
inline void
str_upcase (str_t *str)
str_upcase (str_t str[static 1])
{
strupcase (str->str);
}
@ -313,7 +315,7 @@ str_upcase (str_t *str)
* @param cap: the minimum amount of capacity to resize to
*/
void
str_resize (str_t *str, size_t cap)
str_resize (str_t str[static 1], size_t cap)
{
cap = max (next_pow_of_two (cap), STR_MIN_ALLOC);
if (cap > str->len && cap != str->cap)

View File

@ -31,7 +31,7 @@ test_suite_new (void)
* @param suite: a pointer to the suite to free up
*/
void
test_suite_free (test_suite_t *suite)
test_suite_free (test_suite_t suite[static 1])
{
free (suite->entries);
}
@ -43,7 +43,7 @@ test_suite_free (test_suite_t *suite)
* @param func: the test group function
*/
void
test_suite_add (test_suite_t *suite, test_entry_func_t func)
test_suite_add (test_suite_t suite[static 1], test_entry_func_t func)
{
if (suite->entries_len == suite->entries_cap - 1)
{
@ -64,7 +64,7 @@ test_suite_add (test_suite_t *suite, test_entry_func_t func)
* @return test results (number of successful and failed tests)
*/
test_results_t
test_suite_run (test_suite_t *suite)
test_suite_run (test_suite_t suite[static 1])
{
test_results_t total_result, result;
size_t i;
@ -113,7 +113,7 @@ test_group_new (void)
* `assertion` parameters.
*/
void
test_group_add (test_group_t *group, bool success, const char *assertion, const char *name, const char *file, size_t line)
test_group_add (test_group_t group[static 1], bool success, const char *assertion, const char *name, const char *file, size_t line)
{
if (success)
group->results.success += 1;
@ -131,7 +131,7 @@ test_group_add (test_group_t *group, bool success, const char *assertion, const
* @param group: the group to query
*/
test_results_t
test_group_get_results (test_group_t *group)
test_group_get_results (test_group_t group[static 1])
{
return group->results;
}