You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

StringUtils.java 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. /*
  2. * Copyright (C) 2009-2010, Google Inc.
  3. * and other copyright owners as documented in the project's IP log.
  4. *
  5. * This program and the accompanying materials are made available
  6. * under the terms of the Eclipse Distribution License v1.0 which
  7. * accompanies this distribution, is reproduced below, and is
  8. * available at http://www.eclipse.org/org/documents/edl-v10.php
  9. *
  10. * All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or
  13. * without modification, are permitted provided that the following
  14. * conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above copyright
  17. * notice, this list of conditions and the following disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials provided
  22. * with the distribution.
  23. *
  24. * - Neither the name of the Eclipse Foundation, Inc. nor the
  25. * names of its contributors may be used to endorse or promote
  26. * products derived from this software without specific prior
  27. * written permission.
  28. *
  29. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  30. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  31. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  32. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  33. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  34. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  35. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  36. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  37. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  38. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  39. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  40. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  41. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  42. */
  43. package org.eclipse.jgit.util;
  44. import java.text.MessageFormat;
  45. import java.util.Collection;
  46. import org.eclipse.jgit.internal.JGitText;
  47. /**
  48. * Miscellaneous string comparison utility methods.
  49. */
  50. public final class StringUtils {
  51. private static final char[] LC;
  52. static {
  53. LC = new char['Z' + 1];
  54. for (char c = 0; c < LC.length; c++)
  55. LC[c] = c;
  56. for (char c = 'A'; c <= 'Z'; c++)
  57. LC[c] = (char) ('a' + (c - 'A'));
  58. }
  59. /**
  60. * Convert the input to lowercase.
  61. * <p>
  62. * This method does not honor the JVM locale, but instead always behaves as
  63. * though it is in the US-ASCII locale. Only characters in the range 'A'
  64. * through 'Z' are converted. All other characters are left as-is, even if
  65. * they otherwise would have a lowercase character equivalent.
  66. *
  67. * @param c
  68. * the input character.
  69. * @return lowercase version of the input.
  70. */
  71. public static char toLowerCase(char c) {
  72. return c <= 'Z' ? LC[c] : c;
  73. }
  74. /**
  75. * Convert the input string to lower case, according to the "C" locale.
  76. * <p>
  77. * This method does not honor the JVM locale, but instead always behaves as
  78. * though it is in the US-ASCII locale. Only characters in the range 'A'
  79. * through 'Z' are converted, all other characters are left as-is, even if
  80. * they otherwise would have a lowercase character equivalent.
  81. *
  82. * @param in
  83. * the input string. Must not be null.
  84. * @return a copy of the input string, after converting characters in the
  85. * range 'A'..'Z' to 'a'..'z'.
  86. */
  87. public static String toLowerCase(String in) {
  88. final StringBuilder r = new StringBuilder(in.length());
  89. for (int i = 0; i < in.length(); i++)
  90. r.append(toLowerCase(in.charAt(i)));
  91. return r.toString();
  92. }
  93. /**
  94. * Borrowed from commons-lang <code>StringUtils.capitalize()</code> method.
  95. *
  96. * <p>
  97. * Capitalizes a String changing the first letter to title case as per
  98. * {@link java.lang.Character#toTitleCase(char)}. No other letters are
  99. * changed.
  100. * </p>
  101. * <p>
  102. * A <code>null</code> input String returns <code>null</code>.
  103. * </p>
  104. *
  105. * @param str
  106. * the String to capitalize, may be null
  107. * @return the capitalized String, <code>null</code> if null String input
  108. * @since 4.0
  109. */
  110. public static String capitalize(String str) {
  111. int strLen;
  112. if (str == null || (strLen = str.length()) == 0) {
  113. return str;
  114. }
  115. return new StringBuffer(strLen)
  116. .append(Character.toTitleCase(str.charAt(0)))
  117. .append(str.substring(1)).toString();
  118. }
  119. /**
  120. * Test if two strings are equal, ignoring case.
  121. * <p>
  122. * This method does not honor the JVM locale, but instead always behaves as
  123. * though it is in the US-ASCII locale.
  124. *
  125. * @param a
  126. * first string to compare.
  127. * @param b
  128. * second string to compare.
  129. * @return true if a equals b
  130. */
  131. public static boolean equalsIgnoreCase(String a, String b) {
  132. if (References.isSameObject(a, b)) {
  133. return true;
  134. }
  135. if (a.length() != b.length())
  136. return false;
  137. for (int i = 0; i < a.length(); i++) {
  138. if (toLowerCase(a.charAt(i)) != toLowerCase(b.charAt(i)))
  139. return false;
  140. }
  141. return true;
  142. }
  143. /**
  144. * Compare two strings, ignoring case.
  145. * <p>
  146. * This method does not honor the JVM locale, but instead always behaves as
  147. * though it is in the US-ASCII locale.
  148. *
  149. * @param a
  150. * first string to compare.
  151. * @param b
  152. * second string to compare.
  153. * @since 2.0
  154. * @return an int.
  155. */
  156. public static int compareIgnoreCase(String a, String b) {
  157. for (int i = 0; i < a.length() && i < b.length(); i++) {
  158. int d = toLowerCase(a.charAt(i)) - toLowerCase(b.charAt(i));
  159. if (d != 0)
  160. return d;
  161. }
  162. return a.length() - b.length();
  163. }
  164. /**
  165. * Compare two strings, honoring case.
  166. * <p>
  167. * This method does not honor the JVM locale, but instead always behaves as
  168. * though it is in the US-ASCII locale.
  169. *
  170. * @param a
  171. * first string to compare.
  172. * @param b
  173. * second string to compare.
  174. * @since 2.0
  175. * @return an int.
  176. */
  177. public static int compareWithCase(String a, String b) {
  178. for (int i = 0; i < a.length() && i < b.length(); i++) {
  179. int d = a.charAt(i) - b.charAt(i);
  180. if (d != 0)
  181. return d;
  182. }
  183. return a.length() - b.length();
  184. }
  185. /**
  186. * Parse a string as a standard Git boolean value. See
  187. * {@link #toBooleanOrNull(String)}.
  188. *
  189. * @param stringValue
  190. * the string to parse.
  191. * @return the boolean interpretation of {@code value}.
  192. * @throws java.lang.IllegalArgumentException
  193. * if {@code value} is not recognized as one of the standard
  194. * boolean names.
  195. */
  196. public static boolean toBoolean(String stringValue) {
  197. if (stringValue == null)
  198. throw new NullPointerException(JGitText.get().expectedBooleanStringValue);
  199. final Boolean bool = toBooleanOrNull(stringValue);
  200. if (bool == null)
  201. throw new IllegalArgumentException(MessageFormat.format(JGitText.get().notABoolean, stringValue));
  202. return bool.booleanValue();
  203. }
  204. /**
  205. * Parse a string as a standard Git boolean value.
  206. * <p>
  207. * The terms {@code yes}, {@code true}, {@code 1}, {@code on} can all be
  208. * used to mean {@code true}.
  209. * <p>
  210. * The terms {@code no}, {@code false}, {@code 0}, {@code off} can all be
  211. * used to mean {@code false}.
  212. * <p>
  213. * Comparisons ignore case, via {@link #equalsIgnoreCase(String, String)}.
  214. *
  215. * @param stringValue
  216. * the string to parse.
  217. * @return the boolean interpretation of {@code value} or null in case the
  218. * string does not represent a boolean value
  219. */
  220. public static Boolean toBooleanOrNull(String stringValue) {
  221. if (stringValue == null)
  222. return null;
  223. if (equalsIgnoreCase("yes", stringValue) //$NON-NLS-1$
  224. || equalsIgnoreCase("true", stringValue) //$NON-NLS-1$
  225. || equalsIgnoreCase("1", stringValue) //$NON-NLS-1$
  226. || equalsIgnoreCase("on", stringValue)) //$NON-NLS-1$
  227. return Boolean.TRUE;
  228. else if (equalsIgnoreCase("no", stringValue) //$NON-NLS-1$
  229. || equalsIgnoreCase("false", stringValue) //$NON-NLS-1$
  230. || equalsIgnoreCase("0", stringValue) //$NON-NLS-1$
  231. || equalsIgnoreCase("off", stringValue)) //$NON-NLS-1$
  232. return Boolean.FALSE;
  233. else
  234. return null;
  235. }
  236. /**
  237. * Join a collection of Strings together using the specified separator.
  238. *
  239. * @param parts
  240. * Strings to join
  241. * @param separator
  242. * used to join
  243. * @return a String with all the joined parts
  244. */
  245. public static String join(Collection<String> parts, String separator) {
  246. return StringUtils.join(parts, separator, separator);
  247. }
  248. /**
  249. * Join a collection of Strings together using the specified separator and a
  250. * lastSeparator which is used for joining the second last and the last
  251. * part.
  252. *
  253. * @param parts
  254. * Strings to join
  255. * @param separator
  256. * separator used to join all but the two last elements
  257. * @param lastSeparator
  258. * separator to use for joining the last two elements
  259. * @return a String with all the joined parts
  260. */
  261. public static String join(Collection<String> parts, String separator,
  262. String lastSeparator) {
  263. StringBuilder sb = new StringBuilder();
  264. int i = 0;
  265. int lastIndex = parts.size() - 1;
  266. for (String part : parts) {
  267. sb.append(part);
  268. if (i == lastIndex - 1) {
  269. sb.append(lastSeparator);
  270. } else if (i != lastIndex) {
  271. sb.append(separator);
  272. }
  273. i++;
  274. }
  275. return sb.toString();
  276. }
  277. private StringUtils() {
  278. // Do not create instances
  279. }
  280. /**
  281. * Test if a string is empty or null.
  282. *
  283. * @param stringValue
  284. * the string to check
  285. * @return <code>true</code> if the string is <code>null</code> or empty
  286. */
  287. public static boolean isEmptyOrNull(String stringValue) {
  288. return stringValue == null || stringValue.length() == 0;
  289. }
  290. /**
  291. * Replace CRLF, CR or LF with a single space.
  292. *
  293. * @param in
  294. * A string with line breaks
  295. * @return in without line breaks
  296. * @since 3.1
  297. */
  298. public static String replaceLineBreaksWithSpace(String in) {
  299. char[] buf = new char[in.length()];
  300. int o = 0;
  301. for (int i = 0; i < buf.length; ++i) {
  302. char ch = in.charAt(i);
  303. if (ch == '\r') {
  304. if (i + 1 < buf.length && in.charAt(i + 1) == '\n') {
  305. buf[o++] = ' ';
  306. ++i;
  307. } else
  308. buf[o++] = ' ';
  309. } else if (ch == '\n')
  310. buf[o++] = ' ';
  311. else
  312. buf[o++] = ch;
  313. }
  314. return new String(buf, 0, o);
  315. }
  316. }