diff options
author | Lars Ingebrigtsen <larsi@gnus.org> | 2020-09-25 01:52:10 +0200 |
---|---|---|
committer | Lars Ingebrigtsen <larsi@gnus.org> | 2020-09-25 01:53:16 +0200 |
commit | e51a98b0c2d35648c2d054486f7ba5869e24e4cf (patch) | |
tree | d4c261379c595b034663aa5b3c229e3bd64d99b3 /src | |
parent | e7a69c9204a2b208401b9368a70acad21022c7a3 (diff) | |
download | emacs-e51a98b0c2d35648c2d054486f7ba5869e24e4cf.tar.gz emacs-e51a98b0c2d35648c2d054486f7ba5869e24e4cf.tar.bz2 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.c | 46 |
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 return make_digest_string (digest, SHA1_DIGEST_SIZE); } +DEFUN ("string-search", Fstring_search, Sstring_search, 2, 3, 0, + doc: /* Search for the string NEEDLE in the string HAYSTACK. +The return value is the position of the first instance of NEEDLE in +HAYSTACK. + +The optional START-POS argument says where to start searching in +HAYSTACK. If not given, start at the beginning. */) + (register Lisp_Object needle, Lisp_Object haystack, Lisp_Object start_pos) +{ + ptrdiff_t start_byte = 0, haybytes; + char *res = NULL, *haystart; + + CHECK_STRING (needle); + CHECK_STRING (haystack); + + if (!NILP (start_pos)) + { + CHECK_FIXNUM (start_pos); + start_byte = string_char_to_byte (haystack, XFIXNUM (start_pos)); + } + + haystart = SSDATA (haystack) + start_byte; + haybytes = SBYTES (haystack) - start_byte; + + if (STRING_MULTIBYTE (haystack) == STRING_MULTIBYTE (needle)) + res = memmem (haystart, haybytes, + SSDATA (needle), SBYTES (needle)); + else if (STRING_MULTIBYTE (haystack) && !STRING_MULTIBYTE (needle)) + { + Lisp_Object multi_needle = string_to_multibyte (needle); + res = memmem (haystart, haybytes, + SSDATA (multi_needle), SBYTES (multi_needle)); + } + else if (!STRING_MULTIBYTE (haystack) && STRING_MULTIBYTE (needle)) + { + Lisp_Object uni_needle = Fstring_as_unibyte (needle); + res = memmem (haystart, haybytes, + SSDATA (uni_needle), SBYTES (uni_needle)); + } + + if (! res) + return Qnil; + + return make_int (string_byte_to_char (haystack, res - SSDATA (haystack))); +} void @@ -5494,6 +5539,7 @@ syms_of_fns (void) defsubr (&Sremhash); defsubr (&Smaphash); defsubr (&Sdefine_hash_table_test); + defsubr (&Sstring_search); /* Crypto and hashing stuff. */ DEFSYM (Qiv_auto, "iv-auto"); |