Version française
Home     About     Download     Resources     Contact us    
Browse thread
[Caml-list] Str.string_match raising Invalid_argument "String.sub" in gc
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: neale-caml@w...
Subject: [Caml-list] Str.string_match raising Invalid_argument "String.sub" in gc
Hi, I'm converting over some examples from the Perl cookbook to learn
OCaml and help with the PLEAC project <http://pleac.sourceforge.net/>,
and I'm running into some strange behavior with the Str structure; I
think it's something to do with garbage collection.

  # let rec f l =
      let sep = Str.regexp "^[ \t\n]*\\(.+\\)" in
      match l with
      | [] -> []
      | [""] -> []
      | s :: rest -> if (Str.string_match sep s 0) then
          let foo = print_string ("match " ^ Str.matched_group 1 s ^ "\n") in
          (Str.matched_group 1 s) :: (f rest)
      else
          let foo = print_string "nomatch\n" in
          (f rest);;
  val f : string list -> string list = <fun>
  # f ["  arf"];;
  match arf
  - : string list = ["arf"]
  # f ["  arf";  "barf"];;
  match arf
  match barf
  - : string list = ["  ar"; "barf"]
  # f [" arf"; " barf"];;
  match arf
  match barf
  Uncaught exception: Invalid_argument "String.sub".

First question: why is
  f ["  arf"] = ["arf"]
while
  f ["  arf"; "barf"] = ["  ar"; "barf"]
?  I don't think I've entered any code which should modify the string
after it's been matched.

Second question: What is causing the Invalid_argument exception?

Thanks in advance for any assistance,

Neale
-------------------
Bug reports: http://caml.inria.fr/bin/caml-bugs  FAQ: http://caml.inria.fr/FAQ/
To unsubscribe, mail caml-list-request@inria.fr  Archives: http://caml.inria.fr