Merge branch 'markup-byte-pos' into 'main'

Add g_markup_parse_context_get_offset

See merge request GNOME/glib!4879
This commit is contained in:
Philip Withnall
2025-10-23 06:19:52 +00:00
3 changed files with 31 additions and 0 deletions

View File

@@ -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

View File

@@ -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);

View File

@@ -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))