From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Original-To: caml-list@sympa.inria.fr Delivered-To: caml-list@sympa.inria.fr Received: from mail2-relais-roc.national.inria.fr (mail2-relais-roc.national.inria.fr [192.134.164.83]) by sympa.inria.fr (Postfix) with ESMTPS id 19318820A1 for ; Mon, 19 Aug 2013 20:19:14 +0200 (CEST) X-IronPort-AV: E=Sophos;i="4.89,914,1367964000"; d="scan'208";a="29773438" Received: from cac94-1-81-57-150-173.fbx.proxad.net (HELO [192.168.0.22]) ([81.57.150.173]) by mail2-relais-roc.national.inria.fr with ESMTP/TLS/AES128-SHA; 19 Aug 2013 20:19:13 +0200 Content-Type: text/plain; charset=us-ascii Mime-Version: 1.0 (Apple Message framework v1283) From: Damien Doligez In-Reply-To: <1376312292.29133.3.camel@e130> Date: Mon, 19 Aug 2013 20:19:12 +0200 Content-Transfer-Encoding: quoted-printable Message-Id: <290C776A-0A89-420E-B729-194CEDA07031@inria.fr> References: <1376312292.29133.3.camel@e130> To: caml users X-Mailer: Apple Mail (2.1283) Subject: Re: [Caml-list] String.(r)index_from Hi, Indeed, it was carefully designed this way. When you're doing index_from s 3 '3', you're looking for the character 3 in the empty string. -- Damien On 2013-08-12, at 14:58, Gerd Stolpmann wrote: > IMHO, this is the right behavior. When walking over strings, it is often > practical to consider the position after the last character as legal > position. So far I see, this is consistent in the String module, e.g. > you can also do String.sub "abc" 3 0. >=20 > It's a bit like considering 0 as natural number. >=20 > Gerd >=20 > Am Montag, den 12.08.2013, 12:55 +0200 schrieb Florent Monnier: >> Hi, >>=20 >> Is this the expected behavior? >>=20 >> # let s =3D "012" ;; >> val s : string =3D "012" >>=20 >> # String.index_from s 1 '1' ;; >> - : int =3D 1 >> # String.index_from s 2 '2' ;; >> - : int =3D 2 >> # String.index_from s 3 '3' ;; >> Exception: Not_found. >> # String.index_from s 4 '4' ;; >> Exception: Invalid_argument "String.index_from". >>=20 >> (* =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D *) >>=20 >> # String.rindex_from s 1 '1' ;; >> - : int =3D 1 >> # String.rindex_from s 0 '0' ;; >> - : int =3D 0 >> # String.rindex_from s (-1) '#' ;; >> Exception: Not_found. >> # String.rindex_from s (-2) '#' ;; >> Exception: Invalid_argument "String.rindex_from". >>=20 >> (* =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D *) >>=20 >> # String.contains_from s 2 '2' ;; >> - : bool =3D true >> # String.contains_from s 3 '2' ;; >> - : bool =3D false >> # String.contains_from s 4 '2' ;; >> Exception: Invalid_argument "String.contains_from". >>=20 >> (* =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D *) >>=20 >> If yes, please just ignore this email. >>=20 >> If no, here is a patch for the file "string.ml": >> http://caml.inria.fr/cgi-bin/viewvc.cgi/ocaml/trunk/stdlib/string.ml?vie= w=3Dmarkup&pathrev=3D13748 >>=20 >> ### return exception Invalid_argument instead of false or Not_found >> ### when the index is out of bounds. >> --- stdlib/string.ml.orig 2013-08-12 12:42:17.443013642 +0200 >> +++ stdlib/string.ml 2013-08-12 12:43:24.973014347 +0200 >> @@ -176,7 +176,7 @@ >>=20 >> let index_from s i c =3D >> let l =3D length s in >> - if i < 0 || i > l then invalid_arg "String.index_from" else >> + if i < 0 || i >=3D l then invalid_arg "String.index_from" else >> index_rec s l i c;; >>=20 >> let rec rindex_rec s i c =3D >> @@ -186,12 +186,12 @@ >> let rindex s c =3D rindex_rec s (length s - 1) c;; >>=20 >> let rindex_from s i c =3D >> - if i < -1 || i >=3D length s then invalid_arg "String.rindex_from" el= se >> + if i < 0 || i >=3D length s then invalid_arg "String.rindex_from" else >> rindex_rec s i c;; >>=20 >> let contains_from s i c =3D >> let l =3D length s in >> - if i < 0 || i > l then invalid_arg "String.contains_from" else >> + if i < 0 || i >=3D l then invalid_arg "String.contains_from" else >> try ignore (index_rec s l i c); true with Not_found -> false;; >>=20 >> let contains s c =3D contains_from s 0 c;; >> (* =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D *) >>=20 >>=20 >> The behavior of these functions is then to raise >> an Invalid_argument exception if the index is out of bounds: >>=20 >> # let s =3D "012" ;; >> val s : string =3D "012" >>=20 >> # String.index_from s 3 '3' ;; >> Exception: Invalid_argument "String.index_from". >>=20 >> # String.rindex_from s (-1) '#' ;; >> Exception: Invalid_argument "String.rindex_from". >>=20 >> # String.contains_from s 3 '2' ;; >> Exception: Invalid_argument "String.contains_from". >>=20 >> (* =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D *) >>=20 >> --=20 >> Best regards >> florent >>=20 >=20 > --=20 > ------------------------------------------------------------ > Gerd Stolpmann, Darmstadt, Germany gerd@gerd-stolpmann.de > My OCaml site: http://www.camlcity.org > Contact details: http://www.camlcity.org/contact.html > Company homepage: http://www.gerd-stolpmann.de > ------------------------------------------------------------ >=20