Compare commits

...

3 Commits

@ -51,7 +51,7 @@ void error_demo(void)
catch(&err) {
/* this will only be executed if the callee yeeted an error */
printf(" fails() failed with code %u: %s\n",
errnum(&err), nstr_raw(errmsg(&err)));
errnum(&err), errmsg_raw(&err));
/*
* you always need to call errput() in the outermost function,
@ -67,7 +67,7 @@ void error_demo(void)
succeeds(2, &err);
catch(&err) {
printf(" succeeds() failed with code %u: %s\n",
errnum(&err), nstr_raw(errmsg(&err)));
errnum(&err), errmsg_raw(&err));
errput(&err);
}
@ -75,7 +75,7 @@ void error_demo(void)
chain(3, &err);
catch(&err) {
printf(" chain() failed with code %u: %s\n",
errnum(&err), nstr_raw(errmsg(&err)));
errnum(&err), errmsg_raw(&err));
errput(&err);
}
}

@ -8,6 +8,27 @@
/**
* @defgroup error Error Handling
*
* libneo has a standardized way of handling errors: If a function is somehow
* able to fail at runtime, for example because of invalid parameters or lack of
* system resources, it accepts an argument of type `error *` which is usually
* the last one in the parameter list. When such a function is called and it
* encounters a runtime error, it `yeet()`s that error, does cleanup work if
* required, and then returns an undefined value. If the function call was
* successful, this is indicated by `neat()`. Functions accepting an `error *`
* parameter **MUST** always call either `yeet()` or `neat()`, but not both.
*
* If you call such a function, you have two options. If you don't want to deal
* with error handling, you can simply pass in `nil`, which will print the error
* message to stderr and cause the program to terminate if an error is
* `yeet()`ed. This is useful for quick prototyping and situations where
* encountering an error would be so critical that your program would need to
* halt anyway. To actually handle errors, you declare a local variable of type
* `error` and pass its address to the function. Then, immediately after the
* call, use the `catch()` macro to handle the error. In the error handler,
* you must call `errput()` to release resources attached to the error.
*
* See `demo/error.c` in the source code repository for examples.
*
* @{
*/
@ -57,7 +78,7 @@ void errput(error *err);
* by `err` is an actual error, i.e. it has been `yeet()`ed to.
* Resources for the error must be released using `errput()`.
*/
#define ncatch(err) if ((err) != nil && (err)->_number != 0)
#define ncatch(err) if ((err) != nil && (err)->_number + 1 >= 2)
#ifndef __cplusplus
/**
* @brief Catch an error.
@ -86,7 +107,19 @@ void errput(error *err);
* @param err `error *` to get the message of
* @returns The error message, may be `nil`
*/
#define errmsg(err) ((err) == nil ? (nstr_t *)nil : (err)->_message)
#define errmsg(err) ((err) == nil ? (nstr_t *)0 : (err)->_message)
/**
* @brief Get an optional error message as a raw C string.
*
* @param err `error *` to get the message of
* @returns The error message as a `const char *`, may be `nil`
*/
#define errmsg_raw(err) ( \
( (err) != nil && (err)->_message != nil ) \
? nstr_raw((err)->_message) \
: (const char *)0 \
)
/** @} */

