[Mono-list] Bug in Regex

Dan Lewis dihlewis@yahoo.co.uk
Sun, 12 May 2002 13:31:06 +0100 (BST)


 --- Jonathan Stowe <gellyfish@gellyfish.com> wrote:
> On Sun, 12 May 2002, Dan Lewis wrote:
> > Fixed in CVS.
> >
> 
> I'm not so sure that it is :

Ok, I'm an idiot. It probably helps everyone if I actually type "cvs commit" at
some point.

> Forgive me if am barking up the wrong tree but isnt the
> 
>                while (count -- > 0 && m.Success) {
>                           splits.Add (input.Substring (ptr, m.Index - ptr));
>                           ptr = m.Index + m.Length;
>                }
> 
> in regex.cs always going to result in ptr being larger than m.Index (and
> thus an out of range exception) the second time around the loop ?  Again I
> might be totally wrong but shouldn't there be a Match() within the loop ?

No you're not barking up the wrong tree at all :-) That's was exactly the
problem.

I still haven't got round for writing unit tests for regex's auxilliary
functions (replace, split etc), so there may well plenty of other nasties in
there.

> Although seeing what Stefan is trying to do wouldn't something like :
> 
> using System;
> 
> class Foo
> {
>     public static void Main()
>     {
>        String myString = "This=foo;is=a&a=b;test=1";
> 
>        Char[] gh = {';','&'};
> 
> 
>        String[] substrings = myString.Split(gh);
> 
>        foreach (String foo in substrings)
>        {
>           System.Console.WriteLine(foo);
>        }
>     }
> 
> }
> 
> Cut out the middleman (System.Text.Regex in this case :)

Yes it would. Much as I appreciate people using the code, compiling a regex to
find an "&" is overkill in this case.

Thanks,
  Dan.


__________________________________________________
Do You Yahoo!?
Everything you'll ever need on one web page
from News and Sport to Email and Music Charts
http://uk.my.yahoo.com