001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.codec.binary;
018
019/**
020 * <p>
021 * Operations on {@link CharSequence} that are {@code null} safe.
022 * </p>
023 * <p>
024 * Copied from Apache Commons Lang r1586295 on April 10, 2014 (day of 3.3.2 release).
025 * </p>
026 *
027 * @see CharSequence
028 * @since 1.10
029 */
030public class CharSequenceUtils {
031
032    /**
033     * Green implementation of regionMatches.
034     * 
035     * <p>Note: This function differs from the current implementation in Apache Commons Lang
036     * where the input indices are not valid. It is only used within this package.
037     *
038     * @param cs
039     *            the {@code CharSequence} to be processed
040     * @param ignoreCase
041     *            whether or not to be case insensitive
042     * @param thisStart
043     *            the index to start on the {@code cs} CharSequence
044     * @param substring
045     *            the {@code CharSequence} to be looked for
046     * @param start
047     *            the index to start on the {@code substring} CharSequence
048     * @param length
049     *            character length of the region
050     * @return whether the region matched
051     */
052    static boolean regionMatches(final CharSequence cs, final boolean ignoreCase, final int thisStart,
053            final CharSequence substring, final int start, final int length) {
054        if (cs instanceof String && substring instanceof String) {
055            return ((String) cs).regionMatches(ignoreCase, thisStart, (String) substring, start, length);
056        }
057        int index1 = thisStart;
058        int index2 = start;
059        int tmpLen = length;
060
061        while (tmpLen-- > 0) {
062            final char c1 = cs.charAt(index1++);
063            final char c2 = substring.charAt(index2++);
064
065            if (c1 == c2) {
066                continue;
067            }
068
069            if (!ignoreCase) {
070                return false;
071            }
072
073            // The same check as in String.regionMatches():
074            if (Character.toUpperCase(c1) != Character.toUpperCase(c2) &&
075                    Character.toLowerCase(c1) != Character.toLowerCase(c2)) {
076                return false;
077            }
078        }
079
080        return true;
081    }
082}