src/main/java/tinyrtpl/Rtpl.java
private static int indexOfEndingOverPairs(String s, int beginPos, String ending, String p1, String p2) {
        int endingOccur = s.indexOf(ending, beginPos);
        if (endingOccur < 0) return -1;

        int p1Count = 0;
        int p2Count = 0;
        int p1Len = p1.length();
        int p2Len = p2.length();
        int i;

        while (true) {
            for (i = beginPos; i < endingOccur; i++) {
                if (s.indexOf(p1, i) == i) {
                    p1Count++;
                    i += p1Len - 1;
                    continue;
                }
                if (s.indexOf(p2, i) == i) {
                    p2Count++;
                    if (p2Count == p1Count) {
                        break;
                    }
                    i += p2Len - 1;
                    continue;
                }
            }
            if (p1Count == p2Count) return endingOccur;
            int nextEndingOccur = s.indexOf(ending, endingOccur + 1);
            if (nextEndingOccur < 0) return endingOccur;
            beginPos = endingOccur;
            endingOccur = nextEndingOccur;
        }
    }
/**
     * Find the occurrence of the target pattern, from the beginning position in a source string,
     * where p1 and p2 which will appear in pair and should be skipped. Sometimes p2 equals the pattern.
     * @param s: the string scope
     * @param beginPos: begin position of the indexing task
     * @param ending: the pattern to find
     * @param p1: the pair beginning
     * @param p2: the pair ending
     * @return the occurrence of the ending pattern
     */
Find the occurrence of the target pattern, from the beginning position in a source string,
where p1 and p2 which will appear in pair and should be skipped. Sometimes p2 equals the pattern.
Find the occurrence of the target pattern from the beginning position in a source string where p1 and p2 which will appear in pair and should be skipped . Sometimes p2 equals the pattern .
private static int indexOfEndingOverPairs(String s, int beginPos, String ending, String p1, String p2) {
        int endingOccur = s.indexOf(ending, beginPos);
        if (endingOccur < 0) return -1;
        int p1Count = 0;
        int p2Count = 0;
        int p1Len = p1.length();
        int p2Len = p2.length();
        int i;
        while (true) {
            for (i = beginPos; i < endingOccur; i++) {
                if (s.indexOf(p1, i) == i) {
                    p1Count++;
                    i += p1Len - 1;
                    continue;
                }
                if (s.indexOf(p2, i) == i) {
                    p2Count++;
                    if (p2Count == p1Count) {
                        break;
                    }
                    i += p2Len - 1;
                    continue;
                }
            }
            if (p1Count == p2Count) return endingOccur;
            int nextEndingOccur = s.indexOf(ending, endingOccur + 1);
            if (nextEndingOccur < 0) return endingOccur;
            beginPos = endingOccur;
            endingOccur = nextEndingOccur;
        }
    }
private static int indexOfEndingOverPairs ( String s , int beginPos , String ending , String p1 , String p2 ) { int endingOccur = s . indexOf ( ending , beginPos ) ; if ( endingOccur < 0 ) return - 1 ; int p1Count = 0 ; int p2Count = 0 ; int p1Len = p1 . length ( ) ; int p2Len = p2 . length ( ) ; int i ; while ( true ) { for ( i = beginPos ; i < endingOccur ; i ++ ) { if ( s . indexOf ( p1 , i ) == i ) { p1Count ++ ; i += p1Len - 1 ; continue ; } if ( s . indexOf ( p2 , i ) == i ) { p2Count ++ ; if ( p2Count == p1Count ) { break ; } i += p2Len - 1 ; continue ; } } if ( p1Count == p2Count ) return endingOccur ; int nextEndingOccur = s . indexOf ( ending , endingOccur + 1 ) ; if ( nextEndingOccur < 0 ) return endingOccur ; beginPos = endingOccur ; endingOccur = nextEndingOccur ; } }
Find the occurrence of the target pattern, from the beginning position in a source string,
 where p1 and p2 which will appear in pair and should be skipped.
Find the occurrence of the target pattern from the beginning position in a source string where p1 and p2 which will appear in pair and should be skipped .