Bug 7962 – std.regex: Captures.length() returns incorrect value
Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P2
Component
phobos
Product
D
Version
D2
Platform
x86_64
OS
Linux
Creation time
2012-04-21T13:14:00Z
Last change time
2012-04-23T00:18:37Z
Keywords
pull
Assigned to
nobody
Creator
markusle
Comments
Comment #0 by markusle — 2012-04-21T13:14:03Z
It seems that Captures.length() from std.regex returns bogus lengths for empty captures (I might be using it improperly). Here's a simple example, the second match object claims length 2 while being empty.
sh-4.2$ uname
Linux
sh-4.2$ dmd | grep "DMD"
DMD64 D Compiler v2.059
sh-4.2$ cat test.d
import std.regex;
import std.stdio;
int main() {
auto r = regex(r"(@\w+)", "g");
writeln("match first string -- looks ok");
string text1 = "@This is a test";
auto m1 = match(text1, r);
auto myCapts1 = m1.captures;
writeln("content: ", myCapts1);
writeln("is empty? ", myCapts1.empty());
writeln("length: ", myCapts1.length());
writeln("");
writeln("match second string -- length seems wrong");
string text2 = "(his) is a test";
auto m2 = match(text2, r);
auto myCapts2 = m2.captures;
writeln("content: ", myCapts2);
writeln("is empty? ", myCapts2.empty());
writeln("length : ", myCapts2.length());
return 0;
}
sh-4.2$ dmd test.d
sh-4.2$ ./test
match first string -- looks ok
content: ["@This", "@This"]
is empty? false
length: 2
match second string -- length seems wrong
content: []
is empty? true
length : 2
Comment #1 by dmitry.olsh — 2012-04-21T14:11:59Z
(In reply to comment #0)
> It seems that Captures.length() from std.regex returns bogus lengths for empty
> captures (I might be using it improperly). Here's a simple example, the second
> match object claims length 2 while being empty.
>
Thanks for the detailed info. The problem is that Captures.length is hardwired to the number of sub captures + 1 which is fine as long as there is a match.
For now you may use m.empty ? 0 : m.length as a workaround.
Pull:
https://github.com/D-Programming-Language/phobos/pull/548
P.S. Usage of e.g. length() is deprecated and fails with -property switch. Omit parens for @property members.
Comment #2 by markusle — 2012-04-21T20:50:17Z
(In reply to comment #1)
>
> Thanks for the detailed info. The problem is that Captures.length is hardwired
> to the number of sub captures + 1 which is fine as long as there is a match.
>
> For now you may use m.empty ? 0 : m.length as a workaround.
Thanks a lot for your quick response and I will use your suggested workaround.
> P.S. Usage of e.g. length() is deprecated and fails with -property switch. Omit
> parens for @property members.
Thanks for pointing this out and also the -property switch. Sorry, to be off-topic (with regard to this bug) but incidentally, it doesn't seem to do anything
sh-4.2$ fgrep 'length()' test.d
writeln("length: ", myCapts1.length());
writeln("length : ", myCapts2.length());
sh-4.2$ dmd -property test.d
sh-4.2$
Does it need to be combined with another switch to "enforce"?
Comment #3 by github-bugzilla — 2012-04-22T17:45:36Z
>Does it need to be combined with another switch to "enforce"?
I think it's -w switch that stands for warnings as errors. Or -wi to just enable them. Strange as I thought -property was independent entity.