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