Skip to content

Commit

Permalink
#340 extract reusable regular expression to a constant
Browse files Browse the repository at this point in the history
  • Loading branch information
asolntsev committed Jul 19, 2024
1 parent 621c952 commit 11667a0
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 11667a0

Please sign in to comment.