From ea848032c2f87e6516b27c3b6032ce7a21d31552 Mon Sep 17 00:00:00 2001 From: Matthias Clasen Date: Tue, 21 Oct 2025 18:55:13 -0400 Subject: [PATCH] Add g_markup_parse_context_get_offset This is a small, helpful api that helps for finding error locations in xml files, along the same lines as g_markup_parse_context_get_position. --- glib/gmarkup.c | 26 ++++++++++++++++++++++++++ glib/gmarkup.h | 2 ++ glib/tests/markup-parse.c | 3 +++ 3 files changed, 31 insertions(+) diff --git a/glib/gmarkup.c b/glib/gmarkup.c index f5179742d..cf96f2efc 100644 --- a/glib/gmarkup.c +++ b/glib/gmarkup.c @@ -78,6 +78,7 @@ struct _GMarkupParseContext gint line_number; gint char_number; + gsize offset; GMarkupParseState state; @@ -182,6 +183,7 @@ g_markup_parse_context_new (const GMarkupParser *parser, context->line_number = 1; context->char_number = 1; + context->offset = 0; context->partial_chunk = NULL; context->spare_chunks = NULL; @@ -746,6 +748,7 @@ advance_char (GMarkupParseContext *context) { context->iter++; context->char_number++; + context->offset++; if (G_UNLIKELY (context->iter == context->current_text_end)) return FALSE; @@ -1921,6 +1924,29 @@ g_markup_parse_context_get_position (GMarkupParseContext *context, *char_number = context->char_number; } +/** + * g_markup_parse_context_get_offset: + * @context: a #GMarkupParseContext + * + * Retrieves the current offset from the beginning of the document, + * in bytes. + * + * The information is meant to accompany the values returned by + * [method@GLib.MarkupParseContext.get_position], and comes with the + * same accuracy guarantees. + * + * Returns: the offset + * + * Since: 2.88 + */ +gsize +g_markup_parse_context_get_offset (GMarkupParseContext *context) +{ + g_return_val_if_fail (context != NULL, 0); + + return context->offset; +} + /** * g_markup_parse_context_get_user_data: * @context: a #GMarkupParseContext diff --git a/glib/gmarkup.h b/glib/gmarkup.h index 848a3fff0..184229dd2 100644 --- a/glib/gmarkup.h +++ b/glib/gmarkup.h @@ -227,6 +227,8 @@ GLIB_AVAILABLE_IN_ALL void g_markup_parse_context_get_position (GMarkupParseContext *context, gint *line_number, gint *char_number); +GLIB_AVAILABLE_IN_2_88 +gsize g_markup_parse_context_get_offset (GMarkupParseContext *context); GLIB_AVAILABLE_IN_ALL gpointer g_markup_parse_context_get_user_data (GMarkupParseContext *context); diff --git a/glib/tests/markup-parse.c b/glib/tests/markup-parse.c index 136a9a961..3da12ffe0 100644 --- a/glib/tests/markup-parse.c +++ b/glib/tests/markup-parse.c @@ -158,6 +158,7 @@ test_file (const gchar *filename, GError *local_error = NULL; GMarkupParseContext *context; gint line, col; + gsize offset; guint n_failures = 0; guint n_tests = 0; const gsize chunk_sizes_bytes[] = { 1, 2, 5, 12, 1024 }; @@ -178,6 +179,8 @@ test_file (const gchar *filename, g_markup_parse_context_get_position (context, &line, &col); g_assert_cmpint (line, ==, 1); g_assert_cmpint (col, ==, 1); + offset = g_markup_parse_context_get_offset (context); + g_assert_cmpint (offset, ==, 0); if (!g_markup_parse_context_parse (context, contents, -1, NULL) || !g_markup_parse_context_end_parse (context, NULL))