Gintarus

util

Jul 6th, 2025
15
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 79.80 KB | None | 0 0
  1. /* Decompiler 2111ms, total 2433ms, lines 2620 */
  2. package org.apache.commons.lang3;
  3.  
  4. import java.io.UnsupportedEncodingException;
  5. import java.nio.charset.Charset;
  6. import java.text.Normalizer;
  7. import java.text.Normalizer.Form;
  8. import java.util.ArrayList;
  9. import java.util.Arrays;
  10. import java.util.Iterator;
  11. import java.util.List;
  12. import java.util.Locale;
  13. import java.util.regex.Pattern;
  14.  
  15. public class StringUtils {
  16. public static final String SPACE = " ";
  17. public static final String EMPTY = "";
  18. public static final String LF = "\n";
  19. public static final String CR = "\r";
  20. public static final int INDEX_NOT_FOUND = -1;
  21. private static final int PAD_LIMIT = 8192;
  22. private static final Pattern WHITESPACE_PATTERN = Pattern.compile("(?: |\\u00A0|\\s|[\\s&&[^ ]])\\s*");
  23.  
  24. public static boolean isEmpty(CharSequence cs) {
  25. return cs == null || cs.length() == 0;
  26. }
  27.  
  28. public static boolean isNotEmpty(CharSequence cs) {
  29. return !isEmpty(cs);
  30. }
  31.  
  32. public static boolean isAnyEmpty(CharSequence... css) {
  33. if (ArrayUtils.isEmpty(css)) {
  34. return true;
  35. } else {
  36. CharSequence[] arr$ = css;
  37. int len$ = css.length;
  38.  
  39. for(int i$ = 0; i$ < len$; ++i$) {
  40. CharSequence cs = arr$[i$];
  41. if (isEmpty(cs)) {
  42. return true;
  43. }
  44. }
  45.  
  46. return false;
  47. }
  48. }
  49.  
  50. public static boolean isNoneEmpty(CharSequence... css) {
  51. return !isAnyEmpty(css);
  52. }
  53.  
  54. public static boolean isBlank(CharSequence cs) {
  55. int strLen;
  56. if (cs != null && (strLen = cs.length()) != 0) {
  57. for(int i = 0; i < strLen; ++i) {
  58. if (!Character.isWhitespace(cs.charAt(i))) {
  59. return false;
  60. }
  61. }
  62.  
  63. return true;
  64. } else {
  65. return true;
  66. }
  67. }
  68.  
  69. public static boolean isNotBlank(CharSequence cs) {
  70. return !isBlank(cs);
  71. }
  72.  
  73. public static boolean isAnyBlank(CharSequence... css) {
  74. if (ArrayUtils.isEmpty(css)) {
  75. return true;
  76. } else {
  77. CharSequence[] arr$ = css;
  78. int len$ = css.length;
  79.  
  80. for(int i$ = 0; i$ < len$; ++i$) {
  81. CharSequence cs = arr$[i$];
  82. if (isBlank(cs)) {
  83. return true;
  84. }
  85. }
  86.  
  87. return false;
  88. }
  89. }
  90.  
  91. public static boolean isNoneBlank(CharSequence... css) {
  92. return !isAnyBlank(css);
  93. }
  94.  
  95. public static String trim(String str) {
  96. return str == null ? null : str.trim();
  97. }
  98.  
  99. public static String trimToNull(String str) {
  100. String ts = trim(str);
  101. return isEmpty(ts) ? null : ts;
  102. }
  103.  
  104. public static String trimToEmpty(String str) {
  105. return str == null ? "" : str.trim();
  106. }
  107.  
  108. public static String strip(String str) {
  109. return strip(str, (String)null);
  110. }
  111.  
  112. public static String stripToNull(String str) {
  113. if (str == null) {
  114. return null;
  115. } else {
  116. str = strip(str, (String)null);
  117. return str.isEmpty() ? null : str;
  118. }
  119. }
  120.  
  121. public static String stripToEmpty(String str) {
  122. return str == null ? "" : strip(str, (String)null);
  123. }
  124.  
  125. public static String strip(String str, String stripChars) {
  126. if (isEmpty(str)) {
  127. return str;
  128. } else {
  129. str = stripStart(str, stripChars);
  130. return stripEnd(str, stripChars);
  131. }
  132. }
  133.  
  134. public static String stripStart(String str, String stripChars) {
  135. int strLen;
  136. if (str != null && (strLen = str.length()) != 0) {
  137. int start = 0;
  138. if (stripChars == null) {
  139. while(start != strLen && Character.isWhitespace(str.charAt(start))) {
  140. ++start;
  141. }
  142. } else {
  143. if (stripChars.isEmpty()) {
  144. return str;
  145. }
  146.  
  147. while(start != strLen && stripChars.indexOf(str.charAt(start)) != -1) {
  148. ++start;
  149. }
  150. }
  151.  
  152. return str.substring(start);
  153. } else {
  154. return str;
  155. }
  156. }
  157.  
  158. public static String stripEnd(String str, String stripChars) {
  159. int end;
  160. if (str != null && (end = str.length()) != 0) {
  161. if (stripChars == null) {
  162. while(end != 0 && Character.isWhitespace(str.charAt(end - 1))) {
  163. --end;
  164. }
  165. } else {
  166. if (stripChars.isEmpty()) {
  167. return str;
  168. }
  169.  
  170. while(end != 0 && stripChars.indexOf(str.charAt(end - 1)) != -1) {
  171. --end;
  172. }
  173. }
  174.  
  175. return str.substring(0, end);
  176. } else {
  177. return str;
  178. }
  179. }
  180.  
  181. public static String[] stripAll(String... strs) {
  182. return stripAll(strs, (String)null);
  183. }
  184.  
  185. public static String[] stripAll(String[] strs, String stripChars) {
  186. int strsLen;
  187. if (strs != null && (strsLen = strs.length) != 0) {
  188. String[] newArr = new String[strsLen];
  189.  
  190. for(int i = 0; i < strsLen; ++i) {
  191. newArr[i] = strip(strs[i], stripChars);
  192. }
  193.  
  194. return newArr;
  195. } else {
  196. return strs;
  197. }
  198. }
  199.  
  200. public static String stripAccents(String input) {
  201. if (input == null) {
  202. return null;
  203. } else {
  204. Pattern pattern = Pattern.compile("\\p{InCombiningDiacriticalMarks}+");
  205. String decomposed = Normalizer.normalize(input, Form.NFD);
  206. return pattern.matcher(decomposed).replaceAll("");
  207. }
  208. }
  209.  
  210. public static boolean equals(CharSequence cs1, CharSequence cs2) {
  211. if (cs1 == cs2) {
  212. return true;
  213. } else if (cs1 != null && cs2 != null) {
  214. return cs1 instanceof String && cs2 instanceof String ? cs1.equals(cs2) : CharSequenceUtils.regionMatches(cs1, false, 0, cs2, 0, Math.max(cs1.length(), cs2.length()));
  215. } else {
  216. return false;
  217. }
  218. }
  219.  
  220. public static boolean equalsIgnoreCase(CharSequence str1, CharSequence str2) {
  221. if (str1 != null && str2 != null) {
  222. if (str1 == str2) {
  223. return true;
  224. } else {
  225. return str1.length() != str2.length() ? false : CharSequenceUtils.regionMatches(str1, true, 0, str2, 0, str1.length());
  226. }
  227. } else {
  228. return str1 == str2;
  229. }
  230. }
  231.  
  232. public static int indexOf(CharSequence seq, int searchChar) {
  233. return isEmpty(seq) ? -1 : CharSequenceUtils.indexOf(seq, searchChar, 0);
  234. }
  235.  
  236. public static int indexOf(CharSequence seq, int searchChar, int startPos) {
  237. return isEmpty(seq) ? -1 : CharSequenceUtils.indexOf(seq, searchChar, startPos);
  238. }
  239.  
  240. public static int indexOf(CharSequence seq, CharSequence searchSeq) {
  241. return seq != null && searchSeq != null ? CharSequenceUtils.indexOf(seq, searchSeq, 0) : -1;
  242. }
  243.  
  244. public static int indexOf(CharSequence seq, CharSequence searchSeq, int startPos) {
  245. return seq != null && searchSeq != null ? CharSequenceUtils.indexOf(seq, searchSeq, startPos) : -1;
  246. }
  247.  
  248. public static int ordinalIndexOf(CharSequence str, CharSequence searchStr, int ordinal) {
  249. return ordinalIndexOf(str, searchStr, ordinal, false);
  250. }
  251.  
  252. private static int ordinalIndexOf(CharSequence str, CharSequence searchStr, int ordinal, boolean lastIndex) {
  253. if (str != null && searchStr != null && ordinal > 0) {
  254. if (searchStr.length() == 0) {
  255. return lastIndex ? str.length() : 0;
  256. } else {
  257. int found = 0;
  258. int index = lastIndex ? str.length() : -1;
  259.  
  260. do {
  261. if (lastIndex) {
  262. index = CharSequenceUtils.lastIndexOf(str, searchStr, index - 1);
  263. } else {
  264. index = CharSequenceUtils.indexOf(str, searchStr, index + 1);
  265. }
  266.  
  267. if (index < 0) {
  268. return index;
  269. }
  270.  
  271. ++found;
  272. } while(found < ordinal);
  273.  
  274. return index;
  275. }
  276. } else {
  277. return -1;
  278. }
  279. }
  280.  
  281. public static int indexOfIgnoreCase(CharSequence str, CharSequence searchStr) {
  282. return indexOfIgnoreCase(str, searchStr, 0);
  283. }
  284.  
  285. public static int indexOfIgnoreCase(CharSequence str, CharSequence searchStr, int startPos) {
  286. if (str != null && searchStr != null) {
  287. if (startPos < 0) {
  288. startPos = 0;
  289. }
  290.  
  291. int endLimit = str.length() - searchStr.length() + 1;
  292. if (startPos > endLimit) {
  293. return -1;
  294. } else if (searchStr.length() == 0) {
  295. return startPos;
  296. } else {
  297. for(int i = startPos; i < endLimit; ++i) {
  298. if (CharSequenceUtils.regionMatches(str, true, i, searchStr, 0, searchStr.length())) {
  299. return i;
  300. }
  301. }
  302.  
  303. return -1;
  304. }
  305. } else {
  306. return -1;
  307. }
  308. }
  309.  
  310. public static int lastIndexOf(CharSequence seq, int searchChar) {
  311. return isEmpty(seq) ? -1 : CharSequenceUtils.lastIndexOf(seq, searchChar, seq.length());
  312. }
  313.  
  314. public static int lastIndexOf(CharSequence seq, int searchChar, int startPos) {
  315. return isEmpty(seq) ? -1 : CharSequenceUtils.lastIndexOf(seq, searchChar, startPos);
  316. }
  317.  
  318. public static int lastIndexOf(CharSequence seq, CharSequence searchSeq) {
  319. return seq != null && searchSeq != null ? CharSequenceUtils.lastIndexOf(seq, searchSeq, seq.length()) : -1;
  320. }
  321.  
  322. public static int lastOrdinalIndexOf(CharSequence str, CharSequence searchStr, int ordinal) {
  323. return ordinalIndexOf(str, searchStr, ordinal, true);
  324. }
  325.  
  326. public static int lastIndexOf(CharSequence seq, CharSequence searchSeq, int startPos) {
  327. return seq != null && searchSeq != null ? CharSequenceUtils.lastIndexOf(seq, searchSeq, startPos) : -1;
  328. }
  329.  
  330. public static int lastIndexOfIgnoreCase(CharSequence str, CharSequence searchStr) {
  331. return str != null && searchStr != null ? lastIndexOfIgnoreCase(str, searchStr, str.length()) : -1;
  332. }
  333.  
  334. public static int lastIndexOfIgnoreCase(CharSequence str, CharSequence searchStr, int startPos) {
  335. if (str != null && searchStr != null) {
  336. if (startPos > str.length() - searchStr.length()) {
  337. startPos = str.length() - searchStr.length();
  338. }
  339.  
  340. if (startPos < 0) {
  341. return -1;
  342. } else if (searchStr.length() == 0) {
  343. return startPos;
  344. } else {
  345. for(int i = startPos; i >= 0; --i) {
  346. if (CharSequenceUtils.regionMatches(str, true, i, searchStr, 0, searchStr.length())) {
  347. return i;
  348. }
  349. }
  350.  
  351. return -1;
  352. }
  353. } else {
  354. return -1;
  355. }
  356. }
  357.  
  358. public static boolean contains(CharSequence seq, int searchChar) {
  359. if (isEmpty(seq)) {
  360. return false;
  361. } else {
  362. return CharSequenceUtils.indexOf(seq, searchChar, 0) >= 0;
  363. }
  364. }
  365.  
  366. public static boolean contains(CharSequence seq, CharSequence searchSeq) {
  367. if (seq != null && searchSeq != null) {
  368. return CharSequenceUtils.indexOf(seq, searchSeq, 0) >= 0;
  369. } else {
  370. return false;
  371. }
  372. }
  373.  
  374. public static boolean containsIgnoreCase(CharSequence str, CharSequence searchStr) {
  375. if (str != null && searchStr != null) {
  376. int len = searchStr.length();
  377. int max = str.length() - len;
  378.  
  379. for(int i = 0; i <= max; ++i) {
  380. if (CharSequenceUtils.regionMatches(str, true, i, searchStr, 0, len)) {
  381. return true;
  382. }
  383. }
  384.  
  385. return false;
  386. } else {
  387. return false;
  388. }
  389. }
  390.  
  391. public static boolean containsWhitespace(CharSequence seq) {
  392. if (isEmpty(seq)) {
  393. return false;
  394. } else {
  395. int strLen = seq.length();
  396.  
  397. for(int i = 0; i < strLen; ++i) {
  398. if (Character.isWhitespace(seq.charAt(i))) {
  399. return true;
  400. }
  401. }
  402.  
  403. return false;
  404. }
  405. }
  406.  
  407. public static int indexOfAny(CharSequence cs, char... searchChars) {
  408. if (!isEmpty(cs) && !ArrayUtils.isEmpty(searchChars)) {
  409. int csLen = cs.length();
  410. int csLast = csLen - 1;
  411. int searchLen = searchChars.length;
  412. int searchLast = searchLen - 1;
  413.  
  414. for(int i = 0; i < csLen; ++i) {
  415. char ch = cs.charAt(i);
  416.  
  417. for(int j = 0; j < searchLen; ++j) {
  418. if (searchChars[j] == ch) {
  419. if (i >= csLast || j >= searchLast || !Character.isHighSurrogate(ch)) {
  420. return i;
  421. }
  422.  
  423. if (searchChars[j + 1] == cs.charAt(i + 1)) {
  424. return i;
  425. }
  426. }
  427. }
  428. }
  429.  
  430. return -1;
  431. } else {
  432. return -1;
  433. }
  434. }
  435.  
  436. public static int indexOfAny(CharSequence cs, String searchChars) {
  437. return !isEmpty(cs) && !isEmpty(searchChars) ? indexOfAny(cs, searchChars.toCharArray()) : -1;
  438. }
  439.  
  440. public static boolean containsAny(CharSequence cs, char... searchChars) {
  441. if (!isEmpty(cs) && !ArrayUtils.isEmpty(searchChars)) {
  442. int csLength = cs.length();
  443. int searchLength = searchChars.length;
  444. int csLast = csLength - 1;
  445. int searchLast = searchLength - 1;
  446.  
  447. for(int i = 0; i < csLength; ++i) {
  448. char ch = cs.charAt(i);
  449.  
  450. for(int j = 0; j < searchLength; ++j) {
  451. if (searchChars[j] == ch) {
  452. if (!Character.isHighSurrogate(ch)) {
  453. return true;
  454. }
  455.  
  456. if (j == searchLast) {
  457. return true;
  458. }
  459.  
  460. if (i < csLast && searchChars[j + 1] == cs.charAt(i + 1)) {
  461. return true;
  462. }
  463. }
  464. }
  465. }
  466.  
  467. return false;
  468. } else {
  469. return false;
  470. }
  471. }
  472.  
  473. public static boolean containsAny(CharSequence cs, CharSequence searchChars) {
  474. return searchChars == null ? false : containsAny(cs, CharSequenceUtils.toCharArray(searchChars));
  475. }
  476.  
  477. public static int indexOfAnyBut(CharSequence cs, char... searchChars) {
  478. if (!isEmpty(cs) && !ArrayUtils.isEmpty(searchChars)) {
  479. int csLen = cs.length();
  480. int csLast = csLen - 1;
  481. int searchLen = searchChars.length;
  482. int searchLast = searchLen - 1;
  483.  
  484. label38:
  485. for(int i = 0; i < csLen; ++i) {
  486. char ch = cs.charAt(i);
  487.  
  488. for(int j = 0; j < searchLen; ++j) {
  489. if (searchChars[j] == ch && (i >= csLast || j >= searchLast || !Character.isHighSurrogate(ch) || searchChars[j + 1] == cs.charAt(i + 1))) {
  490. continue label38;
  491. }
  492. }
  493.  
  494. return i;
  495. }
  496.  
  497. return -1;
  498. } else {
  499. return -1;
  500. }
  501. }
  502.  
  503. public static int indexOfAnyBut(CharSequence seq, CharSequence searchChars) {
  504. if (!isEmpty(seq) && !isEmpty(searchChars)) {
  505. int strLen = seq.length();
  506.  
  507. for(int i = 0; i < strLen; ++i) {
  508. char ch = seq.charAt(i);
  509. boolean chFound = CharSequenceUtils.indexOf(searchChars, ch, 0) >= 0;
  510. if (i + 1 < strLen && Character.isHighSurrogate(ch)) {
  511. char ch2 = seq.charAt(i + 1);
  512. if (chFound && CharSequenceUtils.indexOf(searchChars, ch2, 0) < 0) {
  513. return i;
  514. }
  515. } else if (!chFound) {
  516. return i;
  517. }
  518. }
  519.  
  520. return -1;
  521. } else {
  522. return -1;
  523. }
  524. }
  525.  
  526. public static boolean containsOnly(CharSequence cs, char... valid) {
  527. if (valid != null && cs != null) {
  528. if (cs.length() == 0) {
  529. return true;
  530. } else if (valid.length == 0) {
  531. return false;
  532. } else {
  533. return indexOfAnyBut(cs, valid) == -1;
  534. }
  535. } else {
  536. return false;
  537. }
  538. }
  539.  
  540. public static boolean containsOnly(CharSequence cs, String validChars) {
  541. return cs != null && validChars != null ? containsOnly(cs, validChars.toCharArray()) : false;
  542. }
  543.  
  544. public static boolean containsNone(CharSequence cs, char... searchChars) {
  545. if (cs != null && searchChars != null) {
  546. int csLen = cs.length();
  547. int csLast = csLen - 1;
  548. int searchLen = searchChars.length;
  549. int searchLast = searchLen - 1;
  550.  
  551. for(int i = 0; i < csLen; ++i) {
  552. char ch = cs.charAt(i);
  553.  
  554. for(int j = 0; j < searchLen; ++j) {
  555. if (searchChars[j] == ch) {
  556. if (!Character.isHighSurrogate(ch)) {
  557. return false;
  558. }
  559.  
  560. if (j == searchLast) {
  561. return false;
  562. }
  563.  
  564. if (i < csLast && searchChars[j + 1] == cs.charAt(i + 1)) {
  565. return false;
  566. }
  567. }
  568. }
  569. }
  570.  
  571. return true;
  572. } else {
  573. return true;
  574. }
  575. }
  576.  
  577. public static boolean containsNone(CharSequence cs, String invalidChars) {
  578. return cs != null && invalidChars != null ? containsNone(cs, invalidChars.toCharArray()) : true;
  579. }
  580.  
  581. public static int indexOfAny(CharSequence str, CharSequence... searchStrs) {
  582. if (str != null && searchStrs != null) {
  583. int sz = searchStrs.length;
  584. int ret = Integer.MAX_VALUE;
  585. int tmp = false;
  586.  
  587. for(int i = 0; i < sz; ++i) {
  588. CharSequence search = searchStrs[i];
  589. if (search != null) {
  590. int tmp = CharSequenceUtils.indexOf(str, search, 0);
  591. if (tmp != -1 && tmp < ret) {
  592. ret = tmp;
  593. }
  594. }
  595. }
  596.  
  597. return ret == Integer.MAX_VALUE ? -1 : ret;
  598. } else {
  599. return -1;
  600. }
  601. }
  602.  
  603. public static int lastIndexOfAny(CharSequence str, CharSequence... searchStrs) {
  604. if (str != null && searchStrs != null) {
  605. int sz = searchStrs.length;
  606. int ret = -1;
  607. int tmp = false;
  608.  
  609. for(int i = 0; i < sz; ++i) {
  610. CharSequence search = searchStrs[i];
  611. if (search != null) {
  612. int tmp = CharSequenceUtils.lastIndexOf(str, search, str.length());
  613. if (tmp > ret) {
  614. ret = tmp;
  615. }
  616. }
  617. }
  618.  
  619. return ret;
  620. } else {
  621. return -1;
  622. }
  623. }
  624.  
  625. public static String substring(String str, int start) {
  626. if (str == null) {
  627. return null;
  628. } else {
  629. if (start < 0) {
  630. start += str.length();
  631. }
  632.  
  633. if (start < 0) {
  634. start = 0;
  635. }
  636.  
  637. return start > str.length() ? "" : str.substring(start);
  638. }
  639. }
  640.  
  641. public static String substring(String str, int start, int end) {
  642. if (str == null) {
  643. return null;
  644. } else {
  645. if (end < 0) {
  646. end += str.length();
  647. }
  648.  
  649. if (start < 0) {
  650. start += str.length();
  651. }
  652.  
  653. if (end > str.length()) {
  654. end = str.length();
  655. }
  656.  
  657. if (start > end) {
  658. return "";
  659. } else {
  660. if (start < 0) {
  661. start = 0;
  662. }
  663.  
  664. if (end < 0) {
  665. end = 0;
  666. }
  667.  
  668. return str.substring(start, end);
  669. }
  670. }
  671. }
  672.  
  673. public static String left(String str, int len) {
  674. if (str == null) {
  675. return null;
  676. } else if (len < 0) {
  677. return "";
  678. } else {
  679. return str.length() <= len ? str : str.substring(0, len);
  680. }
  681. }
  682.  
  683. public static String right(String str, int len) {
  684. if (str == null) {
  685. return null;
  686. } else if (len < 0) {
  687. return "";
  688. } else {
  689. return str.length() <= len ? str : str.substring(str.length() - len);
  690. }
  691. }
  692.  
  693. public static String mid(String str, int pos, int len) {
  694. if (str == null) {
  695. return null;
  696. } else if (len >= 0 && pos <= str.length()) {
  697. if (pos < 0) {
  698. pos = 0;
  699. }
  700.  
  701. return str.length() <= pos + len ? str.substring(pos) : str.substring(pos, pos + len);
  702. } else {
  703. return "";
  704. }
  705. }
  706.  
  707. public static String substringBefore(String str, String separator) {
  708. if (!isEmpty(str) && separator != null) {
  709. if (separator.isEmpty()) {
  710. return "";
  711. } else {
  712. int pos = str.indexOf(separator);
  713. return pos == -1 ? str : str.substring(0, pos);
  714. }
  715. } else {
  716. return str;
  717. }
  718. }
  719.  
  720. public static String substringAfter(String str, String separator) {
  721. if (isEmpty(str)) {
  722. return str;
  723. } else if (separator == null) {
  724. return "";
  725. } else {
  726. int pos = str.indexOf(separator);
  727. return pos == -1 ? "" : str.substring(pos + separator.length());
  728. }
  729. }
  730.  
  731. public static String substringBeforeLast(String str, String separator) {
  732. if (!isEmpty(str) && !isEmpty(separator)) {
  733. int pos = str.lastIndexOf(separator);
  734. return pos == -1 ? str : str.substring(0, pos);
  735. } else {
  736. return str;
  737. }
  738. }
  739.  
  740. public static String substringAfterLast(String str, String separator) {
  741. if (isEmpty(str)) {
  742. return str;
  743. } else if (isEmpty(separator)) {
  744. return "";
  745. } else {
  746. int pos = str.lastIndexOf(separator);
  747. return pos != -1 && pos != str.length() - separator.length() ? str.substring(pos + separator.length()) : "";
  748. }
  749. }
  750.  
  751. public static String substringBetween(String str, String tag) {
  752. return substringBetween(str, tag, tag);
  753. }
  754.  
  755. public static String substringBetween(String str, String open, String close) {
  756. if (str != null && open != null && close != null) {
  757. int start = str.indexOf(open);
  758. if (start != -1) {
  759. int end = str.indexOf(close, start + open.length());
  760. if (end != -1) {
  761. return str.substring(start + open.length(), end);
  762. }
  763. }
  764.  
  765. return null;
  766. } else {
  767. return null;
  768. }
  769. }
  770.  
  771. public static String[] substringsBetween(String str, String open, String close) {
  772. if (str != null && !isEmpty(open) && !isEmpty(close)) {
  773. int strLen = str.length();
  774. if (strLen == 0) {
  775. return ArrayUtils.EMPTY_STRING_ARRAY;
  776. } else {
  777. int closeLen = close.length();
  778. int openLen = open.length();
  779. List<String> list = new ArrayList();
  780.  
  781. int end;
  782. for(int pos = 0; pos < strLen - closeLen; pos = end + closeLen) {
  783. int start = str.indexOf(open, pos);
  784. if (start < 0) {
  785. break;
  786. }
  787.  
  788. start += openLen;
  789. end = str.indexOf(close, start);
  790. if (end < 0) {
  791. break;
  792. }
  793.  
  794. list.add(str.substring(start, end));
  795. }
  796.  
  797. return list.isEmpty() ? null : (String[])list.toArray(new String[list.size()]);
  798. }
  799. } else {
  800. return null;
  801. }
  802. }
  803.  
  804. public static String[] split(String str) {
  805. return split(str, (String)null, -1);
  806. }
  807.  
  808. public static String[] split(String str, char separatorChar) {
  809. return splitWorker(str, separatorChar, false);
  810. }
  811.  
  812. public static String[] split(String str, String separatorChars) {
  813. return splitWorker(str, separatorChars, -1, false);
  814. }
  815.  
  816. public static String[] split(String str, String separatorChars, int max) {
  817. return splitWorker(str, separatorChars, max, false);
  818. }
  819.  
  820. public static String[] splitByWholeSeparator(String str, String separator) {
  821. return splitByWholeSeparatorWorker(str, separator, -1, false);
  822. }
  823.  
  824. public static String[] splitByWholeSeparator(String str, String separator, int max) {
  825. return splitByWholeSeparatorWorker(str, separator, max, false);
  826. }
  827.  
  828. public static String[] splitByWholeSeparatorPreserveAllTokens(String str, String separator) {
  829. return splitByWholeSeparatorWorker(str, separator, -1, true);
  830. }
  831.  
  832. public static String[] splitByWholeSeparatorPreserveAllTokens(String str, String separator, int max) {
  833. return splitByWholeSeparatorWorker(str, separator, max, true);
  834. }
  835.  
  836. private static String[] splitByWholeSeparatorWorker(String str, String separator, int max, boolean preserveAllTokens) {
  837. if (str == null) {
  838. return null;
  839. } else {
  840. int len = str.length();
  841. if (len == 0) {
  842. return ArrayUtils.EMPTY_STRING_ARRAY;
  843. } else if (separator != null && !"".equals(separator)) {
  844. int separatorLength = separator.length();
  845. ArrayList<String> substrings = new ArrayList();
  846. int numberOfSubstrings = 0;
  847. int beg = 0;
  848. int end = 0;
  849.  
  850. while(end < len) {
  851. end = str.indexOf(separator, beg);
  852. if (end > -1) {
  853. if (end > beg) {
  854. ++numberOfSubstrings;
  855. if (numberOfSubstrings == max) {
  856. end = len;
  857. substrings.add(str.substring(beg));
  858. } else {
  859. substrings.add(str.substring(beg, end));
  860. beg = end + separatorLength;
  861. }
  862. } else {
  863. if (preserveAllTokens) {
  864. ++numberOfSubstrings;
  865. if (numberOfSubstrings == max) {
  866. end = len;
  867. substrings.add(str.substring(beg));
  868. } else {
  869. substrings.add("");
  870. }
  871. }
  872.  
  873. beg = end + separatorLength;
  874. }
  875. } else {
  876. substrings.add(str.substring(beg));
  877. end = len;
  878. }
  879. }
  880.  
  881. return (String[])substrings.toArray(new String[substrings.size()]);
  882. } else {
  883. return splitWorker(str, (String)null, max, preserveAllTokens);
  884. }
  885. }
  886. }
  887.  
  888. public static String[] splitPreserveAllTokens(String str) {
  889. return splitWorker(str, (String)null, -1, true);
  890. }
  891.  
  892. public static String[] splitPreserveAllTokens(String str, char separatorChar) {
  893. return splitWorker(str, separatorChar, true);
  894. }
  895.  
  896. private static String[] splitWorker(String str, char separatorChar, boolean preserveAllTokens) {
  897. if (str == null) {
  898. return null;
  899. } else {
  900. int len = str.length();
  901. if (len == 0) {
  902. return ArrayUtils.EMPTY_STRING_ARRAY;
  903. } else {
  904. List<String> list = new ArrayList();
  905. int i = 0;
  906. int start = 0;
  907. boolean match = false;
  908. boolean lastMatch = false;
  909.  
  910. while(true) {
  911. while(i < len) {
  912. if (str.charAt(i) == separatorChar) {
  913. if (match || preserveAllTokens) {
  914. list.add(str.substring(start, i));
  915. match = false;
  916. lastMatch = true;
  917. }
  918.  
  919. ++i;
  920. start = i;
  921. } else {
  922. lastMatch = false;
  923. match = true;
  924. ++i;
  925. }
  926. }
  927.  
  928. if (match || preserveAllTokens && lastMatch) {
  929. list.add(str.substring(start, i));
  930. }
  931.  
  932. return (String[])list.toArray(new String[list.size()]);
  933. }
  934. }
  935. }
  936. }
  937.  
  938. public static String[] splitPreserveAllTokens(String str, String separatorChars) {
  939. return splitWorker(str, separatorChars, -1, true);
  940. }
  941.  
  942. public static String[] splitPreserveAllTokens(String str, String separatorChars, int max) {
  943. return splitWorker(str, separatorChars, max, true);
  944. }
  945.  
  946. private static String[] splitWorker(String str, String separatorChars, int max, boolean preserveAllTokens) {
  947. if (str == null) {
  948. return null;
  949. } else {
  950. int len = str.length();
  951. if (len == 0) {
  952. return ArrayUtils.EMPTY_STRING_ARRAY;
  953. } else {
  954. List<String> list = new ArrayList();
  955. int sizePlus1 = 1;
  956. int i = 0;
  957. int start = 0;
  958. boolean match = false;
  959. boolean lastMatch = false;
  960. if (separatorChars != null) {
  961. if (separatorChars.length() != 1) {
  962. label87:
  963. while(true) {
  964. while(true) {
  965. if (i >= len) {
  966. break label87;
  967. }
  968.  
  969. if (separatorChars.indexOf(str.charAt(i)) >= 0) {
  970. if (match || preserveAllTokens) {
  971. lastMatch = true;
  972. if (sizePlus1++ == max) {
  973. i = len;
  974. lastMatch = false;
  975. }
  976.  
  977. list.add(str.substring(start, i));
  978. match = false;
  979. }
  980.  
  981. ++i;
  982. start = i;
  983. } else {
  984. lastMatch = false;
  985. match = true;
  986. ++i;
  987. }
  988. }
  989. }
  990. } else {
  991. char sep = separatorChars.charAt(0);
  992.  
  993. label71:
  994. while(true) {
  995. while(true) {
  996. if (i >= len) {
  997. break label71;
  998. }
  999.  
  1000. if (str.charAt(i) == sep) {
  1001. if (match || preserveAllTokens) {
  1002. lastMatch = true;
  1003. if (sizePlus1++ == max) {
  1004. i = len;
  1005. lastMatch = false;
  1006. }
  1007.  
  1008. list.add(str.substring(start, i));
  1009. match = false;
  1010. }
  1011.  
  1012. ++i;
  1013. start = i;
  1014. } else {
  1015. lastMatch = false;
  1016. match = true;
  1017. ++i;
  1018. }
  1019. }
  1020. }
  1021. }
  1022. } else {
  1023. label103:
  1024. while(true) {
  1025. while(true) {
  1026. if (i >= len) {
  1027. break label103;
  1028. }
  1029.  
  1030. if (Character.isWhitespace(str.charAt(i))) {
  1031. if (match || preserveAllTokens) {
  1032. lastMatch = true;
  1033. if (sizePlus1++ == max) {
  1034. i = len;
  1035. lastMatch = false;
  1036. }
  1037.  
  1038. list.add(str.substring(start, i));
  1039. match = false;
  1040. }
  1041.  
  1042. ++i;
  1043. start = i;
  1044. } else {
  1045. lastMatch = false;
  1046. match = true;
  1047. ++i;
  1048. }
  1049. }
  1050. }
  1051. }
  1052.  
  1053. if (match || preserveAllTokens && lastMatch) {
  1054. list.add(str.substring(start, i));
  1055. }
  1056.  
  1057. return (String[])list.toArray(new String[list.size()]);
  1058. }
  1059. }
  1060. }
  1061.  
  1062. public static String[] splitByCharacterType(String str) {
  1063. return splitByCharacterType(str, false);
  1064. }
  1065.  
  1066. public static String[] splitByCharacterTypeCamelCase(String str) {
  1067. return splitByCharacterType(str, true);
  1068. }
  1069.  
  1070. private static String[] splitByCharacterType(String str, boolean camelCase) {
  1071. if (str == null) {
  1072. return null;
  1073. } else if (str.isEmpty()) {
  1074. return ArrayUtils.EMPTY_STRING_ARRAY;
  1075. } else {
  1076. char[] c = str.toCharArray();
  1077. List<String> list = new ArrayList();
  1078. int tokenStart = 0;
  1079. int currentType = Character.getType(c[tokenStart]);
  1080.  
  1081. for(int pos = tokenStart + 1; pos < c.length; ++pos) {
  1082. int type = Character.getType(c[pos]);
  1083. if (type != currentType) {
  1084. if (camelCase && type == 2 && currentType == 1) {
  1085. int newTokenStart = pos - 1;
  1086. if (newTokenStart != tokenStart) {
  1087. list.add(new String(c, tokenStart, newTokenStart - tokenStart));
  1088. tokenStart = newTokenStart;
  1089. }
  1090. } else {
  1091. list.add(new String(c, tokenStart, pos - tokenStart));
  1092. tokenStart = pos;
  1093. }
  1094.  
  1095. currentType = type;
  1096. }
  1097. }
  1098.  
  1099. list.add(new String(c, tokenStart, c.length - tokenStart));
  1100. return (String[])list.toArray(new String[list.size()]);
  1101. }
  1102. }
  1103.  
  1104. public static <T> String join(T... elements) {
  1105. return join((Object[])elements, (String)null);
  1106. }
  1107.  
  1108. public static String join(Object[] array, char separator) {
  1109. return array == null ? null : join((Object[])array, separator, 0, array.length);
  1110. }
  1111.  
  1112. public static String join(long[] array, char separator) {
  1113. return array == null ? null : join((long[])array, separator, 0, array.length);
  1114. }
  1115.  
  1116. public static String join(int[] array, char separator) {
  1117. return array == null ? null : join((int[])array, separator, 0, array.length);
  1118. }
  1119.  
  1120. public static String join(short[] array, char separator) {
  1121. return array == null ? null : join((short[])array, separator, 0, array.length);
  1122. }
  1123.  
  1124. public static String join(byte[] array, char separator) {
  1125. return array == null ? null : join((byte[])array, separator, 0, array.length);
  1126. }
  1127.  
  1128. public static String join(char[] array, char separator) {
  1129. return array == null ? null : join((char[])array, separator, 0, array.length);
  1130. }
  1131.  
  1132. public static String join(float[] array, char separator) {
  1133. return array == null ? null : join((float[])array, separator, 0, array.length);
  1134. }
  1135.  
  1136. public static String join(double[] array, char separator) {
  1137. return array == null ? null : join((double[])array, separator, 0, array.length);
  1138. }
  1139.  
  1140. public static String join(Object[] array, char separator, int startIndex, int endIndex) {
  1141. if (array == null) {
  1142. return null;
  1143. } else {
  1144. int noOfItems = endIndex - startIndex;
  1145. if (noOfItems <= 0) {
  1146. return "";
  1147. } else {
  1148. StringBuilder buf = new StringBuilder(noOfItems * 16);
  1149.  
  1150. for(int i = startIndex; i < endIndex; ++i) {
  1151. if (i > startIndex) {
  1152. buf.append(separator);
  1153. }
  1154.  
  1155. if (array[i] != null) {
  1156. buf.append(array[i]);
  1157. }
  1158. }
  1159.  
  1160. return buf.toString();
  1161. }
  1162. }
  1163. }
  1164.  
  1165. public static String join(long[] array, char separator, int startIndex, int endIndex) {
  1166. if (array == null) {
  1167. return null;
  1168. } else {
  1169. int noOfItems = endIndex - startIndex;
  1170. if (noOfItems <= 0) {
  1171. return "";
  1172. } else {
  1173. StringBuilder buf = new StringBuilder(noOfItems * 16);
  1174.  
  1175. for(int i = startIndex; i < endIndex; ++i) {
  1176. if (i > startIndex) {
  1177. buf.append(separator);
  1178. }
  1179.  
  1180. buf.append(array[i]);
  1181. }
  1182.  
  1183. return buf.toString();
  1184. }
  1185. }
  1186. }
  1187.  
  1188. public static String join(int[] array, char separator, int startIndex, int endIndex) {
  1189. if (array == null) {
  1190. return null;
  1191. } else {
  1192. int noOfItems = endIndex - startIndex;
  1193. if (noOfItems <= 0) {
  1194. return "";
  1195. } else {
  1196. StringBuilder buf = new StringBuilder(noOfItems * 16);
  1197.  
  1198. for(int i = startIndex; i < endIndex; ++i) {
  1199. if (i > startIndex) {
  1200. buf.append(separator);
  1201. }
  1202.  
  1203. buf.append(array[i]);
  1204. }
  1205.  
  1206. return buf.toString();
  1207. }
  1208. }
  1209. }
  1210.  
  1211. public static String join(byte[] array, char separator, int startIndex, int endIndex) {
  1212. if (array == null) {
  1213. return null;
  1214. } else {
  1215. int noOfItems = endIndex - startIndex;
  1216. if (noOfItems <= 0) {
  1217. return "";
  1218. } else {
  1219. StringBuilder buf = new StringBuilder(noOfItems * 16);
  1220.  
  1221. for(int i = startIndex; i < endIndex; ++i) {
  1222. if (i > startIndex) {
  1223. buf.append(separator);
  1224. }
  1225.  
  1226. buf.append(array[i]);
  1227. }
  1228.  
  1229. return buf.toString();
  1230. }
  1231. }
  1232. }
  1233.  
  1234. public static String join(short[] array, char separator, int startIndex, int endIndex) {
  1235. if (array == null) {
  1236. return null;
  1237. } else {
  1238. int noOfItems = endIndex - startIndex;
  1239. if (noOfItems <= 0) {
  1240. return "";
  1241. } else {
  1242. StringBuilder buf = new StringBuilder(noOfItems * 16);
  1243.  
  1244. for(int i = startIndex; i < endIndex; ++i) {
  1245. if (i > startIndex) {
  1246. buf.append(separator);
  1247. }
  1248.  
  1249. buf.append(array[i]);
  1250. }
  1251.  
  1252. return buf.toString();
  1253. }
  1254. }
  1255. }
  1256.  
  1257. public static String join(char[] array, char separator, int startIndex, int endIndex) {
  1258. if (array == null) {
  1259. return null;
  1260. } else {
  1261. int noOfItems = endIndex - startIndex;
  1262. if (noOfItems <= 0) {
  1263. return "";
  1264. } else {
  1265. StringBuilder buf = new StringBuilder(noOfItems * 16);
  1266.  
  1267. for(int i = startIndex; i < endIndex; ++i) {
  1268. if (i > startIndex) {
  1269. buf.append(separator);
  1270. }
  1271.  
  1272. buf.append(array[i]);
  1273. }
  1274.  
  1275. return buf.toString();
  1276. }
  1277. }
  1278. }
  1279.  
  1280. public static String join(double[] array, char separator, int startIndex, int endIndex) {
  1281. if (array == null) {
  1282. return null;
  1283. } else {
  1284. int noOfItems = endIndex - startIndex;
  1285. if (noOfItems <= 0) {
  1286. return "";
  1287. } else {
  1288. StringBuilder buf = new StringBuilder(noOfItems * 16);
  1289.  
  1290. for(int i = startIndex; i < endIndex; ++i) {
  1291. if (i > startIndex) {
  1292. buf.append(separator);
  1293. }
  1294.  
  1295. buf.append(array[i]);
  1296. }
  1297.  
  1298. return buf.toString();
  1299. }
  1300. }
  1301. }
  1302.  
  1303. public static String join(float[] array, char separator, int startIndex, int endIndex) {
  1304. if (array == null) {
  1305. return null;
  1306. } else {
  1307. int noOfItems = endIndex - startIndex;
  1308. if (noOfItems <= 0) {
  1309. return "";
  1310. } else {
  1311. StringBuilder buf = new StringBuilder(noOfItems * 16);
  1312.  
  1313. for(int i = startIndex; i < endIndex; ++i) {
  1314. if (i > startIndex) {
  1315. buf.append(separator);
  1316. }
  1317.  
  1318. buf.append(array[i]);
  1319. }
  1320.  
  1321. return buf.toString();
  1322. }
  1323. }
  1324. }
  1325.  
  1326. public static String join(Object[] array, String separator) {
  1327. return array == null ? null : join(array, separator, 0, array.length);
  1328. }
  1329.  
  1330. public static String join(Object[] array, String separator, int startIndex, int endIndex) {
  1331. if (array == null) {
  1332. return null;
  1333. } else {
  1334. if (separator == null) {
  1335. separator = "";
  1336. }
  1337.  
  1338. int noOfItems = endIndex - startIndex;
  1339. if (noOfItems <= 0) {
  1340. return "";
  1341. } else {
  1342. StringBuilder buf = new StringBuilder(noOfItems * 16);
  1343.  
  1344. for(int i = startIndex; i < endIndex; ++i) {
  1345. if (i > startIndex) {
  1346. buf.append(separator);
  1347. }
  1348.  
  1349. if (array[i] != null) {
  1350. buf.append(array[i]);
  1351. }
  1352. }
  1353.  
  1354. return buf.toString();
  1355. }
  1356. }
  1357. }
  1358.  
  1359. public static String join(Iterator<?> iterator, char separator) {
  1360. if (iterator == null) {
  1361. return null;
  1362. } else if (!iterator.hasNext()) {
  1363. return "";
  1364. } else {
  1365. Object first = iterator.next();
  1366. if (!iterator.hasNext()) {
  1367. return ObjectUtils.toString(first);
  1368. } else {
  1369. StringBuilder buf = new StringBuilder(256);
  1370. if (first != null) {
  1371. buf.append(first);
  1372. }
  1373.  
  1374. while(iterator.hasNext()) {
  1375. buf.append(separator);
  1376. Object obj = iterator.next();
  1377. if (obj != null) {
  1378. buf.append(obj);
  1379. }
  1380. }
  1381.  
  1382. return buf.toString();
  1383. }
  1384. }
  1385. }
  1386.  
  1387. public static String join(Iterator<?> iterator, String separator) {
  1388. if (iterator == null) {
  1389. return null;
  1390. } else if (!iterator.hasNext()) {
  1391. return "";
  1392. } else {
  1393. Object first = iterator.next();
  1394. if (!iterator.hasNext()) {
  1395. return ObjectUtils.toString(first);
  1396. } else {
  1397. StringBuilder buf = new StringBuilder(256);
  1398. if (first != null) {
  1399. buf.append(first);
  1400. }
  1401.  
  1402. while(iterator.hasNext()) {
  1403. if (separator != null) {
  1404. buf.append(separator);
  1405. }
  1406.  
  1407. Object obj = iterator.next();
  1408. if (obj != null) {
  1409. buf.append(obj);
  1410. }
  1411. }
  1412.  
  1413. return buf.toString();
  1414. }
  1415. }
  1416. }
  1417.  
  1418. public static String join(Iterable<?> iterable, char separator) {
  1419. return iterable == null ? null : join(iterable.iterator(), separator);
  1420. }
  1421.  
  1422. public static String join(Iterable<?> iterable, String separator) {
  1423. return iterable == null ? null : join(iterable.iterator(), separator);
  1424. }
  1425.  
  1426. public static String deleteWhitespace(String str) {
  1427. if (isEmpty(str)) {
  1428. return str;
  1429. } else {
  1430. int sz = str.length();
  1431. char[] chs = new char[sz];
  1432. int count = 0;
  1433.  
  1434. for(int i = 0; i < sz; ++i) {
  1435. if (!Character.isWhitespace(str.charAt(i))) {
  1436. chs[count++] = str.charAt(i);
  1437. }
  1438. }
  1439.  
  1440. if (count == sz) {
  1441. return str;
  1442. } else {
  1443. return new String(chs, 0, count);
  1444. }
  1445. }
  1446. }
  1447.  
  1448. public static String removeStart(String str, String remove) {
  1449. if (!isEmpty(str) && !isEmpty(remove)) {
  1450. return str.startsWith(remove) ? str.substring(remove.length()) : str;
  1451. } else {
  1452. return str;
  1453. }
  1454. }
  1455.  
  1456. public static String removeStartIgnoreCase(String str, String remove) {
  1457. if (!isEmpty(str) && !isEmpty(remove)) {
  1458. return startsWithIgnoreCase(str, remove) ? str.substring(remove.length()) : str;
  1459. } else {
  1460. return str;
  1461. }
  1462. }
  1463.  
  1464. public static String removeEnd(String str, String remove) {
  1465. if (!isEmpty(str) && !isEmpty(remove)) {
  1466. return str.endsWith(remove) ? str.substring(0, str.length() - remove.length()) : str;
  1467. } else {
  1468. return str;
  1469. }
  1470. }
  1471.  
  1472. public static String removeEndIgnoreCase(String str, String remove) {
  1473. if (!isEmpty(str) && !isEmpty(remove)) {
  1474. return endsWithIgnoreCase(str, remove) ? str.substring(0, str.length() - remove.length()) : str;
  1475. } else {
  1476. return str;
  1477. }
  1478. }
  1479.  
  1480. public static String remove(String str, String remove) {
  1481. return !isEmpty(str) && !isEmpty(remove) ? replace(str, remove, "", -1) : str;
  1482. }
  1483.  
  1484. public static String remove(String str, char remove) {
  1485. if (!isEmpty(str) && str.indexOf(remove) != -1) {
  1486. char[] chars = str.toCharArray();
  1487. int pos = 0;
  1488.  
  1489. for(int i = 0; i < chars.length; ++i) {
  1490. if (chars[i] != remove) {
  1491. chars[pos++] = chars[i];
  1492. }
  1493. }
  1494.  
  1495. return new String(chars, 0, pos);
  1496. } else {
  1497. return str;
  1498. }
  1499. }
  1500.  
  1501. public static String replaceOnce(String text, String searchString, String replacement) {
  1502. return replace(text, searchString, replacement, 1);
  1503. }
  1504.  
  1505. public static String replacePattern(String source, String regex, String replacement) {
  1506. return Pattern.compile(regex, 32).matcher(source).replaceAll(replacement);
  1507. }
  1508.  
  1509. public static String removePattern(String source, String regex) {
  1510. return replacePattern(source, regex, "");
  1511. }
  1512.  
  1513. public static String replace(String text, String searchString, String replacement) {
  1514. return replace(text, searchString, replacement, -1);
  1515. }
  1516.  
  1517. public static String replace(String text, String searchString, String replacement, int max) {
  1518. if (!isEmpty(text) && !isEmpty(searchString) && replacement != null && max != 0) {
  1519. int start = 0;
  1520. int end = text.indexOf(searchString, start);
  1521. if (end == -1) {
  1522. return text;
  1523. } else {
  1524. int replLength = searchString.length();
  1525. int increase = replacement.length() - replLength;
  1526. increase = increase < 0 ? 0 : increase;
  1527. increase *= max < 0 ? 16 : (max > 64 ? 64 : max);
  1528.  
  1529. StringBuilder buf;
  1530. for(buf = new StringBuilder(text.length() + increase); end != -1; end = text.indexOf(searchString, start)) {
  1531. buf.append(text.substring(start, end)).append(replacement);
  1532. start = end + replLength;
  1533. --max;
  1534. if (max == 0) {
  1535. break;
  1536. }
  1537. }
  1538.  
  1539. buf.append(text.substring(start));
  1540. return buf.toString();
  1541. }
  1542. } else {
  1543. return text;
  1544. }
  1545. }
  1546.  
  1547. public static String replaceEach(String text, String[] searchList, String[] replacementList) {
  1548. return replaceEach(text, searchList, replacementList, false, 0);
  1549. }
  1550.  
  1551. public static String replaceEachRepeatedly(String text, String[] searchList, String[] replacementList) {
  1552. int timeToLive = searchList == null ? 0 : searchList.length;
  1553. return replaceEach(text, searchList, replacementList, true, timeToLive);
  1554. }
  1555.  
  1556. private static String replaceEach(String text, String[] searchList, String[] replacementList, boolean repeat, int timeToLive) {
  1557. if (text != null && !text.isEmpty() && searchList != null && searchList.length != 0 && replacementList != null && replacementList.length != 0) {
  1558. if (timeToLive < 0) {
  1559. throw new IllegalStateException("Aborting to protect against StackOverflowError - output of one loop is the input of another");
  1560. } else {
  1561. int searchLength = searchList.length;
  1562. int replacementLength = replacementList.length;
  1563. if (searchLength != replacementLength) {
  1564. throw new IllegalArgumentException("Search and Replace array lengths don't match: " + searchLength + " vs " + replacementLength);
  1565. } else {
  1566. boolean[] noMoreMatchesForReplIndex = new boolean[searchLength];
  1567. int textIndex = -1;
  1568. int replaceIndex = -1;
  1569. int tempIndex = true;
  1570.  
  1571. int start;
  1572. int tempIndex;
  1573. for(start = 0; start < searchLength; ++start) {
  1574. if (!noMoreMatchesForReplIndex[start] && searchList[start] != null && !searchList[start].isEmpty() && replacementList[start] != null) {
  1575. tempIndex = text.indexOf(searchList[start]);
  1576. if (tempIndex == -1) {
  1577. noMoreMatchesForReplIndex[start] = true;
  1578. } else if (textIndex == -1 || tempIndex < textIndex) {
  1579. textIndex = tempIndex;
  1580. replaceIndex = start;
  1581. }
  1582. }
  1583. }
  1584.  
  1585. if (textIndex == -1) {
  1586. return text;
  1587. } else {
  1588. start = 0;
  1589. int increase = 0;
  1590.  
  1591. int i;
  1592. for(int i = 0; i < searchList.length; ++i) {
  1593. if (searchList[i] != null && replacementList[i] != null) {
  1594. i = replacementList[i].length() - searchList[i].length();
  1595. if (i > 0) {
  1596. increase += 3 * i;
  1597. }
  1598. }
  1599. }
  1600.  
  1601. increase = Math.min(increase, text.length() / 5);
  1602. StringBuilder buf = new StringBuilder(text.length() + increase);
  1603.  
  1604. while(textIndex != -1) {
  1605. for(i = start; i < textIndex; ++i) {
  1606. buf.append(text.charAt(i));
  1607. }
  1608.  
  1609. buf.append(replacementList[replaceIndex]);
  1610. start = textIndex + searchList[replaceIndex].length();
  1611. textIndex = -1;
  1612. replaceIndex = -1;
  1613. tempIndex = true;
  1614.  
  1615. for(i = 0; i < searchLength; ++i) {
  1616. if (!noMoreMatchesForReplIndex[i] && searchList[i] != null && !searchList[i].isEmpty() && replacementList[i] != null) {
  1617. tempIndex = text.indexOf(searchList[i], start);
  1618. if (tempIndex == -1) {
  1619. noMoreMatchesForReplIndex[i] = true;
  1620. } else if (textIndex == -1 || tempIndex < textIndex) {
  1621. textIndex = tempIndex;
  1622. replaceIndex = i;
  1623. }
  1624. }
  1625. }
  1626. }
  1627.  
  1628. i = text.length();
  1629.  
  1630. for(int i = start; i < i; ++i) {
  1631. buf.append(text.charAt(i));
  1632. }
  1633.  
  1634. String result = buf.toString();
  1635. if (!repeat) {
  1636. return result;
  1637. } else {
  1638. return replaceEach(result, searchList, replacementList, repeat, timeToLive - 1);
  1639. }
  1640. }
  1641. }
  1642. }
  1643. } else {
  1644. return text;
  1645. }
  1646. }
  1647.  
  1648. public static String replaceChars(String str, char searchChar, char replaceChar) {
  1649. return str == null ? null : str.replace(searchChar, replaceChar);
  1650. }
  1651.  
  1652. public static String replaceChars(String str, String searchChars, String replaceChars) {
  1653. if (!isEmpty(str) && !isEmpty(searchChars)) {
  1654. if (replaceChars == null) {
  1655. replaceChars = "";
  1656. }
  1657.  
  1658. boolean modified = false;
  1659. int replaceCharsLength = replaceChars.length();
  1660. int strLength = str.length();
  1661. StringBuilder buf = new StringBuilder(strLength);
  1662.  
  1663. for(int i = 0; i < strLength; ++i) {
  1664. char ch = str.charAt(i);
  1665. int index = searchChars.indexOf(ch);
  1666. if (index >= 0) {
  1667. modified = true;
  1668. if (index < replaceCharsLength) {
  1669. buf.append(replaceChars.charAt(index));
  1670. }
  1671. } else {
  1672. buf.append(ch);
  1673. }
  1674. }
  1675.  
  1676. if (modified) {
  1677. return buf.toString();
  1678. } else {
  1679. return str;
  1680. }
  1681. } else {
  1682. return str;
  1683. }
  1684. }
  1685.  
  1686. public static String overlay(String str, String overlay, int start, int end) {
  1687. if (str == null) {
  1688. return null;
  1689. } else {
  1690. if (overlay == null) {
  1691. overlay = "";
  1692. }
  1693.  
  1694. int len = str.length();
  1695. if (start < 0) {
  1696. start = 0;
  1697. }
  1698.  
  1699. if (start > len) {
  1700. start = len;
  1701. }
  1702.  
  1703. if (end < 0) {
  1704. end = 0;
  1705. }
  1706.  
  1707. if (end > len) {
  1708. end = len;
  1709. }
  1710.  
  1711. if (start > end) {
  1712. int temp = start;
  1713. start = end;
  1714. end = temp;
  1715. }
  1716.  
  1717. return (new StringBuilder(len + start - end + overlay.length() + 1)).append(str.substring(0, start)).append(overlay).append(str.substring(end)).toString();
  1718. }
  1719. }
  1720.  
  1721. public static String chomp(String str) {
  1722. if (isEmpty(str)) {
  1723. return str;
  1724. } else if (str.length() == 1) {
  1725. char ch = str.charAt(0);
  1726. return ch != '\r' && ch != '\n' ? str : "";
  1727. } else {
  1728. int lastIdx = str.length() - 1;
  1729. char last = str.charAt(lastIdx);
  1730. if (last == '\n') {
  1731. if (str.charAt(lastIdx - 1) == '\r') {
  1732. --lastIdx;
  1733. }
  1734. } else if (last != '\r') {
  1735. ++lastIdx;
  1736. }
  1737.  
  1738. return str.substring(0, lastIdx);
  1739. }
  1740. }
  1741.  
  1742. @Deprecated
  1743. public static String chomp(String str, String separator) {
  1744. return removeEnd(str, separator);
  1745. }
  1746.  
  1747. public static String chop(String str) {
  1748. if (str == null) {
  1749. return null;
  1750. } else {
  1751. int strLen = str.length();
  1752. if (strLen < 2) {
  1753. return "";
  1754. } else {
  1755. int lastIdx = strLen - 1;
  1756. String ret = str.substring(0, lastIdx);
  1757. char last = str.charAt(lastIdx);
  1758. return last == '\n' && ret.charAt(lastIdx - 1) == '\r' ? ret.substring(0, lastIdx - 1) : ret;
  1759. }
  1760. }
  1761. }
  1762.  
  1763. public static String repeat(String str, int repeat) {
  1764. if (str == null) {
  1765. return null;
  1766. } else if (repeat <= 0) {
  1767. return "";
  1768. } else {
  1769. int inputLength = str.length();
  1770. if (repeat != 1 && inputLength != 0) {
  1771. if (inputLength == 1 && repeat <= 8192) {
  1772. return repeat(str.charAt(0), repeat);
  1773. } else {
  1774. int outputLength = inputLength * repeat;
  1775. switch(inputLength) {
  1776. case 1:
  1777. return repeat(str.charAt(0), repeat);
  1778. case 2:
  1779. char ch0 = str.charAt(0);
  1780. char ch1 = str.charAt(1);
  1781. char[] output2 = new char[outputLength];
  1782.  
  1783. for(int i = repeat * 2 - 2; i >= 0; --i) {
  1784. output2[i] = ch0;
  1785. output2[i + 1] = ch1;
  1786. --i;
  1787. }
  1788.  
  1789. return new String(output2);
  1790. default:
  1791. StringBuilder buf = new StringBuilder(outputLength);
  1792.  
  1793. for(int i = 0; i < repeat; ++i) {
  1794. buf.append(str);
  1795. }
  1796.  
  1797. return buf.toString();
  1798. }
  1799. }
  1800. } else {
  1801. return str;
  1802. }
  1803. }
  1804. }
  1805.  
  1806. public static String repeat(String str, String separator, int repeat) {
  1807. if (str != null && separator != null) {
  1808. String result = repeat(str + separator, repeat);
  1809. return removeEnd(result, separator);
  1810. } else {
  1811. return repeat(str, repeat);
  1812. }
  1813. }
  1814.  
  1815. public static String repeat(char ch, int repeat) {
  1816. char[] buf = new char[repeat];
  1817.  
  1818. for(int i = repeat - 1; i >= 0; --i) {
  1819. buf[i] = ch;
  1820. }
  1821.  
  1822. return new String(buf);
  1823. }
  1824.  
  1825. public static String rightPad(String str, int size) {
  1826. return rightPad(str, size, ' ');
  1827. }
  1828.  
  1829. public static String rightPad(String str, int size, char padChar) {
  1830. if (str == null) {
  1831. return null;
  1832. } else {
  1833. int pads = size - str.length();
  1834. if (pads <= 0) {
  1835. return str;
  1836. } else {
  1837. return pads > 8192 ? rightPad(str, size, String.valueOf(padChar)) : str.concat(repeat(padChar, pads));
  1838. }
  1839. }
  1840. }
  1841.  
  1842. public static String rightPad(String str, int size, String padStr) {
  1843. if (str == null) {
  1844. return null;
  1845. } else {
  1846. if (isEmpty(padStr)) {
  1847. padStr = " ";
  1848. }
  1849.  
  1850. int padLen = padStr.length();
  1851. int strLen = str.length();
  1852. int pads = size - strLen;
  1853. if (pads <= 0) {
  1854. return str;
  1855. } else if (padLen == 1 && pads <= 8192) {
  1856. return rightPad(str, size, padStr.charAt(0));
  1857. } else if (pads == padLen) {
  1858. return str.concat(padStr);
  1859. } else if (pads < padLen) {
  1860. return str.concat(padStr.substring(0, pads));
  1861. } else {
  1862. char[] padding = new char[pads];
  1863. char[] padChars = padStr.toCharArray();
  1864.  
  1865. for(int i = 0; i < pads; ++i) {
  1866. padding[i] = padChars[i % padLen];
  1867. }
  1868.  
  1869. return str.concat(new String(padding));
  1870. }
  1871. }
  1872. }
  1873.  
  1874. public static String leftPad(String str, int size) {
  1875. return leftPad(str, size, ' ');
  1876. }
  1877.  
  1878. public static String leftPad(String str, int size, char padChar) {
  1879. if (str == null) {
  1880. return null;
  1881. } else {
  1882. int pads = size - str.length();
  1883. if (pads <= 0) {
  1884. return str;
  1885. } else {
  1886. return pads > 8192 ? leftPad(str, size, String.valueOf(padChar)) : repeat(padChar, pads).concat(str);
  1887. }
  1888. }
  1889. }
  1890.  
  1891. public static String leftPad(String str, int size, String padStr) {
  1892. if (str == null) {
  1893. return null;
  1894. } else {
  1895. if (isEmpty(padStr)) {
  1896. padStr = " ";
  1897. }
  1898.  
  1899. int padLen = padStr.length();
  1900. int strLen = str.length();
  1901. int pads = size - strLen;
  1902. if (pads <= 0) {
  1903. return str;
  1904. } else if (padLen == 1 && pads <= 8192) {
  1905. return leftPad(str, size, padStr.charAt(0));
  1906. } else if (pads == padLen) {
  1907. return padStr.concat(str);
  1908. } else if (pads < padLen) {
  1909. return padStr.substring(0, pads).concat(str);
  1910. } else {
  1911. char[] padding = new char[pads];
  1912. char[] padChars = padStr.toCharArray();
  1913.  
  1914. for(int i = 0; i < pads; ++i) {
  1915. padding[i] = padChars[i % padLen];
  1916. }
  1917.  
  1918. return (new String(padding)).concat(str);
  1919. }
  1920. }
  1921. }
  1922.  
  1923. public static int length(CharSequence cs) {
  1924. return cs == null ? 0 : cs.length();
  1925. }
  1926.  
  1927. public static String center(String str, int size) {
  1928. return center(str, size, ' ');
  1929. }
  1930.  
  1931. public static String center(String str, int size, char padChar) {
  1932. if (str != null && size > 0) {
  1933. int strLen = str.length();
  1934. int pads = size - strLen;
  1935. if (pads <= 0) {
  1936. return str;
  1937. } else {
  1938. str = leftPad(str, strLen + pads / 2, padChar);
  1939. str = rightPad(str, size, padChar);
  1940. return str;
  1941. }
  1942. } else {
  1943. return str;
  1944. }
  1945. }
  1946.  
  1947. public static String center(String str, int size, String padStr) {
  1948. if (str != null && size > 0) {
  1949. if (isEmpty(padStr)) {
  1950. padStr = " ";
  1951. }
  1952.  
  1953. int strLen = str.length();
  1954. int pads = size - strLen;
  1955. if (pads <= 0) {
  1956. return str;
  1957. } else {
  1958. str = leftPad(str, strLen + pads / 2, padStr);
  1959. str = rightPad(str, size, padStr);
  1960. return str;
  1961. }
  1962. } else {
  1963. return str;
  1964. }
  1965. }
  1966.  
  1967. public static String upperCase(String str) {
  1968. return str == null ? null : str.toUpperCase();
  1969. }
  1970.  
  1971. public static String upperCase(String str, Locale locale) {
  1972. return str == null ? null : str.toUpperCase(locale);
  1973. }
  1974.  
  1975. public static String lowerCase(String str) {
  1976. return str == null ? null : str.toLowerCase();
  1977. }
  1978.  
  1979. public static String lowerCase(String str, Locale locale) {
  1980. return str == null ? null : str.toLowerCase(locale);
  1981. }
  1982.  
  1983. public static String capitalize(String str) {
  1984. int strLen;
  1985. if (str != null && (strLen = str.length()) != 0) {
  1986. char firstChar = str.charAt(0);
  1987. return Character.isTitleCase(firstChar) ? str : (new StringBuilder(strLen)).append(Character.toTitleCase(firstChar)).append(str.substring(1)).toString();
  1988. } else {
  1989. return str;
  1990. }
  1991. }
  1992.  
  1993. public static String uncapitalize(String str) {
  1994. int strLen;
  1995. if (str != null && (strLen = str.length()) != 0) {
  1996. char firstChar = str.charAt(0);
  1997. return Character.isLowerCase(firstChar) ? str : (new StringBuilder(strLen)).append(Character.toLowerCase(firstChar)).append(str.substring(1)).toString();
  1998. } else {
  1999. return str;
  2000. }
  2001. }
  2002.  
  2003. public static String swapCase(String str) {
  2004. if (isEmpty(str)) {
  2005. return str;
  2006. } else {
  2007. char[] buffer = str.toCharArray();
  2008.  
  2009. for(int i = 0; i < buffer.length; ++i) {
  2010. char ch = buffer[i];
  2011. if (Character.isUpperCase(ch)) {
  2012. buffer[i] = Character.toLowerCase(ch);
  2013. } else if (Character.isTitleCase(ch)) {
  2014. buffer[i] = Character.toLowerCase(ch);
  2015. } else if (Character.isLowerCase(ch)) {
  2016. buffer[i] = Character.toUpperCase(ch);
  2017. }
  2018. }
  2019.  
  2020. return new String(buffer);
  2021. }
  2022. }
  2023.  
  2024. public static int countMatches(CharSequence str, CharSequence sub) {
  2025. if (!isEmpty(str) && !isEmpty(sub)) {
  2026. int count = 0;
  2027.  
  2028. for(int idx = 0; (idx = CharSequenceUtils.indexOf(str, sub, idx)) != -1; idx += sub.length()) {
  2029. ++count;
  2030. }
  2031.  
  2032. return count;
  2033. } else {
  2034. return 0;
  2035. }
  2036. }
  2037.  
  2038. public static boolean isAlpha(CharSequence cs) {
  2039. if (isEmpty(cs)) {
  2040. return false;
  2041. } else {
  2042. int sz = cs.length();
  2043.  
  2044. for(int i = 0; i < sz; ++i) {
  2045. if (!Character.isLetter(cs.charAt(i))) {
  2046. return false;
  2047. }
  2048. }
  2049.  
  2050. return true;
  2051. }
  2052. }
  2053.  
  2054. public static boolean isAlphaSpace(CharSequence cs) {
  2055. if (cs == null) {
  2056. return false;
  2057. } else {
  2058. int sz = cs.length();
  2059.  
  2060. for(int i = 0; i < sz; ++i) {
  2061. if (!Character.isLetter(cs.charAt(i)) && cs.charAt(i) != ' ') {
  2062. return false;
  2063. }
  2064. }
  2065.  
  2066. return true;
  2067. }
  2068. }
  2069.  
  2070. public static boolean isAlphanumeric(CharSequence cs) {
  2071. if (isEmpty(cs)) {
  2072. return false;
  2073. } else {
  2074. int sz = cs.length();
  2075.  
  2076. for(int i = 0; i < sz; ++i) {
  2077. if (!Character.isLetterOrDigit(cs.charAt(i))) {
  2078. return false;
  2079. }
  2080. }
  2081.  
  2082. return true;
  2083. }
  2084. }
  2085.  
  2086. public static boolean isAlphanumericSpace(CharSequence cs) {
  2087. if (cs == null) {
  2088. return false;
  2089. } else {
  2090. int sz = cs.length();
  2091.  
  2092. for(int i = 0; i < sz; ++i) {
  2093. if (!Character.isLetterOrDigit(cs.charAt(i)) && cs.charAt(i) != ' ') {
  2094. return false;
  2095. }
  2096. }
  2097.  
  2098. return true;
  2099. }
  2100. }
  2101.  
  2102. public static boolean isAsciiPrintable(CharSequence cs) {
  2103. if (cs == null) {
  2104. return false;
  2105. } else {
  2106. int sz = cs.length();
  2107.  
  2108. for(int i = 0; i < sz; ++i) {
  2109. if (!CharUtils.isAsciiPrintable(cs.charAt(i))) {
  2110. return false;
  2111. }
  2112. }
  2113.  
  2114. return true;
  2115. }
  2116. }
  2117.  
  2118. public static boolean isNumeric(CharSequence cs) {
  2119. if (isEmpty(cs)) {
  2120. return false;
  2121. } else {
  2122. int sz = cs.length();
  2123.  
  2124. for(int i = 0; i < sz; ++i) {
  2125. if (!Character.isDigit(cs.charAt(i))) {
  2126. return false;
  2127. }
  2128. }
  2129.  
  2130. return true;
  2131. }
  2132. }
  2133.  
  2134. public static boolean isNumericSpace(CharSequence cs) {
  2135. if (cs == null) {
  2136. return false;
  2137. } else {
  2138. int sz = cs.length();
  2139.  
  2140. for(int i = 0; i < sz; ++i) {
  2141. if (!Character.isDigit(cs.charAt(i)) && cs.charAt(i) != ' ') {
  2142. return false;
  2143. }
  2144. }
  2145.  
  2146. return true;
  2147. }
  2148. }
  2149.  
  2150. public static boolean isWhitespace(CharSequence cs) {
  2151. if (cs == null) {
  2152. return false;
  2153. } else {
  2154. int sz = cs.length();
  2155.  
  2156. for(int i = 0; i < sz; ++i) {
  2157. if (!Character.isWhitespace(cs.charAt(i))) {
  2158. return false;
  2159. }
  2160. }
  2161.  
  2162. return true;
  2163. }
  2164. }
  2165.  
  2166. public static boolean isAllLowerCase(CharSequence cs) {
  2167. if (cs != null && !isEmpty(cs)) {
  2168. int sz = cs.length();
  2169.  
  2170. for(int i = 0; i < sz; ++i) {
  2171. if (!Character.isLowerCase(cs.charAt(i))) {
  2172. return false;
  2173. }
  2174. }
  2175.  
  2176. return true;
  2177. } else {
  2178. return false;
  2179. }
  2180. }
  2181.  
  2182. public static boolean isAllUpperCase(CharSequence cs) {
  2183. if (cs != null && !isEmpty(cs)) {
  2184. int sz = cs.length();
  2185.  
  2186. for(int i = 0; i < sz; ++i) {
  2187. if (!Character.isUpperCase(cs.charAt(i))) {
  2188. return false;
  2189. }
  2190. }
  2191.  
  2192. return true;
  2193. } else {
  2194. return false;
  2195. }
  2196. }
  2197.  
  2198. public static String defaultString(String str) {
  2199. return str == null ? "" : str;
  2200. }
  2201.  
  2202. public static String defaultString(String str, String defaultStr) {
  2203. return str == null ? defaultStr : str;
  2204. }
  2205.  
  2206. public static <T extends CharSequence> T defaultIfBlank(T str, T defaultStr) {
  2207. return isBlank(str) ? defaultStr : str;
  2208. }
  2209.  
  2210. public static <T extends CharSequence> T defaultIfEmpty(T str, T defaultStr) {
  2211. return isEmpty(str) ? defaultStr : str;
  2212. }
  2213.  
  2214. public static String reverse(String str) {
  2215. return str == null ? null : (new StringBuilder(str)).reverse().toString();
  2216. }
  2217.  
  2218. public static String reverseDelimited(String str, char separatorChar) {
  2219. if (str == null) {
  2220. return null;
  2221. } else {
  2222. String[] strs = split(str, separatorChar);
  2223. ArrayUtils.reverse(strs);
  2224. return join((Object[])strs, separatorChar);
  2225. }
  2226. }
  2227.  
  2228. public static String abbreviate(String str, int maxWidth) {
  2229. return abbreviate(str, 0, maxWidth);
  2230. }
  2231.  
  2232. public static String abbreviate(String str, int offset, int maxWidth) {
  2233. if (str == null) {
  2234. return null;
  2235. } else if (maxWidth < 4) {
  2236. throw new IllegalArgumentException("Minimum abbreviation width is 4");
  2237. } else if (str.length() <= maxWidth) {
  2238. return str;
  2239. } else {
  2240. if (offset > str.length()) {
  2241. offset = str.length();
  2242. }
  2243.  
  2244. if (str.length() - offset < maxWidth - 3) {
  2245. offset = str.length() - (maxWidth - 3);
  2246. }
  2247.  
  2248. String abrevMarker = "...";
  2249. if (offset <= 4) {
  2250. return str.substring(0, maxWidth - 3) + "...";
  2251. } else if (maxWidth < 7) {
  2252. throw new IllegalArgumentException("Minimum abbreviation width with offset is 7");
  2253. } else {
  2254. return offset + maxWidth - 3 < str.length() ? "..." + abbreviate(str.substring(offset), maxWidth - 3) : "..." + str.substring(str.length() - (maxWidth - 3));
  2255. }
  2256. }
  2257. }
  2258.  
  2259. public static String abbreviateMiddle(String str, String middle, int length) {
  2260. if (!isEmpty(str) && !isEmpty(middle)) {
  2261. if (length < str.length() && length >= middle.length() + 2) {
  2262. int targetSting = length - middle.length();
  2263. int startOffset = targetSting / 2 + targetSting % 2;
  2264. int endOffset = str.length() - targetSting / 2;
  2265. StringBuilder builder = new StringBuilder(length);
  2266. builder.append(str.substring(0, startOffset));
  2267. builder.append(middle);
  2268. builder.append(str.substring(endOffset));
  2269. return builder.toString();
  2270. } else {
  2271. return str;
  2272. }
  2273. } else {
  2274. return str;
  2275. }
  2276. }
  2277.  
  2278. public static String difference(String str1, String str2) {
  2279. if (str1 == null) {
  2280. return str2;
  2281. } else if (str2 == null) {
  2282. return str1;
  2283. } else {
  2284. int at = indexOfDifference(str1, str2);
  2285. return at == -1 ? "" : str2.substring(at);
  2286. }
  2287. }
  2288.  
  2289. public static int indexOfDifference(CharSequence cs1, CharSequence cs2) {
  2290. if (cs1 == cs2) {
  2291. return -1;
  2292. } else if (cs1 != null && cs2 != null) {
  2293. int i;
  2294. for(i = 0; i < cs1.length() && i < cs2.length() && cs1.charAt(i) == cs2.charAt(i); ++i) {
  2295. }
  2296.  
  2297. return i >= cs2.length() && i >= cs1.length() ? -1 : i;
  2298. } else {
  2299. return 0;
  2300. }
  2301. }
  2302.  
  2303. public static int indexOfDifference(CharSequence... css) {
  2304. if (css != null && css.length > 1) {
  2305. boolean anyStringNull = false;
  2306. boolean allStringsNull = true;
  2307. int arrayLen = css.length;
  2308. int shortestStrLen = Integer.MAX_VALUE;
  2309. int longestStrLen = 0;
  2310.  
  2311. int firstDiff;
  2312. for(firstDiff = 0; firstDiff < arrayLen; ++firstDiff) {
  2313. if (css[firstDiff] == null) {
  2314. anyStringNull = true;
  2315. shortestStrLen = 0;
  2316. } else {
  2317. allStringsNull = false;
  2318. shortestStrLen = Math.min(css[firstDiff].length(), shortestStrLen);
  2319. longestStrLen = Math.max(css[firstDiff].length(), longestStrLen);
  2320. }
  2321. }
  2322.  
  2323. if (allStringsNull || longestStrLen == 0 && !anyStringNull) {
  2324. return -1;
  2325. } else if (shortestStrLen == 0) {
  2326. return 0;
  2327. } else {
  2328. firstDiff = -1;
  2329.  
  2330. for(int stringPos = 0; stringPos < shortestStrLen; ++stringPos) {
  2331. char comparisonChar = css[0].charAt(stringPos);
  2332.  
  2333. for(int arrayPos = 1; arrayPos < arrayLen; ++arrayPos) {
  2334. if (css[arrayPos].charAt(stringPos) != comparisonChar) {
  2335. firstDiff = stringPos;
  2336. break;
  2337. }
  2338. }
  2339.  
  2340. if (firstDiff != -1) {
  2341. break;
  2342. }
  2343. }
  2344.  
  2345. return firstDiff == -1 && shortestStrLen != longestStrLen ? shortestStrLen : firstDiff;
  2346. }
  2347. } else {
  2348. return -1;
  2349. }
  2350. }
  2351.  
  2352. public static String getCommonPrefix(String... strs) {
  2353. if (strs != null && strs.length != 0) {
  2354. int smallestIndexOfDiff = indexOfDifference(strs);
  2355. if (smallestIndexOfDiff == -1) {
  2356. return strs[0] == null ? "" : strs[0];
  2357. } else {
  2358. return smallestIndexOfDiff == 0 ? "" : strs[0].substring(0, smallestIndexOfDiff);
  2359. }
  2360. } else {
  2361. return "";
  2362. }
  2363. }
  2364.  
  2365. public static int getLevenshteinDistance(CharSequence s, CharSequence t) {
  2366. if (s != null && t != null) {
  2367. int n = s.length();
  2368. int m = t.length();
  2369. if (n == 0) {
  2370. return m;
  2371. } else if (m == 0) {
  2372. return n;
  2373. } else {
  2374. if (n > m) {
  2375. CharSequence tmp = s;
  2376. s = t;
  2377. t = tmp;
  2378. n = m;
  2379. m = tmp.length();
  2380. }
  2381.  
  2382. int[] p = new int[n + 1];
  2383. int[] d = new int[n + 1];
  2384.  
  2385. int i;
  2386. for(i = 0; i <= n; p[i] = i++) {
  2387. }
  2388.  
  2389. for(int j = 1; j <= m; ++j) {
  2390. char t_j = t.charAt(j - 1);
  2391. d[0] = j;
  2392.  
  2393. for(i = 1; i <= n; ++i) {
  2394. int cost = s.charAt(i - 1) == t_j ? 0 : 1;
  2395. d[i] = Math.min(Math.min(d[i - 1] + 1, p[i] + 1), p[i - 1] + cost);
  2396. }
  2397.  
  2398. int[] _d = p;
  2399. p = d;
  2400. d = _d;
  2401. }
  2402.  
  2403. return p[n];
  2404. }
  2405. } else {
  2406. throw new IllegalArgumentException("Strings must not be null");
  2407. }
  2408. }
  2409.  
  2410. public static int getLevenshteinDistance(CharSequence s, CharSequence t, int threshold) {
  2411. if (s != null && t != null) {
  2412. if (threshold < 0) {
  2413. throw new IllegalArgumentException("Threshold must not be negative");
  2414. } else {
  2415. int n = s.length();
  2416. int m = t.length();
  2417. if (n == 0) {
  2418. return m <= threshold ? m : -1;
  2419. } else if (m == 0) {
  2420. return n <= threshold ? n : -1;
  2421. } else {
  2422. if (n > m) {
  2423. CharSequence tmp = s;
  2424. s = t;
  2425. t = tmp;
  2426. n = m;
  2427. m = tmp.length();
  2428. }
  2429.  
  2430. int[] p = new int[n + 1];
  2431. int[] d = new int[n + 1];
  2432. int boundary = Math.min(n, threshold) + 1;
  2433.  
  2434. int j;
  2435. for(j = 0; j < boundary; p[j] = j++) {
  2436. }
  2437.  
  2438. Arrays.fill(p, boundary, p.length, Integer.MAX_VALUE);
  2439. Arrays.fill(d, Integer.MAX_VALUE);
  2440.  
  2441. for(j = 1; j <= m; ++j) {
  2442. char t_j = t.charAt(j - 1);
  2443. d[0] = j;
  2444. int min = Math.max(1, j - threshold);
  2445. int max = Math.min(n, j + threshold);
  2446. if (min > max) {
  2447. return -1;
  2448. }
  2449.  
  2450. if (min > 1) {
  2451. d[min - 1] = Integer.MAX_VALUE;
  2452. }
  2453.  
  2454. for(int i = min; i <= max; ++i) {
  2455. if (s.charAt(i - 1) == t_j) {
  2456. d[i] = p[i - 1];
  2457. } else {
  2458. d[i] = 1 + Math.min(Math.min(d[i - 1], p[i]), p[i - 1]);
  2459. }
  2460. }
  2461.  
  2462. int[] _d = p;
  2463. p = d;
  2464. d = _d;
  2465. }
  2466.  
  2467. if (p[n] <= threshold) {
  2468. return p[n];
  2469. } else {
  2470. return -1;
  2471. }
  2472. }
  2473. }
  2474. } else {
  2475. throw new IllegalArgumentException("Strings must not be null");
  2476. }
  2477. }
  2478.  
  2479. public static boolean startsWith(CharSequence str, CharSequence prefix) {
  2480. return startsWith(str, prefix, false);
  2481. }
  2482.  
  2483. public static boolean startsWithIgnoreCase(CharSequence str, CharSequence prefix) {
  2484. return startsWith(str, prefix, true);
  2485. }
  2486.  
  2487. private static boolean startsWith(CharSequence str, CharSequence prefix, boolean ignoreCase) {
  2488. if (str != null && prefix != null) {
  2489. return prefix.length() > str.length() ? false : CharSequenceUtils.regionMatches(str, ignoreCase, 0, prefix, 0, prefix.length());
  2490. } else {
  2491. return str == null && prefix == null;
  2492. }
  2493. }
  2494.  
  2495. public static boolean startsWithAny(CharSequence string, CharSequence... searchStrings) {
  2496. if (!isEmpty(string) && !ArrayUtils.isEmpty(searchStrings)) {
  2497. CharSequence[] arr$ = searchStrings;
  2498. int len$ = searchStrings.length;
  2499.  
  2500. for(int i$ = 0; i$ < len$; ++i$) {
  2501. CharSequence searchString = arr$[i$];
  2502. if (startsWith(string, searchString)) {
  2503. return true;
  2504. }
  2505. }
  2506.  
  2507. return false;
  2508. } else {
  2509. return false;
  2510. }
  2511. }
  2512.  
  2513. public static boolean endsWith(CharSequence str, CharSequence suffix) {
  2514. return endsWith(str, suffix, false);
  2515. }
  2516.  
  2517. public static boolean endsWithIgnoreCase(CharSequence str, CharSequence suffix) {
  2518. return endsWith(str, suffix, true);
  2519. }
  2520.  
  2521. private static boolean endsWith(CharSequence str, CharSequence suffix, boolean ignoreCase) {
  2522. if (str != null && suffix != null) {
  2523. if (suffix.length() > str.length()) {
  2524. return false;
  2525. } else {
  2526. int strOffset = str.length() - suffix.length();
  2527. return CharSequenceUtils.regionMatches(str, ignoreCase, strOffset, suffix, 0, suffix.length());
  2528. }
  2529. } else {
  2530. return str == null && suffix == null;
  2531. }
  2532. }
  2533.  
  2534. public static String normalizeSpace(String str) {
  2535. return str == null ? null : WHITESPACE_PATTERN.matcher(trim(str)).replaceAll(" ");
  2536. }
  2537.  
  2538. public static boolean endsWithAny(CharSequence string, CharSequence... searchStrings) {
  2539. if (!isEmpty(string) && !ArrayUtils.isEmpty(searchStrings)) {
  2540. CharSequence[] arr$ = searchStrings;
  2541. int len$ = searchStrings.length;
  2542.  
  2543. for(int i$ = 0; i$ < len$; ++i$) {
  2544. CharSequence searchString = arr$[i$];
  2545. if (endsWith(string, searchString)) {
  2546. return true;
  2547. }
  2548. }
  2549.  
  2550. return false;
  2551. } else {
  2552. return false;
  2553. }
  2554. }
  2555.  
  2556. private static String appendIfMissing(String str, CharSequence suffix, boolean ignoreCase, CharSequence... suffixes) {
  2557. if (str != null && !isEmpty(suffix) && !endsWith(str, suffix, ignoreCase)) {
  2558. if (suffixes != null && suffixes.length > 0) {
  2559. CharSequence[] arr$ = suffixes;
  2560. int len$ = suffixes.length;
  2561.  
  2562. for(int i$ = 0; i$ < len$; ++i$) {
  2563. CharSequence s = arr$[i$];
  2564. if (endsWith(str, s, ignoreCase)) {
  2565. return str;
  2566. }
  2567. }
  2568. }
  2569.  
  2570. return str + suffix.toString();
  2571. } else {
  2572. return str;
  2573. }
  2574. }
  2575.  
  2576. public static String appendIfMissing(String str, CharSequence suffix, CharSequence... suffixes) {
  2577. return appendIfMissing(str, suffix, false, suffixes);
  2578. }
  2579.  
  2580. public static String appendIfMissingIgnoreCase(String str, CharSequence suffix, CharSequence... suffixes) {
  2581. return appendIfMissing(str, suffix, true, suffixes);
  2582. }
  2583.  
  2584. private static String prependIfMissing(String str, CharSequence prefix, boolean ignoreCase, CharSequence... prefixes) {
  2585. if (str != null && !isEmpty(prefix) && !startsWith(str, prefix, ignoreCase)) {
  2586. if (prefixes != null && prefixes.length > 0) {
  2587. CharSequence[] arr$ = prefixes;
  2588. int len$ = prefixes.length;
  2589.  
  2590. for(int i$ = 0; i$ < len$; ++i$) {
  2591. CharSequence p = arr$[i$];
  2592. if (startsWith(str, p, ignoreCase)) {
  2593. return str;
  2594. }
  2595. }
  2596. }
  2597.  
  2598. return prefix.toString() + str;
  2599. } else {
  2600. return str;
  2601. }
  2602. }
  2603.  
  2604. public static String prependIfMissing(String str, CharSequence prefix, CharSequence... prefixes) {
  2605. return prependIfMissing(str, prefix, false, prefixes);
  2606. }
  2607.  
  2608. public static String prependIfMissingIgnoreCase(String str, CharSequence prefix, CharSequence... prefixes) {
  2609. return prependIfMissing(str, prefix, true, prefixes);
  2610. }
  2611.  
  2612. @Deprecated
  2613. public static String toString(byte[] bytes, String charsetName) throws UnsupportedEncodingException {
  2614. return charsetName != null ? new String(bytes, charsetName) : new String(bytes, Charset.defaultCharset());
  2615. }
  2616.  
  2617. public static String toEncodedString(byte[] bytes, Charset charset) throws UnsupportedEncodingException {
  2618. return new String(bytes, charset != null ? charset : Charset.defaultCharset());
  2619. }
  2620. }
Advertisement
Add Comment
Please, Sign In to add comment