[gjs] jsapi-util: Rewrite argument counting logic



commit 86b380d3ea1b4b06e7b7474747745c093e07e62d
Author: Jasper St. Pierre <jstpierre mecheye net>
Date:   Wed Jan 2 16:40:16 2013 -0500

    jsapi-util: Rewrite argument counting logic
    
    We're going to need this for maybe args, so let's clean up
    the code now.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=691039

 gjs/jsapi-util.c |   25 ++++++++++++++-----------
 1 files changed, 14 insertions(+), 11 deletions(-)
---
diff --git a/gjs/jsapi-util.c b/gjs/jsapi-util.c
index 8373262..39194f4 100644
--- a/gjs/jsapi-util.c
+++ b/gjs/jsapi-util.c
@@ -1146,8 +1146,8 @@ gjs_parse_args (JSContext  *context,
 #define MAX_UNWIND_STRINGS 16
     gpointer unwind_strings[MAX_UNWIND_STRINGS];
     gboolean ignore_trailing_args = FALSE;
-    guint n_required;
-    guint n_total;
+    guint n_required = 0;
+    guint n_total = 0;
     guint consumed_args;
 
     JS_BeginRequest(context);
@@ -1159,18 +1159,21 @@ gjs_parse_args (JSContext  *context,
         format++;
     }
 
-    /* Check for optional argument specifier */
-    fmt_iter = strchr (format, '|');
-    if (fmt_iter) {
-        /* Be sure there's not another '|' */
-        g_return_val_if_fail (strchr (fmt_iter + 1, '|') == NULL, JS_FALSE);
+    for (fmt_iter = format; *fmt_iter; fmt_iter++) {
+        switch (*fmt_iter) {
+        case '|':
+            n_required = n_total;
+            continue;
+        default:
+            break;
+        }
 
-        n_required = fmt_iter - format;
-        n_total = n_required + strlen (fmt_iter + 1);
-    } else {
-        n_required = n_total = strlen (format);
+        n_total++;
     }
 
+    if (n_required == 0)
+        n_required = n_total;
+
     if (argc < n_required || (argc > n_total && !ignore_trailing_args)) {
         if (n_required == n_total) {
             gjs_throw(context, "Error invoking %s: Expected %d arguments, got %d", function_name,



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