mirror of
https://gitlab.gnome.org/GNOME/glib.git
synced 2025-11-04 18:18:56 +01:00
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.
This commit is contained in:
@@ -78,6 +78,7 @@ struct _GMarkupParseContext
|
|||||||
|
|
||||||
gint line_number;
|
gint line_number;
|
||||||
gint char_number;
|
gint char_number;
|
||||||
|
gsize offset;
|
||||||
|
|
||||||
GMarkupParseState state;
|
GMarkupParseState state;
|
||||||
|
|
||||||
@@ -182,6 +183,7 @@ g_markup_parse_context_new (const GMarkupParser *parser,
|
|||||||
|
|
||||||
context->line_number = 1;
|
context->line_number = 1;
|
||||||
context->char_number = 1;
|
context->char_number = 1;
|
||||||
|
context->offset = 0;
|
||||||
|
|
||||||
context->partial_chunk = NULL;
|
context->partial_chunk = NULL;
|
||||||
context->spare_chunks = NULL;
|
context->spare_chunks = NULL;
|
||||||
@@ -746,6 +748,7 @@ advance_char (GMarkupParseContext *context)
|
|||||||
{
|
{
|
||||||
context->iter++;
|
context->iter++;
|
||||||
context->char_number++;
|
context->char_number++;
|
||||||
|
context->offset++;
|
||||||
|
|
||||||
if (G_UNLIKELY (context->iter == context->current_text_end))
|
if (G_UNLIKELY (context->iter == context->current_text_end))
|
||||||
return FALSE;
|
return FALSE;
|
||||||
@@ -1921,6 +1924,29 @@ g_markup_parse_context_get_position (GMarkupParseContext *context,
|
|||||||
*char_number = context->char_number;
|
*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:
|
* g_markup_parse_context_get_user_data:
|
||||||
* @context: a #GMarkupParseContext
|
* @context: a #GMarkupParseContext
|
||||||
|
|||||||
@@ -227,6 +227,8 @@ GLIB_AVAILABLE_IN_ALL
|
|||||||
void g_markup_parse_context_get_position (GMarkupParseContext *context,
|
void g_markup_parse_context_get_position (GMarkupParseContext *context,
|
||||||
gint *line_number,
|
gint *line_number,
|
||||||
gint *char_number);
|
gint *char_number);
|
||||||
|
GLIB_AVAILABLE_IN_2_88
|
||||||
|
gsize g_markup_parse_context_get_offset (GMarkupParseContext *context);
|
||||||
GLIB_AVAILABLE_IN_ALL
|
GLIB_AVAILABLE_IN_ALL
|
||||||
gpointer g_markup_parse_context_get_user_data (GMarkupParseContext *context);
|
gpointer g_markup_parse_context_get_user_data (GMarkupParseContext *context);
|
||||||
|
|
||||||
|
|||||||
@@ -158,6 +158,7 @@ test_file (const gchar *filename,
|
|||||||
GError *local_error = NULL;
|
GError *local_error = NULL;
|
||||||
GMarkupParseContext *context;
|
GMarkupParseContext *context;
|
||||||
gint line, col;
|
gint line, col;
|
||||||
|
gsize offset;
|
||||||
guint n_failures = 0;
|
guint n_failures = 0;
|
||||||
guint n_tests = 0;
|
guint n_tests = 0;
|
||||||
const gsize chunk_sizes_bytes[] = { 1, 2, 5, 12, 1024 };
|
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_markup_parse_context_get_position (context, &line, &col);
|
||||||
g_assert_cmpint (line, ==, 1);
|
g_assert_cmpint (line, ==, 1);
|
||||||
g_assert_cmpint (col, ==, 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) ||
|
if (!g_markup_parse_context_parse (context, contents, -1, NULL) ||
|
||||||
!g_markup_parse_context_end_parse (context, NULL))
|
!g_markup_parse_context_end_parse (context, NULL))
|
||||||
|
|||||||
Reference in New Issue
Block a user