Misc fixes:
[sixth.git] / src / main / java / eu / svjatoslav / sixth / core / document / Utils.java
1 package eu.svjatoslav.sixth.core.document;
2
3 import eu.svjatoslav.commons.string.String2;
4
5 public class Utils {
6
7     public static final char[] whitespace = new char[]{'\n', '\r', ' ', '\t'};
8
9     public static String addIndentExceptFirstLine(String input, int indent) {
10         String[] lines = input.split("\\r?\\n");
11
12         StringBuilder sb = new StringBuilder();
13
14         if (lines.length >0 ) sb.append(lines[0]);
15
16         for (int i = 1; i< lines.length; i++) {
17             sb.append("\n");
18             sb.append(new String2(" ").repeat(indent).toString());
19             sb.append(lines[i]);
20         }
21
22         return sb.toString();
23     }
24
25     public static boolean isBlank(String s){
26         for (char c : s.toCharArray())
27             if (!isWhitespaceChar(c)) return false;
28
29         return true;
30     }
31
32     /**
33      * @return line indent in characters or -1 if line is blank or empty
34      */
35     public static int getLineIndent(String line){
36         for (int i = 0; i < line.length(); i++) {
37             if (!isWhitespaceChar(line.charAt(i)))
38                 return i;
39         }
40         return -1;
41     }
42
43     public static boolean isWhitespaceChar(char c){
44         for (char whitespaceChar : whitespace)
45             if (whitespaceChar == c) return true;
46
47         return false;
48     }
49
50     public static String removePrefix(String string, int charsToRemove){
51         String2 s = new String2(string);
52         s.trimPrefix(charsToRemove);
53         return s.toString();
54     }
55
56 }