G_match_info_fetch_pos returns incorrect end_pos

I’m looking at the return values of GLib>MatchInfo>fetch_pos. It seems like the value for the returned end_pos is one higher than expected.

For example, I have the following string

I never knew GLib had regex
012345678901234567890123456
0         1         2

and a regex .*(knew).*

  • Applying g_match_info_fetch_pos on capture group 0 (entire string) returns an end position of 27 (one longer than the string).
  • Applying g_match_info_fetch_pos on capture group 1 returns an end position of 12 (one longer than the capture group)

Below is the code and output.

#include <glib-2.0/glib.h>

/* Compile command
   gcc post.c -lglib-2.0 -I/usr/include/glib-2.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include
 */


int main(int argc, char *argv[]) {
	GError *error = NULL;
	gchar *regex_pattern = ".*(knew).*";
	gchar *test_string = "I never knew GLib had regex";
	GMatchInfo *match_info;

	GRegex *regex = g_regex_new (
			regex_pattern,
			G_REGEX_DEFAULT,
			G_REGEX_MATCH_DEFAULT,
			NULL);
	
	g_regex_match (regex, test_string, G_REGEX_MATCH_DEFAULT, &match_info);
	gint start_pos;
	gint end_pos;
	g_match_info_fetch_pos ( match_info, 0, &start_pos, &end_pos);
	g_print("Start/end for position 0: %d/%d\n", start_pos, end_pos);
	g_match_info_fetch_pos ( match_info, 1, &start_pos, &end_pos);
	g_print("Start/end for position 1: %d/%d\n", start_pos, end_pos);

	g_regex_unref(regex);
	return 0;
}
Start/end for position 0: 0/27
Start/end for position 1: 8/12

Should the end positions be 26 and 11?

That’s how it’s meant to work: end_pos returns a pointer to the first character after the end of the match. This is similar to how, for example, g_strtod() works.

It means you can use end_pos directly for the next operation (perhaps processing the remainder of the string separately), and can get the length of a match by doing end_pos - start_pos, rather than (the slightly more confusing) end_pos - start_pos + 1.

The docs could make this clearer. I’ve opened a merge request for that here.

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.