Skip to content

Instantly share code, notes, and snippets.

@jweyrich
Last active July 12, 2017 08:52
Show Gist options
  • Save jweyrich/9803969 to your computer and use it in GitHub Desktop.
Save jweyrich/9803969 to your computer and use it in GitHub Desktop.
//
// Compile and run:
// LDFLAGS="-lpcre" make pcre_ucp_test && ./pcre_ucp_test
//
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <pcre.h>
#define SIZEOF_ARRAY(array) sizeof(array) / sizeof(*array)
bool regex_match(const char *pattern, const char *value, bool use_unicode) {
const char *err;
int errofs;
int ovector[1000];
pcre *re = pcre_compile(pattern, use_unicode ? PCRE_UCP : 0, &err, &errofs, NULL);
if (re == NULL) {
fprintf(stderr, "%s regex compilation failed\n", use_unicode ? "PCRE_UCP" : "ASCII");
exit(EXIT_FAILURE);
}
const int rc = pcre_exec(re, NULL, value, sizeof(value), 0, 0, ovector, SIZEOF_ARRAY(ovector));
pcre_free(re);
return rc > 0;
}
int main() {
const char pattern[] = "^[a-zA-Z]{3,}://.*$";
const char value[] = "http://www.example.com/foo/bar";
bool matches;
matches = regex_match(pattern, value, false);
printf("%s matches: %s\n", "ASCII", matches ? "yes" : "nope");
matches = regex_match(pattern, value, true);
printf("%s matches: %s\n", "PCRE_UCP", matches ? "yes" : "nope");
return 0;
}
@jweyrich
Copy link
Author

Mac output:

jweyrich@ptah$:pev [* master]$ uname -sv && LDFLAGS="-lpcre" make pcre_ucp_test && ./pcre_ucp_test
Darwin Darwin Kernel Version 13.1.0: Thu Jan 16 19:40:37 PST 2014; root:xnu-2422.90.20~2/RELEASE_X86_6
make: `pcre_ucp_test' is up to date.
ASCII matches: yes
PCRE_UCP regex compilation faile

Linux output:

jweyrich@devel:~/pev$ uname -sv && uname -sv && LDFLAGS="-lpcre" make pcre_ucp_test && ./pcre_ucp_test
Linux #1 SMP Debian 3.2.54-2
cc   -lpcre  pcre_ucp_test.c   -o pcre_ucp_test
ASCII matches: nope
PCRE_UCP matches: nope

@akzhan
Copy link

akzhan commented Jul 12, 2017

Just found the same problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment