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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
|
package org.jsoup.helper;
import java.util.Collection;
import java.util.Iterator;
/**
* A minimal String utility class. Designed for internal jsoup use only.
*/
public final class StringUtil {
// memoised padding up to 10
private static final String[] padding = {"", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "};
/**
* Join a collection of strings by a seperator
* @param strings collection of string objects
* @param sep string to place between strings
* @return joined string
*/
public static String join(Collection strings, String sep) {
return join(strings.iterator(), sep);
}
/**
* Join a collection of strings by a seperator
* @param strings iterator of string objects
* @param sep string to place between strings
* @return joined string
*/
public static String join(Iterator strings, String sep) {
if (!strings.hasNext())
return "";
String start = strings.next().toString();
if (!strings.hasNext()) // only one, avoid builder
return start;
StringBuilder sb = new StringBuilder(64).append(start);
while (strings.hasNext()) {
sb.append(sep);
sb.append(strings.next());
}
return sb.toString();
}
/**
* Returns space padding
* @param width amount of padding desired
* @return string of spaces * width
*/
public static String padding(int width) {
if (width < 0)
throw new IllegalArgumentException("width must be > 0");
if (width < padding.length)
return padding[width];
char[] out = new char[width];
for (int i = 0; i < width; i++)
out[i] = ' ';
return String.valueOf(out);
}
/**
* Tests if a string is blank: null, emtpy, or only whitespace (" ", \r\n, \t, etc)
* @param string string to test
* @return if string is blank
*/
public static boolean isBlank(String string) {
if (string == null || string.length() == 0)
return true;
int l = string.length();
for (int i = 0; i < l; i++) {
if (!StringUtil.isWhitespace(string.codePointAt(i)))
return false;
}
return true;
}
/**
* Tests if a string is numeric, i.e. contains only digit characters
* @param string string to test
* @return true if only digit chars, false if empty or null or contains non-digit chrs
*/
public static boolean isNumeric(String string) {
if (string == null || string.length() == 0)
return false;
int l = string.length();
for (int i = 0; i < l; i++) {
if (!Character.isDigit(string.codePointAt(i)))
return false;
}
return true;
}
/**
* Tests if a code point is "whitespace" as defined in the HTML spec.
* @param c code point to test
* @return true if code point is whitespace, false otherwise
*/
public static boolean isWhitespace(int c){
return c == ' ' || c == '\t' || c == '\n' || c == '\f' || c == '\r';
}
public static String normaliseWhitespace(String string) {
StringBuilder sb = new StringBuilder(string.length());
boolean lastWasWhite = false;
boolean modified = false;
int l = string.length();
for (int i = 0; i < l; i++) {
int c = string.codePointAt(i);
if (isWhitespace(c)) {
if (lastWasWhite) {
modified = true;
continue;
}
if (c != ' ')
modified = true;
sb.append(' ');
lastWasWhite = true;
}
else {
sb.appendCodePoint(c);
lastWasWhite = false;
}
}
return modified ? sb.toString() : string;
}
public static boolean in(String needle, String... haystack) {
for (String hay : haystack) {
if (hay.equals(needle))
return true;
}
return false;
}
}
|