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