/api/src/main/java/com/alibaba/nacos/api/utils/StringUtils.java

https://github.com/alibaba/nacos · Java · 187 lines · 58 code · 15 blank · 114 comment · 28 complexity · 49bc5a2d38b9cdd895432f70a622ca58 MD5 · raw file

  1. /*
  2. * Copyright 1999-2018 Alibaba Group Holding Ltd.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.alibaba.nacos.api.utils;
  17. /**
  18. * StringUtils. copy from apache common-lang3.
  19. *
  20. * @author <a href="mailto:lin-mt@outlook.com">lin-mt</a>
  21. */
  22. public class StringUtils {
  23. /**
  24. * The empty String {@code ""}.
  25. *
  26. * @since 2.0
  27. */
  28. public static final String EMPTY = "";
  29. /**
  30. * <p>Checks if a CharSequence is empty ("") or null.</p>
  31. *
  32. * <pre>
  33. * StringUtils.isEmpty(null) = true
  34. * StringUtils.isEmpty("") = true
  35. * StringUtils.isEmpty(" ") = false
  36. * StringUtils.isEmpty("bob") = false
  37. * StringUtils.isEmpty(" bob ") = false
  38. * </pre>
  39. *
  40. * <p>NOTE: This method changed in Lang version 2.0.
  41. * It no longer trims the CharSequence. That functionality is available in isBlank().</p>
  42. *
  43. * @param cs the CharSequence to check, may be null
  44. * @return {@code true} if the CharSequence is empty or null
  45. * @since 3.0 Changed signature from isEmpty(String) to isEmpty(CharSequence)
  46. */
  47. public static boolean isEmpty(final CharSequence cs) {
  48. return cs == null || cs.length() == 0;
  49. }
  50. /**
  51. * <p>Checks if a CharSequence is whitespace, empty ("") or null.</p>
  52. *
  53. * <pre>
  54. * StringUtils.isBlank(null) = true
  55. * StringUtils.isBlank("") = true
  56. * StringUtils.isBlank(" ") = true
  57. * StringUtils.isBlank("bob") = false
  58. * StringUtils.isBlank(" bob ") = false
  59. * </pre>
  60. *
  61. * @param cs the CharSequence to check, may be null
  62. * @return {@code true} if the CharSequence is null, empty or whitespace
  63. * @since 2.0
  64. * @since 3.0 Changed signature from isBlank(String) to isBlank(CharSequence)
  65. */
  66. public static boolean isBlank(final CharSequence cs) {
  67. final int strLen;
  68. if (cs == null || (strLen = cs.length()) == 0) {
  69. return true;
  70. }
  71. for (int i = 0; i < strLen; i++) {
  72. if (!Character.isWhitespace(cs.charAt(i))) {
  73. return false;
  74. }
  75. }
  76. return true;
  77. }
  78. // Trim
  79. //-----------------------------------------------------------------------
  80. /**
  81. * <p>Removes control characters (char &lt;= 32) from both
  82. * ends of this String, handling {@code null} by returning {@code null}.</p>
  83. *
  84. * <p>The String is trimmed using {@link String#trim()}.
  85. * Trim removes start and end characters &lt;= 32.</p>
  86. *
  87. * <pre>
  88. * StringUtils.trim(null) = null
  89. * StringUtils.trim("") = ""
  90. * StringUtils.trim(" ") = ""
  91. * StringUtils.trim("abc") = "abc"
  92. * StringUtils.trim(" abc ") = "abc"
  93. * </pre>
  94. *
  95. * @param str the String to be trimmed, may be null
  96. * @return the trimmed string, {@code null} if null String input
  97. */
  98. public static String trim(final String str) {
  99. return str == null ? null : str.trim();
  100. }
  101. // Equals
  102. //-----------------------------------------------------------------------
  103. /**
  104. * <p>Compares two CharSequences, returning {@code true} if they represent
  105. * equal sequences of characters.</p>
  106. *
  107. * <p>{@code null}s are handled without exceptions. Two {@code null}
  108. * references are considered to be equal. The comparison is case sensitive.</p>
  109. *
  110. * <pre>
  111. * StringUtils.equals(null, null) = true
  112. * StringUtils.equals(null, "abc") = false
  113. * StringUtils.equals("abc", null) = false
  114. * StringUtils.equals("abc", "abc") = true
  115. * StringUtils.equals("abc", "ABC") = false
  116. * </pre>
  117. *
  118. * @param cs1 the first CharSequence, may be {@code null}
  119. * @param cs2 the second CharSequence, may be {@code null}
  120. * @return {@code true} if the CharSequences are equal (case-sensitive), or both {@code null}
  121. * @see Object#equals(Object)
  122. * @since 3.0 Changed signature from equals(String, String) to equals(CharSequence, CharSequence)
  123. */
  124. public static boolean equals(final CharSequence cs1, final CharSequence cs2) {
  125. if (cs1 == cs2) {
  126. return true;
  127. }
  128. if (cs1 == null || cs2 == null) {
  129. return false;
  130. }
  131. if (cs1 instanceof String && cs2 instanceof String) {
  132. return cs1.equals(cs2);
  133. }
  134. return StringUtils.regionMatches(cs1, false, 0, cs2, 0, Math.max(cs1.length(), cs2.length()));
  135. }
  136. /**
  137. * Green implementation of regionMatches.
  138. *
  139. * @param cs the {@code CharSequence} to be processed
  140. * @param ignoreCase whether or not to be case insensitive
  141. * @param thisStart the index to start on the {@code cs} CharSequence
  142. * @param substring the {@code CharSequence} to be looked for
  143. * @param start the index to start on the {@code substring} CharSequence
  144. * @param length character length of the region
  145. * @return whether the region matched
  146. */
  147. public static boolean regionMatches(final CharSequence cs, final boolean ignoreCase, final int thisStart,
  148. final CharSequence substring, final int start, final int length) {
  149. if (cs instanceof String && substring instanceof String) {
  150. return ((String) cs).regionMatches(ignoreCase, thisStart, (String) substring, start, length);
  151. }
  152. int index1 = thisStart;
  153. int index2 = start;
  154. int tmpLen = length;
  155. while (tmpLen-- > 0) {
  156. final char c1 = cs.charAt(index1++);
  157. final char c2 = substring.charAt(index2++);
  158. if (c1 == c2) {
  159. continue;
  160. }
  161. if (!ignoreCase) {
  162. return false;
  163. }
  164. // The same check as in String.regionMatches():
  165. if (Character.toUpperCase(c1) != Character.toUpperCase(c2) && Character.toLowerCase(c1) != Character
  166. .toLowerCase(c2)) {
  167. return false;
  168. }
  169. }
  170. return true;
  171. }
  172. }