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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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. /** Miscellaneous string comparison utility methods. */
  48. public final class StringUtils {
  49. private static final char[] LC;
  50. static {
  51. LC = new char['Z' + 1];
  52. for (char c = 0; c < LC.length; c++)
  53. LC[c] = c;
  54. for (char c = 'A'; c <= 'Z'; c++)
  55. LC[c] = (char) ('a' + (c - 'A'));
  56. }
  57. /**
  58. * Convert the input to lowercase.
  59. * <p>
  60. * This method does not honor the JVM locale, but instead always behaves as
  61. * though it is in the US-ASCII locale. Only characters in the range 'A'
  62. * through 'Z' are converted. All other characters are left as-is, even if
  63. * they otherwise would have a lowercase character equivalent.
  64. *
  65. * @param c
  66. * the input character.
  67. * @return lowercase version of the input.
  68. */
  69. public static char toLowerCase(final char c) {
  70. return c <= 'Z' ? LC[c] : c;
  71. }
  72. /**
  73. * Convert the input string to lower case, according to the "C" locale.
  74. * <p>
  75. * This method does not honor the JVM locale, but instead always behaves as
  76. * though it is in the US-ASCII locale. Only characters in the range 'A'
  77. * through 'Z' are converted, all other characters are left as-is, even if
  78. * they otherwise would have a lowercase character equivalent.
  79. *
  80. * @param in
  81. * the input string. Must not be null.
  82. * @return a copy of the input string, after converting characters in the
  83. * range 'A'..'Z' to 'a'..'z'.
  84. */
  85. public static String toLowerCase(final String in) {
  86. final StringBuilder r = new StringBuilder(in.length());
  87. for (int i = 0; i < in.length(); i++)
  88. r.append(toLowerCase(in.charAt(i)));
  89. return r.toString();
  90. }
  91. /**
  92. * Borrowed from commons-lang <code>StringUtils.capitalize()</code> method.
  93. *
  94. * <p>
  95. * Capitalizes a String changing the first letter to title case as per
  96. * {@link Character#toTitleCase(char)}. No other letters are changed.
  97. * </p>
  98. *
  99. * A <code>null</code> input String returns <code>null</code>.</p>
  100. *
  101. * @param str
  102. * the String to capitalize, may be null
  103. * @return the capitalized String, <code>null</code> if null String input
  104. * @since 4.0
  105. */
  106. public static String capitalize(String str) {
  107. int strLen;
  108. if (str == null || (strLen = str.length()) == 0) {
  109. return str;
  110. }
  111. return new StringBuffer(strLen)
  112. .append(Character.toTitleCase(str.charAt(0)))
  113. .append(str.substring(1)).toString();
  114. }
  115. /**
  116. * Test if two strings are equal, ignoring case.
  117. * <p>
  118. * This method does not honor the JVM locale, but instead always behaves as
  119. * though it is in the US-ASCII locale.
  120. *
  121. * @param a
  122. * first string to compare.
  123. * @param b
  124. * second string to compare.
  125. * @return true if a equals b
  126. */
  127. public static boolean equalsIgnoreCase(final String a, final String b) {
  128. if (a == b)
  129. return true;
  130. if (a.length() != b.length())
  131. return false;
  132. for (int i = 0; i < a.length(); i++) {
  133. if (toLowerCase(a.charAt(i)) != toLowerCase(b.charAt(i)))
  134. return false;
  135. }
  136. return true;
  137. }
  138. /**
  139. * Compare two strings, ignoring case.
  140. * <p>
  141. * This method does not honor the JVM locale, but instead always behaves as
  142. * though it is in the US-ASCII locale.
  143. *
  144. * @param a
  145. * first string to compare.
  146. * @param b
  147. * second string to compare.
  148. * @return negative, zero or positive if a sorts before, is equal to, or
  149. * sorts after b.
  150. * @since 2.0
  151. */
  152. public static int compareIgnoreCase(String a, String b) {
  153. for (int i = 0; i < a.length() && i < b.length(); i++) {
  154. int d = toLowerCase(a.charAt(i)) - toLowerCase(b.charAt(i));
  155. if (d != 0)
  156. return d;
  157. }
  158. return a.length() - b.length();
  159. }
  160. /**
  161. * Compare two strings, honoring case.
  162. * <p>
  163. * This method does not honor the JVM locale, but instead always behaves as
  164. * though it is in the US-ASCII locale.
  165. *
  166. * @param a
  167. * first string to compare.
  168. * @param b
  169. * second string to compare.
  170. * @return negative, zero or positive if a sorts before, is equal to, or
  171. * sorts after b.
  172. * @since 2.0
  173. */
  174. public static int compareWithCase(String a, String b) {
  175. for (int i = 0; i < a.length() && i < b.length(); i++) {
  176. int d = a.charAt(i) - b.charAt(i);
  177. if (d != 0)
  178. return d;
  179. }
  180. return a.length() - b.length();
  181. }
  182. /**
  183. * Parse a string as a standard Git boolean value. See
  184. * {@link #toBooleanOrNull(String)}.
  185. *
  186. * @param stringValue
  187. * the string to parse.
  188. * @return the boolean interpretation of {@code value}.
  189. * @throws IllegalArgumentException
  190. * if {@code value} is not recognized as one of the standard
  191. * boolean names.
  192. */
  193. public static boolean toBoolean(final String stringValue) {
  194. if (stringValue == null)
  195. throw new NullPointerException(JGitText.get().expectedBooleanStringValue);
  196. final Boolean bool = toBooleanOrNull(stringValue);
  197. if (bool == null)
  198. throw new IllegalArgumentException(MessageFormat.format(JGitText.get().notABoolean, stringValue));
  199. return bool.booleanValue();
  200. }
  201. /**
  202. * Parse a string as a standard Git boolean value.
  203. * <p>
  204. * The terms {@code yes}, {@code true}, {@code 1}, {@code on} can all be
  205. * used to mean {@code true}.
  206. * <p>
  207. * The terms {@code no}, {@code false}, {@code 0}, {@code off} can all be
  208. * used to mean {@code false}.
  209. * <p>
  210. * Comparisons ignore case, via {@link #equalsIgnoreCase(String, String)}.
  211. *
  212. * @param stringValue
  213. * the string to parse.
  214. * @return the boolean interpretation of {@code value} or null in case the
  215. * string does not represent a boolean value
  216. */
  217. public static Boolean toBooleanOrNull(final String stringValue) {
  218. if (stringValue == null)
  219. return null;
  220. if (equalsIgnoreCase("yes", stringValue) //$NON-NLS-1$
  221. || equalsIgnoreCase("true", stringValue) //$NON-NLS-1$
  222. || equalsIgnoreCase("1", stringValue) //$NON-NLS-1$
  223. || equalsIgnoreCase("on", stringValue)) //$NON-NLS-1$
  224. return Boolean.TRUE;
  225. else if (equalsIgnoreCase("no", stringValue) //$NON-NLS-1$
  226. || equalsIgnoreCase("false", stringValue) //$NON-NLS-1$
  227. || equalsIgnoreCase("0", stringValue) //$NON-NLS-1$
  228. || equalsIgnoreCase("off", stringValue)) //$NON-NLS-1$
  229. return Boolean.FALSE;
  230. else
  231. return null;
  232. }
  233. /**
  234. * Join a collection of Strings together using the specified separator.
  235. *
  236. * @param parts
  237. * Strings to join
  238. * @param separator
  239. * used to join
  240. * @return a String with all the joined parts
  241. */
  242. public static String join(Collection<String> parts, String separator) {
  243. return StringUtils.join(parts, separator, separator);
  244. }
  245. /**
  246. * Join a collection of Strings together using the specified separator and a
  247. * lastSeparator which is used for joining the second last and the last
  248. * part.
  249. *
  250. * @param parts
  251. * Strings to join
  252. * @param separator
  253. * separator used to join all but the two last elements
  254. * @param lastSeparator
  255. * separator to use for joining the last two elements
  256. * @return a String with all the joined parts
  257. */
  258. public static String join(Collection<String> parts, String separator,
  259. String lastSeparator) {
  260. StringBuilder sb = new StringBuilder();
  261. int i = 0;
  262. int lastIndex = parts.size() - 1;
  263. for (String part : parts) {
  264. sb.append(part);
  265. if (i == lastIndex - 1) {
  266. sb.append(lastSeparator);
  267. } else if (i != lastIndex) {
  268. sb.append(separator);
  269. }
  270. i++;
  271. }
  272. return sb.toString();
  273. }
  274. private StringUtils() {
  275. // Do not create instances
  276. }
  277. /**
  278. * Test if a string is empty or null.
  279. *
  280. * @param stringValue
  281. * the string to check
  282. * @return <code>true</code> if the string is <code>null</code> or empty
  283. */
  284. public static boolean isEmptyOrNull(String stringValue) {
  285. return stringValue == null || stringValue.length() == 0;
  286. }
  287. /**
  288. * Replace CRLF, CR or LF with a single space.
  289. *
  290. * @param in
  291. * A string with line breaks
  292. * @return in without line breaks
  293. * @since 3.1
  294. */
  295. public static String replaceLineBreaksWithSpace(String in) {
  296. char[] buf = new char[in.length()];
  297. int o = 0;
  298. for (int i = 0; i < buf.length; ++i) {
  299. char ch = in.charAt(i);
  300. if (ch == '\r') {
  301. if (i + 1 < buf.length && in.charAt(i + 1) == '\n') {
  302. buf[o++] = ' ';
  303. ++i;
  304. } else
  305. buf[o++] = ' ';
  306. } else if (ch == '\n')
  307. buf[o++] = ' ';
  308. else
  309. buf[o++] = ch;
  310. }
  311. return new String(buf, 0, o);
  312. }
  313. }