[Numpy-svn] r3567 - trunk/numpy/core/src

numpy-svn@scip... numpy-svn@scip...
Wed Feb 28 17:52:35 CST 2007


Author: oliphant
Date: 2007-02-28 17:52:12 -0600 (Wed, 28 Feb 2007)
New Revision: 3567

Modified:
   trunk/numpy/core/src/arrayobject.c
Log:
Fix string comparison so it is not fooled by NULLs in the string.

Modified: trunk/numpy/core/src/arrayobject.c
===================================================================
--- trunk/numpy/core/src/arrayobject.c	2007-02-28 21:35:10 UTC (rev 3566)
+++ trunk/numpy/core/src/arrayobject.c	2007-02-28 23:52:12 UTC (rev 3567)
@@ -4254,6 +4254,7 @@
         PyArray_UCS4 *s1t=s1, *s2t=s2;
         int val;
         intp size;
+        int diff;
 
         if ((intp)s1 % sizeof(PyArray_UCS4) != 0) {
                 size = len1*sizeof(PyArray_UCS4);
@@ -4267,9 +4268,12 @@
         }
         val = PyArray_CompareUCS4(s1t, s2t, MIN(len1,len2));
         if ((val != 0) || (len1 == len2)) goto finish;
-        if (len2 > len1) {sptr = s2t+len1; val = -1;}
-        else {sptr = s1t+len2; val = 1;}
-        if (*sptr != 0) goto finish;
+        if (len2 > len1) {sptr = s2t+len1; val = -1; diff=len2-len1;}
+        else {sptr = s1t+len2; val = 1; diff=len1-len2;}
+        while (diff--) {
+                if (*sptr != 0) goto finish;
+                sptr++;
+        }
         val = 0;
 
  finish:
@@ -4291,13 +4295,17 @@
 {
         char *sptr;
         int val;
+        int diff;
 
         val = strncmp(s1, s2, MIN(len1, len2));
         if ((val != 0) || (len1 == len2)) return val;
-        if (len2 > len1) {sptr = s2+len1; val = -1;}
-        else {sptr = s1+len2; val = 1;}
-        if (*sptr != 0) return val;
-        return 0;
+        if (len2 > len1) {sptr = s2+len1; val = -1; diff=len2-len1;}
+        else {sptr = s1+len2; val = 1; diff=len1-len2;}
+        while (diff--) {
+                if (*sptr != 0) return val; 
+                sptr++;
+        }
+        return 0; /* Only happens if NULLs are everywhere */
 }
 
 /* Borrowed from Numarray */



More information about the Numpy-svn mailing list