Author: ArcRiley
Date: 2009-01-13 22:17:55 -0500 (Tue, 13 Jan 2009)
New Revision: 1476

Modified:
   trunk/concordance/src/utils.c
   trunk/concordance/src/utils.h
Log:
new concordStrToUI function for fast gchar* -> guint conversion with easy error 
checking.  Example:
  if (!concordStrToUI(input, &result)) {error condition};



Modified: trunk/concordance/src/utils.c
===================================================================
--- trunk/concordance/src/utils.c       2009-01-13 21:45:14 UTC (rev 1475)
+++ trunk/concordance/src/utils.c       2009-01-14 03:17:55 UTC (rev 1476)
@@ -130,3 +130,46 @@
     return g_ucs4_to_utf8((gunichar*) uni, len, NULL, NULL, NULL);
   #endif
 }
+
+
+gboolean
+concordStrToUI(const gchar* string, guint* result) {                      /*\
+  cdef :                                                                  \*/
+    gint  d;
+    guint i = 0;
+    guint r = 0;
+
+  /* return error now in case of empty string */
+  if (*string == '\0')
+    return FALSE;
+ 
+  /* continue until hit end of string */
+  while (string[i] != '\0') {
+    /* shift existing value up by 10, ie total input "92":
+       i=0, r=0, r*10=0,  string[0] = "9", r =  0 + 9 =  9
+       i=1, r=9, r*10=90, string[1] = "2", r = 90 + 2 = 92
+       i=2, which is \0, so we exit while loop
+    */
+    r = r * 10;
+
+    /* get digit value from string position and test it.
+
+       gint             g_ascii_digit_value      (gchar c);
+       Returns -1 on error.
+    */
+    d = g_ascii_digit_value(string[i]);
+    if (d == -1)
+      return FALSE;
+
+    r+=d; /* add d to r, see above */
+    i++;  /* increment i for the next loop */
+
+    /* prevent wasted processing time or overflow on long strings */
+    if (i == 10)
+      return FALSE;
+  }
+
+  /* pass r to the result pointer and return */
+  *result = r;
+  return TRUE;
+}

Modified: trunk/concordance/src/utils.h
===================================================================
--- trunk/concordance/src/utils.h       2009-01-13 21:45:14 UTC (rev 1475)
+++ trunk/concordance/src/utils.h       2009-01-14 03:17:55 UTC (rev 1476)
@@ -53,5 +53,6 @@
                                         GMainContext* context);
 gchar*        concordSearchAttributes  (const gchar**, const gchar*);
 gchar*        concordPyUnicodeToUTF8   (PyObject* unicode);
+gboolean      concordStrToUI           (const gchar* string, guint* result);
 
 #endif

_______________________________________________
PySoy-SVN mailing list
PySoy-SVN@pysoy.org
http://www.pysoy.org/mailman/listinfo/pysoy-svn

Reply via email to