Back to index

glibc  2.9
bug-regex22.c
Go to the documentation of this file.
00001 /* Test re.translate != NULL.
00002    Copyright (C) 2004, 2005 Free Software Foundation, Inc.
00003    This file is part of the GNU C Library.
00004    Contributed by Jakub Jelinek <jakub@redhat.com>, 2004.
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 <ctype.h>
00022 #include <locale.h>
00023 #include <regex.h>
00024 #include <stdio.h>
00025 #include <string.h>
00026 
00027 int
00028 main (void)
00029 {
00030   struct re_pattern_buffer re;
00031   char trans[256];
00032   int i, result = 0;
00033   const char *s;
00034 
00035   setlocale (LC_ALL, "de_DE.ISO-8859-1");
00036 
00037   for (i = 0; i < 256; ++i)
00038     trans[i] = tolower (i);
00039 
00040   re_set_syntax (RE_SYNTAX_POSIX_EGREP);
00041 
00042   memset (&re, 0, sizeof (re));
00043   re.translate = (unsigned char *) trans;
00044   s = re_compile_pattern ("\\W", 2, &re);
00045 
00046   if (s != NULL)
00047     {
00048       printf ("failed to compile pattern \"\\W\": %s\n", s);
00049       result = 1;
00050     }
00051   else
00052     {
00053       int ret = re_search (&re, "abc.de", 6, 0, 6, NULL);
00054       if (ret != 3)
00055        {
00056          printf ("1st re_search returned %d\n", ret);
00057          result = 1;
00058        }
00059 
00060       ret = re_search (&re, "\xc4\xd6\xae\xf7", 4, 0, 4, NULL);
00061       if (ret != 2)
00062        {
00063          printf ("2nd re_search returned %d\n", ret);
00064          result = 1;
00065        }
00066       re.translate = NULL;
00067       regfree (&re);
00068     }
00069 
00070   memset (&re, 0, sizeof (re));
00071   re.translate = (unsigned char *) trans;
00072   s = re_compile_pattern ("\\w", 2, &re);
00073 
00074   if (s != NULL)
00075     {
00076       printf ("failed to compile pattern \"\\w\": %s\n", s);
00077       result = 1;
00078     }
00079   else
00080     {
00081       int ret = re_search (&re, ".,!abc", 6, 0, 6, NULL);
00082       if (ret != 3)
00083        {
00084          printf ("3rd re_search returned %d\n", ret);
00085          result = 1;
00086        }
00087 
00088       ret = re_search (&re, "\xae\xf7\xc4\xd6", 4, 0, 4, NULL);
00089       if (ret != 2)
00090        {
00091          printf ("4th re_search returned %d\n", ret);
00092          result = 1;
00093        }
00094       re.translate = NULL;
00095       regfree (&re);
00096     }
00097 
00098   memset (&re, 0, sizeof (re));
00099   re.translate = (unsigned char *) trans;
00100   s = re_compile_pattern ("[[:DIGIT:]]", 11, &re);
00101   if (s == NULL)
00102     {
00103       printf ("compilation of \"[[:DIGIT:]]\" pattern unexpectedly succeeded: %s\n",
00104              s);
00105       result = 1;
00106     }
00107 
00108   memset (&re, 0, sizeof (re));
00109   re.translate = (unsigned char *) trans;
00110   s = re_compile_pattern ("[[:DIGIT:]]", 2, &re);
00111   if (s == NULL)
00112     {
00113       printf ("compilation of \"[[:DIGIT:]]\" pattern unexpectedly succeeded: %s\n",
00114              s);
00115       result = 1;
00116     }
00117 
00118   return result;
00119 }