Source code: org/scoja/server/expr/WithoutLinesFunction.java
1
2 package org.scoja.server.expr;
3
4 import org.scoja.server.core.QStr;
5 import org.scoja.server.core.EventContext;
6
7 /**
8 * Remove <i>end of lines</i> from an String.
9 * Both CR and LF are considered line enders.
10 * White space (space, CR, LF, tabs, ..) after an end of line is removed.
11 * <p>
12 * At configuration files, it is mapped to <code>withoutEOLN</code>
13 * function.
14 */
15 public class WithoutLinesFunction extends String2StringFunction {
16
17 public WithoutLinesFunction(final StringExpression subexpr) {
18 super(subexpr);
19 }
20
21 public QStr eval(final EventContext env) {
22 final QStr qarg1 = super.eval(env);
23 if (!qarg1.hasEOLN()) return qarg1;
24
25 final String arg1 = qarg1.unqualified();
26 final int len = arg1.length();
27 final char[] buffer = new char[len];
28 int s = 0, t = 0;
29
30 boolean wasSpace = true, wasEOLN = true;
31 while (s < len) {
32 final char c = arg1.charAt(s++);
33 if (c == '\n' || c == '\r') {
34 if (!wasSpace) {
35 buffer[t++] = ' ';
36 wasSpace = true;
37 }
38 wasEOLN = true;
39 } else if (wasEOLN && c <= ' ') {
40 } else {
41 buffer[t++] = c;
42 wasSpace = c <= ' ';
43 wasEOLN = wasSpace && wasEOLN;
44 }
45 }
46
47 final int qualities
48 = (qarg1.qualities() & ~QStr.HASNT_EOLN) | QStr.HAS_EOLN;
49 return new QStr(new String(buffer, 0, t), qualities);
50 }
51 }