Merge from trunk.
[bpt/emacs.git] / src / search.c
index ea10e47..d892792 100644 (file)
@@ -53,10 +53,10 @@ struct regexp_cache
 };
 
 /* The instances of that struct.  */
-struct regexp_cache searchbufs[REGEXP_CACHE_SIZE];
+static struct regexp_cache searchbufs[REGEXP_CACHE_SIZE];
 
 /* The head of the linked list; points to the most recently used buffer.  */
-struct regexp_cache *searchbuf_head;
+static struct regexp_cache *searchbuf_head;
 
 
 /* Every call to re_match, etc., must pass &search_regs as the regs
@@ -436,7 +436,7 @@ string_match_1 (Lisp_Object regexp, Lisp_Object string, Lisp_Object start, int p
   return make_number (string_byte_to_char (string, val));
 }
 
-DEFUE ("string-match", Fstring_match, Sstring_match, 2, 3, 0,
+DEFUN ("string-match", Fstring_match, Sstring_match, 2, 3, 0,
        doc: /* Return index of start of first match for REGEXP in STRING, or nil.
 Matching ignores case if `case-fold-search' is non-nil.
 If third arg START is non-nil, start search at that index in STRING.
@@ -683,7 +683,7 @@ scan_buffer (register int target, EMACS_INT start, EMACS_INT end,
            to see where we can avoid some scanning.  */
         if (target == '\n' && newline_cache)
           {
-            EMACS_INT next_change;
+            ptrdiff_t next_change;
             immediate_quit = 0;
             while (region_cache_forward
                    (current_buffer, newline_cache, start_byte, &next_change))
@@ -755,7 +755,7 @@ scan_buffer (register int target, EMACS_INT start, EMACS_INT end,
         /* Consult the newline cache, if appropriate.  */
         if (target == '\n' && newline_cache)
           {
-            EMACS_INT next_change;
+            ptrdiff_t next_change;
             immediate_quit = 0;
             while (region_cache_backward
                    (current_buffer, newline_cache, start_byte, &next_change))
@@ -2284,6 +2284,10 @@ The match found must start at or after that position.
 Optional third argument, if t, means if fail just return nil (no error).
   If not nil and not t, move to limit of search and return nil.
 Optional fourth argument is repeat count--search for successive occurrences.
+
+Search case-sensitivity is determined by the value of the variable
+`case-fold-search', which see.
+
 See also the functions `match-beginning', `match-end', `match-string',
 and `replace-match'.  */)
   (Lisp_Object regexp, Lisp_Object bound, Lisp_Object noerror, Lisp_Object count)
@@ -2300,6 +2304,10 @@ The match found must not extend after that position.
 Optional third argument, if t, means if fail just return nil (no error).
   If not nil and not t, move to limit of search and return nil.
 Optional fourth argument is repeat count--search for successive occurrences.
+
+Search case-sensitivity is determined by the value of the variable
+`case-fold-search', which see.
+
 See also the functions `match-beginning', `match-end', `match-string',
 and `replace-match'.  */)
   (Lisp_Object regexp, Lisp_Object bound, Lisp_Object noerror, Lisp_Object count)
@@ -2319,6 +2327,10 @@ The match found must start at or after that position.
 Optional third argument, if t, means if fail just return nil (no error).
   If not nil and not t, move to limit of search and return nil.
 Optional fourth argument is repeat count--search for successive occurrences.
+
+Search case-sensitivity is determined by the value of the variable
+`case-fold-search', which see.
+
 See also the functions `match-beginning', `match-end', `match-string',
 and `replace-match'.  */)
   (Lisp_Object regexp, Lisp_Object bound, Lisp_Object noerror, Lisp_Object count)
@@ -2336,6 +2348,10 @@ The match found must not extend after that position.
 Optional third argument, if t, means if fail just return nil (no error).
   If not nil and not t, move to limit of search and return nil.
 Optional fourth argument is repeat count--search for successive occurrences.
+
+Search case-sensitivity is determined by the value of the variable
+`case-fold-search', which see.
+
 See also the functions `match-beginning', `match-end', `match-string',
 and `replace-match'.  */)
   (Lisp_Object regexp, Lisp_Object bound, Lisp_Object noerror, Lisp_Object count)
@@ -2624,15 +2640,17 @@ since only regular expressions have distinguished subexpressions.  */)
      perform substitution on the replacement string.  */
   if (NILP (literal))
     {
-      EMACS_INT length = SBYTES (newtext);
+      ptrdiff_t length = SBYTES (newtext);
       unsigned char *substed;
-      EMACS_INT substed_alloc_size, substed_len;
+      ptrdiff_t substed_alloc_size, substed_len;
       int buf_multibyte = !NILP (BVAR (current_buffer, enable_multibyte_characters));
       int str_multibyte = STRING_MULTIBYTE (newtext);
       int really_changed = 0;
 
-      substed_alloc_size = length * 2 + 100;
-      substed = (unsigned char *) xmalloc (substed_alloc_size + 1);
+      substed_alloc_size = ((STRING_BYTES_BOUND - 100) / 2 < length
+                           ? STRING_BYTES_BOUND
+                           : length * 2 + 100);
+      substed = (unsigned char *) xmalloc (substed_alloc_size);
       substed_len = 0;
 
       /* Go thru NEWTEXT, producing the actual text to insert in
@@ -2643,7 +2661,7 @@ since only regular expressions have distinguished subexpressions.  */)
        {
          unsigned char str[MAX_MULTIBYTE_LENGTH];
          const unsigned char *add_stuff = NULL;
-         EMACS_INT add_len = 0;
+         ptrdiff_t add_len = 0;
          int idx = -1;
 
          if (str_multibyte)
@@ -2707,7 +2725,7 @@ since only regular expressions have distinguished subexpressions.  */)
             set up ADD_STUFF and ADD_LEN to point to it.  */
          if (idx >= 0)
            {
-             EMACS_INT begbyte = CHAR_TO_BYTE (search_regs.start[idx]);
+             ptrdiff_t begbyte = CHAR_TO_BYTE (search_regs.start[idx]);
              add_len = CHAR_TO_BYTE (search_regs.end[idx]) - begbyte;
              if (search_regs.start[idx] < GPT && GPT < search_regs.end[idx])
                move_gap (search_regs.start[idx]);
@@ -2718,12 +2736,11 @@ since only regular expressions have distinguished subexpressions.  */)
             is invariably ADD_LEN bytes starting at ADD_STUFF.  */
 
          /* Make sure SUBSTED is big enough.  */
-         if (substed_len + add_len >= substed_alloc_size)
-           {
-             substed_alloc_size = substed_len + add_len + 500;
-             substed = (unsigned char *) xrealloc (substed,
-                                                   substed_alloc_size + 1);
-           }
+         if (substed_alloc_size - substed_len < add_len)
+           substed =
+             xpalloc (substed, &substed_alloc_size,
+                      add_len - (substed_alloc_size - substed_len),
+                      STRING_BYTES_BOUND, 1);
 
          /* Now add to the end of SUBSTED.  */
          if (add_stuff)
@@ -2811,7 +2828,7 @@ match_limit (Lisp_Object num, int beginningp)
                                    : search_regs.end[n]));
 }
 
