| Ticket UUID: | 3e7c4a039d32955f5bd5b6d2de3af84f5c5fca76 | ||
| Title: | tcl_startOfPreviousWord ignores the last character when start is the string length | ||
| Type: | Bug | Created on: | 2026-10-25 11:56:01 |
| Submitter: | serhiy.storchaka | Assigned to: | jan.nijtmans |
| Subsystem: | 69. Other | Severity: | Minor |
| Priority: | 5 Medium | Last modified: | 2026-10-26 14:04:11 |
| Status: | Closed | Closed by: | jan.nijtmans |
| Resolution: | Fixed | Closed on: | 2026-10-26 14:04:11 |
| Version: | 8.6.14, 9.0.5, 9.1.1 | ||
| Description: | ||||
|
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).
| ||||
| User Comments: | ||||
serhiy.storchaka added on 2026-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.nijtmans added on 2026-10-26 14:04:11:
Fix merged to [c79e0372a7|trunk], core-9-0-branch and core-8-6-branch Many thanks! | ||||
