Skip to content
Tcl Source Code Ticket Change Details
Login

Ticket Change Details

Overview

Artifact ID: 4988a3b3afe92840d598f28f54c95e6f6cb73a3b354eb29ea66306d2fb9b9975
Ticket: 3e7c4a039d32955f5bd5b6d2de3af84f5c5fca76
tcl_startOfPreviousWord ignores the last character when start is the string length
User & Date: serhiy.storchaka on 2026-10-25 11:56:01
Changes

  1. assignee changed to: "nobody"
  2. closer changed to: "nobody"
  3. cmimetype changed to: "text/x-fossil-wiki"
  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. foundin changed to: "8.6.14, 9.0.5, 9.1.1"
  6. login: "serhiy.storchaka"
  7. priority changed to: "5 Medium"
  8. resolution changed to: "None"
  9. severity changed to: "Minor"
  10. status changed to: "Open"
  11. submitter changed to: "serhiy.storchaka"
  12. subsystem changed to: "69. Other"
  13. title changed to:
    tcl_startOfPreviousWord ignores the last character when start is the string length
    
  14. type changed to: "Bug"