caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Florent Monnier <monnier.florent@gmail.com>
To: caml-list@inria.fr
Subject: [Caml-list] String.(r)index_from
Date: Mon, 12 Aug 2013 12:55:52 +0200	[thread overview]
Message-ID: <CAE1DttA0x4N+DJBtB8fHOrrKC7qOhhMcLS45Bh1G+08iJrvCLQ@mail.gmail.com> (raw)

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

             reply	other threads:[~2013-08-12 10:55 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-08-12 10:55 Florent Monnier [this message]
2013-08-12 12:58 ` Gerd Stolpmann
2013-08-19 18:19   ` Damien Doligez
2013-08-20 15:23     ` Florent Monnier
2013-08-20 15:53   ` Florent Monnier
2013-08-21  0:42     ` Francois Berenger

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=CAE1DttA0x4N+DJBtB8fHOrrKC7qOhhMcLS45Bh1G+08iJrvCLQ@mail.gmail.com \
    --to=monnier.florent@gmail.com \
    --cc=caml-list@inria.fr \
    /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).