What follow is the descriptions of a possible bug. Unfortunately I'm using MSVC++ and ran into some unexpected behavior using boost::regex_format and a stringbuf. I compiled 1.27.0 without the /Oa flag as suggested in the installation notes. In summary, this works: stringbuf line; ... string tmp = line.str(); if (regex_match(tmp, matchInfo, linerx)) { string name = regex_format(matchInfo, "$1"); } but this doesn't: stringbuf line; ... if (regex_match(line.str(), matchInfo, linerx)) { string name = regex_format(matchInfo, "$1"); } By "doesn't work" I mean that 'name' has spurious characteres. If this might be a bug, let me know and I'll track it down to a small, compilable example.
What follow is the descriptions of a possible bug.
Unfortunately I'm using MSVC++ and ran into some unexpected behavior using boost::regex_format and a stringbuf. I compiled 1.27.0 without the /Oa flag as suggested in the installation notes.
In summary, this works: stringbuf line; ... string tmp = line.str(); if (regex_match(tmp, matchInfo, linerx)) { string name = regex_format(matchInfo, "$1"); } but this doesn't: stringbuf line; ... if (regex_match(line.str(), matchInfo, linerx)) { string name = regex_format(matchInfo, "$1"); } By "doesn't work" I mean that 'name' has spurious characteres.
If this might be a bug, let me know and I'll track it down to a small, compilable example.
Weird, but looks more like a VC bug than a regex one - after all the *only* difference between the two examples is whether the string passed to regex_format is a variable or a temporary, so unless I'm missing something it's got to be a compiler problem? John.
Hi!
Weird, but looks more like a VC bug than a regex one - after all the *only* difference between the two examples is whether the string passed to regex_format is a variable or a temporary, so unless I'm missing something it's got to be a compiler problem?
Well, I don't think, it's a compiler problem.
stringbuf::str() retuns a temporary string. This is passed on to
regex_match().
The real problem is, the data in matchInfo is not stored as a copy of the
processed string, but as a reference to its characters.
So, when the temoprary string is being destroyed, the result in matchInfo is
also lost.
Even the following short code failes, using a temporary string as argument
for regex_match():
#include <string>
#include <iostream>
using namespace std;
#include
Well, I don't think, it's a compiler problem. stringbuf::str() retuns a temporary string. This is passed on to regex_match(). The real problem is, the data in matchInfo is not stored as a copy of the processed string, but as a reference to its characters. So, when the temoprary string is being destroyed, the result in matchInfo is also lost.
Yes of course, sorry I was being particularly dense. The storing of iterators is *by design*, I don't want the overhead of copying an arbitrarily large amount of text, and in any case you need the iterators in order to do search and replace operations... John Maddock http://ourworld.compuserve.com/homepages/john_maddock/index.htm
"the_d.geo"
[...] stringbuf line; ... string tmp = line.str(); if (regex_match(tmp, matchInfo, linerx)) { string name = regex_format(matchInfo, "$1"); } [...]
John has already eyplained, why it won't work with a temporary. In addition to that, you can avoid the copying of the string, too, by taking advantage of the fact that temporaries bound to constant references will won't be destroyed for the reference's lifetime: const string& tmp = line.str(); // note the const ref if (regex_match(tmp, matchInfo, linerx)) { string name = regex_format(matchInfo, "$1"); } Schobi
participants (4)
-
Axel F.
-
Hendrik Schober
-
John Maddock
-
the_d.geo