Java Array Last Index Of lastIndexOfArray(int r[], int rpos, int rend, char d[], int dpos)

Here you can find the source of lastIndexOfArray(int r[], int rpos, int rend, char d[], int dpos)

Description

Get the offset of a last occurance of an int array within a char array.

License

Apache License

Parameter

Parameter Description
r The array containing the data that need to be matched in d.
rpos The index of the first character in r to look for.
rend The index of the last character in r to look for plus 1.
d The array of char that should contain a part of r.
dpos The starting offset in d for the matching.

Return

The offset in d of the last part of r matched in d or -1 if that was not found.

Declaration

protected static int lastIndexOfArray(int r[], int rpos, int rend, char d[], int dpos) 

Method Source Code

//package com.java2s;
/*/*from   w w  w . ja v  a  2s.c  o  m*/
 * Copyright 1999-2005 The Apache Software Foundation.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

public class Main {
    /**
      * Get the offset of a last occurance of an int array within a char array.
      * <br>
      * This method return the index in d of the last occurrence after dpos of
      * that part of array specified by r, starting at rpos and terminating at
      * rend.
      *
      * @param r The array containing the data that need to be matched in d.
      * @param rpos The index of the first character in r to look for.
      * @param rend The index of the last character in r to look for plus 1.
      * @param d The array of char that should contain a part of r.
      * @param dpos The starting offset in d for the matching.
      * @return The offset in d of the last part of r matched in d or -1 if that was
      *         not found.
      */
    protected static int lastIndexOfArray(int r[], int rpos, int rend, char d[], int dpos) {
        // Check if pos and len are legal
        if (rend < rpos)
            throw new IllegalArgumentException("rend < rpos");
        // If we need to match a zero length string return current dpos
        if (rend == rpos)
            return (d.length); //?? dpos?

        // If we need to match a 1 char length string do it simply
        if ((rend - rpos) == 1) {
            // Search for the specified character
            for (int x = d.length - 1; x > dpos; x--)
                if (r[rpos] == d[x])
                    return (x);
        }

        // Main string matching loop. It gets executed if the characters to
        // match are less then the characters left in the d buffer
        int l = d.length - (rend - rpos);
        while (l >= dpos) {
            // Set current startpoint in d
            int y = l;
            // Check every character in d for equity. If the string is matched
            // return dpos
            for (int x = rpos; x <= rend; x++) {
                if (x == rend)
                    return (l);
                if (r[x] != d[y++])
                    break;
            }
            // Decrease l to search for the same string at next offset
            l--;
        }
        // The remaining chars in d buffer were not enough or the string
        // wasn't matched
        return (-1);
    }
}

Related

  1. lastIndexOfAny(String str, String[] searchStrs)
  2. lastIndexOfAny(String str, String[] searchStrs)
  3. lastIndexOfAny(String str, String[] searchStrs)
  4. lastIndexOfAny(String string, char[] anyOf)
  5. lastIndexOfAnyNoCheck(byte[] values, byte[] array, int index, int length)
  6. lastIndexOfChars(String src, char[] chars, int startIndex, int endIndex)
  7. lastIndexOfInsensitive(String o, String[] vals)
  8. lastIndexOfNoCheck(byte value, byte[] array, int index, int length)
  9. lastIndexOfNot(byte[] array, int obj)