aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/fns.c59
1 files changed, 27 insertions, 32 deletions
diff --git a/src/fns.c b/src/fns.c
index 0f768711544..f626fe11b20 100644
--- a/src/fns.c
+++ b/src/fns.c
@@ -5457,16 +5457,11 @@ It should not be used for anything security-related. See
5457static bool 5457static bool
5458string_ascii_p (Lisp_Object string) 5458string_ascii_p (Lisp_Object string)
5459{ 5459{
5460 if (STRING_MULTIBYTE (string)) 5460 ptrdiff_t nbytes = SBYTES (string);
5461 return SBYTES (string) == SCHARS (string); 5461 for (ptrdiff_t i = 0; i < nbytes; i++)
5462 else 5462 if (SREF (string, i) > 127)
5463 { 5463 return false;
5464 ptrdiff_t nbytes = SBYTES (string); 5464 return true;
5465 for (ptrdiff_t i = 0; i < nbytes; i++)
5466 if (SREF (string, i) > 127)
5467 return false;
5468 return true;
5469 }
5470} 5465}
5471 5466
5472DEFUN ("string-search", Fstring_search, Sstring_search, 2, 3, 0, 5467DEFUN ("string-search", Fstring_search, Sstring_search, 2, 3, 0,
@@ -5505,9 +5500,14 @@ Case is always significant and text properties are ignored. */)
5505 haystart = SSDATA (haystack) + start_byte; 5500 haystart = SSDATA (haystack) + start_byte;
5506 haybytes = SBYTES (haystack) - start_byte; 5501 haybytes = SBYTES (haystack) - start_byte;
5507 5502
5508 if (STRING_MULTIBYTE (haystack) == STRING_MULTIBYTE (needle) 5503 /* We can do a direct byte-string search if both strings have the
5509 || string_ascii_p (needle) 5504 same multibyteness, or if at least one of them consists of ASCII
5510 || string_ascii_p (haystack)) 5505 characters only. */
5506 if (STRING_MULTIBYTE (haystack)
5507 ? (STRING_MULTIBYTE (needle)
5508 || SCHARS (haystack) == SBYTES (haystack) || string_ascii_p (needle))
5509 : (!STRING_MULTIBYTE (needle)
5510 || SCHARS (needle) == SBYTES (needle) || string_ascii_p (haystack)))
5511 res = memmem (haystart, haybytes, 5511 res = memmem (haystart, haybytes,
5512 SSDATA (needle), SBYTES (needle)); 5512 SSDATA (needle), SBYTES (needle));
5513 else if (STRING_MULTIBYTE (haystack)) /* unibyte needle */ 5513 else if (STRING_MULTIBYTE (haystack)) /* unibyte needle */
@@ -5521,26 +5521,21 @@ Case is always significant and text properties are ignored. */)
5521 /* The only possible way we can find the multibyte needle in the 5521 /* The only possible way we can find the multibyte needle in the
5522 unibyte stack (since we know that neither are pure-ASCII) is 5522 unibyte stack (since we know that neither are pure-ASCII) is
5523 if they contain "raw bytes" (and no other non-ASCII chars.) */ 5523 if they contain "raw bytes" (and no other non-ASCII chars.) */
5524 ptrdiff_t chars = SCHARS (needle); 5524 ptrdiff_t nbytes = SBYTES (needle);
5525 const unsigned char *src = SDATA (needle); 5525 for (ptrdiff_t i = 0; i < nbytes; i++)
5526 5526 {
5527 for (ptrdiff_t i = 0; i < chars; i++) 5527 int c = SREF (needle, i);
5528 { 5528 if (CHAR_BYTE8_HEAD_P (c))
5529 int c = string_char_advance (&src); 5529 i++; /* Skip raw byte. */
5530 5530 else if (!ASCII_CHAR_P (c))
5531 if (!CHAR_BYTE8_P (c) 5531 return Qnil; /* Found a char that can't be in the haystack. */
5532 && !ASCII_CHAR_P (c)) 5532 }
5533 /* Found a char that can't be in the haystack. */
5534 return Qnil;
5535 }
5536 5533
5537 { 5534 /* "Raw bytes" (aka eighth-bit) are represented differently in
5538 /* "Raw bytes" (aka eighth-bit) are represented differently in 5535 multibyte and unibyte strings. */
5539 multibyte and unibyte strings. */ 5536 Lisp_Object uni_needle = Fstring_to_unibyte (needle);
5540 Lisp_Object uni_needle = Fstring_to_unibyte (needle); 5537 res = memmem (haystart, haybytes,
5541 res = memmem (haystart, haybytes, 5538 SSDATA (uni_needle), SBYTES (uni_needle));
5542 SSDATA (uni_needle), SBYTES (uni_needle));
5543 }
5544 } 5539 }
5545 5540
5546 if (! res) 5541 if (! res)