CharSequenceUtils.java

  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.  *      https://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.  * <p>
  20.  * Operations on {@link CharSequence} that are {@code null} safe.
  21.  * </p>
  22.  * <p>
  23.  * Copied from Apache Commons Lang r1586295 on April 10, 2014 (day of 3.3.2 release).
  24.  * </p>
  25.  *
  26.  * @see CharSequence
  27.  * @since 1.10
  28.  */
  29. public class CharSequenceUtils {

  30.     /**
  31.      * Green implementation of regionMatches.
  32.      *
  33.      * <p>
  34.      * Note: This function differs from the current implementation in Apache Commons Lang
  35.      * where the input indices are not valid. It is only used within this package.
  36.      * </p>
  37.      *
  38.      * @param cs
  39.      *            the {@code CharSequence} to be processed
  40.      * @param ignoreCase
  41.      *            whether or not to be case-insensitive
  42.      * @param thisStart
  43.      *            the index to start on the {@code cs} CharSequence
  44.      * @param substring
  45.      *            the {@code CharSequence} to be looked for
  46.      * @param start
  47.      *            the index to start on the {@code substring} CharSequence
  48.      * @param length
  49.      *            character length of the region
  50.      * @return whether the region matched
  51.      */
  52.     static boolean regionMatches(final CharSequence cs, final boolean ignoreCase, final int thisStart,
  53.             final CharSequence substring, final int start, final int length) {
  54.         if (cs instanceof String && substring instanceof String) {
  55.             return ((String) cs).regionMatches(ignoreCase, thisStart, (String) substring, start, length);
  56.         }
  57.         int index1 = thisStart;
  58.         int index2 = start;
  59.         int tmpLen = length;
  60.         while (tmpLen-- > 0) {
  61.             final char c1 = cs.charAt(index1++);
  62.             final char c2 = substring.charAt(index2++);
  63.             if (c1 == c2) {
  64.                 continue;
  65.             }
  66.             if (!ignoreCase) {
  67.                 return false;
  68.             }
  69.             // The same check as in String.regionMatches():
  70.             if (Character.toUpperCase(c1) != Character.toUpperCase(c2) &&
  71.                     Character.toLowerCase(c1) != Character.toLowerCase(c2)) {
  72.                 return false;
  73.             }
  74.         }
  75.         return true;
  76.     }

  77.     /**
  78.      * Consider private.
  79.      *
  80.      * @deprecated Will be private in the next major version.
  81.      */
  82.     @Deprecated
  83.     public CharSequenceUtils() {
  84.         // empty
  85.     }
  86. }