summaryrefslogtreecommitdiffstats
path: root/generic
diff options
context:
space:
mode:
authorjan.nijtmans <nijtmans@users.sourceforge.net>2020-05-19 14:13:31 (GMT)
committerjan.nijtmans <nijtmans@users.sourceforge.net>2020-05-19 14:13:31 (GMT)
commitfac371b4d60af3609407c3cc9c8c9c20da2de7aa (patch)
treee647a829c39e6347ff879dae407ed1c7436ec5f2 /generic
parent62d7bec31f12d0c52208a06e39bc891125392a2b (diff)
parent51d384bfda7a3eb5a915b19d343c8bb657268d75 (diff)
downloadtk-fac371b4d60af3609407c3cc9c8c9c20da2de7aa.zip
tk-fac371b4d60af3609407c3cc9c8c9c20da2de7aa.tar.gz
tk-fac371b4d60af3609407c3cc9c8c9c20da2de7aa.tar.bz2
Merge 8.6
Diffstat (limited to 'generic')
-rw-r--r--generic/tkInt.h2
-rw-r--r--generic/tkTextIndex.c6
-rw-r--r--generic/tkUtil.c36
3 files changed, 41 insertions, 3 deletions
diff --git a/generic/tkInt.h b/generic/tkInt.h
index 9e83a93..a4c2891 100644
--- a/generic/tkInt.h
+++ b/generic/tkInt.h
@@ -1396,9 +1396,11 @@ MODULE_SCOPE void TkUnixSetXftClipRegion(Region clipRegion);
#if TCL_UTF_MAX > 4
# define TkUtfToUniChar (size_t)Tcl_UtfToUniChar
# define TkUniCharToUtf (size_t)Tcl_UniCharToUtf
+# define TkUtfPrev Tcl_UtfPrev
#else
MODULE_SCOPE size_t TkUtfToUniChar(const char *, int *);
MODULE_SCOPE size_t TkUniCharToUtf(int, char *);
+ MODULE_SCOPE const char *TkUtfPrev(const char *, const char *);
#endif
#if TCL_MAJOR_VERSION > 8
diff --git a/generic/tkTextIndex.c b/generic/tkTextIndex.c
index 1db1208..70a53ae 100644
--- a/generic/tkTextIndex.c
+++ b/generic/tkTextIndex.c
@@ -436,7 +436,7 @@ TkTextMakeByteIndex(
*/
start = segPtr->body.chars + (byteIndex - index);
- p = Tcl_UtfPrev(start, segPtr->body.chars);
+ p = TkUtfPrev(start, segPtr->body.chars);
p += TkUtfToUniChar(p, &ch);
indexPtr->byteIndex += p - start;
}
@@ -2125,7 +2125,7 @@ TkTextIndexBackChars(
if (segPtr->typePtr == &tkTextCharType) {
start = segPtr->body.chars;
end = segPtr->body.chars + segSize;
- for (p = end; ; p = Tcl_UtfPrev(p, start)) {
+ for (p = end; ; p = TkUtfPrev(p, start)) {
if (charCount == 0) {
dstPtr->byteIndex -= (end - p);
goto backwardCharDone;
@@ -2366,7 +2366,7 @@ StartEnd(
}
if (offset > 0) {
chSize = (segPtr->body.chars + offset
- - Tcl_UtfPrev(segPtr->body.chars + offset,
+ - TkUtfPrev(segPtr->body.chars + offset,
segPtr->body.chars));
}
firstChar = 0;
diff --git a/generic/tkUtil.c b/generic/tkUtil.c
index f899cf2..01155c9 100644
--- a/generic/tkUtil.c
+++ b/generic/tkUtil.c
@@ -1266,7 +1266,43 @@ size_t TkUniCharToUtf(int ch, char *buf)
}
return Tcl_UniCharToUtf(ch, buf);
}
+/*
+ *---------------------------------------------------------------------------
+ *
+ * TkUtfPrev --
+ *
+ * Almost the same as Tcl_UtfPrev.
+ * This function is capable of jumping over a upper/lower surrogate pair.
+ * So, might jump back up to 6 bytes.
+ *
+ * Results:
+ * pointer to the first byte of the current UTF-8 character. A surrogate
+ * pair is also handled as being a single entity.
+ *
+ * Side effects:
+ * None.
+ *
+ *---------------------------------------------------------------------------
+ */
+const char *
+TkUtfPrev(
+ const char *src, /* The UTF-8 string. */
+ const char *start) /* Start position of string */
+{
+ const char *p = Tcl_UtfPrev(src, start);
+ const char *first = Tcl_UtfPrev(p, start);
+ int ch;
+
+#if TCL_UTF_MAX == 3
+ if ((src - start > 3) && ((src[-1] & 0xC0) == 0x80) && ((src[-2] & 0xC0) == 0x80)
+ && ((src[-3] & 0xC0) == 0x80) && (UCHAR(src[-4]) >= 0xF0)) {
+ return src - 4;
+ }
+#endif
+
+ return (first + TkUtfToUniChar(first, &ch) >= src) ? first : p ;
+}
#endif