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    *      http://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
37       * where the input indices are not valid. It is only used within this package.
38       * </p>
39       *
40       * @param cs
41       *            the {@code CharSequence} to be processed
42       * @param ignoreCase
43       *            whether or not to be case-insensitive
44       * @param thisStart
45       *            the index to start on the {@code cs} CharSequence
46       * @param substring
47       *            the {@code CharSequence} to be looked for
48       * @param start
49       *            the index to start on the {@code substring} CharSequence
50       * @param length
51       *            character length of the region
52       * @return whether the region matched
53       */
54      static boolean regionMatches(final CharSequence cs, final boolean ignoreCase, final int thisStart,
55              final CharSequence substring, final int start, final int length) {
56          if (cs instanceof String && substring instanceof String) {
57              return ((String) cs).regionMatches(ignoreCase, thisStart, (String) substring, start, length);
58          }
59          int index1 = thisStart;
60          int index2 = start;
61          int tmpLen = length;
62          while (tmpLen-- > 0) {
63              final char c1 = cs.charAt(index1++);
64              final char c2 = substring.charAt(index2++);
65              if (c1 == c2) {
66                  continue;
67              }
68              if (!ignoreCase) {
69                  return false;
70              }
71              // The same check as in String.regionMatches():
72              if (Character.toUpperCase(c1) != Character.toUpperCase(c2) &&
73                      Character.toLowerCase(c1) != Character.toLowerCase(c2)) {
74                  return false;
75              }
76          }
77          return true;
78      }
79  }