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 mail3-relais-sop.national.inria.fr (mail3-relais-sop.national.inria.fr [192.134.164.104]) by sympa.inria.fr (Postfix) with ESMTPS id 863CD820A1 for ; Mon, 12 Aug 2013 14:58:58 +0200 (CEST) Received-SPF: None (mail3-smtp-sop.national.inria.fr: no sender authenticity information available from domain of info@gerd-stolpmann.de) identity=pra; client-ip=212.227.126.187; receiver=mail3-smtp-sop.national.inria.fr; envelope-from="info@gerd-stolpmann.de"; x-sender="info@gerd-stolpmann.de"; x-conformance=sidf_compatible Received-SPF: None (mail3-smtp-sop.national.inria.fr: no sender authenticity information available from domain of info@gerd-stolpmann.de) identity=mailfrom; client-ip=212.227.126.187; receiver=mail3-smtp-sop.national.inria.fr; envelope-from="info@gerd-stolpmann.de"; x-sender="info@gerd-stolpmann.de"; x-conformance=sidf_compatible Received-SPF: Pass (mail3-smtp-sop.national.inria.fr: domain of postmaster@moutng.kundenserver.de designates 212.227.126.187 as permitted sender) identity=helo; client-ip=212.227.126.187; receiver=mail3-smtp-sop.national.inria.fr; envelope-from="info@gerd-stolpmann.de"; x-sender="postmaster@moutng.kundenserver.de"; x-conformance=sidf_compatible; x-record-type="v=spf1" X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AuQBAPTaCFLU4367bWdsb2JhbABagzu/JIEaFg4LCwwGFgMlgiUBBScuJBALRlcGEwmICwi2Bol8hHmBICYHhBEDjmuKJYR8g32KSYFv X-IPAS-Result: AuQBAPTaCFLU4367bWdsb2JhbABagzu/JIEaFg4LCwwGFgMlgiUBBScuJBALRlcGEwmICwi2Bol8hHmBICYHhBEDjmuKJYR8g32KSYFv X-IronPort-AV: E=Sophos;i="4.89,862,1367964000"; d="asc'?scan'208";a="23772708" Received: from moutng.kundenserver.de ([212.227.126.187]) by mail3-smtp-sop.national.inria.fr with ESMTP/TLS/RC4-SHA; 12 Aug 2013 14:58:44 +0200 Received: from office1.lan.sumadev.de (dslb-084-059-075-041.pools.arcor-ip.net [84.59.75.41]) by mrelayeu.kundenserver.de (node=mreu3) with ESMTP (Nemesis) id 0MVFaD-1VcmTg3Y8T-00YTbY; Mon, 12 Aug 2013 14:58:22 +0200 Received: from [192.168.178.21] (546BF816.cm-12-4d.dynamic.ziggo.nl [84.107.248.22]) by office1.lan.sumadev.de (Postfix) with ESMTPSA id 549ABC00CF; Mon, 12 Aug 2013 14:58:21 +0200 (CEST) Message-ID: <1376312292.29133.3.camel@e130> From: Gerd Stolpmann To: Florent Monnier Cc: caml-list@inria.fr Date: Mon, 12 Aug 2013 14:58:12 +0200 In-Reply-To: References: Content-Type: multipart/signed; micalg="pgp-sha1"; protocol="application/pgp-signature"; boundary="=-h0jVbqgrB7p8ddR27Ta3" X-Mailer: Evolution 3.2.3-0ubuntu6 Mime-Version: 1.0 X-Provags-ID: V02:K0:NNZnHnZkb9Ga0nhrKZV3KRszZfLjKe35F5enfb1oxtn 9W8htMxJGMWyQ0WSi+JGeoQPs1XYPyud53GzybJzFb2u9UQ1xw xBKTeFTYzX+eYV/kearivg5dAQnc1o/2sKpWsGsTQT7+WBFgiv YBX2W3QeGJXZ5vJt9K79cU39Zx7v4MlAZ/zH7uIKqtfM24gGlo +vCUUXlDJItjTQhCfKNRESqIzje9VNex69w1LRtRRD6FE5OQhg xc5nqc58sLFIRH4BinJriQd9EbQb+O0VyzQCbOYrP1ADGl5fC/ mJoBCH3kSIkPaj28VA4PUPfY3SjUtFJZfrQW2CpcpeE+2MF+n2 Baey7dvR2PDvScWkbXds= Subject: Re: [Caml-list] String.(r)index_from --=-h0jVbqgrB7p8ddR27Ta3 Content-Type: text/plain; charset="ISO-8859-15" Content-Transfer-Encoding: quoted-printable 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. It's a bit like considering 0 as natural number. Gerd 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?view= =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" else > + 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 ------------------------------------------------------------ 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 ------------------------------------------------------------ --=-h0jVbqgrB7p8ddR27Ta3 Content-Type: application/pgp-signature; name="signature.asc" Content-Description: This is a digitally signed message part Content-Transfer-Encoding: 7bit -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.11 (GNU/Linux) iQEcBAABAgAGBQJSCNvkAAoJEAaM4b9ZLB5TNU0H/ic76vRR0lZiRsxgztaDg/cX 6u9Q5XWM8T/5xdh/SIv7312QUqdP8wTUnePSXCfxqBzz2HptrL3YBf2qmaMTHC88 Kd7fgi0OL+VEf5EeCo6uaXJWDZZLyVZJHVNkmZL7xcEJrRY7itLZfE2dy98KsqDg Y8OpGwMCoKA31I9x3hFFGr94IQ2Nc9z155LBp27FqXIfyvusb1HXRzGawX6Y+QeS Y4V4tV59TZXPpvuHckqxPALSFZG7BiNWI14uAdvEsZ61D6Cagp3R4M+VnGQwU2Hy RDNVtMWxcvG6+JkNp67MI2e39nB8K0WLATQ0M87frr9IFNJgEPNVV7Gqa2lSEto= =koem -----END PGP SIGNATURE----- --=-h0jVbqgrB7p8ddR27Ta3--