[gjs] coverage: Use g_output_stream_printf instead of manual stuff



commit a7b014632b2afb8325b01e7c31ff67f3399e7464
Author: Jasper St. Pierre <jstpierre mecheye net>
Date:   Fri Jan 16 16:57:50 2015 -0800

    coverage: Use g_output_stream_printf instead of manual stuff

 gjs/coverage.cpp |   90 +++++++++--------------------------------------------
 1 files changed, 16 insertions(+), 74 deletions(-)
---
diff --git a/gjs/coverage.cpp b/gjs/coverage.cpp
index 893f17b..2f4d843 100644
--- a/gjs/coverage.cpp
+++ b/gjs/coverage.cpp
@@ -74,19 +74,10 @@ typedef struct _GjsCoverageFunction {
 } GjsCoverageFunction;
 
 static void
-write_string_into_stream(GOutputStream *stream,
-                         const gchar   *string)
-{
-    g_output_stream_write(stream, (gconstpointer) string, strlen(string) * sizeof(gchar), NULL, NULL);
-}
-
-static void
 write_source_file_header(GOutputStream *stream,
                          const gchar   *source_file_path)
 {
-    write_string_into_stream(stream, "SF:");
-    write_string_into_stream(stream, source_file_path);
-    write_string_into_stream(stream, "\n");
+    g_output_stream_printf(stream, NULL, NULL, NULL, "SF:%s\n", source_file_path);
 }
 
 typedef struct _FunctionHitCountData {
@@ -102,17 +93,12 @@ write_function_hit_count(GOutputStream *stream,
                          unsigned int  *n_functions_found,
                          unsigned int  *n_functions_hit)
 {
-    char *line = g_strdup_printf("FNDA:%i,%s\n",
-                                 hit_count,
-                                 function_name);
-
     (*n_functions_found)++;
 
     if (hit_count > 0)
         (*n_functions_hit)++;
 
-    write_string_into_stream(stream, line);
-    g_free(line);
+    g_output_stream_printf(stream, NULL, NULL, NULL, "FNDA:%d,%s\n", hit_count, function_name);
 }
 
 static void
@@ -140,9 +126,7 @@ write_function_foreach_func(gpointer value,
     GOutputStream       *stream = (GOutputStream *) user_data;
     GjsCoverageFunction *function = (GjsCoverageFunction *) value;
 
-    write_string_into_stream(stream, "FN:");
-    write_string_into_stream(stream, function->key);
-    write_string_into_stream(stream, "\n");
+    g_output_stream_printf(stream, NULL, NULL, NULL, "FN:%s\n", function->key);
 }
 
 static void
@@ -166,35 +150,12 @@ write_functions(GOutputStream *data_stream,
 }
 
 static void
-write_uint32_into_stream(GOutputStream *stream,
-                         unsigned int   integer)
-{
-    char buf[32];
-    g_snprintf(buf, 32, "%u", integer);
-    g_output_stream_write(stream, (gconstpointer) buf, strlen(buf) * sizeof(char), NULL, NULL);
-}
-
-static void
-write_int32_into_stream(GOutputStream *stream,
-                        int            integer)
-{
-    char buf[32];
-    g_snprintf(buf, 32, "%i", integer);
-    g_output_stream_write(stream, (gconstpointer) buf, strlen(buf) * sizeof(char), NULL, NULL);
-}
-
-static void
 write_function_coverage(GOutputStream *data_stream,
                         unsigned int  n_found_functions,
                         unsigned int  n_hit_functions)
 {
-    write_string_into_stream(data_stream, "FNF:");
-    write_uint32_into_stream(data_stream, n_found_functions);
-    write_string_into_stream(data_stream, "\n");
-
-    write_string_into_stream(data_stream, "FNH:");
-    write_uint32_into_stream(data_stream, n_hit_functions);
-    write_string_into_stream(data_stream, "\n");
+    g_output_stream_printf(data_stream, NULL, NULL, NULL, "FNF:%d\n", n_found_functions);
+    g_output_stream_printf(data_stream, NULL, NULL, NULL, "FNH:%d\n", n_hit_functions);
 }
 
 typedef struct _WriteAlternativeData {
@@ -232,16 +193,11 @@ write_individual_branch(gpointer branch_ptr,
         if (!branch->hit)
             hit_count_string = g_strdup_printf("-");
         else
-            hit_count_string = g_strdup_printf("%i", alternative_counter);
-
-        char *branch_alternative_line = g_strdup_printf("BRDA:%i,0,%i,%s\n",
-                                                        branch_point,
-                                                        i,
-                                                        hit_count_string);
+            hit_count_string = g_strdup_printf("%d", alternative_counter);
 
-        write_string_into_stream(data->output_stream, branch_alternative_line);
+        g_output_stream_printf(data->output_stream, NULL, NULL, NULL, "BRDA:%d,0,%d,%s\n",
+                               branch_point, i, hit_count_string);
         g_free(hit_count_string);
-        g_free(branch_alternative_line);
 
         ++(*data->n_branch_exits_found);
 
@@ -274,13 +230,8 @@ write_branch_totals(GOutputStream *stream,
                     unsigned int   n_branch_exits_found,
                     unsigned int   n_branch_exits_hit)
 {
-    write_string_into_stream(stream, "BRF:");
-    write_uint32_into_stream(stream, n_branch_exits_found);
-    write_string_into_stream(stream, "\n");
-
-    write_string_into_stream(stream, "BRH:");
-    write_uint32_into_stream(stream, n_branch_exits_hit);
-    write_string_into_stream(stream, "\n");
+    g_output_stream_printf(stream, NULL, NULL, NULL, "BRF:%d\n", n_branch_exits_found);
+    g_output_stream_printf(stream, NULL, NULL, NULL, "BRH:%d\n", n_branch_exits_hit);
 }
 
 static void
@@ -296,11 +247,7 @@ write_line_coverage(GOutputStream *stream,
         if (hit_count_for_line == -1)
             continue;
 
-        write_string_into_stream(stream, "DA:");
-        write_uint32_into_stream(stream, i);
-        write_string_into_stream(stream, ",");
-        write_int32_into_stream(stream, hit_count_for_line);
-        write_string_into_stream(stream, "\n");
+        g_output_stream_printf(stream, NULL, NULL, NULL, "DA:%d,%d\n", i, hit_count_for_line);
 
         if (hit_count_for_line > 0)
             ++(*lines_hit_count);
@@ -314,19 +261,14 @@ write_line_totals(GOutputStream *stream,
                   unsigned int   lines_hit_count,
                   unsigned int   executable_lines_count)
 {
-    write_string_into_stream(stream, "LH:");
-    write_uint32_into_stream(stream, lines_hit_count);
-    write_string_into_stream(stream, "\n");
-
-    write_string_into_stream(stream, "LF:");
-    write_uint32_into_stream(stream, executable_lines_count);
-    write_string_into_stream(stream, "\n");
+    g_output_stream_printf(stream, NULL, NULL, NULL, "LH:%d\n", lines_hit_count);
+    g_output_stream_printf(stream, NULL, NULL, NULL, "LF:%d\n", executable_lines_count);
 }
 
 static void
 write_end_of_record(GOutputStream *stream)
 {
-    write_string_into_stream(stream, "end_of_record\n");
+    g_output_stream_printf(stream, NULL, NULL, NULL, "end_of_record\n");
 }
 
 static void
@@ -496,13 +438,13 @@ get_array_from_js_value(JSContext             *context,
             jsval element;
             if (!JS_GetElement(context, js_array, i, &element)) {
                 g_array_unref(c_side_array);
-                gjs_throw(context, "Failed to get function names array element %i", i);
+                gjs_throw(context, "Failed to get function names array element %d", i);
                 return FALSE;
             }
 
             if (!(inserter(c_side_array, context, &element))) {
                 g_array_unref(c_side_array);
-                gjs_throw(context, "Failed to convert array element %i", i);
+                gjs_throw(context, "Failed to convert array element %d", i);
                 return FALSE;
             }
         }


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