Skip to content
Tcl Source Code Ticket Change Details
Login

Ticket Change Details

Overview

Artifact ID: c4c8b218e3bcccfec1c8be0f82193d4ad01a3153cdf423d75180814474cf3fe7
Ticket: 3e7c4a039d32955f5bd5b6d2de3af84f5c5fca76
tcl_startOfPreviousWord ignores the last character when start is the string length
User & Date: jan.nijtmans on 2026-10-26 14:04:11
Changes

  1. assignee changed to: "jan.nijtmans"
  2. closedate changed to: "2461340.0862499652"
  3. closer changed to: "jan.nijtmans"
  4. comment changed to:
    <tt>tcl_startOfPreviousWord</tt> returns a wrong result when <tt>start</tt> is equal to (or greater than) the length of the string and the last word has one character:
    
    <verbatim>
    % tcl_startOfPreviousWord {a b c} 5
    2
    % tcl_startOfPreviousWord {ab c} 4
    0
    </verbatim>
    
    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): <tt>[string range [string range $str 0 $start] 0 end-1]</tt> drops the last character also when <tt>$start</tt> is beyond the end of the string, while the old <tt>[string range $str 0 $start-1]</tt> did not.
    
    It affects Tk: in an entry or spinbox using these procedures (8.6, or <tt>-locale tcl8</tt> in 9.x), Ctrl+Left from the end of the text skips a final one-character word.
    
    A possible fix that keeps <tt>end-1</tt> working:
    <verbatim>
    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
    </verbatim>
    with tests such as <tt>tcl_startOfPreviousWord "ab c" 4</tt> → 3 and <tt>tcl_startOfPreviousWord "ab c" end+1</tt> → 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).
    
  5. icomment:
    Fix merged to [c79e0372a7|trunk], core-9-0-branch and core-8-6-branch
    
    Many thanks!
    
  6. login: "jan.nijtmans"
  7. mimetype: "text/x-fossil-wiki"
  8. resolution changed to: "Fixed"
  9. status changed to: "Closed"