blob: 5056aa0bb1cb98ae102aa0eb336abc17f07ed635 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
|
/* __ *\
** ________ ___ / / ___ Scala API **
** / __/ __// _ | / / / _ | (c) 2003-2006, LAMP/EPFL **
** __\ \/ /__/ __ |/ /__/ __ | **
** /____/\___/_/ |_/____/_/ | | **
** |/ **
\* */
// $Id$
package scala.xml;
import scala.runtime.compat.StringBuilder;
object TextBuffer {
def fromString(str: String): TextBuffer = {
new TextBuffer().append( str );
}
}
/** this classes is for creating text nodes without surplus whitespace.
* all occurrences of one or more whitespace in strings appended with the
* append method will be replaced by a single space character, and
* leading and trailing space will be removed completely.
*/
class TextBuffer {
val sb = new StringBuilder();
var ws = true;
def appendSpace = if( !ws ) { ws = true; sb.append(' ');} else {};
def appendChar(c:char) = { ws = false; sb.append( c );}
/** appends this string to the text buffer, trimming whitespaces as needed */
def append( cs:Seq[Char] ):TextBuffer = {
for( val c <- cs ) {
if( Utility.isSpace( c ) )
appendSpace;
else
appendChar( c )
}
this
}
/** returns an empty sequence if text is only whitespace */
def toText:Seq[Text] = {
var len = sb.length(); /* invariant */
if( len == 0 ) return Nil;
if( Utility.isSpace( sb.charAt( len - 1 ) )) {
len = len - 1;
sb.setLength( len )
}
if( len == 0 ) return Nil;
List( Text( sb.toString() ) );
}
}
|