Back to index

glibc  2.9
bug-regex6.c
Go to the documentation of this file.
00001 /* Test for regexec.
00002    Copyright (C) 2002 Free Software Foundation, Inc.
00003    This file is part of the GNU C Library.
00004    Contributed by Jakub Jelinek <jakub@redhat.com>, 2002.
00005 
00006    The GNU C Library is free software; you can redistribute it and/or
00007    modify it under the terms of the GNU Lesser General Public
00008    License as published by the Free Software Foundation; either
00009    version 2.1 of the License, or (at your option) any later version.
00010 
00011    The GNU C Library is distributed in the hope that it will be useful,
00012    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014    Lesser General Public License for more details.
00015 
00016    You should have received a copy of the GNU Lesser General Public
00017    License along with the GNU C Library; if not, write to the Free
00018    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00019    02111-1307 USA.  */
00020 
00021 #include <locale.h>
00022 #include <stdio.h>
00023 #include <string.h>
00024 #include <sys/types.h>
00025 #include <regex.h>
00026 
00027 
00028 int
00029 main (int argc, char *argv[])
00030 {
00031   regex_t re;
00032   regmatch_t mat[10];
00033   int i, j, ret = 0;
00034   const char *locales[] = { "C", "de_DE.UTF-8" };
00035   const char *string = "http://www.regex.com/pattern/matching.html#intro";
00036   regmatch_t expect[10] = {
00037     { 0, 48 }, { 0, 5 }, { 0, 4 }, { 5, 20 }, { 7, 20 }, { 20, 42 },
00038     { -1, -1 }, { -1, -1 }, { 42, 48 }, { 43, 48 } };
00039 
00040   for (i = 0; i < sizeof (locales) / sizeof (locales[0]); ++i)
00041     {
00042       if (setlocale (LC_ALL, locales[i]) == NULL)
00043        {
00044          puts ("cannot set locale");
00045          ret = 1;
00046        }
00047       else if (regcomp (&re,
00048                      "^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\\?([^#]*))?(#(.*))?",
00049                      REG_EXTENDED) != REG_NOERROR)
00050        {
00051          puts ("cannot compile the regular expression");
00052          ret = 1;
00053        }
00054       else if (regexec (&re, string, 10, mat, 0) == REG_NOMATCH)
00055        {
00056          puts ("no match");
00057          ret = 1;
00058        }
00059       else
00060        {
00061          if (! memcmp (mat, expect, sizeof (mat)))
00062            printf ("matching ok for %s locale\n", locales[i]);
00063          else
00064            {
00065              printf ("matching failed for %s locale:\n", locales[i]);
00066              ret = 1;
00067              for (j = 0; j < 9; ++j)
00068               if (mat[j].rm_so != -1)
00069                 printf ("%d: %.*s\n", j, mat[j].rm_eo - mat[j].rm_so,
00070                        string + mat[j].rm_so);
00071            }
00072        }
00073     }
00074 
00075   return ret;
00076 }