Bug 4367 – std.regex: Captures is not a random access range

Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P2
Component
phobos
Product
D
Version
D2
Platform
All
OS
Linux
Creation time
2010-06-22T06:12:00Z
Last change time
2011-06-06T08:10:36Z
Keywords
patch
Assigned to
andrei
Creator
graham.fawcett

Attachments

IDFilenameSummaryContent-TypeSize
6734367.patchproposed patchtext/plain709

Comments

Comment #0 by graham.fawcett — 2010-06-22T06:12:47Z
From the std.regex documentation: "Captures captures(). Retrieve the captured parenthesized matches, in the form of a random-access range." The Captures struct is not a random access range, because it is not a forward range. The following test program fails to compile: import std.regex; import std.range; void main() { auto c = match("hello", "[aeiou]").captures; alias typeof(c) C; static assert (isInputRange!C); // pass static assert (isForwardRange!C); // failure static assert (isBidirectionalRange!C); static assert (isRandomAccessRange!C); }
Comment #1 by graham.fawcett — 2010-06-22T08:28:27Z
Created attachment 673 proposed patch This adds .save, .back and .popBack, making Captures a RandomAccessRange. Note: in my .save implementation, I think it's correct to use the same input, but to use a saved copy of matches.
Comment #2 by graham.fawcett — 2010-06-22T08:50:01Z
Hm, did my patch implement 'back' correctly? It's not clear to me whether it should be this: return input[matches[$-1].startIdx .. matches[$-1].endIdx]; (as in my patch), or this: size_t end = length - 1; return input[matches[end].startIdx .. matches[end].endIdx]; The question arises from the definition of length(): @property size_t length() { foreach (i; 0 .. matches.length) { if (matches[i].startIdx >= input.length) return i; } return matches.length; } So should back() use length() as a limit, or should it return the last element of matches?
Comment #3 by dmitry.olsh — 2011-06-06T08:10:36Z