Empty subexpressions captures in std.regex

PC petevik38 at yahoo.com.au
Sun Jul 11 04:29:16 PDT 2010


Hi, I've been lurking in this group for a few months, have read
through TDPL (which is great Andrei) and have started using D for
some
small programs. So far it's been a joy to use (you may have a C++
convert on your hands) and with the convenience of rdmd, I've been
using it where I'd normally use a scripting language.

It's been pretty good for this especially as Phobos has had almost
everything I've wanted to do covered. I have run into some issues
with
std.regex matching empty subexpressions though (dmd 2.047, win32):

    auto r1 = regex( "(a*)b" );
    auto m = match( "b", r1 );
    writefln( "captures = %s, empty = %s", m.captures.length,
m.empty );

=> captures = 0, empty = true

If I disable the call to optimize, it gives the expected results:

=> captures = 2, empty = false

Also, with optimize disabled:

    auto r = regex("([^,]*),([^,]*),([^,]*)");
    m = match( ",,", r );
    writefln( "captures = %s, empty = %s", m.captures.length,
m.empty );

=> captures = 3, empty = false

I noticed in Captures:

        @property size_t length()
        {
            foreach (i; 0 .. matches.length)
            {
                if (matches[i].startIdx >= input.length) return i;
            }
            return matches.length;
        }

In this case matches[3].startIdx = 2 and matches[3].endIdx=2. Should
this line be:

     if (matches[i].startIdx > input.length) return i;


Anyway kudos to everyone involved with D, I'm certainly going to be
using it a lot in the future.


More information about the Digitalmars-d mailing list