Source code
Description: source: package: org.apache.commons.codec.binary, class: CharSequenceUtils

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