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 8.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. /*
  2. * Copyright (C) 2009-2010, Google Inc. and others
  3. *
  4. * This program and the accompanying materials are made available under the
  5. * terms of the Eclipse Distribution License v. 1.0 which is available at
  6. * https://www.eclipse.org/org/documents/edl-v10.php.
  7. *
  8. * SPDX-License-Identifier: BSD-3-Clause
  9. */
  10. package org.eclipse.jgit.util;
  11. import java.text.MessageFormat;
  12. import java.util.Collection;
  13. import org.eclipse.jgit.internal.JGitText;
  14. /**
  15. * Miscellaneous string comparison utility methods.
  16. */
  17. public final class StringUtils {
  18. private static final char[] LC;
  19. static {
  20. LC = new char['Z' + 1];
  21. for (char c = 0; c < LC.length; c++)
  22. LC[c] = c;
  23. for (char c = 'A'; c <= 'Z'; c++)
  24. LC[c] = (char) ('a' + (c - 'A'));
  25. }
  26. /**
  27. * Convert the input to lowercase.
  28. * <p>
  29. * This method does not honor the JVM locale, but instead always behaves as
  30. * though it is in the US-ASCII locale. Only characters in the range 'A'
  31. * through 'Z' are converted. All other characters are left as-is, even if
  32. * they otherwise would have a lowercase character equivalent.
  33. *
  34. * @param c
  35. * the input character.
  36. * @return lowercase version of the input.
  37. */
  38. public static char toLowerCase(char c) {
  39. return c <= 'Z' ? LC[c] : c;
  40. }
  41. /**
  42. * Convert the input string to lower case, according to the "C" locale.
  43. * <p>
  44. * This method does not honor the JVM locale, but instead always behaves as
  45. * though it is in the US-ASCII locale. Only characters in the range 'A'
  46. * through 'Z' are converted, all other characters are left as-is, even if
  47. * they otherwise would have a lowercase character equivalent.
  48. *
  49. * @param in
  50. * the input string. Must not be null.
  51. * @return a copy of the input string, after converting characters in the
  52. * range 'A'..'Z' to 'a'..'z'.
  53. */
  54. public static String toLowerCase(String in) {
  55. final StringBuilder r = new StringBuilder(in.length());
  56. for (int i = 0; i < in.length(); i++)
  57. r.append(toLowerCase(in.charAt(i)));
  58. return r.toString();
  59. }
  60. /**
  61. * Borrowed from commons-lang <code>StringUtils.capitalize()</code> method.
  62. *
  63. * <p>
  64. * Capitalizes a String changing the first letter to title case as per
  65. * {@link java.lang.Character#toTitleCase(char)}. No other letters are
  66. * changed.
  67. * </p>
  68. * <p>
  69. * A <code>null</code> input String returns <code>null</code>.
  70. * </p>
  71. *
  72. * @param str
  73. * the String to capitalize, may be null
  74. * @return the capitalized String, <code>null</code> if null String input
  75. * @since 4.0
  76. */
  77. public static String capitalize(String str) {
  78. int strLen;
  79. if (str == null || (strLen = str.length()) == 0) {
  80. return str;
  81. }
  82. return new StringBuilder(strLen)
  83. .append(Character.toTitleCase(str.charAt(0)))
  84. .append(str.substring(1)).toString();
  85. }
  86. /**
  87. * Test if two strings are equal, ignoring case.
  88. * <p>
  89. * This method does not honor the JVM locale, but instead always behaves as
  90. * though it is in the US-ASCII locale.
  91. *
  92. * @param a
  93. * first string to compare.
  94. * @param b
  95. * second string to compare.
  96. * @return true if a equals b
  97. */
  98. public static boolean equalsIgnoreCase(String a, String b) {
  99. if (References.isSameObject(a, b)) {
  100. return true;
  101. }
  102. if (a.length() != b.length())
  103. return false;
  104. for (int i = 0; i < a.length(); i++) {
  105. if (toLowerCase(a.charAt(i)) != toLowerCase(b.charAt(i)))
  106. return false;
  107. }
  108. return true;
  109. }
  110. /**
  111. * Compare two strings, ignoring case.
  112. * <p>
  113. * This method does not honor the JVM locale, but instead always behaves as
  114. * though it is in the US-ASCII locale.
  115. *
  116. * @param a
  117. * first string to compare.
  118. * @param b
  119. * second string to compare.
  120. * @since 2.0
  121. * @return an int.
  122. */
  123. public static int compareIgnoreCase(String a, String b) {
  124. for (int i = 0; i < a.length() && i < b.length(); i++) {
  125. int d = toLowerCase(a.charAt(i)) - toLowerCase(b.charAt(i));
  126. if (d != 0)
  127. return d;
  128. }
  129. return a.length() - b.length();
  130. }
  131. /**
  132. * Compare two strings, honoring case.
  133. * <p>
  134. * This method does not honor the JVM locale, but instead always behaves as
  135. * though it is in the US-ASCII locale.
  136. *
  137. * @param a
  138. * first string to compare.
  139. * @param b
  140. * second string to compare.
  141. * @since 2.0
  142. * @return an int.
  143. */
  144. public static int compareWithCase(String a, String b) {
  145. for (int i = 0; i < a.length() && i < b.length(); i++) {
  146. int d = a.charAt(i) - b.charAt(i);
  147. if (d != 0)
  148. return d;
  149. }
  150. return a.length() - b.length();
  151. }
  152. /**
  153. * Parse a string as a standard Git boolean value. See
  154. * {@link #toBooleanOrNull(String)}.
  155. *
  156. * @param stringValue
  157. * the string to parse.
  158. * @return the boolean interpretation of {@code value}.
  159. * @throws java.lang.IllegalArgumentException
  160. * if {@code value} is not recognized as one of the standard
  161. * boolean names.
  162. */
  163. public static boolean toBoolean(String stringValue) {
  164. if (stringValue == null)
  165. throw new NullPointerException(JGitText.get().expectedBooleanStringValue);
  166. final Boolean bool = toBooleanOrNull(stringValue);
  167. if (bool == null)
  168. throw new IllegalArgumentException(MessageFormat.format(JGitText.get().notABoolean, stringValue));
  169. return bool.booleanValue();
  170. }
  171. /**
  172. * Parse a string as a standard Git boolean value.
  173. * <p>
  174. * The terms {@code yes}, {@code true}, {@code 1}, {@code on} can all be
  175. * used to mean {@code true}.
  176. * <p>
  177. * The terms {@code no}, {@code false}, {@code 0}, {@code off} can all be
  178. * used to mean {@code false}.
  179. * <p>
  180. * Comparisons ignore case, via {@link #equalsIgnoreCase(String, String)}.
  181. *
  182. * @param stringValue
  183. * the string to parse.
  184. * @return the boolean interpretation of {@code value} or null in case the
  185. * string does not represent a boolean value
  186. */
  187. public static Boolean toBooleanOrNull(String stringValue) {
  188. if (stringValue == null)
  189. return null;
  190. if (equalsIgnoreCase("yes", stringValue) //$NON-NLS-1$
  191. || equalsIgnoreCase("true", stringValue) //$NON-NLS-1$
  192. || equalsIgnoreCase("1", stringValue) //$NON-NLS-1$
  193. || equalsIgnoreCase("on", stringValue)) //$NON-NLS-1$
  194. return Boolean.TRUE;
  195. else if (equalsIgnoreCase("no", stringValue) //$NON-NLS-1$
  196. || equalsIgnoreCase("false", stringValue) //$NON-NLS-1$
  197. || equalsIgnoreCase("0", stringValue) //$NON-NLS-1$
  198. || equalsIgnoreCase("off", stringValue)) //$NON-NLS-1$
  199. return Boolean.FALSE;
  200. else
  201. return null;
  202. }
  203. /**
  204. * Join a collection of Strings together using the specified separator.
  205. *
  206. * @param parts
  207. * Strings to join
  208. * @param separator
  209. * used to join
  210. * @return a String with all the joined parts
  211. */
  212. public static String join(Collection<String> parts, String separator) {
  213. return StringUtils.join(parts, separator, separator);
  214. }
  215. /**
  216. * Join a collection of Strings together using the specified separator and a
  217. * lastSeparator which is used for joining the second last and the last
  218. * part.
  219. *
  220. * @param parts
  221. * Strings to join
  222. * @param separator
  223. * separator used to join all but the two last elements
  224. * @param lastSeparator
  225. * separator to use for joining the last two elements
  226. * @return a String with all the joined parts
  227. */
  228. public static String join(Collection<String> parts, String separator,
  229. String lastSeparator) {
  230. StringBuilder sb = new StringBuilder();
  231. int i = 0;
  232. int lastIndex = parts.size() - 1;
  233. for (String part : parts) {
  234. sb.append(part);
  235. if (i == lastIndex - 1) {
  236. sb.append(lastSeparator);
  237. } else if (i != lastIndex) {
  238. sb.append(separator);
  239. }
  240. i++;
  241. }
  242. return sb.toString();
  243. }
  244. private StringUtils() {
  245. // Do not create instances
  246. }
  247. /**
  248. * Test if a string is empty or null.
  249. *
  250. * @param stringValue
  251. * the string to check
  252. * @return <code>true</code> if the string is <code>null</code> or empty
  253. */
  254. public static boolean isEmptyOrNull(String stringValue) {
  255. return stringValue == null || stringValue.length() == 0;
  256. }
  257. /**
  258. * Replace CRLF, CR or LF with a single space.
  259. *
  260. * @param in
  261. * A string with line breaks
  262. * @return in without line breaks
  263. * @since 3.1
  264. */
  265. public static String replaceLineBreaksWithSpace(String in) {
  266. char[] buf = new char[in.length()];
  267. int o = 0;
  268. for (int i = 0; i < buf.length; ++i) {
  269. char ch = in.charAt(i);
  270. switch (ch) {
  271. case '\r':
  272. if (i + 1 < buf.length && in.charAt(i + 1) == '\n') {
  273. buf[o++] = ' ';
  274. ++i;
  275. } else
  276. buf[o++] = ' ';
  277. break;
  278. case '\n':
  279. buf[o++] = ' ';
  280. break;
  281. default:
  282. buf[o++] = ch;
  283. break;
  284. }
  285. }
  286. return new String(buf, 0, o);
  287. }
  288. }