summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorstanton <stanton>1999-04-02 23:51:48 (GMT)
committerstanton <stanton>1999-04-02 23:51:48 (GMT)
commit811d03220ce440ae353975f01e56d188526ad5c5 (patch)
treea250b9a4a39790116359ff41aca37a3eb3ff9f46
parentfc3097753ebb787422aff705939057714ed5a2e5 (diff)
downloadtk-811d03220ce440ae353975f01e56d188526ad5c5.zip
tk-811d03220ce440ae353975f01e56d188526ad5c5.tar.gz
tk-811d03220ce440ae353975f01e56d188526ad5c5.tar.bz2
* tests/text.test:
* generic/tkText.c: Fixed handling of Unicode in text searches. The -count option was returning byte counts instead of character counts. [Bug: 1056, 1148, 1666]
-rw-r--r--generic/tkText.c25
-rw-r--r--tests/text.test23
2 files changed, 41 insertions, 7 deletions
diff --git a/generic/tkText.c b/generic/tkText.c
index d0e0acf..0feb398 100644
--- a/generic/tkText.c
+++ b/generic/tkText.c
@@ -13,7 +13,7 @@
* See the file "license.terms" for information on usage and redistribution
* of this file, and for a DISCLAIMER OF ALL WARRANTIES.
*
- * RCS: @(#) $Id: tkText.c,v 1.1.4.5 1999/02/16 11:39:32 lfb Exp $
+ * RCS: @(#) $Id: tkText.c,v 1.1.4.6 1999/04/02 23:51:48 stanton Exp $
*/
#include "default.h"
@@ -1760,6 +1760,8 @@ TextSearchCmd(textPtr, interp, argc, argv)
}
do {
int thisLength;
+ Tcl_UniChar ch;
+
if (exact) {
p = strstr(startOfLine + firstByte, /* INTL: Native. */
pattern);
@@ -1790,7 +1792,7 @@ TextSearchCmd(textPtr, interp, argc, argv)
}
matchByte = i;
matchLength = thisLength;
- firstByte = matchByte + 1;
+ firstByte += Tcl_UtfToUniChar(startOfLine + matchByte, &ch);
} while (backwards);
/*
@@ -1800,6 +1802,15 @@ TextSearchCmd(textPtr, interp, argc, argv)
*/
if (matchByte >= 0) {
+ int numChars;
+
+ /*
+ * Convert the byte length to a character count.
+ */
+
+ numChars = Tcl_NumUtfChars(startOfLine + matchByte,
+ matchLength);
+
/*
* The index information returned by the regular expression
* parser only considers textual information: it doesn't
@@ -1819,7 +1830,7 @@ TextSearchCmd(textPtr, interp, argc, argv)
for (leftToScan += matchLength; leftToScan > 0;
segPtr = segPtr->nextPtr) {
if (segPtr->typePtr != &tkTextCharType) {
- matchLength += segPtr->size;
+ numChars += segPtr->size;
continue;
}
leftToScan -= segPtr->size;
@@ -1834,7 +1845,7 @@ TextSearchCmd(textPtr, interp, argc, argv)
}
}
if (varName != NULL) {
- sprintf(buffer, "%d", matchLength);
+ sprintf(buffer, "%d", numChars);
if (Tcl_SetVar(interp, varName, buffer, TCL_LEAVE_ERR_MSG)
== NULL) {
code = TCL_ERROR;
@@ -1913,6 +1924,7 @@ TkTextGetTabs(interp, tkwin, string)
char **argv;
TkTextTabArray *tabArrayPtr;
TkTextTab *tabPtr;
+ Tcl_UniChar ch;
if (Tcl_SplitList(interp, string, &argc, &argv) != TCL_OK) {
return NULL;
@@ -1955,11 +1967,12 @@ TkTextGetTabs(interp, tkwin, string)
if ((i+1) == argc) {
continue;
}
- c = UCHAR(argv[i+1][0]);
- if (!isalpha(c)) {
+ Tcl_UtfToUniChar(argv[i+1], &ch);
+ if (!Tcl_UniCharIsAlpha(ch)) {
continue;
}
i += 1;
+ c = argv[i][0];
if ((c == 'l') && (strncmp(argv[i], "left",
strlen(argv[i])) == 0)) {
tabPtr->alignment = LEFT;
diff --git a/tests/text.test b/tests/text.test
index 278871b..4eb45f1 100644
--- a/tests/text.test
+++ b/tests/text.test
@@ -6,7 +6,7 @@
# Copyright (c) 1998-1999 by Scriptics Corporation.
# All rights reserved.
#
-# RCS: @(#) $Id: text.test,v 1.1.4.5 1999/03/24 02:55:00 hershey Exp $
+# RCS: @(#) $Id: text.test,v 1.1.4.6 1999/04/02 23:51:49 stanton Exp $
if {[lsearch [namespace children] ::tcltest] == -1} {
source [file join [pwd] [file dirname [info script]] defs.tcl]
@@ -1082,6 +1082,27 @@ test text-20.62 {TextSearchCmd, freeing copy of pattern} {
set p $p$p$p$p$p
.t search -nocase $p 1.0
} {}
+test text-20.63 {TextSearchCmd, unicode} {
+ .t delete 1.0 end
+ .t insert end "foo\u30c9\u30cabar"
+ .t search \u30c9\u30ca 1.0
+} 1.3
+test text-20.64 {TextSearchCmd, unicode} {
+ .t delete 1.0 end
+ .t insert end "foo\u30c9\u30cabar"
+ list [.t search -count n \u30c9\u30ca 1.0] $n
+} {1.3 2}
+test text-20.65 {TextSearchCmd, unicode with non-text segments} {
+ .t delete 1.0 end
+ button .b1 -text baz
+ .t insert end "foo\u30c9"
+ .t window create end -window .b1
+ .t insert end "\u30cabar"
+ set result [list [.t search -count n \u30c9\u30ca 1.0] $n]
+ destroy .b1
+ set result
+} {1.3 3}
+
eval destroy [winfo child .]
text .t2 -highlightthickness 0 -bd 0 -relief flat -padx 0 -width 100