From mboxrd@z Thu Jan 1 00:00:00 1970 Received: (from weis@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id RAA10082 for caml-red; Mon, 11 Dec 2000 17:59:55 +0100 (MET) Received: from nez-perce.inria.fr (nez-perce.inria.fr [192.93.2.78]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id OAA21051 for ; Fri, 8 Dec 2000 14:14:14 +0100 (MET) Received: from moutvdom01.kundenserver.de (moutvdom01.kundenserver.de [195.20.224.200]) by nez-perce.inria.fr (8.11.1/8.10.0) with ESMTP id eB8DEE522877 for ; Fri, 8 Dec 2000 14:14:14 +0100 (MET) Received: from [195.20.224.219] (helo=mrvdom03.kundenserver.de) by moutvdom01.kundenserver.de with esmtp (Exim 2.12 #2) id 144NLg-0000Zp-00; Fri, 8 Dec 2000 14:14:04 +0100 Received: from drms-3e364b69.pool.mediaways.net ([62.54.75.105] helo=ice.gerd-stolpmann.de) by mrvdom03.kundenserver.de with esmtp (Exim 2.12 #2) id 144NLb-0005bF-00; Fri, 8 Dec 2000 14:13:59 +0100 Received: from localhost (localhost [[UNIX: localhost]]) by ice.gerd-stolpmann.de (8.9.3/8.9.3) id OAA17932; Fri, 8 Dec 2000 14:13:56 +0100 From: Gerd Stolpmann Reply-To: gerd@gerd-stolpmann.de Organization: privat To: eijiro_sumii@anet.ne.jp, caml-list@inria.fr Subject: Re: substring match like "strstr" Date: Fri, 8 Dec 2000 13:57:49 +0100 X-Mailer: KMail [version 1.0.28] Content-Type: text/plain Cc: sumii@venus.is.s.u-tokyo.ac.jp References: <20001208150443I.sumii@yl.is.s.u-tokyo.ac.jp> In-Reply-To: <20001208150443I.sumii@yl.is.s.u-tokyo.ac.jp> MIME-Version: 1.0 Message-Id: <00120814135508.00625@ice> Content-Transfer-Encoding: 8bit Sender: weis@pauillac.inria.fr 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 ----------------------------------------------------------------------------