aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorLars Ingebrigtsen2020-09-25 01:52:10 +0200
committerLars Ingebrigtsen2020-09-25 01:53:16 +0200
commite51a98b0c2d35648c2d054486f7ba5869e24e4cf (patch)
treed4c261379c595b034663aa5b3c229e3bd64d99b3 /src
parente7a69c9204a2b208401b9368a70acad21022c7a3 (diff)
downloademacs-e51a98b0c2d35648c2d054486f7ba5869e24e4cf.tar.gz
emacs-e51a98b0c2d35648c2d054486f7ba5869e24e4cf.zip
Add a new function 'string-search'
* doc/lispref/strings.texi (Text Comparison): Document it. * src/fns.c (Fstring_search): New function.
Diffstat (limited to 'src')
-rw-r--r--src/fns.c46
1 files changed, 46 insertions, 0 deletions
diff --git a/src/fns.c b/src/fns.c
index a3b8d6ef57d..3927e4306e6 100644
--- a/src/fns.c
+++ b/src/fns.c
@@ -5454,6 +5454,51 @@ It should not be used for anything security-related. See
5454 return make_digest_string (digest, SHA1_DIGEST_SIZE); 5454 return make_digest_string (digest, SHA1_DIGEST_SIZE);
5455} 5455}
5456 5456
5457DEFUN ("string-search", Fstring_search, Sstring_search, 2, 3, 0,
5458 doc: /* Search for the string NEEDLE in the string HAYSTACK.
5459The return value is the position of the first instance of NEEDLE in
5460HAYSTACK.
5461
5462The optional START-POS argument says where to start searching in
5463HAYSTACK. If not given, start at the beginning. */)
5464 (register Lisp_Object needle, Lisp_Object haystack, Lisp_Object start_pos)
5465{
5466 ptrdiff_t start_byte = 0, haybytes;
5467 char *res = NULL, *haystart;
5468
5469 CHECK_STRING (needle);
5470 CHECK_STRING (haystack);
5471
5472 if (!NILP (start_pos))
5473 {
5474 CHECK_FIXNUM (start_pos);
5475 start_byte = string_char_to_byte (haystack, XFIXNUM (start_pos));
5476 }
5477
5478 haystart = SSDATA (haystack) + start_byte;
5479 haybytes = SBYTES (haystack) - start_byte;
5480
5481 if (STRING_MULTIBYTE (haystack) == STRING_MULTIBYTE (needle))
5482 res = memmem (haystart, haybytes,
5483 SSDATA (needle), SBYTES (needle));
5484 else if (STRING_MULTIBYTE (haystack) && !STRING_MULTIBYTE (needle))
5485 {
5486 Lisp_Object multi_needle = string_to_multibyte (needle);
5487 res = memmem (haystart, haybytes,
5488 SSDATA (multi_needle), SBYTES (multi_needle));
5489 }
5490 else if (!STRING_MULTIBYTE (haystack) && STRING_MULTIBYTE (needle))
5491 {
5492 Lisp_Object uni_needle = Fstring_as_unibyte (needle);
5493 res = memmem (haystart, haybytes,
5494 SSDATA (uni_needle), SBYTES (uni_needle));
5495 }
5496
5497 if (! res)
5498 return Qnil;
5499
5500 return make_int (string_byte_to_char (haystack, res - SSDATA (haystack)));
5501}
5457 5502
5458 5503
5459void 5504void
@@ -5494,6 +5539,7 @@ syms_of_fns (void)
5494 defsubr (&Sremhash); 5539 defsubr (&Sremhash);
5495 defsubr (&Smaphash); 5540 defsubr (&Smaphash);
5496 defsubr (&Sdefine_hash_table_test); 5541 defsubr (&Sdefine_hash_table_test);
5542 defsubr (&Sstring_search);
5497 5543
5498 /* Crypto and hashing stuff. */ 5544 /* Crypto and hashing stuff. */
5499 DEFSYM (Qiv_auto, "iv-auto"); 5545 DEFSYM (Qiv_auto, "iv-auto");