1
2
3
4
5
6
7
8
9
10
11 package org.eclipse.jgit.util;
12
13 import java.text.MessageFormat;
14 import java.util.Collection;
15
16 import org.eclipse.jgit.internal.JGitText;
17
18
19
20
21 public final class StringUtils {
22 private static final char[] LC;
23
24 static {
25 LC = new char['Z' + 1];
26 for (char c = 0; c < LC.length; c++)
27 LC[c] = c;
28 for (char c = 'A'; c <= 'Z'; c++)
29 LC[c] = (char) ('a' + (c - 'A'));
30 }
31
32
33
34
35
36
37
38
39
40
41
42
43
44 public static char toLowerCase(char c) {
45 return c <= 'Z' ? LC[c] : c;
46 }
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61 public static String toLowerCase(String in) {
62 final StringBuilder r = new StringBuilder(in.length());
63 for (int i = 0; i < in.length(); i++)
64 r.append(toLowerCase(in.charAt(i)));
65 return r.toString();
66 }
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86 public static String capitalize(String str) {
87 int strLen;
88 if (str == null || (strLen = str.length()) == 0) {
89 return str;
90 }
91 return new StringBuilder(strLen)
92 .append(Character.toTitleCase(str.charAt(0)))
93 .append(str.substring(1)).toString();
94 }
95
96
97
98
99
100
101
102
103
104
105
106
107
108 public static boolean equalsIgnoreCase(String a, String b) {
109 if (References.isSameObject(a, b)) {
110 return true;
111 }
112 if (a.length() != b.length())
113 return false;
114 for (int i = 0; i < a.length(); i++) {
115 if (toLowerCase(a.charAt(i)) != toLowerCase(b.charAt(i)))
116 return false;
117 }
118 return true;
119 }
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134 public static int compareIgnoreCase(String a, String b) {
135 for (int i = 0; i < a.length() && i < b.length(); i++) {
136 int d = toLowerCase(a.charAt(i)) - toLowerCase(b.charAt(i));
137 if (d != 0)
138 return d;
139 }
140 return a.length() - b.length();
141 }
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156 public static int compareWithCase(String a, String b) {
157 for (int i = 0; i < a.length() && i < b.length(); i++) {
158 int d = a.charAt(i) - b.charAt(i);
159 if (d != 0)
160 return d;
161 }
162 return a.length() - b.length();
163 }
164
165
166
167
168
169
170
171
172
173
174
175
176 public static boolean toBoolean(String stringValue) {
177 if (stringValue == null)
178 throw new NullPointerException(JGitText.get().expectedBooleanStringValue);
179
180 final Boolean bool = toBooleanOrNull(stringValue);
181 if (bool == null)
182 throw new IllegalArgumentException(MessageFormat.format(JGitText.get().notABoolean, stringValue));
183
184 return bool.booleanValue();
185 }
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203 public static Boolean toBooleanOrNull(String stringValue) {
204 if (stringValue == null)
205 return null;
206
207 if (equalsIgnoreCase("yes", stringValue)
208 || equalsIgnoreCase("true", stringValue)
209 || equalsIgnoreCase("1", stringValue)
210 || equalsIgnoreCase("on", stringValue))
211 return Boolean.TRUE;
212 else if (equalsIgnoreCase("no", stringValue)
213 || equalsIgnoreCase("false", stringValue)
214 || equalsIgnoreCase("0", stringValue)
215 || equalsIgnoreCase("off", stringValue))
216 return Boolean.FALSE;
217 else
218 return null;
219 }
220
221
222
223
224
225
226
227
228
229
230 public static String join(Collection<String> parts, String separator) {
231 return StringUtils.join(parts, separator, separator);
232 }
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247 public static String join(Collection<String> parts, String separator,
248 String lastSeparator) {
249 StringBuilder sb = new StringBuilder();
250 int i = 0;
251 int lastIndex = parts.size() - 1;
252 for (String part : parts) {
253 sb.append(part);
254 if (i == lastIndex - 1) {
255 sb.append(lastSeparator);
256 } else if (i != lastIndex) {
257 sb.append(separator);
258 }
259 i++;
260 }
261 return sb.toString();
262 }
263
264 private StringUtils() {
265
266 }
267
268
269
270
271
272
273
274
275 public static boolean isEmptyOrNull(String stringValue) {
276 return stringValue == null || stringValue.length() == 0;
277 }
278
279
280
281
282
283
284
285
286
287 public static String replaceLineBreaksWithSpace(String in) {
288 char[] buf = new char[in.length()];
289 int o = 0;
290 for (int i = 0; i < buf.length; ++i) {
291 char ch = in.charAt(i);
292 switch (ch) {
293 case '\r':
294 if (i + 1 < buf.length && in.charAt(i + 1) == '\n') {
295 buf[o++] = ' ';
296 ++i;
297 } else
298 buf[o++] = ' ';
299 break;
300 case '\n':
301 buf[o++] = ' ';
302 break;
303 default:
304 buf[o++] = ch;
305 break;
306 }
307 }
308 return new String(buf, 0, o);
309 }
310 }