2 #define I3__FILE__ "regex.c"
4 * vim:ts=4:sw=4:expandtab
6 * i3 - an improved dynamic tiling window manager
7 * © 2009 Michael Stapelberg and contributors (see also: LICENSE)
9 * regex.c: Interface to libPCRE (perl compatible regular expressions).
15 * Creates a new 'regex' struct containing the given pattern and a PCRE
16 * compiled regular expression. Also, calls pcre_study because this regex will
17 * most likely be used often (like for every new window and on every relevant
18 * property change of existing windows).
20 * Returns NULL if the pattern could not be compiled into a regular expression
21 * (and ELOGs an appropriate error message).
24 struct regex *regex_new(const char *pattern) {
26 int errorcode, offset;
28 struct regex *re = scalloc(1, sizeof(struct regex));
29 re->pattern = sstrdup(pattern);
30 int options = PCRE_UTF8;
31 /* We use PCRE_UCP so that \B, \b, \D, \d, \S, \s, \W, \w and some POSIX
32 * character classes play nicely with Unicode */
34 while (!(re->regex = pcre_compile2(pattern, options, &errorcode, &error, &offset, NULL))) {
35 /* If the error is that PCRE was not compiled with UTF-8 support we
36 * disable it and try again */
37 if (errorcode == 32) {
38 options &= ~PCRE_UTF8;
41 ELOG("PCRE regular expression compilation failed at %d: %s\n",
45 re->extra = pcre_study(re->regex, 0, &error);
46 /* If an error happened, we print the error message, but continue.
47 * Studying the regular expression leads to faster matching, but it’s not
48 * absolutely necessary. */
50 ELOG("PCRE regular expression studying failed: %s\n", error);
56 * Frees the given regular expression. It must not be used afterwards!
59 void regex_free(struct regex *regex) {
69 * Checks if the given regular expression matches the given input and returns
70 * true if it does. In either case, it logs the outcome using LOG(), so it will
71 * be visible without debug logging.
74 bool regex_matches(struct regex *regex, const char *input) {
77 /* We use strlen() because pcre_exec() expects the length of the input
79 if ((rc = pcre_exec(regex->regex, regex->extra, input, strlen(input), 0, 0, NULL, 0)) == 0) {
80 LOG("Regular expression \"%s\" matches \"%s\"\n",
81 regex->pattern, input);
85 if (rc == PCRE_ERROR_NOMATCH) {
86 LOG("Regular expression \"%s\" does not match \"%s\"\n",
87 regex->pattern, input);
91 ELOG("PCRE error %d while trying to use regular expression \"%s\" on input \"%s\", see pcreapi(3)\n",
92 rc, regex->pattern, input);