* buffer.h (FETCH_MULTIBYTE_CHAR): Define as inline.
[bpt/emacs.git] / src / regex.c
index b769937..7ef53c6 100644 (file)
@@ -2,7 +2,7 @@
    0.12.  (Implements POSIX draft P1003.2/D11.2, except for some of the
    internationalization features.)
 
-   Copyright (C) 1993-2011  Free Software Foundation, Inc.
+   Copyright (C) 1993-2012  Free Software Foundation, Inc.
 
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
   #pragma alloca
 #endif
 
+/* Ignore some GCC warnings for now.  This section should go away
+   once the Emacs and Gnulib regex code is merged.  */
+#if (__GNUC__ == 4 && 3 <= __GNUC_MINOR__) || 4 < __GNUC__
+# pragma GCC diagnostic ignored "-Wstrict-overflow"
+# ifndef emacs
+#  pragma GCC diagnostic ignored "-Wunused-but-set-variable"
+#  pragma GCC diagnostic ignored "-Wunused-function"
+#  pragma GCC diagnostic ignored "-Wunused-macros"
+#  pragma GCC diagnostic ignored "-Wunused-result"
+#  pragma GCC diagnostic ignored "-Wunused-variable"
+# endif
+#endif
+
 #ifdef HAVE_CONFIG_H
 # include <config.h>
 #endif
@@ -53,7 +66,7 @@
        (HAVE_WCTYPE_H && HAVE_WCHAR_H && HAVE_BTOWC && !emacs)
 #endif
 
-/* For platform which support the ISO C amendement 1 functionality we
+/* For platform which support the ISO C amendment 1 functionality we
    support user defined character classes.  */
 #if WIDE_CHAR_SUPPORT
 /* Solaris 2.5 has a bug: <wchar.h> must be included before <wctype.h>.  */
 
 # include <setjmp.h>
 # include "lisp.h"
+# include "character.h"
 # include "buffer.h"
 
 /* Make syntax table lookup grant data in gl_state.  */
 # define SYNTAX_ENTRY_VIA_PROPERTY
 
 # include "syntax.h"
-# include "character.h"
 # include "category.h"
 
 # ifdef malloc
 
 /* When used in Emacs's lib-src, we need xmalloc and xrealloc. */
 
-void *
+static void *
 xmalloc (size_t size)
 {
   register void *val;
@@ -211,7 +224,7 @@ xmalloc (size_t size)
   return val;
 }
 
