Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add re_is_anchored interface. #500

Merged
merged 1 commit into from
Oct 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions include/re/re.h
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,22 @@ re_comp(enum re_dialect dialect,
const struct fsm_alloc *alloc,
enum re_flags flags, struct re_err *err);

/* Parse and analyze the regex enough to determine whether it is
* anchored at the start and/or end.
*
* As long as the result is checked for RE_IS_ANCHORED_ERROR first,
* the result can be used like a bitset. */
enum re_is_anchored_res {
RE_IS_ANCHORED_NONE = 0x00,
RE_IS_ANCHORED_START = 0x01,
RE_IS_ANCHORED_END = 0x02,
RE_IS_ANCHORED_BOTH = 0x03,
RE_IS_ANCHORED_ERROR = 0xFFFF,
};
enum re_is_anchored_res
re_is_anchored(enum re_dialect dialect, re_getchar_fun *f, void *opaque,
enum re_flags flags, struct re_err *err);

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this ought to be exposed to re(1) as a CLI interface in a similar way to re_is_literal. Which means tests for the CLI tool, and manpage stuff too.

I'm going to 👍 this PR now, we can come back and add those in the future.

/*
* Return a human-readable string describing a given error code. The string
* returned has static storage, and must not be freed.
Expand Down
1 change: 1 addition & 0 deletions src/libre/libre.syms
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ re_is_literal
re_flags
re_strerror
re_perror
re_is_anchored

ast_print
ast_print_dot
Expand Down
37 changes: 37 additions & 0 deletions src/libre/re.c
Original file line number Diff line number Diff line change
Expand Up @@ -335,3 +335,40 @@ re_is_literal(enum re_dialect dialect, int (*getc)(void *opaque), void *opaque,
return -1;
}

enum re_is_anchored_res
re_is_anchored(enum re_dialect dialect, re_getchar_fun *getc, void *opaque,
enum re_flags flags, struct re_err *err)
{
/* FIXME: copy/pasted from above, factor out common code later. */

struct ast *ast;
const struct dialect *m;
int unsatisfiable;

assert(getc != NULL);

m = re_dialect(dialect);
if (m == NULL) {
if (err != NULL) { err->e = RE_EBADDIALECT; }
return RE_IS_ANCHORED_ERROR;
}

flags |= m->flags;

ast = re_parse(dialect, getc, opaque, flags, err, &unsatisfiable);
if (ast == NULL) {
return RE_IS_ANCHORED_ERROR;
}

/* Copy anchoring flags, ending up with NONE, START, END, or BOTH. */
enum re_is_anchored_res res = RE_IS_ANCHORED_NONE;
if (ast->expr->flags & AST_FLAG_ANCHORED_START) {
res |= RE_IS_ANCHORED_START;
}
if (ast->expr->flags & AST_FLAG_ANCHORED_END) {
res |= RE_IS_ANCHORED_END;
}

ast_free(ast);
return res;
}