From 9bc418357e2bb6731bb101bbc424ba532379f983 Mon Sep 17 00:00:00 2001 From: Maschell Date: Thu, 28 Feb 2019 22:13:52 +0100 Subject: [PATCH] [NUSTitle] Improve getFSTEntriesByRegEx --- src/de/mas/wiiu/jnus/NUSTitle.java | 46 ++++++++++++++++++++---------- 1 file changed, 31 insertions(+), 15 deletions(-) diff --git a/src/de/mas/wiiu/jnus/NUSTitle.java b/src/de/mas/wiiu/jnus/NUSTitle.java index e0972fe..1d13925 100644 --- a/src/de/mas/wiiu/jnus/NUSTitle.java +++ b/src/de/mas/wiiu/jnus/NUSTitle.java @@ -100,24 +100,40 @@ public class NUSTitle { } public List getFSTEntriesByRegEx(String regEx) { - List files = getAllFSTEntriesFlat(); - Pattern p = Pattern.compile(regEx); - - List result = new ArrayList<>(); - - for (FSTEntry f : files) { - String match = f.getFullPath().replace(File.separator, "/"); - Matcher m = p.matcher(match); - if (m.matches()) { - result.add(f); - } - } - return result; + return getFSTEntriesByRegEx(regEx, FST.getRoot()); } - + + public List getFSTEntriesByRegEx(String regEx, FSTEntry entry) { + return getFSTEntriesByRegEx(regEx, entry, true); + } + + public List getFSTEntriesByRegEx(String regEx, boolean onlyInPackage) { + return getFSTEntriesByRegEx(regEx, FST.getRoot(), onlyInPackage); + } + + public List getFSTEntriesByRegEx(String regEx, FSTEntry entry, boolean allowNotInPackage) { + Pattern p = Pattern.compile(regEx); + return getFSTEntriesByRegExStream(p, entry, allowNotInPackage).collect(Collectors.toList()); + } + + private Stream getFSTEntriesByRegExStream(Pattern p, FSTEntry entry, boolean allowNotInPackage) { + return entry.getChildren().stream()// + .filter(e -> allowNotInPackage || !e.isNotInPackage()) // + .flatMap(e -> { + if (!e.isDir()) { + if (p.matcher(entry.getFullPath().replace("/", File.separator)).matches()) { + return Stream.of(entry); + } else { + return Stream.empty(); + } + } + return getFSTEntriesByRegExStream(p, e, allowNotInPackage); + }); + } + public Optional getFileEntryDir(String string) { return getFileEntryDir(string.replace("/", File.separator), FST.getRoot()); - } + } public Optional getFileEntryDir(String string, FSTEntry curEntry) { for (val curChild : curEntry.getDirChildren()) {