Tcl Source Code

Artifact [d053696d71]
Login

Artifact d053696d714f9a368a15f27299a6c9d23cb54de6:

Attachment "TclUtfcasecmp-3.patch" to ticket [1236896fff] added by rmax 2005-07-15 02:16:37.
--- generic/tclCmdIL.c	14 Jul 2005 12:17:35 -0000	1.77
+++ generic/tclCmdIL.c	14 Jul 2005 19:03:09 -0000
@@ -3304,7 +3304,7 @@
 	    dataType = INTEGER;
 	    break;
 	case LSEARCH_NOCASE:		/* -nocase */
-	    strCmpFn = strcasecmp;
+	    strCmpFn = TclUtfCasecmp;
 	    noCase = 1;
 	    break;
 	case LSEARCH_NOT:		/* -not */
@@ -3654,7 +3654,7 @@
 			 * compilation of memcmp
 			 */
 			if (noCase) {
-			    match = (strcasecmp(bytes, patternBytes) == 0);
+			    match = (TclUtfCasecmp(bytes, patternBytes) == 0);
 			} else {
 			    match = (memcmp(bytes, patternBytes,
 				    (size_t) length) == 0);
@@ -4020,7 +4020,7 @@
 	    sortInfo.sortMode = SORTMODE_INTEGER;
 	    break;
 	case LSORT_NOCASE:
-	    sortInfo.strCmpFn = strcasecmp;
+	    sortInfo.strCmpFn = TclUtfCasecmp;
 	    break;
 	case LSORT_REAL:
 	    sortInfo.sortMode = SORTMODE_REAL;
--- generic/tclCmdMZ.c	20 Jun 2005 07:49:11 -0000	1.126
+++ generic/tclCmdMZ.c	14 Jul 2005 19:03:09 -0000
@@ -2586,7 +2586,7 @@
 	    matchVarObj = objv[i];
 	    numMatchesSaved = -1;
 	} else if (index == OPT_NOCASE) {
-	    strCmpFn = strcasecmp;
+	    strCmpFn = TclUtfCasecmp;
 	    noCase = 1;
 	} else {
 	    if ( foundmode ) {
--- generic/tclInt.decls	5 Jul 2005 18:15:55 -0000	1.90
+++ generic/tclInt.decls	14 Jul 2005 19:03:09 -0000
@@ -897,6 +897,10 @@
     TclPlatformType *TclGetPlatform(void)
 }
 
+declare 225 generic {
+    int TclUtfCasecmp( CONST char *cs, CONST char *ct )
+}
+
 ##############################################################################
 
 # Define the platform specific internal Tcl interface. These functions are
--- generic/tclUtf.c	10 May 2005 18:34:51 -0000	1.34
+++ generic/tclUtf.c	14 Jul 2005 19:03:09 -0000
@@ -1103,6 +1103,64 @@
 /*
  *----------------------------------------------------------------------
  *
+ * TclUtfCasecmp --
+ *
+ *	Compare string cs to string ct case insensitively.
+ *
+ * Results:
+ *	Return <0 if cs < ct, 0 if cs == ct, or >0 if cs > ct.
+ *
+ * Side effects:
+ *	None.
+ *
+ *----------------------------------------------------------------------
+ */
+
+int
+TclUtfCasecmp(cs, ct)
+    CONST char *cs;		/* UTF string to compare to ct. */
+    CONST char *ct;		/* UTF string cs is compared to. */
+{
+    while (*cs != '\0' && *ct != '\0') {
+	
+	char s = *cs, t = *ct;
+	if (((s | t) & 0x80) == 0) {
+	    /* We have 7bit ASCII characters in both strings */
+	    if ((s == t) || ((s|0x20) >= 'a' && (s|0x20) <= 'z'
+			     && (s|0x20) == (t|0x20))) {
+		/* The characters are equivalent */
+		cs++; ct++;
+	    } else {
+		return ((s|0x20) - (t|0x20));
+	    }
+	} else {
+	    Tcl_UniChar ch1, ch2;
+	    cs += TclUtfToUniChar(cs, &ch1);
+	    ct += TclUtfToUniChar(ct, &ch2);
+	    if (ch1 != ch2) {
+		ch1 = Tcl_UniCharToLower(ch1);
+		ch2 = Tcl_UniCharToLower(ch2);
+		if (ch1 != ch2) {
+		    return (ch1 - ch2);
+		}
+	    }
+	}
+    }
+    if ((*cs | *ct) == '\0') {
+	/* the strings are equivalent */
+	return 0;
+    } else if (*ct == '\0') {
+	/* ct is a prefix of cs */
+	return 1;
+    } else {
+	/* cs is a prefix of ct */
+	return -1;
+    }	    
+}
+
+/*
+ *----------------------------------------------------------------------
+ *
  * Tcl_UniCharToUpper --
  *
  *	Compute the uppercase equivalent of the given Unicode character.
Index: tests/cmdIL.test
===================================================================
RCS file: /cvsroot/tcl/tcl/tests/cmdIL.test,v
retrieving revision 1.26
diff -u -u -r1.26 cmdIL.test
--- tests/cmdIL.test	1 Jun 2005 11:00:35 -0000	1.26
+++ tests/cmdIL.test	14 Jul 2005 19:03:09 -0000
@@ -431,6 +431,9 @@
 } -result 0 -cleanup {
     rename test_lsort ""
 }
+test cmdIL-5.6 {lsort -nocase with non-ASCII characters} -body {
+    lsort -nocase {\u00c4a \u20aca \u00c4c \u00e4b Ac ab Aa}
+} -result "Aa ab Ac \u00c4a \u00e4b \u00c4c \u20aca"
 
 # Compiled version
 test cmdIL-6.1 {lassign command syntax} -body {