Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: libadblockplus-android/src/org/apache/commons/lang/CharUtils.java

Issue 29415555: Issue 5148 - Remove Apache String Utils code (Closed)
Patch Set: Created April 17, 2017, 7:11 a.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 /*
2 * Simplified by Andrey Novikov for AdBlock Plus
3 */
4
5 /*
6 * Licensed to the Apache Software Foundation (ASF) under one or more
7 * contributor license agreements. See the NOTICE file distributed with
8 * this work for additional information regarding copyright ownership.
9 * The ASF licenses this file to You under the Apache License, Version 2.0
10 * (the "License"); you may not use this file except in compliance with
11 * the License. You may obtain a copy of the License at
12 *
13 * http://www.apache.org/licenses/LICENSE-2.0
14 *
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 */
21 package org.apache.commons.lang;
22
23 /**
24 * <p>Operations on char primitives and Character objects.</p>
25 *
26 * <p>This class tries to handle <code>null</code> input gracefully.
27 * An exception will not be thrown for a <code>null</code> input.
28 * Each method documents its behaviour in more detail.</p>
29 *
30 * @author Stephen Colebourne
31 * @since 2.1
32 * @version $Id: CharUtils.java 437554 2006-08-28 06:21:41Z bayard $
33 */
34 public class CharUtils
35 {
36 private static final String CHAR_STRING =
37 "\u0000\u0001\u0002\u0003\u0004\u0005\u0006\u0007" +
38 "\b\t\n\u000b\f\r\u000e\u000f" +
39 "\u0010\u0011\u0012\u0013\u0014\u0015\u0016\u0017" +
40 "\u0018\u0019\u001a\u001b\u001c\u001d\u001e\u001f" +
41 "\u0020\u0021\"\u0023\u0024\u0025\u0026\u0027" +
42 "\u0028\u0029\u002a\u002b\u002c\u002d\u002e\u002f" +
43 "\u0030\u0031\u0032\u0033\u0034\u0035\u0036\u0037" +
44 "\u0038\u0039\u003a\u003b\u003c\u003d\u003e\u003f" +
45 "\u0040\u0041\u0042\u0043\u0044\u0045\u0046\u0047" +
46 "\u0048\u0049\u004a\u004b\u004c\u004d\u004e\u004f" +
47 "\u0050\u0051\u0052\u0053\u0054\u0055\u0056\u0057" +
48 "\u0058\u0059\u005a\u005b\\\u005d\u005e\u005f" +
49 "\u0060\u0061\u0062\u0063\u0064\u0065\u0066\u0067" +
50 "\u0068\u0069\u006a\u006b\u006c\u006d\u006e\u006f" +
51 "\u0070\u0071\u0072\u0073\u0074\u0075\u0076\u0077" +
52 "\u0078\u0079\u007a\u007b\u007c\u007d\u007e\u007f";
53
54 private static final String[] CHAR_STRING_ARRAY = new String[128];
55 private static final Character[] CHAR_ARRAY = new Character[128];
56
57 /**
58 * <code>\u000a</code> linefeed LF ('\n').
59 *
60 * @see <a href="http://java.sun.com/docs/books/jls/third_edition/html/lexical .html#101089">JLF: Escape Sequences
61 * for Character and String Literals</a>
62 * @since 2.2
63 */
64 public static final char LF = '\n';
65
66 /**
67 * <code>\u000d</code> carriage return CR ('\r').
68 *
69 * @see <a href="http://java.sun.com/docs/books/jls/third_edition/html/lexical .html#101089">JLF: Escape Sequences
70 * for Character and String Literals</a>
71 * @since 2.2
72 */
73 public static final char CR = '\r';
74
75 static
76 {
77 for (int i = 127; i >= 0; i--)
78 {
79 CHAR_STRING_ARRAY[i] = CHAR_STRING.substring(i, i + 1);
80 CHAR_ARRAY[i] = new Character((char) i);
81 }
82 }
83
84 /**
85 * <p><code>CharUtils</code> instances should NOT be constructed in standard p rogramming.
86 * Instead, the class should be used as <code>CharUtils.toString('c');</code>. </p>
87 *
88 * <p>This constructor is public to permit tools that require a JavaBean insta nce
89 * to operate.</p>
90 */
91 public CharUtils()
92 {
93 super();
94 }
95
96 //-----------------------------------------------------------------------
97
98 /**
99 * <p>Converts the character to a Character.</p>
100 *
101 * <p>For ASCII 7 bit characters, this uses a cache that will return the
102 * same Character object each time.</p>
103 *
104 * <pre>
105 * CharUtils.toCharacterObject(' ') = ' '
106 * CharUtils.toCharacterObject('A') = 'A'
107 * </pre>
108 *
109 * @param ch the character to convert
110 * @return a Character of the specified character
111 */
112 public static Character toCharacterObject(char ch)
113 {
114 if (ch < CHAR_ARRAY.length)
115 {
116 return CHAR_ARRAY[ch];
117 }
118 return new Character(ch);
119 }
120
121 /**
122 * <p>Converts the String to a Character using the first character, returning
123 * null for empty Strings.</p>
124 *
125 * <p>For ASCII 7 bit characters, this uses a cache that will return the
126 * same Character object each time.</p>
127 *
128 * <pre>
129 * CharUtils.toCharacterObject(null) = null
130 * CharUtils.toCharacterObject("") = null
131 * CharUtils.toCharacterObject("A") = 'A'
132 * CharUtils.toCharacterObject("BA") = 'B'
133 * </pre>
134 *
135 * @param str the character to convert
136 * @return the Character value of the first letter of the String
137 */
138 public static Character toCharacterObject(String str)
139 {
140 if (StringUtils.isEmpty(str))
141 {
142 return null;
143 }
144 return toCharacterObject(str.charAt(0));
145 }
146
147 //-----------------------------------------------------------------------
148
149 /**
150 * <p>Converts the Character to a char throwing an exception for <code>null</c ode>.</p>
151 *
152 * <pre>
153 * CharUtils.toChar(null) = IllegalArgumentException
154 * CharUtils.toChar(' ') = ' '
155 * CharUtils.toChar('A') = 'A'
156 * </pre>
157 *
158 * @param ch the character to convert
159 * @return the char value of the Character
160 * @throws IllegalArgumentException if the Character is null
161 */
162 public static char toChar(Character ch)
163 {
164 if (ch == null)
165 {
166 throw new IllegalArgumentException("The Character must not be null");
167 }
168 return ch.charValue();
169 }
170
171 /**
172 * <p>Converts the Character to a char handling <code>null</code>.</p>
173 *
174 * <pre>
175 * CharUtils.toChar(null, 'X') = 'X'
176 * CharUtils.toChar(' ', 'X') = ' '
177 * CharUtils.toChar('A', 'X') = 'A'
178 * </pre>
179 *
180 * @param ch the character to convert
181 * @param defaultValue the value to use if the Character is null
182 * @return the char value of the Character or the default if null
183 */
184 public static char toChar(Character ch, char defaultValue)
185 {
186 if (ch == null)
187 {
188 return defaultValue;
189 }
190 return ch.charValue();
191 }
192
193 //-----------------------------------------------------------------------
194
195 /**
196 * <p>Converts the String to a char using the first character, throwing
197 * an exception on empty Strings.</p>
198 *
199 * <pre>
200 * CharUtils.toChar(null) = IllegalArgumentException
201 * CharUtils.toChar("") = IllegalArgumentException
202 * CharUtils.toChar("A") = 'A'
203 * CharUtils.toChar("BA") = 'B'
204 * </pre>
205 *
206 * @param str the character to convert
207 * @return the char value of the first letter of the String
208 * @throws IllegalArgumentException if the String is empty
209 */
210 public static char toChar(String str)
211 {
212 if (StringUtils.isEmpty(str))
213 {
214 throw new IllegalArgumentException("The String must not be empty");
215 }
216 return str.charAt(0);
217 }
218
219 /**
220 * <p>Converts the String to a char using the first character, defaulting
221 * the value on empty Strings.</p>
222 *
223 * <pre>
224 * CharUtils.toChar(null, 'X') = 'X'
225 * CharUtils.toChar("", 'X') = 'X'
226 * CharUtils.toChar("A", 'X') = 'A'
227 * CharUtils.toChar("BA", 'X') = 'B'
228 * </pre>
229 *
230 * @param str the character to convert
231 * @param defaultValue the value to use if the Character is null
232 * @return the char value of the first letter of the String or the default if null
233 */
234 public static char toChar(String str, char defaultValue)
235 {
236 if (StringUtils.isEmpty(str))
237 {
238 return defaultValue;
239 }
240 return str.charAt(0);
241 }
242
243 //-----------------------------------------------------------------------
244
245 /**
246 * <p>Converts the character to the Integer it represents, throwing an
247 * exception if the character is not numeric.</p>
248 *
249 * <p>This method coverts the char '1' to the int 1 and so on.</p>
250 *
251 * <pre>
252 * CharUtils.toIntValue('3') = 3
253 * CharUtils.toIntValue('A') = IllegalArgumentException
254 * </pre>
255 *
256 * @param ch the character to convert
257 * @return the int value of the character
258 * @throws IllegalArgumentException if the character is not ASCII numeric
259 */
260 public static int toIntValue(char ch)
261 {
262 if (isAsciiNumeric(ch) == false)
263 {
264 throw new IllegalArgumentException("The character " + ch + " is not in the range '0' - '9'");
265 }
266 return ch - 48;
267 }
268
269 /**
270 * <p>Converts the character to the Integer it represents, throwing an
271 * exception if the character is not numeric.</p>
272 *
273 * <p>This method coverts the char '1' to the int 1 and so on.</p>
274 *
275 * <pre>
276 * CharUtils.toIntValue('3', -1) = 3
277 * CharUtils.toIntValue('A', -1) = -1
278 * </pre>
279 *
280 * @param ch the character to convert
281 * @param defaultValue the default value to use if the character is not numeri c
282 * @return the int value of the character
283 */
284 public static int toIntValue(char ch, int defaultValue)
285 {
286 if (isAsciiNumeric(ch) == false)
287 {
288 return defaultValue;
289 }
290 return ch - 48;
291 }
292
293 /**
294 * <p>Converts the character to the Integer it represents, throwing an
295 * exception if the character is not numeric.</p>
296 *
297 * <p>This method coverts the char '1' to the int 1 and so on.</p>
298 *
299 * <pre>
300 * CharUtils.toIntValue(null) = IllegalArgumentException
301 * CharUtils.toIntValue('3') = 3
302 * CharUtils.toIntValue('A') = IllegalArgumentException
303 * </pre>
304 *
305 * @param ch the character to convert, not null
306 * @return the int value of the character
307 * @throws IllegalArgumentException if the Character is not ASCII numeric or i s null
308 */
309 public static int toIntValue(Character ch)
310 {
311 if (ch == null)
312 {
313 throw new IllegalArgumentException("The character must not be null");
314 }
315 return toIntValue(ch.charValue());
316 }
317
318 /**
319 * <p>Converts the character to the Integer it represents, throwing an
320 * exception if the character is not numeric.</p>
321 *
322 * <p>This method coverts the char '1' to the int 1 and so on.</p>
323 *
324 * <pre>
325 * CharUtils.toIntValue(null, -1) = -1
326 * CharUtils.toIntValue('3', -1) = 3
327 * CharUtils.toIntValue('A', -1) = -1
328 * </pre>
329 *
330 * @param ch the character to convert
331 * @param defaultValue the default value to use if the character is not numeri c
332 * @return the int value of the character
333 */
334 public static int toIntValue(Character ch, int defaultValue)
335 {
336 if (ch == null)
337 {
338 return defaultValue;
339 }
340 return toIntValue(ch.charValue(), defaultValue);
341 }
342
343 //-----------------------------------------------------------------------
344
345 /**
346 * <p>Converts the character to a String that contains the one character.</p>
347 *
348 * <p>For ASCII 7 bit characters, this uses a cache that will return the
349 * same String object each time.</p>
350 *
351 * <pre>
352 * CharUtils.toString(' ') = " "
353 * CharUtils.toString('A') = "A"
354 * </pre>
355 *
356 * @param ch the character to convert
357 * @return a String containing the one specified character
358 */
359 public static String toString(char ch)
360 {
361 if (ch < 128)
362 {
363 return CHAR_STRING_ARRAY[ch];
364 }
365 return new String(new char[]{ch});
366 }
367
368 /**
369 * <p>Converts the character to a String that contains the one character.</p>
370 *
371 * <p>For ASCII 7 bit characters, this uses a cache that will return the
372 * same String object each time.</p>
373 *
374 * <p>If <code>null</code> is passed in, <code>null</code> will be returned.</ p>
375 *
376 * <pre>
377 * CharUtils.toString(null) = null
378 * CharUtils.toString(' ') = " "
379 * CharUtils.toString('A') = "A"
380 * </pre>
381 *
382 * @param ch the character to convert
383 * @return a String containing the one specified character
384 */
385 public static String toString(Character ch)
386 {
387 if (ch == null)
388 {
389 return null;
390 }
391 return toString(ch.charValue());
392 }
393
394 //--------------------------------------------------------------------------
395
396 /**
397 * <p>Converts the string to the unicode format '\u0020'.</p>
398 *
399 * <p>This format is the Java source code format.</p>
400 *
401 * <pre>
402 * CharUtils.unicodeEscaped(' ') = "\u0020"
403 * CharUtils.unicodeEscaped('A') = "\u0041"
404 * </pre>
405 *
406 * @param ch the character to convert
407 * @return the escaped unicode string
408 */
409 public static String unicodeEscaped(char ch)
410 {
411 if (ch < 0x10)
412 {
413 return "\\u000" + Integer.toHexString(ch);
414 } else if (ch < 0x100)
415 {
416 return "\\u00" + Integer.toHexString(ch);
417 } else if (ch < 0x1000)
418 {
419 return "\\u0" + Integer.toHexString(ch);
420 }
421 return "\\u" + Integer.toHexString(ch);
422 }
423
424 /**
425 * <p>Converts the string to the unicode format '\u0020'.</p>
426 *
427 * <p>This format is the Java source code format.</p>
428 *
429 * <p>If <code>null</code> is passed in, <code>null</code> will be returned.</ p>
430 *
431 * <pre>
432 * CharUtils.unicodeEscaped(null) = null
433 * CharUtils.unicodeEscaped(' ') = "\u0020"
434 * CharUtils.unicodeEscaped('A') = "\u0041"
435 * </pre>
436 *
437 * @param ch the character to convert, may be null
438 * @return the escaped unicode string, null if null input
439 */
440 public static String unicodeEscaped(Character ch)
441 {
442 if (ch == null)
443 {
444 return null;
445 }
446 return unicodeEscaped(ch.charValue());
447 }
448
449 //--------------------------------------------------------------------------
450
451 /**
452 * <p>Checks whether the character is ASCII 7 bit.</p>
453 *
454 * <pre>
455 * CharUtils.isAscii('a') = true
456 * CharUtils.isAscii('A') = true
457 * CharUtils.isAscii('3') = true
458 * CharUtils.isAscii('-') = true
459 * CharUtils.isAscii('\n') = true
460 * CharUtils.isAscii('&copy;') = false
461 * </pre>
462 *
463 * @param ch the character to check
464 * @return true if less than 128
465 */
466 public static boolean isAscii(char ch)
467 {
468 return ch < 128;
469 }
470
471 /**
472 * <p>Checks whether the character is ASCII 7 bit printable.</p>
473 * <p/>
474 * <pre>
475 * CharUtils.isAsciiPrintable('a') = true
476 * CharUtils.isAsciiPrintable('A') = true
477 * CharUtils.isAsciiPrintable('3') = true
478 * CharUtils.isAsciiPrintable('-') = true
479 * CharUtils.isAsciiPrintable('\n') = false
480 * CharUtils.isAsciiPrintable('&copy;') = false
481 * </pre>
482 *
483 * @param ch the character to check
484 * @return true if between 32 and 126 inclusive
485 */
486 public static boolean isAsciiPrintable(char ch)
487 {
488 return ch >= 32 && ch < 127;
489 }
490
491 /**
492 * <p>Checks whether the character is ASCII 7 bit control.</p>
493 *
494 * <pre>
495 * CharUtils.isAsciiControl('a') = false
496 * CharUtils.isAsciiControl('A') = false
497 * CharUtils.isAsciiControl('3') = false
498 * CharUtils.isAsciiControl('-') = false
499 * CharUtils.isAsciiControl('\n') = true
500 * CharUtils.isAsciiControl('&copy;') = false
501 * </pre>
502 *
503 * @param ch the character to check
504 * @return true if less than 32 or equals 127
505 */
506 public static boolean isAsciiControl(char ch)
507 {
508 return ch < 32 || ch == 127;
509 }
510
511 /**
512 * <p>Checks whether the character is ASCII 7 bit alphabetic.</p>
513 * <p/>
514 * <pre>
515 * CharUtils.isAsciiAlpha('a') = true
516 * CharUtils.isAsciiAlpha('A') = true
517 * CharUtils.isAsciiAlpha('3') = false
518 * CharUtils.isAsciiAlpha('-') = false
519 * CharUtils.isAsciiAlpha('\n') = false
520 * CharUtils.isAsciiAlpha('&copy;') = false
521 * </pre>
522 *
523 * @param ch the character to check
524 * @return true if between 65 and 90 or 97 and 122 inclusive
525 */
526 public static boolean isAsciiAlpha(char ch)
527 {
528 return (ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z');
529 }
530
531 /**
532 * <p>Checks whether the character is ASCII 7 bit alphabetic upper case.</p>
533 *
534 * <pre>
535 * CharUtils.isAsciiAlphaUpper('a') = false
536 * CharUtils.isAsciiAlphaUpper('A') = true
537 * CharUtils.isAsciiAlphaUpper('3') = false
538 * CharUtils.isAsciiAlphaUpper('-') = false
539 * CharUtils.isAsciiAlphaUpper('\n') = false
540 * CharUtils.isAsciiAlphaUpper('&copy;') = false
541 * </pre>
542 *
543 * @param ch the character to check
544 * @return true if between 65 and 90 inclusive
545 */
546 public static boolean isAsciiAlphaUpper(char ch)
547 {
548 return ch >= 'A' && ch <= 'Z';
549 }
550
551 /**
552 * <p>Checks whether the character is ASCII 7 bit alphabetic lower case.</p>
553 * <p/>
554 * <pre>
555 * CharUtils.isAsciiAlphaLower('a') = true
556 * CharUtils.isAsciiAlphaLower('A') = false
557 * CharUtils.isAsciiAlphaLower('3') = false
558 * CharUtils.isAsciiAlphaLower('-') = false
559 * CharUtils.isAsciiAlphaLower('\n') = false
560 * CharUtils.isAsciiAlphaLower('&copy;') = false
561 * </pre>
562 *
563 * @param ch the character to check
564 * @return true if between 97 and 122 inclusive
565 */
566 public static boolean isAsciiAlphaLower(char ch)
567 {
568 return ch >= 'a' && ch <= 'z';
569 }
570
571 /**
572 * <p>Checks whether the character is ASCII 7 bit numeric.</p>
573 *
574 * <pre>
575 * CharUtils.isAsciiNumeric('a') = false
576 * CharUtils.isAsciiNumeric('A') = false
577 * CharUtils.isAsciiNumeric('3') = true
578 * CharUtils.isAsciiNumeric('-') = false
579 * CharUtils.isAsciiNumeric('\n') = false
580 * CharUtils.isAsciiNumeric('&copy;') = false
581 * </pre>
582 *
583 * @param ch the character to check
584 * @return true if between 48 and 57 inclusive
585 */
586 public static boolean isAsciiNumeric(char ch)
587 {
588 return ch >= '0' && ch <= '9';
589 }
590
591 /**
592 * <p>Checks whether the character is ASCII 7 bit numeric.</p>
593 *
594 * <pre>
595 * CharUtils.isAsciiAlphanumeric('a') = true
596 * CharUtils.isAsciiAlphanumeric('A') = true
597 * CharUtils.isAsciiAlphanumeric('3') = true
598 * CharUtils.isAsciiAlphanumeric('-') = false
599 * CharUtils.isAsciiAlphanumeric('\n') = false
600 * CharUtils.isAsciiAlphanumeric('&copy;') = false
601 * </pre>
602 *
603 * @param ch the character to check
604 * @return true if between 48 and 57 or 65 and 90 or 97 and 122 inclusive
605 */
606 public static boolean isAsciiAlphanumeric(char ch)
607 {
608 return (ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z') || (ch >= '0' && ch <= '9');
609 }
610
611 }
OLDNEW

Powered by Google App Engine
This is Rietveld