2021-07-29 21:23:00 +02:00
|
|
|
package org.schabi.newpipe.extractor.utils;
|
|
|
|
|
2021-07-29 21:33:45 +02:00
|
|
|
import javax.annotation.Nonnull;
|
2021-07-29 21:23:00 +02:00
|
|
|
|
2022-03-17 15:57:30 +01:00
|
|
|
public final class StringUtils {
|
2021-07-29 21:33:45 +02:00
|
|
|
|
|
|
|
private StringUtils() {
|
2021-07-29 21:23:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-07-31 16:05:24 +02:00
|
|
|
* @param string The string to search in.
|
2021-07-29 21:23:00 +02:00
|
|
|
* @param start A string from which to start searching.
|
2021-07-31 16:05:24 +02:00
|
|
|
* @return A substring where each '{' matches a '}'.
|
2021-07-29 21:33:45 +02:00
|
|
|
* @throws IndexOutOfBoundsException If {@code string} does not contain {@code start}
|
2021-07-31 16:05:24 +02:00
|
|
|
* or parenthesis could not be matched .
|
2021-07-29 21:23:00 +02:00
|
|
|
*/
|
2021-07-29 21:33:45 +02:00
|
|
|
@Nonnull
|
2022-03-17 15:57:30 +01:00
|
|
|
public static String matchToClosingParenthesis(@Nonnull final String string,
|
|
|
|
@Nonnull final String start) {
|
2021-07-29 21:23:00 +02:00
|
|
|
int startIndex = string.indexOf(start);
|
|
|
|
if (startIndex < 0) {
|
|
|
|
throw new IndexOutOfBoundsException();
|
|
|
|
}
|
|
|
|
|
|
|
|
startIndex += start.length();
|
|
|
|
int endIndex = startIndex;
|
|
|
|
while (string.charAt(endIndex) != '{') {
|
|
|
|
++endIndex;
|
|
|
|
}
|
|
|
|
++endIndex;
|
|
|
|
|
|
|
|
int openParenthesis = 1;
|
2021-07-31 16:05:24 +02:00
|
|
|
while (openParenthesis > 0) {
|
2021-07-29 21:23:00 +02:00
|
|
|
switch (string.charAt(endIndex)) {
|
|
|
|
case '{':
|
|
|
|
++openParenthesis;
|
|
|
|
break;
|
|
|
|
case '}':
|
|
|
|
--openParenthesis;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
++endIndex;
|
|
|
|
}
|
|
|
|
|
|
|
|
return string.substring(startIndex, endIndex);
|
|
|
|
}
|
|
|
|
}
|