-DEFUE ("match-beginning", Fmatch_beginning, Smatch_beginning, 1, 1, 0,
+DEFUN ("match-beginning", Fmatch_beginning, Smatch_beginning, 1, 1, 0,
        doc: /* Return position of start of text matched by last search.
 SUBEXP, a number, specifies which parenthesized expression in the last
   regexp.
@@ -2823,7 +2840,7 @@ Zero means the entire text matched by the whole regexp or whole string.  */)
   return match_limit (subexp, 1);
 }
 
-DEFUE ("match-end", Fmatch_end, Smatch_end, 1, 1, 0,
+DEFUN ("match-end", Fmatch_end, Smatch_end, 1, 1, 0,
        doc: /* Return position of end of text matched by last search.
 SUBEXP, a number, specifies which parenthesized expression in the last
   regexp.
@@ -2835,7 +2852,7 @@ Zero means the entire text matched by the whole regexp or whole string.  */)
   return match_limit (subexp, 0);
 }
 
-DEFUE ("match-data", Fmatch_data, Smatch_data, 0, 3, 0,
+DEFUN ("match-data", Fmatch_data, Smatch_data, 0, 3, 0,
        doc: /* Return a list containing all info on what the last search matched.
 Element 2N is `(match-beginning N)'; element 2N + 1 is `(match-end N)'.
 All the elements are markers or nil (nil if the Nth pair didn't match)
@@ -2950,14 +2967,14 @@ Return value is undefined if the last search failed.  */)
    But it was ill-conceived: those supposedly-internal markers get exposed via
    the undo-list, so freeing them here is unsafe.  */
 
-DEFUE ("set-match-data", Fset_match_data, Sset_match_data, 1, 2, 0,
+DEFUN ("set-match-data", Fset_match_data, Sset_match_data, 1, 2, 0,
        doc: /* Set internal data on last search match from elements of LIST.
 LIST should have been created by calling `match-data' previously.
 
 If optional arg RESEAT is non-nil, make markers on LIST point nowhere.  */)
   (register Lisp_Object list, Lisp_Object reseat)
 {
-  register int i;
+  ptrdiff_t i;
   register Lisp_Object marker;
 
   if (running_asynch_code)
@@ -2971,31 +2988,21 @@ If optional arg RESEAT is non-nil, make markers on LIST point nowhere.  */)
 
   /* Allocate registers if they don't already exist.  */
   {
-    int length = XFASTINT (Flength (list)) / 2;
+    ptrdiff_t length = XFASTINT (Flength (list)) / 2;
 
     if (length > search_regs.num_regs)
       {
-       if (search_regs.num_regs == 0)
-         {
-           search_regs.start
-             = (regoff_t *) xmalloc (length * sizeof (regoff_t));
-           search_regs.end
-             = (regoff_t *) xmalloc (length * sizeof (regoff_t));
-         }
-       else
-         {
-           search_regs.start
-             = (regoff_t *) xrealloc (search_regs.start,
-                                      length * sizeof (regoff_t));
-           search_regs.end
-             = (regoff_t *) xrealloc (search_regs.end,
-                                      length * sizeof (regoff_t));
-         }
-
-       for (i = search_regs.num_regs; i < length; i++)
+       ptrdiff_t num_regs = search_regs.num_regs;
+       search_regs.start =
+         xpalloc (search_regs.start, &num_regs, length - num_regs,
+                  min (PTRDIFF_MAX, UINT_MAX), sizeof (regoff_t));
+       search_regs.end =
+         xrealloc (search_regs.end, num_regs * sizeof (regoff_t));
+
+       for (i = search_regs.num_regs; i < num_regs; i++)
          search_regs.start[i] = -1;
 
-       search_regs.num_regs = length;
+       search_regs.num_regs = num_regs;
       }
 
     for (i = 0; CONSP (list); i++)
@@ -3181,10 +3188,8 @@ syms_of_search (void)
     }
   searchbuf_head = &searchbufs[0];
 
-  Qsearch_failed = intern_c_string ("search-failed");
-  staticpro (&Qsearch_failed);
-  Qinvalid_regexp = intern_c_string ("invalid-regexp");
-  staticpro (&Qinvalid_regexp);
+  DEFSYM (Qsearch_failed, "search-failed");
+  DEFSYM (Qinvalid_regexp, "invalid-regexp");
 
   Fput (Qsearch_failed, Qerror_conditions,
        pure_cons (Qsearch_failed, pure_cons (Qerror, Qnil)));