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