wrap_string

one of the documented procedures in this installation of the ACS
Usage:
wrap_string   input   { threshold "80" }
What it does:
wraps a string to be no wider than 80 columns by inserting line breaks
Defined in: /web/philip/packages/acs-core/utilities-procs.tcl

Source code:


    set result_rows [list]
    set start_of_line_index 0
    while 1 {
	set this_line [string range $input $start_of_line_index [expr $start_of_line_index + $threshold - 1]]
	if { $this_line == "" } {
	    return [join $result_rows "\n"]
	}
	set first_new_line_pos [string first "\n" $this_line]
	if { $first_new_line_pos != -1 } {
	    # there is a newline
	    lappend result_rows [string range $input $start_of_line_index [expr $start_of_line_index + $first_new_line_pos - 1]]
	    set start_of_line_index [expr $start_of_line_index + $first_new_line_pos + 1]
	    continue
	}
	if { [expr $start_of_line_index + $threshold + 1] >= [string length $input] } {
	    # we're on the last line and it is < threshold so just return it
		lappend result_rows $this_line
		return [join $result_rows "\n"]
	}
	set last_space_pos [string last " " $this_line]
	if { $last_space_pos == -1 } {
	    # no space found!  Try the first space in the whole rest of the string
	    set next_space_pos [string first " " [string range $input $start_of_line_index end]]
	    set next_newline_pos [string first "\n" [string range $input $start_of_line_index end]]
	    if {$next_space_pos == -1} {
		set last_space_pos $next_newline_pos
	    } elseif {$next_space_pos < $next_newline_pos} {
		set last_space_pos $next_space_pos
	    } else {
		set last_space_pos $next_newline_pos
	    }
	    if { $last_space_pos == -1 } {
		# didn't find any more whitespace, append the whole thing as a line
		lappend result_rows [string range $input $start_of_line_index end]
		return [join $result_rows "\n"]
	    } 
	}
	# OK, we have a last space pos of some sort
	set real_index_of_space [expr $start_of_line_index + $last_space_pos]
	lappend result_rows [string range $input $start_of_line_index [expr $real_index_of_space - 1]]
	set start_of_line_index [expr $start_of_line_index + $last_space_pos + 1]
    }


philg@mit.edu