Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

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: Alain Frisch <frisch@c...>
Subject: Re: [Caml-list] Str.string_match raising Invalid_argument "String.sub" in gc
On 23 Aug 2001, Neale Pickett wrote:

> I contest that this is obvious.  s is a different string each time f is
> called, and so even though I do call Str.string_match multiple times,
> it's with a different s.  The manual for the Str libary says only that I
> must pass in the same s as was given to string_match, which implies that
> s is somehow keyed to its matches.  It sounds as though I shouldn't do
> the following:
>   Str.string_match sep s 0;
>   Str.string_match sep s' 0;
>   print_string (Str.matched_group 1 s);
> If this is the case, why does Str.matched_group even bother requiring
> the original string?

Indeed, you shouldn't.

The manual says:

val matched_string: string -> string

matched_string s returns the substring of s that was matched by the latest
string_match, search_forward or search_backward. The user must make sure
that the parameter s is the same string that was passed to the matching or
searching function.

Note the "latest".

The approach you suggest (that the library keeps a reference to
the last matched string) is acceptable. I guess it was not
implemented like that because this would prevent garbage collection
of the last matched string.  (maybe a "release_internal_buffer" function
would have been better)


Bug reports:  FAQ:
To unsubscribe, mail  Archives: