|
Packit |
6c4009 |
/* Test for regexec.
|
|
Packit |
6c4009 |
Copyright (C) 2002-2018 Free Software Foundation, Inc.
|
|
Packit |
6c4009 |
This file is part of the GNU C Library.
|
|
Packit |
6c4009 |
Contributed by Jakub Jelinek <jakub@redhat.com>, 2002.
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
The GNU C Library is free software; you can redistribute it and/or
|
|
Packit |
6c4009 |
modify it under the terms of the GNU Lesser General Public
|
|
Packit |
6c4009 |
License as published by the Free Software Foundation; either
|
|
Packit |
6c4009 |
version 2.1 of the License, or (at your option) any later version.
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
The GNU C Library is distributed in the hope that it will be useful,
|
|
Packit |
6c4009 |
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
Packit |
6c4009 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
Packit |
6c4009 |
Lesser General Public License for more details.
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
You should have received a copy of the GNU Lesser General Public
|
|
Packit |
6c4009 |
License along with the GNU C Library; if not, see
|
|
Packit |
6c4009 |
<http://www.gnu.org/licenses/>. */
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
#include <locale.h>
|
|
Packit |
6c4009 |
#include <stdio.h>
|
|
Packit |
6c4009 |
#include <string.h>
|
|
Packit |
6c4009 |
#include <sys/types.h>
|
|
Packit |
6c4009 |
#include <regex.h>
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
int
|
|
Packit |
6c4009 |
main (int argc, char *argv[])
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
regex_t re;
|
|
Packit |
6c4009 |
regmatch_t mat[10];
|
|
Packit |
6c4009 |
int i, j, ret = 0;
|
|
Packit |
6c4009 |
const char *locales[] = { "C", "de_DE.UTF-8" };
|
|
Packit |
6c4009 |
const char *string = "http://www.regex.com/pattern/matching.html#intro";
|
|
Packit |
6c4009 |
regmatch_t expect[10] = {
|
|
Packit |
6c4009 |
{ 0, 48 }, { 0, 5 }, { 0, 4 }, { 5, 20 }, { 7, 20 }, { 20, 42 },
|
|
Packit |
6c4009 |
{ -1, -1 }, { -1, -1 }, { 42, 48 }, { 43, 48 } };
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
for (i = 0; i < sizeof (locales) / sizeof (locales[0]); ++i)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
if (setlocale (LC_ALL, locales[i]) == NULL)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
puts ("cannot set locale");
|
|
Packit |
6c4009 |
ret = 1;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
else if (regcomp (&re,
|
|
Packit |
6c4009 |
"^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\\?([^#]*))?(#(.*))?",
|
|
Packit |
6c4009 |
REG_EXTENDED) != REG_NOERROR)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
puts ("cannot compile the regular expression");
|
|
Packit |
6c4009 |
ret = 1;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
else if (regexec (&re, string, 10, mat, 0) == REG_NOMATCH)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
puts ("no match");
|
|
Packit |
6c4009 |
ret = 1;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
else
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
if (! memcmp (mat, expect, sizeof (mat)))
|
|
Packit |
6c4009 |
printf ("matching ok for %s locale\n", locales[i]);
|
|
Packit |
6c4009 |
else
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
printf ("matching failed for %s locale:\n", locales[i]);
|
|
Packit |
6c4009 |
ret = 1;
|
|
Packit |
6c4009 |
for (j = 0; j < 9; ++j)
|
|
Packit |
6c4009 |
if (mat[j].rm_so != -1)
|
|
Packit |
6c4009 |
printf ("%d: %.*s\n", j, mat[j].rm_eo - mat[j].rm_so,
|
|
Packit |
6c4009 |
string + mat[j].rm_so);
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
return ret;
|
|
Packit |
6c4009 |
}
|