Avoid nullptr subtraction using intptr_t casts

Andrew's review on the previous fix noted that the nullptr checks produced slightly worse codegen. Replace the pointer subtraction with intptr_t subtraction, which avoids the undefined behaviour of subtracting two null pointers without introducing extra branches.
This commit is contained in:
2026-06-29 15:14:36 -04:00
parent bd53e57b8e
commit bf3f2fe810
+6 -11
View File
@@ -83,10 +83,7 @@ struct Parser3 {
[[nodiscard]] WeaselJsonStatus parse(char *buf, int len);
void flushNumber(bool done, char *buf) {
int len = 0;
if (dataBegin != nullptr && buf != nullptr) {
len = buf - dataBegin;
}
int len = (intptr_t)buf - (intptr_t)dataBegin;
assert(len >= 0);
if (done || len > 0) {
callbacks->on_number_data(userdata, dataBegin ? dataBegin : "", len,
@@ -95,13 +92,11 @@ struct Parser3 {
}
void flushString(bool done, char *buf) {
int len = 0;
if (dataBegin != nullptr) {
if (!(flags & WeaselJsonRaw)) {
len = writeBuf - dataBegin;
} else if (buf != nullptr) {
len = buf - dataBegin;
}
int len;
if (!(flags & WeaselJsonRaw)) {
len = (intptr_t)writeBuf - (intptr_t)dataBegin;
} else {
len = (intptr_t)buf - (intptr_t)dataBegin;
}
assert(len >= 0);
if (done || len > 0) {