-void *
+static void *
 xrealloc (void *block, size_t size)
 {
   register void *val;
@@ -421,17 +434,7 @@ init_syntax_once (void)
 
 #endif /* not emacs */
 \f
-/* We remove any previous definition of `SIGN_EXTEND_CHAR',
-   since ours (we hope) works properly with all combinations of
-   machines, compilers, `char' and `unsigned char' argument types.
-   (Per Bothner suggested the basic approach.)  */
-#undef SIGN_EXTEND_CHAR
-#if __STDC__
-# define SIGN_EXTEND_CHAR(c) ((signed char) (c))
-#else  /* not __STDC__ */
-/* As in Harbison and Steele.  */
-# define SIGN_EXTEND_CHAR(c) ((((unsigned char) (c)) ^ 128) - 128)
-#endif
+#define SIGN_EXTEND_CHAR(c) ((signed char) (c))
 \f
 /* Should we use malloc or alloca?  If REGEX_MALLOC is not defined, we
    use `alloca' instead of `malloc'.  This is because using malloc in
@@ -540,12 +543,12 @@ typedef char boolean;
 #define false 0
 #define true 1
 
-static regoff_t re_match_2_internal _RE_ARGS ((struct re_pattern_buffer *bufp,
-                                              re_char *string1, size_t size1,
-                                              re_char *string2, size_t size2,
-                                              ssize_t pos,
-                                              struct re_registers *regs,
-                                              ssize_t stop));
+static regoff_t re_match_2_internal (struct re_pattern_buffer *bufp,
+                                    re_char *string1, size_t size1,
+                                    re_char *string2, size_t size2,
+                                    ssize_t pos,
+                                    struct re_registers *regs,
+                                    ssize_t stop);
 \f
 /* These are the command codes that appear in compiled regular
    expressions.  Some opcodes are followed by argument bytes.  A
@@ -722,11 +725,8 @@ typedef enum
   } while (0)
 
 #ifdef DEBUG
-static void extract_number _RE_ARGS ((int *dest, re_char *source));
 static void
-extract_number (dest, source)
-    int *dest;
-    re_char *source;
+extract_number (int *dest, re_char *source)
 {
   int temp = SIGN_EXTEND_CHAR (*(source + 1));
   *dest = *source & 0377;
@@ -750,12 +750,8 @@ extract_number (dest, source)
   } while (0)
 
 #ifdef DEBUG
-static void extract_number_and_incr _RE_ARGS ((int *destination,
-                                              re_char **source));
 static void
-extract_number_and_incr (destination, source)
-    int *destination;
-    re_char **source;
+extract_number_and_incr (int *destination, re_char **source)
 {
   extract_number (destination, *source);
   *source += 2;
@@ -1659,25 +1655,22 @@ do {                                                                    \
 \f
 /* Subroutine declarations and macros for regex_compile.  */
 
-static reg_errcode_t regex_compile _RE_ARGS ((re_char *pattern, size_t size,
-                                             reg_syntax_t syntax,
-                                             struct re_pattern_buffer *bufp));
-static void store_op1 _RE_ARGS ((re_opcode_t op, unsigned char *loc, int arg));
-static void store_op2 _RE_ARGS ((re_opcode_t op, unsigned char *loc,
-                                int arg1, int arg2));
-static void insert_op1 _RE_ARGS ((re_opcode_t op, unsigned char *loc,
-                                 int arg, unsigned char *end));
-static void insert_op2 _RE_ARGS ((re_opcode_t op, unsigned char *loc,
-                                 int arg1, int arg2, unsigned char *end));
-static boolean at_begline_loc_p _RE_ARGS ((re_char *pattern,
-                                          re_char *p,
-                                          reg_syntax_t syntax));
-static boolean at_endline_loc_p _RE_ARGS ((re_char *p,
-                                          re_char *pend,
-                                          reg_syntax_t syntax));
-static re_char *skip_one_char _RE_ARGS ((re_char *p));
-static int analyse_first _RE_ARGS ((re_char *p, re_char *pend,
-                                   char *fastmap, const int multibyte));
+static reg_errcode_t regex_compile (re_char *pattern, size_t size,
+                                   reg_syntax_t syntax,
+                                   struct re_pattern_buffer *bufp);
+static void store_op1 (re_opcode_t op, unsigned char *loc, int arg);
+static void store_op2 (re_opcode_t op, unsigned char *loc, int arg1, int arg2);
+static void insert_op1 (re_opcode_t op, unsigned char *loc,
+                       int arg, unsigned char *end);
+static void insert_op2 (re_opcode_t op, unsigned char *loc,
+                       int arg1, int arg2, unsigned char *end);
+static boolean at_begline_loc_p (re_char *pattern, re_char *p,
+                                reg_syntax_t syntax);
+static boolean at_endline_loc_p (re_char *p, re_char *pend,
+                                reg_syntax_t syntax);
+static re_char *skip_one_char (re_char *p);
+static int analyse_first (re_char *p, re_char *pend,
+                         char *fastmap, const int multibyte);
 
 /* Fetch the next character in the uncompiled pattern, with no
    translation.  */
@@ -2429,9 +2422,8 @@ regex_grow_registers (int num_regs)
 
 #endif /* not MATCH_MAY_ALLOCATE */
 \f
-static boolean group_in_compile_stack _RE_ARGS ((compile_stack_type
-                                                compile_stack,
-                                                regnum_t regnum));
+static boolean group_in_compile_stack (compile_stack_type compile_stack,
+                                      regnum_t regnum);
 
 /* `regex_compile' compiles PATTERN (of length SIZE) according to SYNTAX.
    Returns one of error codes defined in `regex.h', or zero for success.
@@ -3832,18 +3824,37 @@ static boolean
 at_begline_loc_p (const re_char *pattern, const re_char *p, reg_syntax_t syntax)
 {
   re_char *prev = p - 2;
-  boolean prev_prev_backslash = prev > pattern && prev[-1] == '\\';
+  boolean odd_backslashes;
 
-  return
-       /* After a subexpression?  */
-       (*prev == '(' && (syntax & RE_NO_BK_PARENS || prev_prev_backslash))
-       /* After an alternative?         */
-    || (*prev == '|' && (syntax & RE_NO_BK_VBAR || prev_prev_backslash))
-       /* After a shy subexpression?  */
-    || ((syntax & RE_SHY_GROUPS) && prev - 2 >= pattern
-       && prev[-1] == '?' && prev[-2] == '('
-       && (syntax & RE_NO_BK_PARENS
-           || (prev - 3 >= pattern && prev[-3] == '\\')));
+  /* After a subexpression?  */
+  if (*prev == '(')
+    odd_backslashes = (syntax & RE_NO_BK_PARENS) == 0;
+
+  /* After an alternative?  */
+  else if (*prev == '|')
+    odd_backslashes = (syntax & RE_NO_BK_VBAR) == 0;
+
+  /* After a shy subexpression?  */
+  else if (*prev == ':' && (syntax & RE_SHY_GROUPS))
+    {
+      /* Skip over optional regnum.  */
+      while (prev - 1 >= pattern && prev[-1] >= '0' && prev[-1] <= '9')
+       --prev;
+
+      if (!(prev - 2 >= pattern
+           && prev[-1] == '?' && prev[-2] == '('))
+       return false;
+      prev -= 2;
+      odd_backslashes = (syntax & RE_NO_BK_PARENS) == 0;
+    }
+  else
+    return false;
+
+  /* Count the number of preceding backslashes.  */
+  p = prev;
+  while (prev - 1 >= pattern && prev[-1] == '\\')
+    --prev;
+  return (p - prev) & odd_backslashes;
 }
 
 
@@ -4541,10 +4552,10 @@ WEAK_ALIAS (__re_search_2, re_search_2)
 \f
 /* Declarations and macros for re_match_2.  */
 
-static int bcmp_translate _RE_ARGS ((re_char *s1, re_char *s2,
-                                     register ssize_t len,
-                                     RE_TRANSLATE_TYPE translate,
-                                     const int multibyte));
+static int bcmp_translate (re_char *s1, re_char *s2,
+                          register ssize_t len,
+                          RE_TRANSLATE_TYPE translate,
+                          const int multibyte);
 
 /* This converts PTR, a pointer into one of the search strings `string1'
    and `string2' into an offset from the beginning of that string.  */