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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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(final 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(final 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(final String a, final String b) {
  132. if (a == b)
  133. return true;
  134. if (a.length() != b.length())
  135. return false;
  136. for (int i = 0; i < a.length(); i++) {
  137. if (toLowerCase(a.charAt(i)) != toLowerCase(b.charAt(i)))
  138. return false;
  139. }
  140. return true;
  141. }
  142. /**
  143. * Compare two strings, ignoring case.
  144. * <p>
  145. * This method does not honor the JVM locale, but instead always behaves as
  146. * though it is in the US-ASCII locale.
  147. *
  148. * @param a
  149. * first string to compare.
  150. * @param b
  151. * second string to compare.
  152. * @since 2.0
  153. * @return an int.
  154. */
  155. public static int compareIgnoreCase(String a, String b) {
  156. for (int i = 0; i < a.length() && i < b.length(); i++) {
  157. int d = toLowerCase(a.charAt(i)) - toLowerCase(b.charAt(i));
  158. if (d != 0)
  159. return d;
  160. }
  161. return a.length() - b.length();
  162. }
  163. /**
  164. * Compare two strings, honoring case.
  165. * <p>
  166. * This method does not honor the JVM locale, but instead always behaves as
  167. * though it is in the US-ASCII locale.
  168. *
  169. * @param a
  170. * first string to compare.
  171. * @param b
  172. * second string to compare.
  173. * @since 2.0
  174. * @return an int.
  175. */
  176. public static int compareWithCase(String a, String b) {
  177. for (int i = 0; i < a.length() && i < b.length(); i++) {
  178. int d = a.charAt(i) - b.charAt(i);
  179. if (d != 0)
  180. return d;
  181. }
  182. return a.length() - b.length();
  183. }
  184. /**
  185. * Parse a string as a standard Git boolean value. See
  186. * {@link #toBooleanOrNull(String)}.
  187. *
  188. * @param stringValue
  189. * the string to parse.
  190. * @return the boolean interpretation of {@code value}.
  191. * @throws java.lang.IllegalArgumentException
  192. * if {@code value} is not recognized as one of the standard
  193. * boolean names.
  194. */
  195. public static boolean toBoolean(final String stringValue) {
  196. if (stringValue == null)
  197. throw new NullPointerException(JGitText.get().expectedBooleanStringValue);
  198. final Boolean bool = toBooleanOrNull(stringValue);
  199. if (bool == null)
  200. throw new IllegalArgumentException(MessageFormat.format(JGitText.get().notABoolean, stringValue));
  201. return bool.booleanValue();
  202. }
  203. /**
  204. * Parse a string as a standard Git boolean value.
  205. * <p>
  206. * The terms {@code yes}, {@code true}, {@code 1}, {@code on} can all be
  207. * used to mean {@code true}.
  208. * <p>
  209. * The terms {@code no}, {@code false}, {@code 0}, {@code off} can all be
  210. * used to mean {@code false}.
  211. * <p>
  212. * Comparisons ignore case, via {@link #equalsIgnoreCase(String, String)}.
  213. *
  214. * @param stringValue
  215. * the string to parse.
  216. * @return the boolean interpretation of {@code value} or null in case the
  217. * string does not represent a boolean value
  218. */
  219. public static Boolean toBooleanOrNull(final String stringValue) {
  220. if (stringValue == null)
  221. return null;
  222. if (equalsIgnoreCase("yes", stringValue) //$NON-NLS-1$
  223. || equalsIgnoreCase("true", stringValue) //$NON-NLS-1$
  224. || equalsIgnoreCase("1", stringValue) //$NON-NLS-1$
  225. || equalsIgnoreCase("on", stringValue)) //$NON-NLS-1$
  226. return Boolean.TRUE;
  227. else if (equalsIgnoreCase("no", stringValue) //$NON-NLS-1$
  228. || equalsIgnoreCase("false", stringValue) //$NON-NLS-1$
  229. || equalsIgnoreCase("0", stringValue) //$NON-NLS-1$
  230. || equalsIgnoreCase("off", stringValue)) //$NON-NLS-1$
  231. return Boolean.FALSE;
  232. else
  233. return null;
  234. }
  235. /**
  236. * Join a collection of Strings together using the specified separator.
  237. *
  238. * @param parts
  239. * Strings to join
  240. * @param separator
  241. * used to join
  242. * @return a String with all the joined parts
  243. */
  244. public static String join(Collection<String> parts, String separator) {
  245. return StringUtils.join(parts, separator, separator);
  246. }
  247. /**
  248. * Join a collection of Strings together using the specified separator and a
  249. * lastSeparator which is used for joining the second last and the last
  250. * part.
  251. *
  252. * @param parts
  253. * Strings to join
  254. * @param separator
  255. * separator used to join all but the two last elements
  256. * @param lastSeparator
  257. * separator to use for joining the last two elements
  258. * @return a String with all the joined parts
  259. */
  260. public static String join(Collection<String> parts, String separator,
  261. String lastSeparator) {
  262. StringBuilder sb = new StringBuilder();
  263. int i = 0;
  264. int lastIndex = parts.size() - 1;
  265. for (String part : parts) {
  266. sb.append(part);
  267. if (i == lastIndex - 1) {
  268. sb.append(lastSeparator);
  269. } else if (i != lastIndex) {
  270. sb.append(separator);
  271. }
  272. i++;
  273. }
  274. return sb.toString();
  275. }
  276. private StringUtils() {
  277. // Do not create instances
  278. }
  279. /**
  280. * Test if a string is empty or null.
  281. *
  282. * @param stringValue
  283. * the string to check
  284. * @return <code>true</code> if the string is <code>null</code> or empty
  285. */
  286. public static boolean isEmptyOrNull(String stringValue) {
  287. return stringValue == null || stringValue.length() == 0;
  288. }
  289. /**
  290. * Replace CRLF, CR or LF with a single space.
  291. *
  292. * @param in
  293. * A string with line breaks
  294. * @return in without line breaks
  295. * @since 3.1
  296. */
  297. public static String replaceLineBreaksWithSpace(String in) {
  298. char[] buf = new char[in.length()];
  299. int o = 0;
  300. for (int i = 0; i < buf.length; ++i) {
  301. char ch = in.charAt(i);
  302. if (ch == '\r') {
  303. if (i + 1 < buf.length && in.charAt(i + 1) == '\n') {
  304. buf[o++] = ' ';
  305. ++i;
  306. } else
  307. buf[o++] = ' ';
  308. } else if (ch == '\n')
  309. buf[o++] = ' ';
  310. else
  311. buf[o++] = ch;
  312. }
  313. return new String(buf, 0, o);
  314. }
  315. }