D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 7962 - std.regex: Captures.length() returns incorrect value
Summary: std.regex: Captures.length() returns incorrect value
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: phobos (show other issues)
Version: D2
Hardware: x86_64 Linux
: P2 normal
Assignee: No Owner
URL:
Keywords: pull
Depends on:
Blocks:
 
Reported: 2012-04-21 13:14 UTC by Markus Dittrich
Modified: 2012-04-23 00:18 UTC (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Markus Dittrich 2012-04-21 13:14:03 UTC
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 Dmitry Olshansky 2012-04-21 14:11:59 UTC
(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 Markus Dittrich 2012-04-21 20:50:17 UTC
(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 github-bugzilla 2012-04-22 17:45:36 UTC
Commits pushed to master at https://github.com/D-Programming-Language/phobos

https://github.com/D-Programming-Language/phobos/commit/6c9414c8c9f1df9f2c42229cf1b7eb8bd90d076b
fix Issue 7962  Captures.length() returns incorrect value

https://github.com/D-Programming-Language/phobos/commit/70ad8e4aaeda91d8c1db19b07ac3e3c3db2d23a6
Merge pull request #548 from blackwhale/fix-captures

fix Issue 7962  Captures.length() returns incorrect value
Comment 4 Dmitry Olshansky 2012-04-23 00:18:37 UTC
>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.