From: Gerd Stolpmann <gerd@gerd-stolpmann.de>
To: eijiro_sumii@anet.ne.jp, caml-list@inria.fr
Cc: sumii@venus.is.s.u-tokyo.ac.jp
Subject: Re: substring match like "strstr"
Date: Fri, 8 Dec 2000 13:57:49 +0100 [thread overview]
Message-ID: <00120814135508.00625@ice> (raw)
In-Reply-To: <20001208150443I.sumii@yl.is.s.u-tokyo.ac.jp>
On Fri, 08 Dec 2000, eijiro_sumii@anet.ne.jp wrote:
>Hi all,
>
>Is there a substring matching function (like "strstr" in libc) in the
>standard OCaml library? Of course there are many ways to implement it
>(by writing it from scratch, using the Str library, interfacing
>"strstr" in libc, etc.), but they are overkill for my purpose. So I'm
>wondering whether such a function already exists, but I couldn't find
>it in the manual...
There isn't such a function.
You can write it yourself:
let find_substring s1 s2 =
(* find s2 in s1, or raise Not_found *)
if String.length s2 > String.length s1 then raise Not_found;
let b = String.create (String.length s2) in
let rec search k =
if k > String.length s1 - String.length s2 then raise Not_found;
String.blit ~src:s1 ~src_pos:k ~dst:b ~dst_pos:0 ~len:(String.length s2);
if b = s2 then
k
else search (k+1)
in
search 0
;;
This version of find_substring avoids superflous heap allocations, and I think
it is tricky anough to be included into the stdlib. (However, if we had strstr
as primitive, even the allocation of b could be avoided.)
If speed is important, I recommend
let find_substring s1 s2 =
Str.search_forward (Str.regexp (Str.quote s2)) s1 0
;;
I hope these solutions aren't overkilling.
Gerd
--
----------------------------------------------------------------------------
Gerd Stolpmann Telefon: +49 6151 997705 (privat)
Viktoriastr. 100
64293 Darmstadt EMail: gerd@gerd-stolpmann.de
Germany
----------------------------------------------------------------------------
next prev parent reply other threads:[~2000-12-11 16:59 UTC|newest]
Thread overview: 23+ messages / expand[flat|nested] mbox.gz Atom feed top
2000-12-08 6:04 eijiro_sumii
2000-12-08 12:57 ` Gerd Stolpmann [this message]
2000-12-10 13:16 ` eijiro_sumii
2000-12-10 15:39 ` Gerd Stolpmann
2000-12-11 3:57 ` eijiro_sumii
2000-12-12 13:58 ` Julian Assange
2000-12-11 21:07 ` Chris Hecker
2000-12-11 22:22 ` Gerd Stolpmann
2000-12-12 5:06 ` Chris Hecker
2000-12-12 12:28 ` Jean-Christophe Filliatre
2000-12-13 10:02 ` eijiro_sumii
2000-12-13 10:17 ` Eijiro Sumii
2000-12-13 10:53 ` Julian Assange
2000-12-13 13:28 ` Eijiro Sumii
2000-12-12 3:28 ` eijiro_sumii
2000-12-13 1:12 ` John Prevost
2000-12-13 2:35 ` Chris Hecker
2000-12-12 10:07 ` Sven LUTHER
2000-12-14 3:36 ` eijiro_sumii
2000-12-14 6:48 ` Chris Hecker
2000-12-14 8:02 ` eijiro_sumii
2000-12-14 21:53 ` Stephan Tolksdorf
2000-12-14 21:12 Ruchira Datta
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=00120814135508.00625@ice \
--to=gerd@gerd-stolpmann.de \
--cc=caml-list@inria.fr \
--cc=eijiro_sumii@anet.ne.jp \
--cc=sumii@venus.is.s.u-tokyo.ac.jp \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).