Skip to content

Commit 33649df

Browse files
committed
precommit, cmake, and coverage improvements
1 parent 606e3c2 commit 33649df

11 files changed

Lines changed: 295 additions & 7 deletions

.pre-commit-config.yaml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
repos:
2+
- repo: local
3+
hooks:
4+
- id: clang-format
5+
name: clang-format
6+
entry: clang-format -i --style=LLVM
7+
language: system
8+
types_or: [c, c++]
9+
require_serial: true
10+
- id: cppcheck
11+
name: cppcheck
12+
entry: cppcheck --error-exitcode=1 --force
13+
language: system
14+
types_or: [c, c++]
15+
- id: custom-checks
16+
name: Build, Test, Valgrind, Coverage, Shields
17+
entry: ./scripts/run_custom_checks.sh
18+
language: system
19+
pass_filenames: false

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
c89stringutils
22
==============
33
[![License](https://img.shields.io/badge/license-Apache--2.0%20OR%20MIT-blue.svg)](https://opensource.org/licenses/Apache-2.0)
4+
![Doc Coverage](https://img.shields.io/badge/doc__coverage-100.00%25-brightgreen)
5+
![Test Coverage](https://img.shields.io/badge/test__coverage-100.00%25-brightgreen)
46
[![CI for Linux, Windows, macOS](https://github.com/offscale/c89stringutils/actions/workflows/ci.yml/badge.svg)](https://github.com/offscale/c89stringutils/actions/workflows/ci.yml)
57
[![C89](https://img.shields.io/badge/C-89-blue)](https://en.wikipedia.org/wiki/C89_(C_version))
68

c89stringutils/c89stringutils_export_pregen.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
/* for older targets and non CMake using build systems on MSVC */
1+
/**
2+
* @file c89stringutils_export_pregen.h
3+
* @brief Export macros for older targets and non CMake using build systems on
4+
* MSVC
5+
*/
26
#ifndef C89STRINGUTILS_EXPORT_H
37
#define C89STRINGUTILS_EXPORT_H
48

c89stringutils/c89stringutils_log.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
/**
2+
* @file c89stringutils_log.h
3+
* @brief Logging utilities for c89stringutils.
4+
*/
15
#ifndef C89STRINGUTILS_LOG_H
26
#define C89STRINGUTILS_LOG_H
37

c89stringutils/c89stringutils_string_extras.c

Lines changed: 79 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
/*
2-
* string functions helpful on Linux (and sometimes BSD)
1+
/**
2+
* @file c89stringutils_string_extras.c
3+
* @brief string functions helpful on Linux (and sometimes BSD)
34
* are now made available on other platforms (Windows, SunOS, &etc.)
4-
* */
5+
*/
56

67
/* clang-format off */
78
#include "c89stringutils_string_extras.h"
@@ -20,6 +21,11 @@
2021
#pragma GCC diagnostic ignored "-Wnonnull-compare"
2122
#endif
2223

24+
/**
25+
* @brief Log debug message
26+
* @param fmt The format string.
27+
* @param ... The arguments.
28+
*/
2329
void c89stringutils_log_debug(const char *fmt, ...) {
2430
int rc;
2531
va_list args;
@@ -61,6 +67,14 @@ void c89stringutils_log_debug(const char *fmt, ...) {
6167
#define _vsnprintf vsnprintf
6268
#endif /* ANY_BSD */
6369

70+
/**
71+
* @brief Implement vsnprintf for platforms that don't have it.
72+
* @param buffer The buffer to write to.
73+
* @param count The maximum number of characters to write.
74+
* @param format The format string.
75+
* @param args The va_list of arguments.
76+
* @return The number of characters written, or a negative value on error.
77+
*/
6478
static int wtf_vsnprintf(char *buffer, size_t count, const char *format,
6579
va_list args) {
6680
int rc;
@@ -96,6 +110,14 @@ static int wtf_vsnprintf(char *buffer, size_t count, const char *format,
96110

97111
#define HAVE_STRNCASECMP_H
98112

113+
/**
114+
* @brief Compare at most n characters of two strings, ignoring case.
115+
* @param s1 The first string to compare.
116+
* @param s2 The second string to compare.
117+
* @param n The maximum number of characters to compare.
118+
* @return An integer less than, equal to, or greater than zero if s1 is found,
119+
* respectively, to be less than, to match, or be greater than s2.
120+
*/
99121
int strncasecmp(const char *s1, const char *s2, size_t n) {
100122
int rc;
101123
if (s1 == NULL || s2 == NULL) {
@@ -106,6 +128,13 @@ int strncasecmp(const char *s1, const char *s2, size_t n) {
106128
return rc;
107129
}
108130

131+
/**
132+
* @brief Compare two strings, ignoring case.
133+
* @param s1 The first string to compare.
134+
* @param s2 The second string to compare.
135+
* @return An integer less than, equal to, or greater than zero if s1 is found,
136+
* respectively, to be less than, to match, or be greater than s2.
137+
*/
109138
int strcasecmp(const char *s1, const char *s2) {
110139
int rc;
111140
if (s1 == NULL || s2 == NULL) {
@@ -121,6 +150,15 @@ int strcasecmp(const char *s1, const char *s2) {
121150
#ifndef HAVE_STRNSTR
122151
#define HAVE_STRNSTR
123152

153+
/**
154+
* @brief Locate a substring in a string, looking at no more than len
155+
* characters.
156+
* @param buffer The string to search.
157+
* @param target The substring to find.
158+
* @param bufferLength The maximum number of characters to search.
159+
* @return A pointer to the first occurrence of little in big, or NULL if not
160+
* found.
161+
*/
124162
char *strnstr(const char *buffer, const char *target, size_t bufferLength) {
125163
size_t targetLength;
126164
const char *start;
@@ -154,6 +192,13 @@ char *strnstr(const char *buffer, const char *target, size_t bufferLength) {
154192

155193
/* `strcasestr` from MUSL */
156194

195+
/**
196+
* @brief Locate a substring in a string, ignoring case.
197+
* @param h The string to search.
198+
* @param n The substring to find.
199+
* @return A pointer to the first occurrence of little in big, or NULL if not
200+
* found.
201+
*/
157202
char *strcasestr(const char *h, const char *n) {
158203
size_t l;
159204
int rc;
@@ -179,6 +224,11 @@ char *strcasestr(const char *h, const char *n) {
179224
#define HAVE_STRERRORLEN_S
180225
/* MIT licensed function from Safe C Library */
181226

227+
/**
228+
* @brief Get the length of a string describing an error number.
229+
* @param errnum The error number.
230+
* @return The length of the string describing the error.
231+
*/
182232
size_t strerrorlen_s(errno_t errnum) {
183233
#ifndef ESNULLP
184234
#define ESNULLP (400) /* null ptr */
@@ -245,6 +295,15 @@ size_t strerrorlen_s(errno_t errnum) {
245295

246296
#define INIT_SZ 128
247297

298+
/**
299+
* @brief Write formatted output to a dynamically allocated string using a
300+
* va_list.
301+
* @param str A pointer to a string pointer where the allocated string will be
302+
* stored.
303+
* @param fmt The format string.
304+
* @param ap The va_list of arguments.
305+
* @return The number of characters printed, or -1 on error.
306+
*/
248307
extern int vasprintf(char **str, const char *fmt, va_list ap) {
249308
int rc;
250309
va_list ap2;
@@ -314,6 +373,14 @@ extern int vasprintf(char **str, const char *fmt, va_list ap) {
314373
return rc;
315374
}
316375

376+
/**
377+
* @brief Write formatted output to a dynamically allocated string.
378+
* @param str A pointer to a string pointer where the allocated string will be
379+
* stored.
380+
* @param fmt The format string.
381+
* @param ... The arguments.
382+
* @return The number of characters printed, or -1 on error.
383+
*/
317384
extern int asprintf(char **str, const char *fmt, ...) {
318385
int rc;
319386
va_list ap;
@@ -352,6 +419,15 @@ extern int asprintf(char **str, const char *fmt, ...) {
352419

353420
#ifndef HAVE_JASPRINTF
354421
#define HAVE_JASPRINTF
422+
/**
423+
* @brief `jasprintf`, a version of `asprintf` that concatenates on successive
424+
* calls: char *s = NULL; jasprintf(&s, "foo%s", "bar"); jasprintf(&s, "can%s",
425+
* "haz"); free(s);
426+
* @param unto The string to append to.
427+
* @param fmt The format string.
428+
* @param ... The arguments.
429+
* @return The concatenated string.
430+
*/
355431
char *jasprintf(char **unto, const char *fmt, ...) {
356432
va_list args;
357433
size_t base_length;

c89stringutils/c89stringutils_string_extras.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
/*
2-
* string functions helpful on Linux (and sometimes BSD)
1+
/**
2+
* @file c89stringutils_string_extras.h
3+
* @brief string functions helpful on Linux (and sometimes BSD)
34
* are now made available on other platforms (Windows, SunOS, &etc.)
4-
* */
5+
*/
56

67
#ifndef C89STRINGUTILS_STRING_EXTRAS_H
78
#define C89STRINGUTILS_STRING_EXTRAS_H

c89stringutils/tests/test.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
/**
2+
* @file test.c
3+
* @brief Main entry point for greatest tests.
4+
*/
15
/* clang-format off */
26
#include <greatest.h>
37
#include <c89stringutils_log.h>
@@ -8,6 +12,12 @@
812
/* Add definitions that need to be in the test runner's main file. */
913
GREATEST_MAIN_DEFS();
1014

15+
/**
16+
* @brief Main function for tests.
17+
* @param argc Number of arguments.
18+
* @param argv Argument values.
19+
* @return Exit status.
20+
*/
1121
int main(int argc, char **argv) {
1222
GREATEST_MAIN_BEGIN();
1323
RUN_SUITE(strnstr_suite);

0 commit comments

Comments
 (0)