View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      https://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.codec.binary;
18  
19  /**
20   * <p>
21   * Operations on {@link CharSequence} that are {@code null} safe.
22   * </p>
23   * <p>
24   * Copied from Apache Commons Lang r1586295 on April 10, 2014 (day of 3.3.2 release).
25   * </p>
26   *
27   * @see CharSequence
28   * @since 1.10
29   */
30  public class CharSequenceUtils {
31  
32      /**
33       * Green implementation of regionMatches.
34       *
35       * <p>
36       * Note: This function differs from the current implementation in Apache Commons Lang where the input indices are not valid. It is only used within this
37       * package.
38       * </p>
39       *
40       * @param cs         the {@code CharSequence} to be processed.
41       * @param ignoreCase whether or not to be case-insensitive.
42       * @param thisStart  the index to start on the {@code cs} CharSequence.
43       * @param substring  the {@code CharSequence} to be looked for.
44       * @param start      the index to start on the {@code substring} CharSequence.
45       * @param length     character length of the region.
46       * @return whether the region matched.
47       */
48      static boolean regionMatches(final CharSequence cs, final boolean ignoreCase, final int thisStart,
49              final CharSequence substring, final int start, final int length) {
50          if (cs instanceof String && substring instanceof String) {
51              return ((String) cs).regionMatches(ignoreCase, thisStart, (String) substring, start, length);
52          }
53          int index1 = thisStart;
54          int index2 = start;
55          int tmpLen = length;
56          while (tmpLen-- > 0) {
57              final char c1 = cs.charAt(index1++);
58              final char c2 = substring.charAt(index2++);
59              if (c1 == c2) {
60                  continue;
61              }
62              if (!ignoreCase) {
63                  return false;
64              }
65              // The same check as in String.regionMatches():
66              if (Character.toUpperCase(c1) != Character.toUpperCase(c2) &&
67                      Character.toLowerCase(c1) != Character.toLowerCase(c2)) {
68                  return false;
69              }
70          }
71          return true;
72      }
73  
74      /**
75       * Consider private.
76       *
77       * @deprecated Will be private in the next major version.
78       */
79      @Deprecated
80      public CharSequenceUtils() {
81          // empty
82      }
83  }