After an evaluation, GNOME has moved from Bugzilla to GitLab. Learn more about GitLab.
No new issues can be reported in GNOME Bugzilla anymore.
To report an issue in a GNOME project, go to GNOME GitLab.
Do not go to GNOME Gitlab for: Bluefish, Doxygen, GnuCash, GStreamer, java-gnome, LDTP, NetworkManager, Tomboy.
Bug 636911 - There is no Glib::MatchInfo in glibmm, making its regex API very limited
There is no Glib::MatchInfo in glibmm, making its regex API very limited
Status: RESOLVED FIXED
Product: glibmm
Classification: Bindings
Component: general
2.27.x
Other All
: Normal normal
: ---
Assigned To: gtkmm-forge
gtkmm-forge
Depends on:
Blocks:
 
 
Reported: 2010-12-09 20:55 UTC by Alexander Shaduri
Modified: 2010-12-13 04:07 UTC
See Also:
GNOME target: ---
GNOME version: ---


Attachments
Patch to add a Glib::MatchInfo class. (14.18 KB, patch)
2010-12-10 07:05 UTC, José Alburquerque
none Details | Review
Updated patch adding a fetch_all() method. (14.18 KB, patch)
2010-12-10 18:47 UTC, José Alburquerque
none Details | Review
Correct updated patch adding a fetch_all() method. (14.16 KB, patch)
2010-12-10 18:54 UTC, José Alburquerque
none Details | Review

Description Alexander Shaduri 2010-12-09 20:55:42 UTC
There isn't any Glib::MatchInfo class in glibmm (corresponding to GMatchInfo in glib). This makes Glib::Regex almost useless, because the user cannot retrieve the actual matches (he/she can only test for matched / not matched).

Using C API is an option, but it's a pain (especially with all the string handling needed for regular expressions).

Thanks
Comment 1 José Alburquerque 2010-12-10 07:05:32 UTC
Created attachment 176164 [details] [review]
Patch to add a Glib::MatchInfo class.

I believe the attached patch adds the new class.  Keep in mind that the C functions in the new class are merely wrapped.  There is no C++ like iterator provided because I thought that just wrapping things first would be best.  The methods in Regex accepting a Glib::MatchInfo have also been wrapped.

I should say that testing could be useful since I did not do that.
Comment 2 Alexander Shaduri 2010-12-10 10:52:38 UTC
Thanks!

A C++ iterator interface would be nice, but it's certainly not required. If I'm not mistaken, it could be added later without breaking ABI.

A fetch_all() method seems to be still absent though (it should probably return some kind of array of ustrings).

Thanks again.
Comment 3 José Alburquerque 2010-12-10 18:46:00 UTC
(In reply to comment #2)
> Thanks!
> 
> A C++ iterator interface would be nice, but it's certainly not required. If I'm
> not mistaken, it could be added later without breaking ABI.

That's right.  I was also sort of thinking a little and thought that an auto-increment operator, a boolean operator and a dereferencing operator could be added to the class so that it can have some of "iterating" capability that way.  I suppose there might be better ideas so I'll leave that as a TODO.

> 
> A fetch_all() method seems to be still absent though (it should probably return
> some kind of array of ustrings).

The following updated patch adds that method, using Glib::StringArrayHandle to wrap that method.  As I understand it, temporary types (such as Glib::StringArrayHandle) are planned to be removed to avoid the confusion of new developers but since it is being used in Regex already, I wrapped it that way also.

> 
> Thanks again.
Comment 4 José Alburquerque 2010-12-10 18:47:33 UTC
Created attachment 176201 [details] [review]
Updated patch adding a fetch_all() method.
Comment 5 José Alburquerque 2010-12-10 18:54:16 UTC
Created attachment 176202 [details] [review]
Correct updated patch adding a fetch_all() method.

Sorry about that.  The previous patch was not the right one.
Comment 6 Alexander Shaduri 2010-12-12 17:54:32 UTC
I "tested" the latest patch (applied against glibmm-2.25.5 since I have glib-2.26.0). Well, basically it works correctly for the following code:

Glib::RefPtr<Glib::Regex> regex = Glib::Regex::create("cd(.*)4(e).*");
Glib::ustring s("abcd1234efgh");
Glib::MatchInfo match_info;

std::cout << "Pattern=" << regex->get_pattern()
	<< ", with string=" << s << ", result="
	<< std::boolalpha << regex->match(s, match_info)
	<< std::endl;
std::cout << "Number of matches: " << match_info.get_match_count() << "\n";
for (int i = 0; i < match_info.get_match_count(); ++i) {
	std::cout << "Match " << i << ": " << match_info.fetch(i) << "\n";
}
std::vector<Glib::ustring> all = match_info.fetch_all();
for (std::size_t i = 0; i < all.size(); ++i) {
	std::cout << "Match " << i << ": " << all.at(i) << "\n";
}


This is not a comprehensive test of course, but the patch is trivial enough that I think it's safe to merge.

Thanks!
Comment 7 José Alburquerque 2010-12-13 04:07:20 UTC
Well then, pushed to git master so that the Regex API is more useful to those needing and/or using it.