bug-gnulib
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[bug-gnulib] making regex.c work in C++


From: Bruno Haible
Subject: [bug-gnulib] making regex.c work in C++
Date: Mon, 14 Mar 2005 15:11:20 +0100
User-agent: KMail/1.5

Hi,

GNU clisp is now using the gnulib regex. GNU clisp also must be buildable
with a C++ compiler (set CC="g++"). It makes the configure + Makefile stuff
easier if regex.c also can be built with the same compiler, and doesn't need
to use a different compiler than the rest of clisp.

Three easy patches are needed to make regex.c compile in C++ mode. May I
apply them to gnulib/lib/regex.c ?


Patch 1: 'not' is a keyword equivalent to '&&' in C++. It leads to syntax
errors.

        * lib/regex.c (byte_re_match_2_internal): Rename local variable
        'not' to 'negate'.

*** clisp-20050308/modules/regexp/regex.c.bak   2005-03-08 23:48:36.000000000 
+0100
--- clisp-20050308/modules/regexp/regex.c       2005-03-13 02:58:15.000000000 
+0100
***************
*** 6280,6288 ****
            uint32_t nrules;
  # endif /* _LIBC */
  #endif /* WCHAR */
!           boolean not = (re_opcode_t) *(p - 1) == charset_not;
  
!             DEBUG_PRINT2 ("EXECUTING charset%s.\n", not ? "_not" : "");
            PREFETCH ();
            c = TRANSLATE (*d); /* The character to match.  */
  #ifdef WCHAR
--- 6280,6288 ----
            uint32_t nrules;
  # endif /* _LIBC */
  #endif /* WCHAR */
!           boolean negate = (re_opcode_t) *(p - 1) == charset_not;
  
!             DEBUG_PRINT2 ("EXECUTING charset%s.\n", negate ? "_not" : "");
            PREFETCH ();
            c = TRANSLATE (*d); /* The character to match.  */
  #ifdef WCHAR
***************
*** 6615,6634 ****
              if (c == *workp)
                goto char_set_matched;
  
!           not = !not;
  
          char_set_matched:
!           if (not) goto fail;
  #else
              /* Cast to `unsigned' instead of `unsigned char' in case the
                 bit list is a full 32 bytes long.  */
            if (c < (unsigned) (*p * BYTEWIDTH)
                && p[1 + c / BYTEWIDTH] & (1 << (c % BYTEWIDTH)))
!             not = !not;
  
            p += 1 + *p;
  
!           if (!not) goto fail;
  #undef WORK_BUFFER_SIZE
  #endif /* WCHAR */
            SET_REGS_MATCHED ();
--- 6615,6634 ----
              if (c == *workp)
                goto char_set_matched;
  
!           negate = !negate;
  
          char_set_matched:
!           if (negate) goto fail;
  #else
              /* Cast to `unsigned' instead of `unsigned char' in case the
                 bit list is a full 32 bytes long.  */
            if (c < (unsigned) (*p * BYTEWIDTH)
                && p[1 + c / BYTEWIDTH] & (1 << (c % BYTEWIDTH)))
!             negate = !negate;
  
            p += 1 + *p;
  
!           if (!negate) goto fail;
  #undef WORK_BUFFER_SIZE
  #endif /* WCHAR */
            SET_REGS_MATCHED ();
