From 11667a00d1c64a1e572db784dcf149d014dc6150 Mon Sep 17 00:00:00 2001 From: Andrei Solntsev Date: Fri, 19 Jul 2024 22:34:22 +0300 Subject: [PATCH] #340 extract reusable regular expression to a constant --- .../org/xhtmlrenderer/protocols/data/DataURLConnection.java | 6 ++++-- .../java/org/xhtmlrenderer/swt/simple/SWTTextControl.java | 5 ++++- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/flying-saucer-core/src/main/java/org/xhtmlrenderer/protocols/data/DataURLConnection.java b/flying-saucer-core/src/main/java/org/xhtmlrenderer/protocols/data/DataURLConnection.java index d209d0422..69aaeee7c 100644 --- a/flying-saucer-core/src/main/java/org/xhtmlrenderer/protocols/data/DataURLConnection.java +++ b/flying-saucer-core/src/main/java/org/xhtmlrenderer/protocols/data/DataURLConnection.java @@ -27,6 +27,7 @@ import java.nio.charset.UnsupportedCharsetException; import java.util.HashMap; import java.util.Map; +import java.util.regex.Pattern; public class DataURLConnection extends URLConnection { @@ -176,13 +177,14 @@ class Base64 { private static final byte[] EMPTY_BYTE_ARRAY = {}; private static final String _map = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="; + private static final Pattern RE_FORBIDDEN_CHARACTERS = Pattern.compile("[^" + _map + "]+"); - public static byte [] decode(String s) { + public static byte[] decode(String s) { if (s == null || s.length() < 4) return EMPTY_BYTE_ARRAY; - s = s.replaceAll("[^A-Za-z0-9+/=]+", ""); + s = RE_FORBIDDEN_CHARACTERS.matcher(s).replaceAll(""); if (s.length() < 4) return EMPTY_BYTE_ARRAY; diff --git a/flying-saucer-swt/src/main/java/org/xhtmlrenderer/swt/simple/SWTTextControl.java b/flying-saucer-swt/src/main/java/org/xhtmlrenderer/swt/simple/SWTTextControl.java index dd7bd0072..52e75a886 100644 --- a/flying-saucer-swt/src/main/java/org/xhtmlrenderer/swt/simple/SWTTextControl.java +++ b/flying-saucer-swt/src/main/java/org/xhtmlrenderer/swt/simple/SWTTextControl.java @@ -30,8 +30,11 @@ import org.xhtmlrenderer.simple.xhtml.controls.TextControl; import org.xhtmlrenderer.swt.BasicRenderer; +import java.util.regex.Pattern; + public class SWTTextControl extends SWTXhtmlControl { + private static final Pattern RE_NEWLINE = Pattern.compile("\n"); private String _sizeText; private boolean _noChangeText = false; @@ -108,7 +111,7 @@ public int getIdealHeight() { } private static String encodeDelimiter(String text) { - return text.replaceAll("\n", Text.DELIMITER); + return RE_NEWLINE.matcher(text).replaceAll(Text.DELIMITER); } private static String decodeDelimiter(String text) {