Author: spouliot
Date: 2007-09-27 14:34:30 -0400 (Thu, 27 Sep 2007)
New Revision: 86526

Modified:
   trunk/moon/src/ChangeLog
   trunk/moon/src/array.cpp
Log:
array.cpp: Fix issues on double parsing, including bug #328915 and test cases 
in /moon/test/xaml/points-torture.xaml

Modified: trunk/moon/src/ChangeLog
===================================================================
--- trunk/moon/src/ChangeLog    2007-09-27 18:22:49 UTC (rev 86525)
+++ trunk/moon/src/ChangeLog    2007-09-27 18:34:30 UTC (rev 86526)
@@ -1,3 +1,8 @@
+2007-09-27  Sebastien Pouliot  <[EMAIL PROTECTED]>
+
+       * array.cpp: Fix issues on double parsing, including bug #328915 and
+       test cases in /moon/test/xaml/points-torture.xaml
+
 2007-09-27  Rolf Bjarne Kvinge <[EMAIL PROTECTED]> 
 
        * animation.cpp: Add a few null checks.

Modified: trunk/moon/src/array.cpp
===================================================================
--- trunk/moon/src/array.cpp    2007-09-27 18:22:49 UTC (rev 86525)
+++ trunk/moon/src/array.cpp    2007-09-27 18:34:30 UTC (rev 86526)
@@ -10,29 +10,13 @@
 #include <config.h>
 #include <string.h>
 #include <stdlib.h>
+#include <errno.h>
 
 #include <gtk/gtk.h>
 
 #include "array.h"
 
-static char**
-split_str (const char* s, int *count)
-{
-       char *trimmed = g_strdup (s);
-       g_strstrip (trimmed);
-       int n;
-       // FIXME - what are all the valid separators ? I've seen ',' and ' '
-       char** values = g_strsplit_set (trimmed, ", ", 0);
-       if (count) {
-               // count non-NULL entries (which means we must skip NULLs later 
too)
-               for (n = 0; values[n]; n++);
-               *count = n;
-       }
-       g_free (trimmed);
-       return values;
-}
 
-
 DoubleArray *
 double_array_new (int count, double *values)
 {
@@ -43,21 +27,63 @@
        return p;
 }
 
+
+static bool
+is_separator (char c)
+{
+       switch (c) {
+       case ',':
+       case ' ':
+       case '\r':
+       case '\n':
+       case '\t':
+               return true;
+       default:
+               return false;
+       }
+}
+
+
 double *
 double_array_from_str (const char *s, int *count)
 {
-       char **values = split_str (s, count);
-       int i, n;
-       
-       double *doubles = new double [*count];
-       for (i = 0, n = 0; i < *count; i++) {
-               char *value = values[i];
-               if (value)
-                       doubles[n++] = strtod (value, NULL);
+       int n = 16;
+       int pos = 0;
+       double *doubles = (double*)g_malloc (n * sizeof (double));
+       char *start = (char*)s;
+       char *end = NULL;
+
+       errno = 0;
+       // note: we use g_ascii_strtod because in some locale, like french, 
+       // comma is used as the decimal point
+       double dv = g_ascii_strtod (start, &end);
+       if (errno != 0 || (start == end))
+               goto error;
+
+       doubles[pos++] = dv;
+       while (*end) {
+               if (is_separator (*end)) {
+                       end++;
+               } else {
+                       start = end;
+                       errno = 0;
+                       dv = g_ascii_strtod (start, &end);
+                       if (errno != 0 || (start == end))
+                               goto error;
+                       if (pos == n) {
+                               n <<= 1; // double array size
+                               doubles = (double*)g_realloc (doubles, n * 
sizeof (double));
+                       }
+                       doubles[pos++] = dv;
+               }
        }
 
-       g_strfreev (values);
+       *count = pos;
        return doubles;
+error:
+       g_free (doubles);
+       *count = 0;
+       return NULL;
 }
 
 
@@ -76,25 +102,25 @@
 point_array_from_str (const char *s, int* count)
 {
        int i, j, n = 0;
-       bool x = true;
-       char** values = split_str (s, &n);
+       double *doubles = double_array_from_str (s, &n);
+       if (!doubles) {
+               *count = 0;
+               return NULL;
+       }
+       // invalid if doubles are not in pair
+       if ((n & 1) == 1) {
+               g_free (doubles);
+               *count = 0;
+               return NULL;
+       }
 
-       *count = (n >> 1); // 2 doubles for each point
-       Point *points = new Point [*count];
-       for (i = 0, j = 0; i < n; i++) {
-               char *value = values[i];
-               if (value) {
-                       if (x) {
-                               points[j].x = strtod (value, NULL);
-                               x = false;
-                       } else {
-                               points[j++].y = strtod (value, NULL);
-                               x = true;
-                       }
-               }
+       n >>= 1; // 2 doubles for each point
+       Point *points = new Point [n];
+       for (i = 0, j = 0; j < n; j++) {
+               points[j].x = doubles [i++];
+               points[j].y = doubles [i++];
        }
-
-       g_strfreev (values);
+       g_free (doubles);
+       *count = n;
        return points;
 }
-

_______________________________________________
Mono-patches maillist  -  [email protected]
http://lists.ximian.com/mailman/listinfo/mono-patches

Reply via email to