[gtk/wip/otte/json: 10/11] jsonparser: Add gtk_json_parser_select_string()




commit 654bdd0cd8e7a920e8f48a7e05480c3a951c0d90
Author: Benjamin Otte <otte redhat com>
Date:   Tue Dec 7 13:41:32 2021 +0100

    jsonparser: Add gtk_json_parser_select_string()
    
    Same semantics as select_member(), useful for values where only a known
    set of values is accepted, like enumerations encoded as strings.

 gtk/json/gtkjsonparser.c        | 42 +++++++++++++++++++++++++++++++++--------
 gtk/json/gtkjsonparserprivate.h |  2 ++
 2 files changed, 36 insertions(+), 8 deletions(-)
---
diff --git a/gtk/json/gtkjsonparser.c b/gtk/json/gtkjsonparser.c
index 7d3fc3e6db..0b843dfe81 100644
--- a/gtk/json/gtkjsonparser.c
+++ b/gtk/json/gtkjsonparser.c
@@ -1376,24 +1376,21 @@ gtk_json_parser_find_member (GtkJsonParser *self,
   return FALSE;
 }
 
-gssize
-gtk_json_parser_select_member (GtkJsonParser      *self,
-                               const char * const *options)
+static gssize
+json_string_iter_run_select (const guchar       *string_data,
+                             const char * const *options)
 {
   JsonStringIter iter;
   gssize i, j;
   gsize found, len;
 
-  if (!gtk_json_parser_supports_member (self))
-    return -1;
-
-  if (options[0] == NULL)
+  if (options == NULL || options[0] == NULL)
     return -1;
 
   found = 0;
   i = 0;
 
-  for (len = json_string_iter_init (&iter, self->block->member_name);
+  for (len = json_string_iter_init (&iter, string_data);
        len > 0;
        len = json_string_iter_next (&iter))
     {
@@ -1429,6 +1426,16 @@ gtk_json_parser_select_member (GtkJsonParser      *self,
 
   return -1;
 }
+                     
+gssize
+gtk_json_parser_select_member (GtkJsonParser      *self,
+                               const char * const *options)
+{
+  if (!gtk_json_parser_supports_member (self))
+    return -1;
+
+  return json_string_iter_run_select (self->block->member_name, options);
+}
 
 gboolean
 gtk_json_parser_get_boolean (GtkJsonParser *self)
@@ -1587,6 +1594,25 @@ gtk_json_parser_get_string (GtkJsonParser *self)
   return gtk_json_unescape_string (self->block->value);
 }
 
+gssize
+gtk_json_parser_select_string (GtkJsonParser      *self,
+                               const char * const *options)
+{
+  if (self->error)
+    return -1;
+
+  if (self->block->value == NULL)
+    return -1;
+
+  if (*self->block->value != '"')
+    {
+      gtk_json_parser_type_error (self, "Expected a string");
+      return -1;
+    }
+
+  return json_string_iter_run_select (self->block->value, options);
+}
+
 gboolean
 gtk_json_parser_start_object (GtkJsonParser *self)
 {
diff --git a/gtk/json/gtkjsonparserprivate.h b/gtk/json/gtkjsonparserprivate.h
index 38f508ab9f..83f7237441 100644
--- a/gtk/json/gtkjsonparserprivate.h
+++ b/gtk/json/gtkjsonparserprivate.h
@@ -71,6 +71,8 @@ double                  gtk_json_parser_get_number              (GtkJsonParser
 int                     gtk_json_parser_get_int                 (GtkJsonParser          *self);
 guint                   gtk_json_parser_get_uint                (GtkJsonParser          *self);
 char *                  gtk_json_parser_get_string              (GtkJsonParser          *self);
+gssize                  gtk_json_parser_select_string           (GtkJsonParser          *self,
+                                                                 const char * const     *options);
 
 gboolean                gtk_json_parser_start_object            (GtkJsonParser          *self);
 gboolean                gtk_json_parser_start_array             (GtkJsonParser          *self);


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]