Skip to content

Commit

Permalink
Allow regexes to contain ␀ byte
Browse files Browse the repository at this point in the history
  • Loading branch information
lluchs committed Jun 14, 2024
1 parent a7aac10 commit 2e6cca0
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 8 deletions.
7 changes: 5 additions & 2 deletions text-regex-tre.c
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,11 @@ void text_regex_free(Regex *r) {
free(r);
}

int text_regex_compile(Regex *regex, const char *string, int cflags) {
int r = tre_regcomp(&regex->regex, string, cflags);
int text_regex_compile(Regex *regex, const char *string, size_t len, int cflags) {
int r = len > 0
? tre_regncomp(&regex->regex, string, len, cflags)
: tre_regcomp(&regex->regex, string, cflags);

if (r)
tre_regcomp(&regex->regex, "\0\0", 0);
return r;
Expand Down
2 changes: 1 addition & 1 deletion text-regex.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Regex *text_regex_new(void) {
return r;
}

int text_regex_compile(Regex *regex, const char *string, int cflags) {
int text_regex_compile(Regex *regex, const char *string, size_t len, int cflags) {
int r = regcomp(&regex->regex, string, cflags);
if (r)
regcomp(&regex->regex, "\0\0", 0);
Expand Down
2 changes: 1 addition & 1 deletion text-regex.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ typedef struct Regex Regex;
typedef Filerange RegexMatch;

Regex *text_regex_new(void);
int text_regex_compile(Regex*, const char *pattern, int cflags);
int text_regex_compile(Regex*, const char *pattern, size_t len, int cflags);
size_t text_regex_nsub(Regex*);
void text_regex_free(Regex*);
int text_regex_match(Regex*, const char *data, int eflags);
Expand Down
2 changes: 1 addition & 1 deletion vis-prompt.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ static const char *prompt_enter(Vis *vis, const char *keys, const Arg *arg) {
else if (prompt->file == vis->search_file)
pattern = "^(/|\\?)";
int cflags = REG_EXTENDED|REG_NEWLINE|(REG_ICASE*vis->ignorecase);
if (pattern && regex && text_regex_compile(regex, pattern, cflags) == 0) {
if (pattern && regex && text_regex_compile(regex, pattern, 0, cflags) == 0) {
size_t end = text_line_end(txt, pos);
size_t prev = text_search_backward(txt, end, regex);
if (prev > pos)
Expand Down
8 changes: 5 additions & 3 deletions vis.c
Original file line number Diff line number Diff line change
Expand Up @@ -1579,17 +1579,19 @@ void vis_insert_nl(Vis *vis) {
}

Regex *vis_regex(Vis *vis, const char *pattern) {
if (!pattern && !(pattern = register_get(vis, &vis->registers[VIS_REG_SEARCH], NULL)))
size_t len = 0;
if (!pattern && !(pattern = register_get(vis, &vis->registers[VIS_REG_SEARCH], &len)))
return NULL;
Regex *regex = text_regex_new();
if (!regex)
return NULL;
int cflags = REG_EXTENDED|REG_NEWLINE|(REG_ICASE*vis->ignorecase);
if (text_regex_compile(regex, pattern, cflags) != 0) {
if (text_regex_compile(regex, pattern, len, cflags) != 0) {
text_regex_free(regex);
return NULL;
}
register_put0(vis, &vis->registers[VIS_REG_SEARCH], pattern);
if (len == 0)
register_put0(vis, &vis->registers[VIS_REG_SEARCH], pattern);
return regex;
}

Expand Down

0 comments on commit 2e6cca0

Please sign in to comment.