From dca7e48d4af80cbf4ca689b2e9f1e1b4fa91d5ce Mon Sep 17 00:00:00 2001 From: Avril Date: Wed, 14 Jul 2021 01:59:23 +0100 Subject: [PATCH] `s_strncpy_n()`, `s_strncat_n()` trivial implementation. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fortune for naka's current commit: Great blessing − 大吉 --- src/display.c | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/src/display.c b/src/display.c index 1ac0ca0..9fa3c24 100644 --- a/src/display.c +++ b/src/display.c @@ -11,6 +11,13 @@ #define FLAGSET(f, flag) (( (f) & (flag) ) == (flag)) #define DFLAGSET(f, flagname) FLAGSET(f, DISPF_SHOW_ ## flagname) +#define CMP(x,cmp,y) ( (x) cmp (y) ? (x) : (y) ) +#define CMPx(x,cmp,y) ({ let _x = (x); let _y = (y); CMP(_x, cmp, _y); }) +#define MIN(x, y) CMP((x), <, (y)) +#define MINx(x, y) CMPx((x), <, (y)) +#define MAX(x, y) CMP((x), >, (y)) +#define MAXx(x, y) CMPx((x), >, (y)) + // Returns number of bytes written to `dest`. Otherwise same as `strncpy()`. static usize strncpy_n(char*pOUT dest, const char* restrict src, usize n) { @@ -24,13 +31,19 @@ static usize strncpy_n(char*pOUT dest, const char* restrict src, usize n) // Will not write past `dn` bytes of `dest`, will not read past `sn` bytes of `src`. Otherwise, same as `strncpy_n()`. static usize s_strncpy_n(usize sn, usize dn; char dest[static pOUT dn], usize dn, const char src[static restrict sn], usize sn) { - TODO("double-bounded strncpy"); + usize i; + usize n = MIN(sn,dn); + for(i=0;i