TABLE OF CONTENTS


::pwtk::parseRangeString

SYNOPSIS

proc ::pwtk::parseRangeString {rangeString lastIndex} {

PURPOSE

Evaulate the range-string and expand it into explicit list of indices. The ranges can be specified as follows (note that commas are optional):

      1, 2, 5, 6 -- this expands to 1 2 5 6    
      1, 4-8, 11 -- this expands to 1 4 5 6 7 8 11
      -5         -- this expands to 1 2 3 4 5
      5-         -- this expands to 5 6 7 ... lastIndex
      5-end      -- this expands to 5 6 7 ... lastIndex
      5-last     -- this expands to 5 6 7 ... lastIndex 
      all        -- this expands to 1 2 3 ... lastIndex
      begin-last -- this expands to 1 3 4 ... lastIndex
      first-last -- this expands to 1 3 4 ... lastIndex

ARGUMENTS

SOURCE

    if { $rangeString eq {} } { return {} }
    set rangeString [normalizeRangeString_ $rangeString]

    if { [string match "all" [string trim $rangeString '\"]] } {
        set rangeString "1-"
    }    
    # replace "end" and "last" with $lastIndex
    set rangeString [regsub -all {(end|last)} $rangeString $lastIndex]
    set rangeString [regsub -all {(begin|first|1st)} $rangeString 1]

    foreach item $rangeString {
        # check for syntax correctness, possible values: 
        #                                digit, digit-digit, digit-, -digit
        if { ! [regexp {^[0-9]+-?[0-9]*$} $item] && ! [regexp {^-?[0-9]*$} $item] } {
            ::pwtk::error "syntax error in range specification: $rangeString" 1
        }
        
        append range "[expandSequenceString_ $item $lastIndex] "
    }
    
    return [string trim $range]
}