clay
clay copied to clipboard
`CLAY__STRING_LENGTH` internal macro uses incorrect formula
CLAY__STRING_LENGTH is defined as:
#define CLAY__STRING_LENGTH(s) ((sizeof(s) / sizeof((s)[0])) - sizeof((s)[0]))
However, this is incorrect. It technically works under most circumstances, which is why it's gone unnoticed, however, this returns the wrong length for non-UTF-8 strings. Looking at the corrected macro makes it clear why:
#define CLAY__STRING_LENGTH(s) ((sizeof(s) / sizeof((s)[0])) - 1)
Instead of simply subtracting the teminating null after calculating the array length, the current definition overthinks things and instead subtracts the size of the characters in bytes.
While this admittedly isn't a very critical bug, it's still a silly one, it stil actually cropped up in my environment somehow, and it is a one-line fix.
This cropped up because I was porting Clay to UTF-16 to work faster with Direct2D.
Thanks for finding this one! We have some work to do around utf16 and 32 handling, should come later this week 🙂
For now, should I drop the version I'm using here? All I really did was change all the chars to wchar_ts along with a few other minor tweaks to get it working, but it does work.