/*
 * Copyright 2000-2016 JetBrains s.r.o.
 *
 * 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.
 */
package com.intellij.openapi.util.text;

import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;

import static java.lang.Math.min;

/**
 * @author Irina.Chernushina on 12/2/2015.
 */
public class InjectorMatchingEndFinder {
  public static int findMatchingEnd(@NotNull String startSymbol, @NotNull String endSymbol, @NotNull String text, int afterStartIdx) {
    if (afterStartIdx < 0) return -1;

    return findForLongSymbols(text, afterStartIdx, startSymbol, endSymbol);
  }

  private static int findForLongSymbols(@NotNull final String text, int afterStartIdx,
                                                         final @NotNull String startSymbol, final @NotNull String endSymbol) {
    int totalNumStarts = 1;
    int lookFrom = afterStartIdx;
    while (totalNumStarts > 0) {
      --totalNumStarts;
      int nextEndIdx = text.indexOf(endSymbol, lookFrom);
      if (nextEndIdx == -1) return -1;
      final int numStarts = getOccurrenceCount(text, lookFrom, nextEndIdx, startSymbol);
      if (numStarts > 0) {
        totalNumStarts += numStarts;
      }
      lookFrom = nextEndIdx + endSymbol.length();
      if (totalNumStarts == 0) return nextEndIdx;
    }
    return -1;
  }

  @Contract(pure = true)
  private static int getOccurrenceCount(@NotNull String text, final int from, final int toExcluding, final String s) {
    int res = 0;
    int i = from;
    final int limit = min(text.length(), toExcluding);
    while (i < limit) {
      i = text.indexOf(s, i);
      if (i >= 0 && i < limit) {
        res++;
        i+=s.length();
      }
      else {
        break;
      }
    }
    return res;
  }
}

@@ -719,16 +719,59 @@
 NotNull;
+%0Aimport org.jetbrains.annotations.Nullable;
 %0A%0Aimport
@@ -1073,78 +1073,1658 @@
     
-return findForLongSymbols(text, afterStartIdx, startSymbol, endSymbol)
+final Character shortStartSymbol = defineShortSymbol(startSymbol);%0A    final Character shortEndSymbol = defineShortSymbol(endSymbol);%0A%0A    if (shortStartSymbol != null %7C%7C shortEndSymbol != null) %7B%0A      return findForShortSymbols(nonNullifyShortSymbol(shortStartSymbol, startSymbol),%0A                                 nonNullifyShortSymbol(shortEndSymbol, endSymbol),%0A                                 text, afterStartIdx, endSymbol);%0A    %7D%0A    return findForLongSymbols(text, afterStartIdx, startSymbol, endSymbol);%0A  %7D%0A%0A  private static char nonNullifyShortSymbol(@Nullable Character shortStartSymbol, @NotNull String startSymbol) %7B%0A    return shortStartSymbol == null ? startSymbol.charAt(0) : shortStartSymbol;%0A  %7D%0A%0A  private static Character defineShortSymbol(final String s) %7B%0A    if (s.length() == 2 && s.charAt(0) == s.charAt(1)) return s.charAt(0);%0A    return null;%0A  %7D%0A%0A  private static int findForShortSymbols(char shortStartSymbol, char shortEndSymbol, @NotNull final String text,%0A                                                          int afterStartIdx, final @NotNull String endSymbol) %7B%0A    int totalNumStarts = 1;%0A    int lookFrom = afterStartIdx;%0A    while (totalNumStarts %3E 0) %7B%0A      --totalNumStarts;%0A      int nextEndIdx = text.indexOf(endSymbol, lookFrom);%0A      if (nextEndIdx == -1) return -1;%0A      final int numStarts = getOccurrenceCount(text, lookFrom, nextEndIdx, shortStartSymbol);%0A      final int numEnds = getOccurrenceCount(text, lookFrom, nextEndIdx, shortEndSymbol);%0A      totalNumStarts += numStarts - numEnds;%0A      lookFrom = nextEndIdx + 1;%0A      if (totalNumStarts %3C= 0) return nextEndIdx;%0A    %7D%0A    return -1
 ;%0A  
@@ -3443,16 +3443,434 @@
 1;%0A  %7D%0A%0A
+  @Contract(pure = true)%0A  private static int getOccurrenceCount(@NotNull String text, final int from, final int toExcluding, final char c) %7B%0A    int res = 0;%0A    int i = from;%0A    final int limit = min(text.length(), toExcluding);%0A    while (i %3C limit) %7B%0A      i = text.indexOf(c, i);%0A      if (i %3E= 0 && i %3C limit) %7B%0A        res++;%0A        i++;%0A      %7D%0A      else %7B%0A        break;%0A      %7D%0A    %7D%0A    return res;%0A  %7D%0A%0A
   @Contr

Revert "don't try to trim start/end (WEB-27121)"