Skip to content

Commit 11b5a22

Browse files
committed
Deduplicate code of getStringResultFromRegexArray methods in Utils
Also revert indentation in Utils.mixedNumberWordToLong.
1 parent 9ca647a commit 11b5a22

File tree

1 file changed

+9
-16
lines changed
  • extractor/src/main/java/org/schabi/newpipe/extractor/utils

1 file changed

+9
-16
lines changed

extractor/src/main/java/org/schabi/newpipe/extractor/utils/Utils.java

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import java.util.LinkedList;
1616
import java.util.List;
1717
import java.util.Map;
18+
import java.util.Objects;
1819
import java.util.regex.Pattern;
1920

2021
public final class Utils {
@@ -73,9 +74,8 @@ public static long mixedNumberWordToLong(final String numberWord)
7374
multiplier = Parser.matchGroup("[\\d]+([\\.,][\\d]+)?([KMBkmb])+", numberWord, 2);
7475
} catch (final ParsingException ignored) {
7576
}
76-
77-
final double count = Double.parseDouble(Parser.matchGroup1("([\\d]+([\\.,][\\d]+)?)",
78-
numberWord).replace(",", "."));
77+
final double count = Double.parseDouble(
78+
Parser.matchGroup1("([\\d]+([\\.,][\\d]+)?)", numberWord).replace(",", "."));
7979
switch (multiplier.toUpperCase()) {
8080
case "K":
8181
return (long) (count * 1e3);
@@ -391,19 +391,12 @@ public static String getStringResultFromRegexArray(@Nonnull final String input,
391391
@Nonnull final String[] regexes,
392392
final int group)
393393
throws Parser.RegexException {
394-
for (final String regex : regexes) {
395-
try {
396-
final String result = Parser.matchGroup(regex, input, group);
397-
if (result != null) {
398-
return result;
399-
}
400-
401-
// Continue if the result is null
402-
} catch (final Parser.RegexException ignored) {
403-
}
404-
}
405-
406-
throw new Parser.RegexException("No regex matched the input on group " + group);
394+
return getStringResultFromRegexArray(input,
395+
Arrays.stream(regexes)
396+
.filter(Objects::nonNull)
397+
.map(Pattern::compile)
398+
.toArray(Pattern[]::new),
399+
group);
407400
}
408401

409402
/**

0 commit comments

Comments
 (0)