From 0ac8d2d6cc6e8e3837bfa03b81b3d266cf410876 Mon Sep 17 00:00:00 2001 From: Reinhard Pointner Date: Fri, 1 Apr 2016 17:48:01 +0000 Subject: [PATCH] Fix https://www.filebot.net/forums/viewtopic.php?f=5&t=3579&p=20057#p20057 --- .../filebot/archive/SevenZipExecutable.java | 4 ++- .../format/ExpressionFormatMethods.java | 13 ++++--- source/net/filebot/media/MediaDetection.java | 3 +- .../similarity/CommonSequenceMatcher.java | 3 +- .../net/filebot/similarity/Normalization.java | 22 ++++++++---- .../filebot/similarity/SeriesNameMatcher.java | 2 +- .../net/filebot/ui/rename/HistoryDialog.java | 3 +- .../net/filebot/ui/rename/PresetEditor.java | 4 ++- .../filebot/ui/subtitle/SubtitleViewer.java | 35 ++++++++----------- source/net/filebot/util/FileUtilities.java | 3 +- source/net/filebot/util/StringUtilities.java | 2 +- 11 files changed, 52 insertions(+), 42 deletions(-) diff --git a/source/net/filebot/archive/SevenZipExecutable.java b/source/net/filebot/archive/SevenZipExecutable.java index e4c2d13b..477d450a 100644 --- a/source/net/filebot/archive/SevenZipExecutable.java +++ b/source/net/filebot/archive/SevenZipExecutable.java @@ -1,5 +1,7 @@ package net.filebot.archive; +import static net.filebot.util.StringUtilities.*; + import static java.nio.charset.StandardCharsets.*; import static java.util.Arrays.*; import static net.filebot.Logging.*; @@ -55,7 +57,7 @@ public class SevenZipExecutable implements ArchiveExtractor { if (returnCode == 0) { return output; } else { - throw new IOException(String.format("%s failed with exit code %d: %s", get7zCommand(), returnCode, output.replaceAll("\\s+", " ").trim())); + throw new IOException(String.format("%s failed with exit code %d: %s", get7zCommand(), returnCode, SPACE.matcher(output).replaceAll(" ").trim())); } } catch (InterruptedException e) { throw new IOException(String.format("%s timed out", get7zCommand()), e); diff --git a/source/net/filebot/format/ExpressionFormatMethods.java b/source/net/filebot/format/ExpressionFormatMethods.java index ba0ae0de..4a8591f6 100644 --- a/source/net/filebot/format/ExpressionFormatMethods.java +++ b/source/net/filebot/format/ExpressionFormatMethods.java @@ -117,7 +117,10 @@ public class ExpressionFormatMethods { * e.g. "Doctor Who" -> "Doctor_Who" */ public static String space(String self, String replacement) { - return self.replaceAll("[:?._]", " ").trim().replaceAll("\\s+", replacement); + self = self.replaceAll("[:?._]", " ").trim(); + + // replace space sequences with a single blank + return Normalization.replaceSpace(self, replacement); } /** @@ -126,7 +129,7 @@ public class ExpressionFormatMethods { * e.g. "Sissi: The Young Empress" -> "Sissi - The Young Empress" */ public static String colon(String self, String replacement) { - return self.replaceAll("\\s*[:]\\s*", replacement); + return compile("\\s*[:]\\s*", UNICODE_CHARACTER_CLASS).matcher(self).replaceAll(replacement); } /** @@ -151,7 +154,7 @@ public class ExpressionFormatMethods { } public static String sortName(String self, String replacement) { - return compile("^(The|A|An)\\s(.+)", CASE_INSENSITIVE).matcher(self).replaceFirst(replacement).trim(); + return compile("^(The|A|An)\\s(.+)", CASE_INSENSITIVE | UNICODE_CHARACTER_CLASS).matcher(self).replaceFirst(replacement).trim(); } public static String sortInitial(String self) { @@ -268,7 +271,7 @@ public class ExpressionFormatMethods { } public static String replaceTrailingBrackets(String self, String replacement) { - return self.replaceAll("\\s*[(]([^)]*)[)]$", replacement).trim(); + return compile("\\s*[(]([^)]*)[)]$", UNICODE_CHARACTER_CLASS).matcher(self).replaceAll(replacement).trim(); } /** @@ -285,7 +288,7 @@ public class ExpressionFormatMethods { String[] patterns = new String[] { "\\s*[(](\\w+)[)]$", "\\W+Part (\\w+)\\W*$" }; for (String pattern : patterns) { - Matcher matcher = compile(pattern, CASE_INSENSITIVE).matcher(self); + Matcher matcher = compile(pattern, CASE_INSENSITIVE | UNICODE_CHARACTER_CLASS).matcher(self); if (matcher.find()) { return matcher.replaceAll(replacement).trim(); } diff --git a/source/net/filebot/media/MediaDetection.java b/source/net/filebot/media/MediaDetection.java index 7dbfa50b..7d3ebc17 100644 --- a/source/net/filebot/media/MediaDetection.java +++ b/source/net/filebot/media/MediaDetection.java @@ -1350,10 +1350,9 @@ public class MediaDetection { private static class HighPerformanceMatcher extends CommonSequenceMatcher { private static final Collator collator = getLenientCollator(Locale.ENGLISH); - private static final Pattern space = Pattern.compile("\\s+"); public static CollationKey[] prepare(String sequence) { - String[] words = space.split(sequence); + String[] words = SPACE.split(sequence); CollationKey[] keys = new CollationKey[words.length]; for (int i = 0; i < words.length; i++) { keys[i] = collator.getCollationKey(words[i]); diff --git a/source/net/filebot/similarity/CommonSequenceMatcher.java b/source/net/filebot/similarity/CommonSequenceMatcher.java index b9e33ea2..bec74ed8 100644 --- a/source/net/filebot/similarity/CommonSequenceMatcher.java +++ b/source/net/filebot/similarity/CommonSequenceMatcher.java @@ -2,6 +2,7 @@ package net.filebot.similarity; import static java.util.Arrays.*; import static java.util.Collections.*; +import static net.filebot.util.StringUtilities.*; import java.text.CollationKey; import java.text.Collator; @@ -77,7 +78,7 @@ public class CommonSequenceMatcher { } public CollationKey[] split(String sequence) { - return getCollationKeys(sequence.split("\\s+")); + return getCollationKeys(SPACE.split(sequence)); } private final Map collationKeyDictionary = synchronizedMap(new HashMap(256)); diff --git a/source/net/filebot/similarity/Normalization.java b/source/net/filebot/similarity/Normalization.java index e3699c69..39a676da 100644 --- a/source/net/filebot/similarity/Normalization.java +++ b/source/net/filebot/similarity/Normalization.java @@ -1,22 +1,22 @@ package net.filebot.similarity; import static java.util.regex.Pattern.*; +import static net.filebot.util.StringUtilities.*; +import java.util.regex.Matcher; import java.util.regex.Pattern; public class Normalization { private static final Pattern apostrophe = compile("['`´‘’ʻ]+"); - private static final Pattern punctuation = compile("[\\p{Punct}\\s]+"); - - private static final Pattern space = compile("\\s+"); + private static final Pattern punctuation = compile("[\\p{Punct}\\p{Space}]+", Pattern.UNICODE_CHARACTER_CLASS); private static final Pattern spaceLikePunctuation = compile("[:?._]"); - private static final Pattern[] brackets = new Pattern[] { compile("\\([^\\(]*\\)"), compile("\\[[^\\[]*\\]"), compile("\\{[^\\{]*\\}") }; private static final Pattern trailingParentheses = compile("(? 0) { diff --git a/source/net/filebot/ui/rename/HistoryDialog.java b/source/net/filebot/ui/rename/HistoryDialog.java index 5930a6e0..5fa2bbca 100644 --- a/source/net/filebot/ui/rename/HistoryDialog.java +++ b/source/net/filebot/ui/rename/HistoryDialog.java @@ -9,6 +9,7 @@ import static net.filebot.Logging.*; import static net.filebot.Settings.*; import static net.filebot.UserFiles.*; import static net.filebot.media.XattrMetaInfo.*; +import static net.filebot.util.StringUtilities.*; import static net.filebot.util.ui.SwingUI.*; import java.awt.Color; @@ -225,7 +226,7 @@ class HistoryDialog extends JDialog { List filterList = new ArrayList(); // filter by all words - for (String word : filterEditor.getText().split("\\s+")) { + for (String word : SPACE.split(filterEditor.getText())) { filterList.add(new HistoryFilter(word)); } diff --git a/source/net/filebot/ui/rename/PresetEditor.java b/source/net/filebot/ui/rename/PresetEditor.java index e6cc74a5..ca7415ea 100644 --- a/source/net/filebot/ui/rename/PresetEditor.java +++ b/source/net/filebot/ui/rename/PresetEditor.java @@ -3,6 +3,7 @@ package net.filebot.ui.rename; import static java.awt.Font.*; import static javax.swing.BorderFactory.*; import static net.filebot.Logging.*; +import static net.filebot.similarity.Normalization.*; import static net.filebot.util.ui.SwingUI.*; import java.awt.Component; @@ -205,9 +206,10 @@ public class PresetEditor extends JDialog { private RSyntaxTextArea createEditor() { final RSyntaxTextArea editor = new RSyntaxTextArea(new RSyntaxDocument(SyntaxConstants.SYNTAX_STYLE_GROOVY) { + @Override public void insertString(int offs, String str, AttributeSet a) throws BadLocationException { - super.insertString(offs, str.replaceAll("\\s", " "), a); // FORCE SINGLE LINE + super.insertString(offs, replaceSpace(str, " "), a); // FORCE SINGLE LINE } }, null, 1, 80); diff --git a/source/net/filebot/ui/subtitle/SubtitleViewer.java b/source/net/filebot/ui/subtitle/SubtitleViewer.java index cd407ad3..f01b27f6 100644 --- a/source/net/filebot/ui/subtitle/SubtitleViewer.java +++ b/source/net/filebot/ui/subtitle/SubtitleViewer.java @@ -3,6 +3,7 @@ package net.filebot.ui.subtitle; import static java.awt.Font.*; import static java.util.Collections.*; import static java.util.regex.Pattern.*; +import static net.filebot.similarity.Normalization.*; import static net.filebot.util.ui.SwingUI.*; import java.awt.Color; @@ -130,34 +131,26 @@ public class SubtitleViewer extends JFrame { @Override public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) { - return super.getTableCellRendererComponent(table, value.toString().replaceAll("\\s+", " "), isSelected, hasFocus, row, column); + return super.getTableCellRendererComponent(table, replaceSpace(value.toString(), " "), isSelected, hasFocus, row, column); } }); // focus around selected time stamp - installAction(table, KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), new AbstractAction("focus") { + installAction(table, KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), newAction("focus", evt -> { + // disable row filter + setTableFilter(null); - @Override - public void actionPerformed(ActionEvent e) { - // disable row filter - setTableFilter(null); + // ensure selected row is visible and roughly in the center of the table + Rectangle focus = table.getCellRect(Math.max(table.getSelectedRow() - 7, 0), 0, true); + focus.height = table.getSize().height; + table.scrollRectToVisible(focus); + })); - // ensure selected row is visible and roughly in the center of the table - Rectangle focus = table.getCellRect(Math.max(table.getSelectedRow() - 7, 0), 0, true); - focus.height = table.getSize().height; - table.scrollRectToVisible(focus); + table.addMouseListener(mouseClicked(evt -> { + if (SwingUtilities.isLeftMouseButton(evt) && evt.getClickCount() == 2) { + table.getActionMap().get("focus").actionPerformed(null); } - }); - - table.addMouseListener(new MouseInputAdapter() { - - @Override - public void mouseClicked(MouseEvent e) { - if (SwingUtilities.isLeftMouseButton(e) && e.getClickCount() == 2) { - table.getActionMap().get("focus").actionPerformed(null); - } - } - }); + })); return table; } diff --git a/source/net/filebot/util/FileUtilities.java b/source/net/filebot/util/FileUtilities.java index eabc7ab5..f7e3877d 100644 --- a/source/net/filebot/util/FileUtilities.java +++ b/source/net/filebot/util/FileUtilities.java @@ -4,6 +4,7 @@ import static java.nio.charset.StandardCharsets.*; import static java.util.Arrays.*; import static java.util.Collections.*; import static net.filebot.Logging.*; +import static net.filebot.util.StringUtilities.*; import java.io.BufferedInputStream; import java.io.File; @@ -571,7 +572,7 @@ public final class FileUtilities { */ public static String validateFileName(CharSequence filename) { // strip invalid characters from file name - return ILLEGAL_CHARACTERS.matcher(filename).replaceAll("").replaceAll("\\s+", " ").trim(); + return SPACE.matcher(ILLEGAL_CHARACTERS.matcher(filename).replaceAll("")).replaceAll(" ").trim(); } public static boolean isInvalidFileName(CharSequence filename) { diff --git a/source/net/filebot/util/StringUtilities.java b/source/net/filebot/util/StringUtilities.java index b9d02fa5..5590d654 100644 --- a/source/net/filebot/util/StringUtilities.java +++ b/source/net/filebot/util/StringUtilities.java @@ -14,10 +14,10 @@ import java.util.stream.Stream; public final class StringUtilities { - public static final Pattern SPACE = Pattern.compile("\\s+"); public static final Pattern DIGIT = Pattern.compile("\\d+"); public static final Pattern NON_DIGIT = Pattern.compile("\\D+"); public static final Pattern PIPE = Pattern.compile("|", Pattern.LITERAL); + public static final Pattern SPACE = Pattern.compile("\\s+", Pattern.UNICODE_CHARACTER_CLASS); // French No-Break Space U+00A0 public static List matchIntegers(CharSequence s) { if (s == null || s.length() == 0) {