add number.h (and adapt existing code)
This commit is contained in:
parent
0627084381
commit
8e9ff3b108
@ -2,6 +2,7 @@
|
|||||||
// SPDX license identifier: MIT
|
// SPDX license identifier: MIT
|
||||||
|
|
||||||
#include <ds/error.h>
|
#include <ds/error.h>
|
||||||
|
#include <ds/number.h>
|
||||||
|
|
||||||
void *error_reserved_for_error = NULL;
|
void *error_reserved_for_error = NULL;
|
||||||
|
|
||||||
@ -20,37 +21,25 @@ void error_term() {
|
|||||||
size_t error_to_string(char *buf, size_t size, Error e, bool destroy) {
|
size_t error_to_string(char *buf, size_t size, Error e, bool destroy) {
|
||||||
size_t written = 0;
|
size_t written = 0;
|
||||||
if (e.has_location) {
|
if (e.has_location) {
|
||||||
size_t left = size > written ? size - written : 0;
|
written += snprintf(buf + written, sub_clamped(size, written), "%s:%zu: ", e.file, e.line);
|
||||||
written += snprintf(buf + written, left, "%s:%zu: ", e.file, e.line);
|
|
||||||
}
|
}
|
||||||
switch (e.kind) {
|
switch (e.kind) {
|
||||||
case ErrorNone: {
|
case ErrorNone:
|
||||||
size_t left = size > written ? size - written : 0;
|
written += snprintf(buf + written, sub_clamped(size, written), "Success");
|
||||||
written += snprintf(buf + written, left, "Success");
|
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
case ErrorOutOfMemory: {
|
case ErrorOutOfMemory:
|
||||||
size_t left = size > written ? size - written : 0;
|
written += snprintf(buf + written, sub_clamped(size, written), "Out of memory");
|
||||||
written += snprintf(buf + written, left, "Out of memory");
|
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
case ErrorString: {
|
case ErrorString: {
|
||||||
size_t left = size > written ? size - written : 0;
|
written += snprintf(buf + written, sub_clamped(size, written), "%s", e.str);
|
||||||
written += snprintf(buf + written, left, "%s", e.str);
|
if (e.str_on_heap && destroy)
|
||||||
if (e.str_on_heap && destroy)
|
free(e.str);
|
||||||
free(e.str);
|
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if (e.has_annex) {
|
if (e.has_annex) {
|
||||||
{
|
written += snprintf(buf + written, sub_clamped(size, written), ": ");
|
||||||
size_t left = size > written ? size - written : 0;
|
written += error_to_string(buf + written, sub_clamped(size, written), *e.annex, destroy);
|
||||||
written += snprintf(buf + written, left, ": ");
|
|
||||||
}
|
|
||||||
{
|
|
||||||
size_t left = size > written ? size - written : 0;
|
|
||||||
written += error_to_string(buf + written, left, *e.annex, destroy);
|
|
||||||
}
|
|
||||||
if (destroy)
|
if (destroy)
|
||||||
free(e.annex);
|
free(e.annex);
|
||||||
}
|
}
|
||||||
|
@ -4,6 +4,7 @@
|
|||||||
#define GENERIC_IMPL_STATIC
|
#define GENERIC_IMPL_STATIC
|
||||||
|
|
||||||
#include <ds/fmt.h>
|
#include <ds/fmt.h>
|
||||||
|
#include <ds/number.h>
|
||||||
#include <ds/types.h>
|
#include <ds/types.h>
|
||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
@ -531,7 +532,7 @@ size_t _fmtsv(const char *restrict file, size_t line, char *restrict buf, size_t
|
|||||||
};
|
};
|
||||||
_fmtcv(file, line, &ctx, format, args);
|
_fmtcv(file, line, &ctx, format, args);
|
||||||
if (size > 0)
|
if (size > 0)
|
||||||
buf[ctxs.written < size - 1 ? ctxs.written : size - 1] = 0;
|
buf[min(ctxs.written, size - 1)] = 0;
|
||||||
return ctxs.written;
|
return ctxs.written;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user