From e251c6463913bcd7c9c40b4dc168010f6de68b5a Mon Sep 17 00:00:00 2001 From: Thomas Voss Date: Tue, 5 Nov 2024 21:09:53 +0100 Subject: Check for whitespace after regex using islbrk() --- src/main.c | 11 +++++------ src/util.c | 14 ++++++++++++++ src/util.h | 3 +++ src/work.c | 13 ------------- 4 files changed, 22 insertions(+), 19 deletions(-) diff --git a/src/main.c b/src/main.c index 89e3007..120a88e 100644 --- a/src/main.c +++ b/src/main.c @@ -330,14 +330,13 @@ pattern_comp(u8view_t pat) reopts |= PCRE2_UCP; for (;;) { - if (ucsgnext(&g, &pat) == 0) + if (ucsgnext(&g, &pat) == 0 || islbrk(g)) break; - if (g.len != 1) - goto bad_flag; - if (uprop_is_pat_ws(*g.p)) + int w = ucsnext(&ch, &g); + if (uprop_is_pat_ws(ch)) break; - switch (*g.p) { + switch (ch) { case 'i': reopts |= PCRE2_CASELESS; break; case 'I': reopts &= ~PCRE2_CASELESS; break; case 'l': reopts |= PCRE2_LITERAL; break; @@ -345,7 +344,7 @@ pattern_comp(u8view_t pat) case 'u': reopts |= PCRE2_UCP; break; case 'U': reopts &= ~PCRE2_UCP; break; default: - bad_flag: + VSHFT(&g, -w); cerr(EXIT_FATAL, "Unknown regex flag %s%.*s%s", lquot, SV_PRI_ARGS(g), rquot); } diff --git a/src/util.c b/src/util.c index b5b1f8a..a946119 100644 --- a/src/util.c +++ b/src/util.c @@ -4,6 +4,8 @@ #include #include +#include +#include #include #include "exitcodes.h" @@ -47,4 +49,16 @@ getenv_posnum(const char *ev, int fallback) return (int)n; } return fallback; +} + +bool +islbrk(u8view_t g) +{ + return ucseq(g, U8("\n")) + || ucseq(g, U8("\v")) + || ucseq(g, U8("\f")) + || ucseq(g, U8("\r\n")) + || ucseq(g, U8("\x85")) + || ucseq(g, U8("\u2028")) + || ucseq(g, U8("\u2029")); } \ No newline at end of file diff --git a/src/util.h b/src/util.h index 705d0ee..ab3e6d6 100644 --- a/src/util.h +++ b/src/util.h @@ -1,7 +1,10 @@ #ifndef GRAB_UTIL_H #define GRAB_UTIL_H +#include + int getenv_posnum(const char *ev, int fallback); void pcre2_bitch_and_die(int ec, const char *fmt, ...); +bool islbrk(u8view_t g); #endif /* !GRAB_UTIL_H */ \ No newline at end of file diff --git a/src/work.c b/src/work.c index c8a3e21..5bc8b24 100644 --- a/src/work.c +++ b/src/work.c @@ -41,7 +41,6 @@ typedef struct { static void compute_pos(const char8_t *p, pos_state_t *ps); static bool has_lbrk_p(u8view_t sv); -static bool islbrk(u8view_t g); static int svposcmp(const void *a, const void *b); static void write_match_to_buffer(u8view_t sv, u8view_t *hl); @@ -482,18 +481,6 @@ has_lbrk_p(u8view_t sv) return false; } -bool -islbrk(u8view_t g) -{ - return ucseq(g, U8("\n")) - || ucseq(g, U8("\v")) - || ucseq(g, U8("\f")) - || ucseq(g, U8("\r\n")) - || ucseq(g, U8("\x85")) - || ucseq(g, U8("\u2028")) - || ucseq(g, U8("\u2029")); -} - int svposcmp(const void *a_, const void *b_) { -- cgit v1.2.3