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