Java String Last Index Of lastIndexOfUnnested(String str, char chr, String openBalance, String closeBalance)

Here you can find the source of lastIndexOfUnnested(String str, char chr, String openBalance, String closeBalance)

Description

last Index Of Unnested

License

Open Source License

Declaration

public static int lastIndexOfUnnested(String str, char chr, String openBalance, String closeBalance)
            throws IllegalArgumentException 

Method Source Code

//package com.java2s;
/*// w ww  .j  av a  2 s . c  o m
 * ParsingAid.java
 *
 * Created on May 21, 2007, 4:42 PM
 *
 *  Copyright (C) 2010 Tal Eisenberg
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

public class Main {
    public static int lastIndexOfUnnested(String str, char chr, String openBalance, String closeBalance)
            throws IllegalArgumentException {
        return lastIndexOfUnnested(str, chr, str.length() - 1, openBalance, closeBalance);
    }

    public static int lastIndexOfUnnested(String str, char chr, int fromIdx, String openBalance,
            String closeBalance) throws IllegalArgumentException {
        int idx = fromIdx + 1;
        boolean nested;
        do {
            idx = str.lastIndexOf(chr, idx - 1);
            if (idx == -1)
                return -1;
            else
                nested = isNested(str, idx, openBalance, closeBalance);
        } while (nested);
        return idx;
    }

    /**
     * Returns true if the character in the index parameter is nested inside balanced characters. 
     * for example, using the String "a(b,c),(d,e[f]),g'abcdefg'":
     * isNested(str, 0, "(['",")]'") will return false while the same call with an index of 3 will 
     * return true. The length of openBalance must be exactly the same as the length of closeBalance.
     * each character in openBalance is paired with the character of the same index in closeBalance.
     * For example if openBalance="([" closeBalance should be ")]" if the same type of brackets are to be paired.
     * 
     * @param str   The String that will be parsed
     * @param index The index of the character that will be checked.
     * @param openBalance   A String containing all the balance opening characters.
     * @param closeBalance  A String containing all the balance closing characters.
     */
    public static boolean isNested(String str, int index, String openBalance, String closeBalance)
            throws IllegalArgumentException {
        if (index == 0)
            return false; // first char can't be nested...
        char[] openers = openBalance.toCharArray();
        char[] closers = closeBalance.toCharArray();
        if (closers.length != openers.length)
            throw new IllegalArgumentException(
                    "There must be the same number of balance opener and balance closer characters.");
        for (int i = 0; i < openers.length; i++) {
            //make sure the balance is 0 before index
            int count = 0;
            for (int x = 0; x < index; x++) {
                if (str.charAt(x) == openers[i])
                    count++;
                else if (str.charAt(x) == closers[i])
                    count--;
            }
            if (closers[i] == openers[i]) {
                if (count % 2 != 0)
                    return true;
            } else if (count != 0)
                return true;
        }
        return false;
    }
}

Related

  1. lastIndexOfNonWhitespace(final String src)
  2. lastIndexOfOrdinal(String value, String part, int startIndex, int count, boolean caseSensitive)
  3. lastIndexOfPathSeparator(String str)
  4. lastIndexOfSeparator(String path)
  5. lastIndexOfUCL(String value)
  6. lastIndexOfWhitespace(String s)