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 | package Torello.Java;
import Torello.Java.Function.IntCharFunction;
class Indent
{
// Uses this array as a parameter to StrReplace
private static final char[] cArrIndent = { '\n' };
static String run
(final String s, int n, boolean spaceOrTab, boolean trimBlankLines)
{
if (n < 1) throw new NException(
"The value passed to parameter 'n' was [" + n + "], but this is expected to be an " +
"integer greater than or equal to 1."
);
if (s.length() == 0) return "";
final String padding = String.format("%1$"+ n + "s", spaceOrTab ? " " : "\t");
// This replacement-function does a 'look-ahead'. If the next character after a newline
// character '\n' is *also* a '\n', then the first '\n' is left alone (not indented)
IntCharFunction<String> replFunc = (int i, char c) ->
{
while (i < (s.length() - 1))
{
c = s.charAt(++i);
if (c == '\n') return "\n";
if ((c != ' ') && (c != '\t')) return "\n" + padding;
}
return "\n";
};
// NOTE: private static final char[] cArrIndent = { '\n' };
String ret = StrReplace.r(s, cArrIndent, replFunc);
if (trimBlankLines) ret = StrIndent.trimWhiteSpaceOnlyLines(ret);
// Indent the first line of text - insert the padding before the final returned string.
//
// NOTE: This is somewhat inefficient, because the whole array needs to be copied again.
// Perhaps switching to RegEx and matching '^' is better (because of this reason).
//
// Special Case: The first character, itself, is a new-line.
return (ret.charAt(0) != '\n') ? (padding + ret) : ret;
}
}
|