@ -40,14 +40,15 @@ struct _neo_nstr_init_info {
#define nstr_raw(nstr) ((nstr)->_data)
/**
* @brief Statically initialize a neo string (file scope only).
* @brief Statically define an already declared neo string (file scope only).
*
* The string will be initialized before `main` is called.
*
* @param name Name of the (already declared) `nstr_t *` to initialize
* @param content A `const char *` with the contents of the string
* @see NSTR
*/
#define NSTR_INIT(name, content) \
#define NSTR_DEFINE(name, content) \
__neo_section(.data.__neo.nstr_array) \
struct _neo_nstr_init_info __neo_nstr_init_info_##name = { \
.dest = &name, \
@ -62,9 +63,9 @@ struct _neo_nstr_init_info {
* @param name Name of the `nstr_t *` variable to be declared
* @param content A `const char *` with the contents of the string
*/
#define NSTR_DEFINE(name, content) \
#define NSTR(name, content) \
nstr_t *name = nil; \
NSTR_INIT(name, content)
NSTR_DEFINE(name, content)
/**
* @brief Copy a regular C string to a neo string.

@ -1,4 +1,4 @@
/** See the end of this file for copyright and license terms. */
/* See the end of this file for copyright and license terms. */
#include <stdarg.h>
#include <stdio.h>

@ -1,4 +1,4 @@
/** See the end of this file for copyright and license terms. */
/* See the end of this file for copyright and license terms. */
#include <errno.h>

@ -1,4 +1,4 @@
/** See the end of this file for copyright and license terms. */
/* See the end of this file for copyright and license terms. */
#pragma once

@ -1,4 +1,4 @@
/** See the end of this file for copyright and license terms. */
/* See the end of this file for copyright and license terms. */
#include "neo/list.h"

@ -1,4 +1,4 @@
/** See the end of this file for copyright and license terms. */
/* See the end of this file for copyright and license terms. */
#include <errno.h>
#include <stdlib.h>

@ -1,4 +1,4 @@
/** See the end of this file for copyright and license terms. */
/* See the end of this file for copyright and license terms. */
#include <errno.h>
#include <stdio.h>

@ -1,4 +1,4 @@
/** See the end of this file for copyright and license terms. */
/* See the end of this file for copyright and license terms. */
#include <stdatomic.h>

@ -1,4 +1,4 @@
/** See the end of this file for copyright and license terms. */
/* See the end of this file for copyright and license terms. */
#include <errno.h>
#include <string.h>

@ -1,4 +1,4 @@
/** See the end of this file for copyright and license terms. */
/* See the end of this file for copyright and license terms. */
/* strnlen */
#define _POSIX_C_SOURCE 200809L

@ -1,4 +1,4 @@
/** See the end of this file for copyright and license terms. */
/* See the end of this file for copyright and license terms. */
/* strnlen */
#define _POSIX_C_SOURCE 200809L

@ -1,4 +1,4 @@
/** See the end of this file for copyright and license terms. */
/* See the end of this file for copyright and license terms. */
#include <errno.h>
#include <string.h>

@ -1,4 +1,4 @@
/** See the end of this file for copyright and license terms. */
/* See the end of this file for copyright and license terms. */
#include <errno.h>

@ -1,4 +1,4 @@
/** See the end of this file for copyright and license terms. */
/* See the end of this file for copyright and license terms. */
#include <errno.h>
#include <string.h>

@ -1,4 +1,4 @@
/** See the end of this file for copyright and license terms. */
/* See the end of this file for copyright and license terms. */
/*
* The UTF-8 conversion functions are based on the branchless UTF-8 decoder by

@ -1,4 +1,4 @@
/** See the end of this file for copyright and license terms. */
/* See the end of this file for copyright and license terms. */
#include <errno.h>

@ -1,4 +1,4 @@
/** See the end of this file for copyright and license terms. */
/* See the end of this file for copyright and license terms. */
#include <vector>
#include <catch2/catch.hpp>

@ -1,4 +1,4 @@
/** See the end of this file for copyright and license terms. */
/* See the end of this file for copyright and license terms. */
#include <catch2/catch.hpp>
#include <errno.h>

@ -1,4 +1,4 @@
/** See the end of this file for copyright and license terms. */
/* See the end of this file for copyright and license terms. */
#include <catch2/catch.hpp>
#include <errno.h>

@ -1,4 +1,4 @@
/** See the end of this file for copyright and license terms. */
/* See the end of this file for copyright and license terms. */
#include <catch2/catch.hpp>
#include <errno.h>

@ -1,4 +1,4 @@
/** See the end of this file for copyright and license terms. */
/* See the end of this file for copyright and license terms. */
#include <catch2/catch.hpp>
#include <errno.h>

@ -1,4 +1,4 @@
/** See the end of this file for copyright and license terms. */
/* See the end of this file for copyright and license terms. */
#include <catch2/catch.hpp>
#include <errno.h>

@ -1,4 +1,4 @@
/** See the end of this file for copyright and license terms. */
/* See the end of this file for copyright and license terms. */
#include <catch2/catch.hpp>
#include <errno.h>

@ -1,4 +1,4 @@
/** See the end of this file for copyright and license terms. */
/* See the end of this file for copyright and license terms. */
#include <catch2/catch.hpp>
#include <errno.h>

@ -1,4 +1,4 @@
/** See the end of this file for copyright and license terms. */
/* See the end of this file for copyright and license terms. */
#define CATCH_CONFIG_MAIN
#include <catch2/catch.hpp>

@ -1,4 +1,4 @@
/** See the end of this file for copyright and license terms. */
/* See the end of this file for copyright and license terms. */
#include <catch2/catch.hpp>
#include <errno.h>

@ -1,4 +1,4 @@
/** See the end of this file for copyright and license terms. */
/* See the end of this file for copyright and license terms. */
#include <catch2/catch.hpp>
#include <errno.h>

@ -1,4 +1,4 @@
/** See the end of this file for copyright and license terms. */
/* See the end of this file for copyright and license terms. */
#include <catch2/catch.hpp>
#include <errno.h>

@ -1,4 +1,4 @@
/** See the end of this file for copyright and license terms. */
/* See the end of this file for copyright and license terms. */
#include <catch2/catch.hpp>
#include <errno.h>
@ -60,9 +60,9 @@ TEST_CASE( "nnstr: Error if raw strig is nil", "[string/nstr,c]")
errput(&err);
}
NSTR_DEFINE(static_test_string_1, "i'm gay,,,");
NSTR(static_test_string_1, "i'm gay,,,");
TEST_CASE( "_neo_nstr_init_array: Statically initialize ASCII string", "[string/nstr.c]" )
TEST_CASE( "NSTR: Statically initialize ASCII string", "[string/nstr.c]" )
{
nstr_t *expected_s1 = nstr("i'm gay,,,", nil);
@ -70,9 +70,10 @@ TEST_CASE( "_neo_nstr_init_array: Statically initialize ASCII string", "[string/
REQUIRE( nlen(static_test_string_1) == 10 );
}
nstr_t *static_test_string_2;
NSTR_DEFINE(static_test_string_2, "i'm gay\xf0\x9f\xa5\xba,,,");
TEST_CASE( "_neo_nstr_init_array: Statically initialize UTF-8 string", "[string/nstr.c]" )
TEST_CASE( "NSTR_DEFINE: Statically initialize UTF-8 string", "[string/nstr.c]" )
{
nstr_t *expected_s2 = nstr("i'm gay\xf0\x9f\xa5\xba,,,", nil);

@ -1,4 +1,4 @@
/** See the end of this file for copyright and license terms. */
/* See the end of this file for copyright and license terms. */
#include <catch2/catch.hpp>
#include <errno.h>

@ -1,4 +1,4 @@
/** See the end of this file for copyright and license terms. */
/* See the end of this file for copyright and license terms. */
#include <catch2/catch.hpp>
#include <errno.h>

@ -1,4 +1,4 @@
/** See the end of this file for copyright and license terms. */
/* See the end of this file for copyright and license terms. */
#include <catch2/catch.hpp>
#include <errno.h>

@ -1,11 +1,11 @@
/** See the end of this file for copyright and license terms. */
/* See the end of this file for copyright and license terms. */
#include <catch2/catch.hpp>
#include <errno.h>
#include <neo.h>
TEST_CASE( "nstrmul: Repeat a string", "[string/nstrcat.c]" )
TEST_CASE( "nstrmul: Repeat a string", "[string/nstrmul.c]" )
{
error err;
nstr_t *s = nstr("aaaaa", nil);
@ -23,14 +23,14 @@ TEST_CASE( "nstrmul: Repeat a string", "[string/nstrcat.c]" )
nput(actual);
}
TEST_CASE( "nstrmul: Duplicate a string if count is 1", "[string/nstrcat.c]" )
TEST_CASE( "nstrmul: Duplicate a string if count is 1", "[string/nstrmull.c]" )
{
error err;
nstr_t *s = nstr("aaaaa", nil);
nstr_t *mul = nstrmul(s, 1, &err);
REQUIRE( nul != nil );
REQUIRE( mul != nil );
REQUIRE( nlen(mul) == 5 );
REQUIRE( nstreq(s, mul, nil) );
REQUIRE( errnum(&err) == 0 );
@ -39,36 +39,25 @@ TEST_CASE( "nstrmul: Duplicate a string if count is 1", "[string/nstrcat.c]" )
nput(mul);
}
TEST_CASE( "nstrmul: Error if string is nil", "[string/nstrcat.c]" )
{
error err;
nstr_t *mul = nstrmul(nil, 1, &err);
nstr_t *expected_msg = nstr("String is nil", nil);
REQUIRE( mul == nil );
REQUIRE( errnum(&err) == EFAULT );
REQUIRE( nstreq(expected_msg, errmsg(&err), nil) );
errput(&err);
}
TEST_CASE( "nstrmul: Return empty string if count is 0", "[string/nstrcat.c]" )
TEST_CASE( "nstrmul: Return empty string if count is 0", "[string/nstrmul.c]" )
{
error err;
nstr_t *s = nstr("aaaaa", nil);
nstr_t *mul = nstrmul(s, 0, &err);
nstr_t *expected_msg = nstr("String is nil", nil);
nstr_t *expected = nstr("", nil);
nstr_t *actual = nstrmul(s, 0, &err);
REQUIRE( mul == nil );
REQUIRE( errnum(&err) == EFAULT );
REQUIRE( nstreq(expected_msg, errmsg(&err), nil) );
REQUIRE( actual != nil );
REQUIRE( nlen(actual) == 0 );
REQUIRE( nstreq(expected, actual, nil) );
REQUIRE( errnum(&err) == 0 );
errput(&err);
nput(expected);
nput(actual);
nput(s);
}
TEST_CASE( "nstrmul: Error if string is nil", "[string/nstrcat.c]" )
TEST_CASE( "nstrmul: Error if string is nil", "[string/nstrmul.c]" )
{
error err;
nstr_t *mul = nstrmul(nil, 3, &err);

@ -9,6 +9,7 @@ target_sources(neo_test PRIVATE
string/nstrcat.cpp
string/nstrcmp.cpp
string/nstrdup.cpp
string/nstrmul.cpp
string/u2nstr.cpp
)

@ -1,4 +1,4 @@
/** See the end of this file for copyright and license terms. */
/* See the end of this file for copyright and license terms. */
#include <catch2/catch.hpp>
#include <errno.h>

@ -1,4 +1,4 @@
/** See the end of this file for copyright and license terms. */
/* See the end of this file for copyright and license terms. */
#include <catch2/catch.hpp>
#include <errno.h>

@ -1,4 +1,4 @@
/** See the end of this file for copyright and license terms. */
/* See the end of this file for copyright and license terms. */
#include <catch2/catch.hpp>
#include <errno.h>

@ -1,4 +1,4 @@
/** See the end of this file for copyright and license terms. */
/* See the end of this file for copyright and license terms. */
#include <catch2/catch.hpp>
#include <errno.h>

@ -1,4 +1,4 @@
/** See the end of this file for copyright and license terms. */
/* See the end of this file for copyright and license terms. */
#include <catch2/catch.hpp>
#include <errno.h>

@ -1,4 +1,4 @@
/** See the end of this file for copyright and license terms. */
/* See the end of this file for copyright and license terms. */
#include <catch2/catch.hpp>
#include <errno.h>

@ -1,4 +1,4 @@
/** See the end of this file for copyright and license terms. */
/* See the end of this file for copyright and license terms. */
#include <catch2/catch.hpp>
#include <errno.h>

Loading…
Cancel
Save