Add tiny optimization for string-search

* src/fns.c (Fstring_search): Add tiny optimization for needles
that are longer than the haystack (bug#43598).
This commit is contained in:
Lars Ingebrigtsen 2020-09-27 00:35:11 +02:00
parent baf331e40c
commit 104688feb4
2 changed files with 10 additions and 1 deletions

View file

@ -5468,6 +5468,7 @@ Case is always significant and text properties are ignored. */)
{
ptrdiff_t start_byte = 0, haybytes;
char *res, *haystart;
EMACS_INT start = 0;
CHECK_STRING (needle);
CHECK_STRING (haystack);
@ -5475,12 +5476,17 @@ Case is always significant and text properties are ignored. */)
if (!NILP (start_pos))
{
CHECK_FIXNUM (start_pos);
EMACS_INT start = XFIXNUM (start_pos);
start = XFIXNUM (start_pos);
if (start < 0 || start > SCHARS (haystack))
xsignal1 (Qargs_out_of_range, start_pos);
start_byte = string_char_to_byte (haystack, start);
}
/* If NEEDLE is longer than (the remaining length of) haystack, then
we can't have a match, and return early. */
if (SCHARS (needle) > SCHARS (haystack) - start)
return Qnil;
haystart = SSDATA (haystack) + start_byte;
haybytes = SBYTES (haystack) - start_byte;

View file

@ -461,6 +461,9 @@ See https://debbugs.gnu.org/cgi/bugreport.cgi?bug=19350."
(should (equal (string-replace "azot" "bar" "foozotbar")
"foozotbar"))
(should (equal (string-replace "fo" "bar" "lafofofozot")
"labarbarbarzot"))
(should (equal (string-replace "\377" "x" "a\377b")
"axb"))
(should (equal (string-replace "\377" "x" "a\377ø")