From 82f7f9ee1849947ed6de227279e623d8fc3a1ee1 Mon Sep 17 00:00:00 2001 From: Tavian Barnes Date: Thu, 24 Feb 2022 15:56:08 -0500 Subject: regex: Rework error handling --- regex.h | 30 ++++++++++++------------------ 1 file changed, 12 insertions(+), 18 deletions(-) (limited to 'regex.h') diff --git a/regex.h b/regex.h index ed509e4..63cd120 100644 --- a/regex.h +++ b/regex.h @@ -18,8 +18,6 @@ #ifndef BFS_REGEX_H #define BFS_REGEX_H -#include - /** * A compiled regular expression. */ @@ -54,34 +52,32 @@ enum bfs_regexec_flags { /** * Wrapper for regcomp() that supports additional regex types. * - * @param expr + * @param[out] preg + * Will hold the compiled regex. + * @param pattern * The regular expression to compile. * @param type * The regular expression syntax to use. * @param flags * Regex compilation flags. - * @param[out] err - * Will hold the error code if compilation fails. * @return - * The compiled regular expression, or NULL on error. + * 0 on success, -1 on failure. */ -struct bfs_regex *bfs_regcomp(const char *expr, enum bfs_regex_type type, enum bfs_regcomp_flags flags, int *err); +int bfs_regcomp(struct bfs_regex **preg, const char *pattern, enum bfs_regex_type type, enum bfs_regcomp_flags flags); /** * Wrapper for regexec(). * - * @param expr + * @param regex * The regular expression to execute. * @param str * The string to match against. * @param flags * Regex execution flags. - * @param[out] err - * Will hold the error code if execution fails. * @return - * Whether the regex matched. + * 1 for a match, 0 for no match, -1 on failure. */ -bool bfs_regexec(struct bfs_regex *regex, const char *str, enum bfs_regexec_flags flags, int *err); +int bfs_regexec(struct bfs_regex *regex, const char *str, enum bfs_regexec_flags flags); /** * Free a compiled regex. @@ -89,15 +85,13 @@ bool bfs_regexec(struct bfs_regex *regex, const char *str, enum bfs_regexec_flag void bfs_regfree(struct bfs_regex *regex); /** - * Dynamically allocate a regex error message. + * Get a human-readable regex error message. * - * @param err - * The error code to stringify. * @param regex - * The compiled regex, or NULL if compilation failed. + * The compiled regex. * @return - * A human-readable description of the error, allocated with malloc(). + * A human-readable description of the error, which should be free()'d. */ -char *bfs_regerror(int err, const struct bfs_regex *regex); +char *bfs_regerror(const struct bfs_regex *regex); #endif // BFS_REGEX_H -- cgit v1.2.3