From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: 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 AE9BA820A1 for ; Mon, 12 Aug 2013 12:55:53 +0200 (CEST) Received-SPF: None (mail2-smtp-roc.national.inria.fr: no sender authenticity information available from domain of monnier.florent@gmail.com) identity=pra; client-ip=209.85.212.173; receiver=mail2-smtp-roc.national.inria.fr; envelope-from="monnier.florent@gmail.com"; x-sender="monnier.florent@gmail.com"; x-conformance=sidf_compatible Received-SPF: Pass (mail2-smtp-roc.national.inria.fr: domain of monnier.florent@gmail.com designates 209.85.212.173 as permitted sender) identity=mailfrom; client-ip=209.85.212.173; receiver=mail2-smtp-roc.national.inria.fr; envelope-from="monnier.florent@gmail.com"; x-sender="monnier.florent@gmail.com"; x-conformance=sidf_compatible; x-record-type="v=spf1" Received-SPF: None (mail2-smtp-roc.national.inria.fr: no sender authenticity information available from domain of postmaster@mail-wi0-f173.google.com) identity=helo; client-ip=209.85.212.173; receiver=mail2-smtp-roc.national.inria.fr; envelope-from="monnier.florent@gmail.com"; x-sender="postmaster@mail-wi0-f173.google.com"; x-conformance=sidf_compatible X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AqICACG+CFLRVdStk2dsb2JhbABagztQsAGPZQgWDgEBAQEHCwsJFAQkglIZARseAxIQXQERAQUBiDIBAw8MlWuDAIxRgwKEBAoZJw1kh3QBBQyOaYFjg3sDl2SBLIh5hUcWKYRDOoE1 X-IPAS-Result: AqICACG+CFLRVdStk2dsb2JhbABagztQsAGPZQgWDgEBAQEHCwsJFAQkglIZARseAxIQXQERAQUBiDIBAw8MlWuDAIxRgwKEBAoZJw1kh3QBBQyOaYFjg3sDl2SBLIh5hUcWKYRDOoE1 X-IronPort-AV: E=Sophos;i="4.89,861,1367964000"; d="scan'208";a="29126889" Received: from mail-wi0-f173.google.com ([209.85.212.173]) by mail2-smtp-roc.national.inria.fr with ESMTP/TLS/RC4-SHA; 12 Aug 2013 12:55:53 +0200 Received: by mail-wi0-f173.google.com with SMTP id en1so1525099wid.0 for ; Mon, 12 Aug 2013 03:55:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:date:message-id:subject:from:to:content-type; bh=nbkL3Ph35JOeM7UX8Qf39Jk0aV4YEc05pFDnz5QP1+k=; b=PnYmnF9sAyEsFa84+5oFJI9wz7MaJN3jtOoglW6bYq3uOR4L3+9sq5B56R1IDfPKJ/ FN2ddieTiVbYGZRteF8ZYD51rauoHHBpuvvbN1VOwhTUzIwHxoj3Yt9PUXwXN7eD9Hw+ Fx13GG5sFuWTHaNLY/hrugTG2PJHYsFWWdaITLGKNa85fA0wn2g7N9vT/Fo7xn8ica1V wRjtHdCd47XTi0Ck/V8pJuRWaI9G+VXlVeETKWtaUpx7zolSJ0wIvxXjvrxZxv7SDCGj E3+QUNPfLaFYuex6VVDRMYIwqmLWnotiMQ9Osfieft7zKu0NIs/55yCd/H1Lcs559+HT TSfg== MIME-Version: 1.0 X-Received: by 10.194.248.230 with SMTP id yp6mr6435115wjc.78.1376304953080; Mon, 12 Aug 2013 03:55:53 -0700 (PDT) Received: by 10.194.16.5 with HTTP; Mon, 12 Aug 2013 03:55:52 -0700 (PDT) Date: Mon, 12 Aug 2013 12:55:52 +0200 Message-ID: From: Florent Monnier To: caml-list@inria.fr Content-Type: text/plain; charset=ISO-8859-1 X-Validation-by: monnier.florent@gmail.com Subject: [Caml-list] String.(r)index_from Hi, Is this the expected behavior? # let s = "012" ;; val s : string = "012" # String.index_from s 1 '1' ;; - : int = 1 # String.index_from s 2 '2' ;; - : int = 2 # String.index_from s 3 '3' ;; Exception: Not_found. # String.index_from s 4 '4' ;; Exception: Invalid_argument "String.index_from". (* ====================== *) # String.rindex_from s 1 '1' ;; - : int = 1 # String.rindex_from s 0 '0' ;; - : int = 0 # String.rindex_from s (-1) '#' ;; Exception: Not_found. # String.rindex_from s (-2) '#' ;; Exception: Invalid_argument "String.rindex_from". (* ====================== *) # String.contains_from s 2 '2' ;; - : bool = true # String.contains_from s 3 '2' ;; - : bool = false # String.contains_from s 4 '2' ;; Exception: Invalid_argument "String.contains_from". (* ====================== *) If yes, please just ignore this email. 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=markup&pathrev=13748 ### 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 @@ let index_from s i c = let l = length s in - if i < 0 || i > l then invalid_arg "String.index_from" else + if i < 0 || i >= l then invalid_arg "String.index_from" else index_rec s l i c;; let rec rindex_rec s i c = @@ -186,12 +186,12 @@ let rindex s c = rindex_rec s (length s - 1) c;; let rindex_from s i c = - if i < -1 || i >= length s then invalid_arg "String.rindex_from" else + if i < 0 || i >= length s then invalid_arg "String.rindex_from" else rindex_rec s i c;; let contains_from s i c = let l = length s in - if i < 0 || i > l then invalid_arg "String.contains_from" else + if i < 0 || i >= l then invalid_arg "String.contains_from" else try ignore (index_rec s l i c); true with Not_found -> false;; let contains s c = contains_from s 0 c;; (* ====================== *) The behavior of these functions is then to raise an Invalid_argument exception if the index is out of bounds: # let s = "012" ;; val s : string = "012" # String.index_from s 3 '3' ;; Exception: Invalid_argument "String.index_from". # String.rindex_from s (-1) '#' ;; Exception: Invalid_argument "String.rindex_from". # String.contains_from s 3 '2' ;; Exception: Invalid_argument "String.contains_from". (* ====================== *) -- Best regards florent