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.  *      http://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</code> 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.      * @param cs
  34.      *            the <code>CharSequence</code> to be processed
  35.      * @param ignoreCase
  36.      *            whether or not to be case insensitive
  37.      * @param thisStart
  38.      *            the index to start on the <code>cs</code> CharSequence
  39.      * @param substring
  40.      *            the <code>CharSequence</code> to be looked for
  41.      * @param start
  42.      *            the index to start on the <code>substring</code> CharSequence
  43.      * @param length
  44.      *            character length of the region
  45.      * @return whether the region matched
  46.      */
  47.     static boolean regionMatches(final CharSequence cs, final boolean ignoreCase, final int thisStart,
  48.             final CharSequence substring, final int start, final int length) {
  49.         if (cs instanceof String && substring instanceof String) {
  50.             return ((String) cs).regionMatches(ignoreCase, thisStart, (String) substring, start, length);
  51.         }
  52.         int index1 = thisStart;
  53.         int index2 = start;
  54.         int tmpLen = length;

  55.         while (tmpLen-- > 0) {
  56.             final char c1 = cs.charAt(index1++);
  57.             final char c2 = substring.charAt(index2++);

  58.             if (c1 == c2) {
  59.                 continue;
  60.             }

  61.             if (!ignoreCase) {
  62.                 return false;
  63.             }

  64.             // The same check as in String.regionMatches():
  65.             if (Character.toUpperCase(c1) != Character.toUpperCase(c2) &&
  66.                     Character.toLowerCase(c1) != Character.toLowerCase(c2)) {
  67.                 return false;
  68.             }
  69.         }

  70.         return true;
  71.     }
  72. }