Basic implementation of more sensible string mutating functions for display format string creation. (`strncat_n()`, `strncpy_n()`.)

TODO: Better `dest` parameter bounds checking.

Fortune for naka's current commit: Future small blessing − 末小吉
display_format_safety
Avril 3 years ago
parent 8de332dbd4
commit e9ccf5e2f2
Signed by: flanchan
GPG Key ID: 284488987C31F630

@ -11,7 +11,24 @@
#define FLAGSET(f, flag) (( (f) & (flag) ) == (flag))
#define DFLAGSET(f, flagname) FLAGSET(f, DISPF_SHOW_ ## flagname)
//TODO: This probably won't work. Write wrappers for strncpy(), strncat() that return number of bytes written isntead of useless pointer that we already have.
// Returns number of bytes written to `dest`. Otherwise same as `strncpy()`.
static usize strncpy_n(char*pOUT dest, const char* restrict src, usize n)
{
usize i;
for(i=0;i<n;i++)
if(! (*dest++ = *src++)) return i;
dest[i] = 0;
return i;
}
// Returns number of bytes written to (dest+strlen(dest)). Otherwise same as `strncat()`.
static inline usize strncat_n(char*pOUT dest, const char* restrict src, usize n)
{
usize dl = strlen(dest);
return strncpy_n(dest+dl, src, n);
}
//TODO: This works, but seems inherantly unsafe. Rewrite to use the above `strn*_n()` functions for better length tracking.
static usize _display_get_fmt(dispflags_t flags, bool matched, usize _len, char str[pOUT _len])
{
register usize len = _len;

Loading…
Cancel
Save