-- Copyright 1993-1998, by the Cecil Project -- Department of Computer Science and Engineering, University of Washington -- See the LICENSE file for license information. (--DOC The `text_lines' data structure represents a series of lines with newlines separating them. The `new_text_lines' function breaks a string after newlines into separate text lines. The `lines' function provides access to the lines of text. The `indent' function adds `count' spaces to the front of each text line. The `as_collector' and `as_string' functions convert the `text_lines' object to a `collector' or a flat `string' representation, with embedded newlines. --) template object text_lines; field lines(@:text_lines):array[string] := new_array[string](); method new_text_lines(s:string):text_lines { let tl:text_lines := concrete object isa text_lines; let var f:int := 0; s.do_associations(&(i:int,c:char){ if(c = '\n', { tl.lines.add_last(s.copy_from(f, i)); f := i.succ; }); }); tl.lines.add_last(s.copy_from(f, s.length)); tl } method indent(tl@:text_lines, i:int):void { let padding:string := new_m_vstring(i, ' '); do(tl.lines.length, &(i:int){ if((tl.lines!i).non_empty, { tl.lines!i := padding || (tl.lines!i); }); }); } method as_collector(tl@:text_lines):collector[string] { let var c:collector[string] := new_collector[string](); let var first:bool := true; tl.lines.do(&(s:string){ if(first, { c := c && s; }, { c := c && "\n" && s; }); first := false; }); c } method as_string(tl@:text_lines):string { tl.as_collector.flat_string }