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>
036     * Note: This function differs from the current implementation in Apache Commons Lang
037     * where the input indices are not valid. It is only used within this package.
038     * </p>
039     *
040     * @param cs
041     *            the {@code CharSequence} to be processed
042     * @param ignoreCase
043     *            whether or not to be case-insensitive
044     * @param thisStart
045     *            the index to start on the {@code cs} CharSequence
046     * @param substring
047     *            the {@code CharSequence} to be looked for
048     * @param start
049     *            the index to start on the {@code substring} CharSequence
050     * @param length
051     *            character length of the region
052     * @return whether the region matched
053     */
054    static boolean regionMatches(final CharSequence cs, final boolean ignoreCase, final int thisStart,
055            final CharSequence substring, final int start, final int length) {
056        if (cs instanceof String && substring instanceof String) {
057            return ((String) cs).regionMatches(ignoreCase, thisStart, (String) substring, start, length);
058        }
059        int index1 = thisStart;
060        int index2 = start;
061        int tmpLen = length;
062        while (tmpLen-- > 0) {
063            final char c1 = cs.charAt(index1++);
064            final char c2 = substring.charAt(index2++);
065            if (c1 == c2) {
066                continue;
067            }
068            if (!ignoreCase) {
069                return false;
070            }
071            // The same check as in String.regionMatches():
072            if (Character.toUpperCase(c1) != Character.toUpperCase(c2) &&
073                    Character.toLowerCase(c1) != Character.toLowerCase(c2)) {
074                return false;
075            }
076        }
077        return true;
078    }
079}