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