Files
Toy/source/toy_print.c
T
Ratstail91 b718b35097 FIX: Substrings had corner-cases with incorrect results, read more
I found this while writing unit tests for Toy_Function, where one
(native) function was named 'identity' and another (custom) was named
'ident' to avoid a naming clash. The rename didn't resolve the clash, so
after some digging, I found that strings compared to substrings would
return a match, despite being different.

This took some awkward corner-case handling, as it turns out
'deepCompareUtil' only returns zero when no differences have been found,
not when a match has been found. I also added checks for this to
Toy_String's unit test, with the parameters checked in both orders i.e.
(a,b) and (b,a), because paranoia is your friend.

The rope pattern is powerful, but also gives you enough rope to hang
yourself.
2026-04-26 22:52:24 +10:00

56 lines
1.0 KiB
C

#include "toy_print.h"
#include <stdio.h>
static void outDefault(const char* msg) {
fprintf(stdout, "%s\n", msg);
}
static void errDefault(const char* msg) {
fprintf(stderr, "%s\n", msg);
}
static void assertDefault(const char* msg) {
fprintf(stderr, "%s\n", msg);
}
static Toy_callbackType printCallback = outDefault;
static Toy_callbackType errorCallback = errDefault;
static Toy_callbackType assertCallback = assertDefault;
void Toy_print(const char* msg) {
printCallback(msg);
}
void Toy_error(const char* msg) {
errorCallback(msg);
}
void Toy_assertFailure(const char* msg) {
assertCallback(msg);
}
void Toy_setPrintCallback(Toy_callbackType cb) {
printCallback = cb;
}
void Toy_setErrorCallback(Toy_callbackType cb) {
errorCallback = cb;
}
void Toy_setAssertFailureCallback(Toy_callbackType cb) {
assertCallback = cb;
}
void Toy_resetPrintCallback(void) {
printCallback = outDefault;
}
void Toy_resetErrorCallback(void) {
errorCallback = errDefault;
}
void Toy_resetAssertFailureCallback(void) {
assertCallback = assertDefault;
}