diff options
-rw-r--r-- | include/macros.h | 6 | ||||
-rw-r--r-- | include/mbstring.h | 15 | ||||
-rw-r--r-- | lib/mbstring/rtou8.c | 4 | ||||
-rw-r--r-- | lib/mbstring/u8chr.c | 4 | ||||
-rw-r--r-- | lib/mbstring/u8cmp.c | 6 | ||||
-rw-r--r-- | lib/mbstring/u8rchr.c | 2 | ||||
-rw-r--r-- | lib/optparse/optparse.c | 26 |
7 files changed, 32 insertions, 31 deletions
diff --git a/include/macros.h b/include/macros.h index 24a5a5d..6adc1e4 100644 --- a/include/macros.h +++ b/include/macros.h @@ -6,9 +6,9 @@ #define lengthof(a) (sizeof(a) / sizeof(*(a))) -#define memeq(x, y, n) (!memcmp(x, y, n)) -#define streq(x, y) (!strcmp(x, y)) -#define u8eq(x, y) (!u8cmp(x, y)) +#define memeq(x, y, n) (!memcmp(x, y, n)) +#define streq(x, y) (!strcmp(x, y)) +#define u8eq(x, n, y, m) (!u8cmp(x, n, y, m)) #ifdef NDEBUG # include <stddef.h> diff --git a/include/mbstring.h b/include/mbstring.h index 9a95836..98c8137 100644 --- a/include/mbstring.h +++ b/include/mbstring.h @@ -8,7 +8,8 @@ #include "__rune.h" #include "__u8view.h" -#define U8V(s) ((struct u8view){.p = (s), .len = sizeof(s) - 1}) +#define U8V(s) ((struct u8view){.p = (s), .len = sizeof(s) - 1}) +#define U8_ARGS(s) ((s).p), ((s).len) /* clang-format off */ #define U8_BYTE_1(x) (((x) & 0x80) == 0x00) @@ -31,10 +32,10 @@ bool u8haspfx(const char8_t *, size_t, const char8_t *, size_t); bool u8hassfx(const char8_t *, size_t, const char8_t *, size_t); char8_t *u8chk(const char8_t *, size_t); -char8_t *u8chr(const char8_t *, rune, size_t); -char8_t *u8rchr(const char8_t *, rune, size_t); -int rtou8(char8_t *, rune, size_t); -int u8cmp(struct u8view, struct u8view); +char8_t *u8chr(const char8_t *, size_t, rune); +char8_t *u8rchr(const char8_t *, size_t, rune); +int rtou8(char8_t *, size_t, rune); +int u8cmp(const char8_t *, size_t, const char8_t *, size_t); int u8next(rune *, const char8_t **, size_t *); int u8prev(rune *, const char8_t **, const char8_t *); int u8tor(rune *, const char8_t *); @@ -44,8 +45,8 @@ size_t u8spn(const char8_t *, size_t, const rune *, size_t); #if !__MLIB_NO_MACRO_WRAPPER # define u8chk(s, n) __MLIB_Q_PTR(char8_t, u8chk, (s), (s), (n)) -# define u8chr(s, ch, n) __MLIB_Q_PTR(char8_t, u8chr, (s), (s), (ch), (n)) -# define u8rchr(s, ch, n) __MLIB_Q_PTR(char8_t, u8rchr, (s), (s), (ch), (n)) +# define u8chr(s, n, ch) __MLIB_Q_PTR(char8_t, u8chr, (s), (s), (n), (ch)) +# define u8rchr(s, n, ch) __MLIB_Q_PTR(char8_t, u8rchr, (s), (s), (n), (ch)) #endif #endif /* !MLIB_MBSTRING_H */ diff --git a/lib/mbstring/rtou8.c b/lib/mbstring/rtou8.c index 0ddac89..c11e89c 100644 --- a/lib/mbstring/rtou8.c +++ b/lib/mbstring/rtou8.c @@ -4,7 +4,7 @@ #include "rune.h" int -rtou8(char8_t *s, rune ch, size_t n) +rtou8(char8_t *s, size_t n, rune ch) { if (ch <= U8_1B_MAX) { if (n >= 1) @@ -33,5 +33,5 @@ rtou8(char8_t *s, rune ch, size_t n) return 4; } - return rtou8(s, RUNE_ERROR, n); + return rtou8(s, n, RUNE_ERROR); } diff --git a/lib/mbstring/u8chr.c b/lib/mbstring/u8chr.c index b7df221..2ce4e11 100644 --- a/lib/mbstring/u8chr.c +++ b/lib/mbstring/u8chr.c @@ -75,10 +75,10 @@ memmem4(const char8_t *h, size_t k, const char8_t *n) } char8_t * -u8chr(const char8_t *s, rune ch, size_t n) +u8chr(const char8_t *s, size_t n, rune ch) { char8_t buf[U8_LEN_MAX]; - int m = rtou8(buf, ch, sizeof(buf)); + int m = rtou8(buf, sizeof(buf), ch); if (n < (size_t)m) return nullptr; diff --git a/lib/mbstring/u8cmp.c b/lib/mbstring/u8cmp.c index 732896c..8bd2400 100644 --- a/lib/mbstring/u8cmp.c +++ b/lib/mbstring/u8cmp.c @@ -3,9 +3,7 @@ #include "mbstring.h" int -u8cmp(struct u8view x, struct u8view y) +u8cmp(const char8_t *x, size_t n, const char8_t *y, size_t m) { - if (x.len != y.len) - return x.len > y.len ? +1 : -1; - return memcmp(x.p, y.p, x.len); + return n != m ? (n > m ? +1 : -1) : memcmp(x, y, n); } diff --git a/lib/mbstring/u8rchr.c b/lib/mbstring/u8rchr.c index 1ffe445..df6be06 100644 --- a/lib/mbstring/u8rchr.c +++ b/lib/mbstring/u8rchr.c @@ -65,7 +65,7 @@ memrchr4(const char8_t *h, size_t k, const char8_t *n) } char8_t * -u8rchr(const char8_t *s, rune ch, size_t n) +u8rchr(const char8_t *s, size_t n, rune ch) { char8_t buf[U8_LEN_MAX]; int m = rtou8(buf, ch, sizeof(buf)); diff --git a/lib/optparse/optparse.c b/lib/optparse/optparse.c index ec2e470..931bf09 100644 --- a/lib/optparse/optparse.c +++ b/lib/optparse/optparse.c @@ -9,8 +9,9 @@ #define OPT_MSG_MISSING "option requires an argument" #define OPT_MSG_TOOMANY "option takes no arguments" -#define IS_SHORTOPT(s) ((s).len > 1 && (s).p[0] == '-' && (s).p[1] != '-') -#define IS_LONGOPT(s) ((s).len > 2 && (s).p[0] == '-' && (s).p[1] == '-') +#define IS_DASHDASH(s) ((s).len == 2 && (s).p[0] == '-' && (s).p[1] == '-') +#define IS_LONGOPT(s) ((s).len >= 3 && (s).p[0] == '-' && (s).p[1] == '-') +#define IS_SHORTOPT(s) ((s).len >= 2 && (s).p[0] == '-' && (s).p[1] != '-') #define error(st, msg, x) \ _Generic((x), struct u8view: error_s, rune: error_r)((st), (msg), (x)) @@ -39,7 +40,7 @@ optparse(struct optparse *st, const struct op_option *opts, size_t nopts) return 0; opt.len = strlen(opt.p); - if (u8eq(opt, U8V("--"))) { + if (IS_DASHDASH(opt)) { st->optind++; return 0; } @@ -63,12 +64,8 @@ optparse(struct optparse *st, const struct op_option *opts, size_t nopts) }; for (size_t i = 0; i < nopts; i++) { - if (!u8haspfx(opts[i].longopt.p, opts[i].longopt.len, opt_no_eq.p, - opt_no_eq.len)) - { + if (!u8haspfx(U8_ARGS(opts[i].longopt), U8_ARGS(opt_no_eq))) continue; - } - if (o) return error(st, OPT_MSG_INVALID, opt_no_eq); o = opts + i; @@ -87,8 +84,10 @@ optparse(struct optparse *st, const struct op_option *opts, size_t nopts) st->optarg = (struct u8view){}; else { ASSUME(opt.len > opt_no_eq.len); - st->optarg.p = eq_p + 1; - st->optarg.len = opt.len - opt_no_eq.len + 1; + st->optarg = (struct u8view){ + .p = eq_p + 1, + .len = opt.len - opt_no_eq.len + 1, + }; } break; case OPT_REQ: @@ -99,11 +98,14 @@ optparse(struct optparse *st, const struct op_option *opts, size_t nopts) st->optarg.len = strlen(st->optarg.p); } else { ASSUME(opt.len > opt_no_eq.len); - st->optarg.p = eq_p + 1; - st->optarg.len = opt.len - opt_no_eq.len + 1; + st->optarg = (struct u8view){ + .p = eq_p + 1, + .len = opt.len - opt_no_eq.len + 1, + }; } break; } + return o->shortopt; } |