***************
*** 7128,7142 ****
                else if ((re_opcode_t) p1[3] == charset
                         || (re_opcode_t) p1[3] == charset_not)
                  {
!                   int not = (re_opcode_t) p1[3] == charset_not;
  
                    if (c < (unsigned) (p1[4] * BYTEWIDTH)
                        && p1[5 + c / BYTEWIDTH] & (1 << (c % BYTEWIDTH)))
!                     not = !not;
  
!                     /* `not' is equal to 1 if c would match, which means
                          that we can't change to pop_failure_jump.  */
!                   if (!not)
                        {
                        p[-3] = (unsigned char) pop_failure_jump;
                          DEBUG_PRINT1 ("  No match => pop_failure_jump.\n");
--- 7128,7142 ----
                else if ((re_opcode_t) p1[3] == charset
                         || (re_opcode_t) p1[3] == charset_not)
                  {
!                   int negate = (re_opcode_t) p1[3] == charset_not;
  
                    if (c < (unsigned) (p1[4] * BYTEWIDTH)
                        && p1[5 + c / BYTEWIDTH] & (1 << (c % BYTEWIDTH)))
!                     negate = !negate;
  
!                     /* `negate' is equal to 1 if c would match, which means
                          that we can't change to pop_failure_jump.  */
!                   if (!negate)
                        {
                        p[-3] = (unsigned char) pop_failure_jump;
                          DEBUG_PRINT1 ("  No match => pop_failure_jump.\n");


Patch 2: A 'goto restore_best_regs;' statement jumps into the scope of
same_str_p.

        * lib/regex.c (byte_re_match_2_internal): Reduce scope of same_str_p
        variable.

*** clisp-20050308/modules/regexp/regex.c.bak   2005-03-08 23:48:36.000000000 
+0100
--- clisp-20050308/modules/regexp/regex.c       2005-03-13 02:58:15.000000000 
+0100
***************
*** 6013,6031 ****
             longest match, try backtracking.  */
          if (d != end_match_2)
            {
-             /* 1 if this match ends in the same string (string1 or string2)
-                as the best previous match.  */
-             boolean same_str_p = (FIRST_STRING_P (match_end)
-                                   == MATCHING_IN_FIRST_STRING);
              /* 1 if this match is the best seen so far.  */
              boolean best_match_p;
  
!             /* AIX compiler got confused when this was combined
!                with the previous declaration.  */
!             if (same_str_p)
!               best_match_p = d > match_end;
!             else
!               best_match_p = !MATCHING_IN_FIRST_STRING;
  
              DEBUG_PRINT1 ("backtracking.\n");
  
--- 6010,6031 ----
             longest match, try backtracking.  */
          if (d != end_match_2)
            {
              /* 1 if this match is the best seen so far.  */
              boolean best_match_p;
  
!             {
!               /* 1 if this match ends in the same string (string1 or string2)
!                  as the best previous match.  */
!               boolean same_str_p = (FIRST_STRING_P (match_end)
!                                     == MATCHING_IN_FIRST_STRING);
! 
!               /* AIX compiler got confused when this was combined
!                  with the previous declaration.  */
!               if (same_str_p)
!                 best_match_p = d > match_end;
!               else
!                 best_match_p = !MATCHING_IN_FIRST_STRING;
!             }
  
              DEBUG_PRINT1 ("backtracking.\n");
  


Patch 3: A realloc() result is directly assigne to an 'UCHAR_T *' variable
without a cast.

        * lib/regex.c (EXTEND_BUFFER, regcomp): Cast the realloc/malloc
        results.

*** clisp-20050308/modules/regexp/regex.c.bak   2005-03-08 23:48:36.000000000 
+0100
--- clisp-20050308/modules/regexp/regex.c       2005-03-13 02:58:15.000000000 
+0100
***************
*** 2082,2088 ****
      bufp->allocated <<= 1;                                            \
      if (bufp->allocated > MAX_BUF_SIZE)                                       
\
        bufp->allocated = MAX_BUF_SIZE;                                 \
!     bufp->buffer = REALLOC (COMPILED_BUFFER_VAR, bufp->allocated);    \
      if (COMPILED_BUFFER_VAR == NULL)                                  \
        return REG_ESPACE;                                              \
      /* If the buffer moved, move all the pointers into it.  */                
\
--- 2079,2085 ----
      bufp->allocated <<= 1;                                            \
      if (bufp->allocated > MAX_BUF_SIZE)                                       
\
        bufp->allocated = MAX_BUF_SIZE;                                 \
!     bufp->buffer = (UCHAR_T *) REALLOC (COMPILED_BUFFER_VAR, 
bufp->allocated); \
      if (COMPILED_BUFFER_VAR == NULL)                                  \
        return REG_ESPACE;                                              \
      /* If the buffer moved, move all the pointers into it.  */                
\
***************
*** 8017,8030 ****
    preg->used = 0;
  
    /* Try to allocate space for the fastmap.  */
!   preg->fastmap = malloc (1 << BYTEWIDTH);
  
    if (cflags & REG_ICASE)
      {
        unsigned i;
  
!       preg->translate = malloc (CHAR_SET_SIZE
!                               * sizeof (*(RE_TRANSLATE_TYPE)0));
        if (preg->translate == NULL)
          return (int) REG_ESPACE;
  
--- 8017,8031 ----
    preg->used = 0;
  
    /* Try to allocate space for the fastmap.  */
!   preg->fastmap = (char *) malloc (1 << BYTEWIDTH);
  
    if (cflags & REG_ICASE)
      {
        unsigned i;
  
!       preg->translate =
!       (RE_TRANSLATE_TYPE)
!       malloc (CHAR_SET_SIZE * sizeof (*(RE_TRANSLATE_TYPE)0));
        if (preg->translate == NULL)
          return (int) REG_ESPACE;
  





reply via email to

[Prev in Thread] Current Thread [Next in Thread]