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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. /** Miscellaneous string comparison utility methods. */
  45. public final class StringUtils {
  46. private static final char[] LC;
  47. static {
  48. LC = new char['Z' + 1];
  49. for (char c = 0; c < LC.length; c++)
  50. LC[c] = c;
  51. for (char c = 'A'; c <= 'Z'; c++)
  52. LC[c] = (char) ('a' + (c - 'A'));
  53. }
  54. /**
  55. * Convert the input to lowercase.
  56. * <p>
  57. * This method does not honor the JVM locale, but instead always behaves as
  58. * though it is in the US-ASCII locale. Only characters in the range 'A'
  59. * through 'Z' are converted. All other characters are left as-is, even if
  60. * they otherwise would have a lowercase character equivalent.
  61. *
  62. * @param c
  63. * the input character.
  64. * @return lowercase version of the input.
  65. */
  66. public static char toLowerCase(final char c) {
  67. return c <= 'Z' ? LC[c] : c;
  68. }
  69. /**
  70. * Convert the input string to lower case, according to the "C" locale.
  71. * <p>
  72. * This method does not honor the JVM locale, but instead always behaves as
  73. * though it is in the US-ASCII locale. Only characters in the range 'A'
  74. * through 'Z' are converted, all other characters are left as-is, even if
  75. * they otherwise would have a lowercase character equivalent.
  76. *
  77. * @param in
  78. * the input string. Must not be null.
  79. * @return a copy of the input string, after converting characters in the
  80. * range 'A'..'Z' to 'a'..'z'.
  81. */
  82. public static String toLowerCase(final String in) {
  83. final StringBuilder r = new StringBuilder(in.length());
  84. for (int i = 0; i < in.length(); i++)
  85. r.append(toLowerCase(in.charAt(i)));
  86. return r.toString();
  87. }
  88. /**
  89. * Test if two strings are equal, ignoring case.
  90. * <p>
  91. * This method does not honor the JVM locale, but instead always behaves as
  92. * though it is in the US-ASCII locale.
  93. *
  94. * @param a
  95. * first string to compare.
  96. * @param b
  97. * second string to compare.
  98. * @return true if a equals b
  99. */
  100. public static boolean equalsIgnoreCase(final String a, final String b) {
  101. if (a == b)
  102. return true;
  103. if (a.length() != b.length())
  104. return false;
  105. for (int i = 0; i < a.length(); i++) {
  106. if (toLowerCase(a.charAt(i)) != toLowerCase(b.charAt(i)))
  107. return false;
  108. }
  109. return true;
  110. }
  111. /**
  112. * Parse a string as a standard Git boolean value.
  113. * <p>
  114. * The terms {@code yes}, {@code true}, {@code 1}, {@code on} can all be
  115. * used to mean {@code true}.
  116. * <p>
  117. * The terms {@code no}, {@code false}, {@code 0}, {@code off} can all be
  118. * used to mean {@code false}.
  119. * <p>
  120. * Comparisons ignore case, via {@link #equalsIgnoreCase(String, String)}.
  121. *
  122. * @param stringValue
  123. * the string to parse.
  124. * @return the boolean interpretation of {@code value}.
  125. * @throws IllegalArgumentException
  126. * if {@code value} is not recognized as one of the standard
  127. * boolean names.
  128. */
  129. public static boolean toBoolean(final String stringValue) {
  130. if (stringValue == null)
  131. throw new NullPointerException("Expected boolean string value");
  132. if (equalsIgnoreCase("yes", stringValue)
  133. || equalsIgnoreCase("true", stringValue)
  134. || equalsIgnoreCase("1", stringValue)
  135. || equalsIgnoreCase("on", stringValue)) {
  136. return true;
  137. } else if (equalsIgnoreCase("no", stringValue)
  138. || equalsIgnoreCase("false", stringValue)
  139. || equalsIgnoreCase("0", stringValue)
  140. || equalsIgnoreCase("off", stringValue)) {
  141. return false;
  142. } else {
  143. throw new IllegalArgumentException("Not a boolean: " + stringValue);
  144. }
  145. }
  146. private StringUtils() {
  147. // Do not create instances
  148. }
  149. }