Skip to content
Tcl Source Code View Ticket
Login

View Ticket

2026-10-26
14:04 • Closed ticket [3e7c4a039d]: tcl_startOfPreviousWord ignores the last character w... artifact: c4c8b218e3 user: jan.nijtmans
14:01
Fix [3e7c4a039d]: tcl_startOfPreviousWord ignores the last character when start is the string length check-in: 31222d571c user: jan.nijtmans tags: core-8-6-branch
13:08
Fix [3e7c4a039d]: tcl_startOfPreviousWord ignores the last character when start is the string length check-in: 840edb7f12 user: jan.nijtmans tags: core-9-0-branch
12:02
Fix [3e7c4a039d]: tcl_startOfPreviousWord ignores the last character when start is the string length check-in: c79e0372a7 user: jan.nijtmans tags: trunk, main
2026-10-25
12:03 • Ticket [3e7c4a039d] tcl_startOfPreviousWord ignores the last character when star... artifact: 0b307ec870 user: serhiy.storchaka
12:03
Fix [3e7c4a039d]: tcl_startOfPreviousWord ignores the last character when start is the string length... closed check-in: 5e430a21cd user: serhiy.storchaka tags: word-previous-last-char
11:56 • New ticket [3e7c4a039d] tcl_startOfPreviousWord ignores the last character when ... artifact: 4988a3b3af user: serhiy.storchaka

tcl_startOfPreviousWord ignores the last character when start is the string length

Closed / Fixed3e7c4a039d32955f5bd5b6d2de3af84f5c5fca76Created 38 days agoUpdated 37 days ago
Descriptionwiki2026-10-25 11:56:01
tcl_startOfPreviousWord returns a wrong result when start is equal to (or greater than) the length of the string and the last word has one character:
% tcl_startOfPreviousWord {a b c} 5
2
% tcl_startOfPreviousWord {ab c} 4
0

The expected results are 4 and 3, which 8.5 and 8.6.13 return. The last character is dropped, so the last word is not found when it has one character. For longer words the result happens to be right (word-2.6).

This is a regression from the fix for [16e25e1402] ([8f65470f72], 8.6.14): [string range [string range $str 0 $start] 0 end-1] drops the last character also when $start is beyond the end of the string, while the old [string range $str 0 $start-1] did not.

It affects Tk: in an entry or spinbox using these procedures (8.6, or -locale tcl8 in 9.x), Ctrl+Left from the end of the text skips a final one-character word.

A possible fix that keeps end-1 working:

set prefix [string range $str 0 $start]
if {[string index $str $start] ne ""} {
    set prefix [string range $prefix 0 end-1]
}
regexp -indices -- $WordBreakRE(previous) $prefix result word
with tests such as tcl_startOfPreviousWord "ab c" 4 → 3 and tcl_startOfPreviousWord "ab c" end+1 → 3. With it, all word-2.* cases still give the same results (checked with 8.6.18 and 9.1.1 by redefining the procedure).
Referenced by4
  • 31222d571cFix [3e7c4a039d]: tcl_startOfPreviousWord ignores the last character when start is the string length check-in2026-10-26 14:01:47
  • 840edb7f12Fix [3e7c4a039d]: tcl_startOfPreviousWord ignores the last character when start is the string length check-in2026-10-26 13:08:21
  • c79e0372a7Fix [3e7c4a039d]: tcl_startOfPreviousWord ignores the last character when start is the string length check-in2026-10-26 12:02:28
  • 5e430a21cdFix [3e7c4a039d]: tcl_startOfPreviousWord ignores the last character when start is the string length. Drop the character at $start only if there is one (regression from [16e25e1402] in 8.6.14). check-in2026-10-25 12:03:35
serhiy.storchakawiki2026-10-25 12:03:56

Proposed fix in branch word-previous-last-char (the change suggested above), with tests word-2.10..2.14.

jan.nijtmanswiki2026-10-26 14:04:11

Fix merged to [trunk], core-9-0-branch and core-8-6-branch

Many thanks!

Log in (anonymous is fine) to add a comment.