Skip to content

Commit

Permalink
Merge pull request #79 from sebthom/refact
Browse files Browse the repository at this point in the history
refact: code cleanup
  • Loading branch information
headius authored Jan 15, 2025
2 parents 03ac9a5 + d5b8f78 commit 2044540
Show file tree
Hide file tree
Showing 32 changed files with 492 additions and 495 deletions.
2 changes: 1 addition & 1 deletion src/org/joni/Analyser.java
Original file line number Diff line number Diff line change
Expand Up @@ -1607,7 +1607,7 @@ private Node expandCaseFoldString(Node node) {
int altNum = 1;

ListNode topRoot = null, root = null;
ObjPtr<Node> prevNode = new ObjPtr<Node>();
ObjPtr<Node> prevNode = new ObjPtr<>();
StringNode stringNode = null;

while (p < end) {
Expand Down
103 changes: 51 additions & 52 deletions src/org/joni/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,69 +22,68 @@
import java.io.PrintStream;

public interface Config extends org.jcodings.Config {
final int REGEX_MAX_LENGTH = ConfigSupport.getInt("joni.regex_max_length", -1);
final int CHAR_TABLE_SIZE = ConfigSupport.getInt("joni.char_table_size", 256);
final boolean USE_NO_INVALID_QUANTIFIER = ConfigSupport.getBoolean("joni.use_no_invalid_quantifier", true);
final int SCANENV_MEMNODES_SIZE = ConfigSupport.getInt("joni.scanenv_memnodes_size", 8);

final boolean USE_NAMED_GROUP = ConfigSupport.getBoolean("joni.use_named_group", true);
final boolean USE_SUBEXP_CALL = ConfigSupport.getBoolean("joni.use_subexp_call", true);
final boolean USE_PERL_SUBEXP_CALL = ConfigSupport.getBoolean("joni.use_perl_subexp_call", true);
final boolean USE_BACKREF_WITH_LEVEL = ConfigSupport.getBoolean("joni.use_backref_with_level", true); /* \k<name+n>, \k<name-n> */

final boolean USE_MONOMANIAC_CHECK_CAPTURES_IN_ENDLESS_REPEAT = ConfigSupport.getBoolean("joni.use_monomaniac_check_captures_in_endless_repeat", true); /* /(?:()|())*\2/ */
final boolean USE_NEWLINE_AT_END_OF_STRING_HAS_EMPTY_LINE = ConfigSupport.getBoolean("joni.use_newline_at_end_of_string_has_empty_line", true); /* /\n$/ =~ "\n" */
final boolean USE_WARNING_REDUNDANT_NESTED_REPEAT_OPERATOR = ConfigSupport.getBoolean("joni.use_warning_redundant_nested_repeat_operator", true);

final boolean CASE_FOLD_IS_APPLIED_INSIDE_NEGATIVE_CCLASS = ConfigSupport.getBoolean("joni.case_fold_is_applied_inside_negative_cclass", true);

final boolean USE_MATCH_RANGE_MUST_BE_INSIDE_OF_SPECIFIED_RANGE = ConfigSupport.getBoolean("joni.use_match_range_must_be_inside_of_specified_range", false);
final boolean USE_CAPTURE_HISTORY = ConfigSupport.getBoolean("joni.use_capture_history", false);
final boolean USE_VARIABLE_META_CHARS = ConfigSupport.getBoolean("joni.use_variable_meta_chars", true);
final boolean USE_WORD_BEGIN_END = ConfigSupport.getBoolean("joni.use_word_begin_end", true); /* "\<": word-begin, "\>": word-end */
final boolean USE_FIND_LONGEST_SEARCH_ALL_OF_RANGE = ConfigSupport.getBoolean("joni.use_find_longest_search_all_of_range", true);
final boolean USE_SUNDAY_QUICK_SEARCH = ConfigSupport.getBoolean("joni.use_sunday_quick_search", true);
final boolean USE_CEC = ConfigSupport.getBoolean("joni.use_cec", false);
final boolean USE_DYNAMIC_OPTION = ConfigSupport.getBoolean("joni.use_dynamic_option", false);
final boolean USE_BYTE_MAP = ConfigSupport.getBoolean("joni.use_byte_map", OptExactInfo.OPT_EXACT_MAXLEN <= CHAR_TABLE_SIZE);
final boolean USE_INT_MAP_BACKWARD = ConfigSupport.getBoolean("joni.use_int_map_backward", false);

final int NREGION = ConfigSupport.getInt("joni.nregion", 10);
final int MAX_BACKREF_NUM = ConfigSupport.getInt("joni.max_backref_num", 1000);
final int MAX_CAPTURE_GROUP_NUM = ConfigSupport.getInt("joni.max_capture_group_num", 32767);
final int MAX_REPEAT_NUM = ConfigSupport.getInt("joni.max_multi_byte_ranges_num", 100000);
final int MAX_MULTI_BYTE_RANGES_NUM = ConfigSupport.getInt("joni.max_multi_byte_ranges_num", 10000);
int REGEX_MAX_LENGTH = ConfigSupport.getInt("joni.regex_max_length", -1);
int CHAR_TABLE_SIZE = ConfigSupport.getInt("joni.char_table_size", 256);
boolean USE_NO_INVALID_QUANTIFIER = ConfigSupport.getBoolean("joni.use_no_invalid_quantifier", true);
int SCANENV_MEMNODES_SIZE = ConfigSupport.getInt("joni.scanenv_memnodes_size", 8);

boolean USE_NAMED_GROUP = ConfigSupport.getBoolean("joni.use_named_group", true);
boolean USE_SUBEXP_CALL = ConfigSupport.getBoolean("joni.use_subexp_call", true);
boolean USE_PERL_SUBEXP_CALL = ConfigSupport.getBoolean("joni.use_perl_subexp_call", true);
boolean USE_BACKREF_WITH_LEVEL = ConfigSupport.getBoolean("joni.use_backref_with_level", true); /* \k<name+n>, \k<name-n> */

boolean USE_MONOMANIAC_CHECK_CAPTURES_IN_ENDLESS_REPEAT = ConfigSupport.getBoolean("joni.use_monomaniac_check_captures_in_endless_repeat", true); /* /(?:()|())*\2/ */
boolean USE_NEWLINE_AT_END_OF_STRING_HAS_EMPTY_LINE = ConfigSupport.getBoolean("joni.use_newline_at_end_of_string_has_empty_line", true); /* /\n$/ =~ "\n" */
boolean USE_WARNING_REDUNDANT_NESTED_REPEAT_OPERATOR = ConfigSupport.getBoolean("joni.use_warning_redundant_nested_repeat_operator", true);

boolean CASE_FOLD_IS_APPLIED_INSIDE_NEGATIVE_CCLASS = ConfigSupport.getBoolean("joni.case_fold_is_applied_inside_negative_cclass", true);

boolean USE_MATCH_RANGE_MUST_BE_INSIDE_OF_SPECIFIED_RANGE = ConfigSupport.getBoolean("joni.use_match_range_must_be_inside_of_specified_range", false);
boolean USE_CAPTURE_HISTORY = ConfigSupport.getBoolean("joni.use_capture_history", false);
boolean USE_VARIABLE_META_CHARS = ConfigSupport.getBoolean("joni.use_variable_meta_chars", true);
boolean USE_WORD_BEGIN_END = ConfigSupport.getBoolean("joni.use_word_begin_end", true); /* "\<": word-begin, "\>": word-end */
boolean USE_FIND_LONGEST_SEARCH_ALL_OF_RANGE = ConfigSupport.getBoolean("joni.use_find_longest_search_all_of_range", true);
boolean USE_SUNDAY_QUICK_SEARCH = ConfigSupport.getBoolean("joni.use_sunday_quick_search", true);
boolean USE_CEC = ConfigSupport.getBoolean("joni.use_cec", false);
boolean USE_DYNAMIC_OPTION = ConfigSupport.getBoolean("joni.use_dynamic_option", false);
boolean USE_BYTE_MAP = ConfigSupport.getBoolean("joni.use_byte_map", OptExactInfo.OPT_EXACT_MAXLEN <= CHAR_TABLE_SIZE);
boolean USE_INT_MAP_BACKWARD = ConfigSupport.getBoolean("joni.use_int_map_backward", false);

int NREGION = ConfigSupport.getInt("joni.nregion", 10);
int MAX_BACKREF_NUM = ConfigSupport.getInt("joni.max_backref_num", 1000);
int MAX_CAPTURE_GROUP_NUM = ConfigSupport.getInt("joni.max_capture_group_num", 32767);
int MAX_REPEAT_NUM = ConfigSupport.getInt("joni.max_multi_byte_ranges_num", 100000);
int MAX_MULTI_BYTE_RANGES_NUM = ConfigSupport.getInt("joni.max_multi_byte_ranges_num", 10000);

// internal config
final boolean USE_OP_PUSH_OR_JUMP_EXACT = ConfigSupport.getBoolean("joni.use_op_push_or_jump_exact", true);
final boolean USE_QTFR_PEEK_NEXT = ConfigSupport.getBoolean("joni.use_qtfr_peek_next", true);
boolean USE_OP_PUSH_OR_JUMP_EXACT = ConfigSupport.getBoolean("joni.use_op_push_or_jump_exact", true);
boolean USE_QTFR_PEEK_NEXT = ConfigSupport.getBoolean("joni.use_qtfr_peek_next", true);

final int INIT_MATCH_STACK_SIZE = ConfigSupport.getInt("joni.init_match_stack_size", 64);
int INIT_MATCH_STACK_SIZE = ConfigSupport.getInt("joni.init_match_stack_size", 64);

final boolean OPTIMIZE = ConfigSupport.getBoolean("joni.optimize", true);
@Deprecated
final boolean DONT_OPTIMIZE = !OPTIMIZE;
boolean OPTIMIZE = ConfigSupport.getBoolean("joni.optimize", true);
@Deprecated boolean DONT_OPTIMIZE = !OPTIMIZE;

// use embedded string templates in Regex object as byte arrays instead of compiling them into int bytecode array
final boolean USE_STRING_TEMPLATES = ConfigSupport.getBoolean("joni.use_string_templates", true);
boolean USE_STRING_TEMPLATES = ConfigSupport.getBoolean("joni.use_string_templates", true);


final int MAX_CAPTURE_HISTORY_GROUP = ConfigSupport.getInt("joni.max_capture_history_group", 31);
int MAX_CAPTURE_HISTORY_GROUP = ConfigSupport.getInt("joni.max_capture_history_group", 31);


final int CHECK_STRING_THRESHOLD_LEN = ConfigSupport.getInt("joni.check_string_threshold_len", 7);
final int CHECK_BUFF_MAX_SIZE = ConfigSupport.getInt("joni.check_buff_max_size", 0x4000);
int CHECK_STRING_THRESHOLD_LEN = ConfigSupport.getInt("joni.check_string_threshold_len", 7);
int CHECK_BUFF_MAX_SIZE = ConfigSupport.getInt("joni.check_buff_max_size", 0x4000);

final PrintStream log = System.out;
final PrintStream err = System.err;
PrintStream log = System.out;
PrintStream err = System.err;

final boolean DEBUG_ALL = ConfigSupport.getBoolean("joni.debug.all", false);
boolean DEBUG_ALL = ConfigSupport.getBoolean("joni.debug.all", false);

final boolean DEBUG = ConfigSupport.getBoolean("joni.debug", false) || DEBUG_ALL;
final boolean DEBUG_PARSE_TREE = ConfigSupport.getBoolean("joni.debug.parse.tree", false) || DEBUG_ALL;
final boolean DEBUG_PARSE_TREE_RAW = ConfigSupport.getBoolean("joni.debug.parse.tree.raw", true) || DEBUG_ALL;
final boolean DEBUG_COMPILE = ConfigSupport.getBoolean("joni.debug.compile", false) || DEBUG_ALL;
final boolean DEBUG_COMPILE_BYTE_CODE_INFO = ConfigSupport.getBoolean("joni.debug.compile.bytecode.info", false) || DEBUG_ALL;
final boolean DEBUG_SEARCH = ConfigSupport.getBoolean("joni.debug.search", false) || DEBUG_ALL;
final boolean DEBUG_MATCH = ConfigSupport.getBoolean("joni.debug.match", false) || DEBUG_ALL;
boolean DEBUG = ConfigSupport.getBoolean("joni.debug", false) || DEBUG_ALL;
boolean DEBUG_PARSE_TREE = ConfigSupport.getBoolean("joni.debug.parse.tree", false) || DEBUG_ALL;
boolean DEBUG_PARSE_TREE_RAW = ConfigSupport.getBoolean("joni.debug.parse.tree.raw", true) || DEBUG_ALL;
boolean DEBUG_COMPILE = ConfigSupport.getBoolean("joni.debug.compile", false) || DEBUG_ALL;
boolean DEBUG_COMPILE_BYTE_CODE_INFO = ConfigSupport.getBoolean("joni.debug.compile.bytecode.info", false) || DEBUG_ALL;
boolean DEBUG_SEARCH = ConfigSupport.getBoolean("joni.debug.search", false) || DEBUG_ALL;
boolean DEBUG_MATCH = ConfigSupport.getBoolean("joni.debug.match", false) || DEBUG_ALL;
}
2 changes: 1 addition & 1 deletion src/org/joni/Lexer.java
Original file line number Diff line number Diff line change
Expand Up @@ -522,7 +522,7 @@ private boolean strExistCheckWithEsc(int[]s, int n, int bad) {
return false;
}

private static final int send[] = new int[]{':', ']'};
private static final int[] send = new int[]{':', ']'};

private void fetchTokenInCCFor_charType(boolean flag, int type) {
token.type = TokenType.CHAR_TYPE;
Expand Down
2 changes: 1 addition & 1 deletion src/org/joni/MinMaxLen.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ final class MinMaxLen {
int max; /* max byte length */

/* 1000 / (min-max-dist + 1) */
private static final short distValues[] = {
private static final short[] distValues = {
1000, 500, 333, 250, 200, 167, 143, 125, 111, 100,
91, 83, 77, 71, 67, 63, 59, 56, 53, 50,
48, 45, 43, 42, 40, 38, 37, 36, 34, 33,
Expand Down
2 changes: 1 addition & 1 deletion src/org/joni/NameEntry.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public final class NameEntry {

int backNum;
int backRef1;
int backRefs[];
int[] backRefs;

public NameEntry(byte[]bytes, int p, int end) {
name = bytes;
Expand Down
2 changes: 1 addition & 1 deletion src/org/joni/OptExactInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ final class OptExactInfo {
final OptAnchorInfo anchor = new OptAnchorInfo();
boolean reachEnd;
int ignoreCase; /* -1: unset, 0: case sensitive, 1: ignore case */
final byte bytes[] = new byte[OPT_EXACT_MAXLEN];
final byte[] bytes = new byte[OPT_EXACT_MAXLEN];
int length;

boolean isFull() {
Expand Down
4 changes: 2 additions & 2 deletions src/org/joni/OptMapInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ final class OptMapInfo {
final MinMaxLen mmd = new MinMaxLen(); /* info position */
final OptAnchorInfo anchor = new OptAnchorInfo();
int value; /* weighted value */
final byte map[] = new byte[Config.CHAR_TABLE_SIZE];
final byte[] map = new byte[Config.CHAR_TABLE_SIZE];

void clear() {
mmd.clear();
Expand Down Expand Up @@ -99,7 +99,7 @@ void altMerge(OptMapInfo other, Encoding enc) {
anchor.altMerge(other.anchor);
}

static final short ByteValTable[] = {
static final short[] ByteValTable = {
5, 1, 1, 1, 1, 1, 1, 1, 1, 10, 10, 1, 1, 10, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
12, 4, 7, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5,
Expand Down
6 changes: 3 additions & 3 deletions src/org/joni/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ protected Parser(Regex regex, Syntax syntax, byte[]bytes, int p, int end, WarnCa

private static final int POSIX_BRACKET_NAME_MIN_LEN = 4;
private static final int POSIX_BRACKET_CHECK_LIMIT_LENGTH = 20;
private static final byte BRACKET_END[] = ":]".getBytes();
private static final byte[] BRACKET_END = ":]".getBytes();
private boolean parsePosixBracket(CClassNode cc, CClassNode ascCc) {
mark();

Expand Down Expand Up @@ -309,7 +309,7 @@ private CClassNode parseCharClass(ObjPtr<CClassNode> ascNode) {
break;

case CC_CC_OPEN: /* [ */
ObjPtr<CClassNode> ascPtr = new ObjPtr<CClassNode>();
ObjPtr<CClassNode> ascPtr = new ObjPtr<>();
CClassNode acc = parseCharClass(ascPtr);
cc.or(acc, env);
if (ascPtr.p != null) {
Expand Down Expand Up @@ -817,7 +817,7 @@ private Node parseExp(TokenType term) {
break;

case CC_OPEN: {
ObjPtr<CClassNode> ascPtr = new ObjPtr<CClassNode>();
ObjPtr<CClassNode> ascPtr = new ObjPtr<>();
CClassNode cc = parseCharClass(ascPtr);
int code = cc.isOneChar();
if (code != -1) return parseStringLoop(StringNode.fromCodePoint(code, enc), group);
Expand Down
3 changes: 1 addition & 2 deletions src/org/joni/Regex.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
import org.joni.exception.ErrorMessages;
import org.joni.exception.InternalException;
import org.joni.exception.ValueException;
import org.joni.Config;

public final class Regex {
int[] code; /* compiled pattern */
Expand Down Expand Up @@ -237,7 +236,7 @@ void nameAdd(byte[]name, int nameP, int nameEnd, int backRef, Syntax syntax) {

NameEntry e = null;
if (nameTable == null) {
nameTable = new BytesHash<NameEntry>(); // 13, oni defaults to 5
nameTable = new BytesHash<>(); // 13, oni defaults to 5
} else {
e = nameFind(name, nameP, nameEnd);
}
Expand Down
4 changes: 2 additions & 2 deletions src/org/joni/ScanEnvironment.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public final class ScanEnvironment {

int numNamed; // USE_NAMED_GROUP

public EncloseNode memNodes[];
public EncloseNode[] memNodes;

// USE_COMBINATION_EXPLOSION_CHECK
int numCombExpCheck;
Expand All @@ -54,7 +54,7 @@ public final class ScanEnvironment {
private int warningsFlag;

int numPrecReadNotNodes;
Node precReadNotNodes[];
Node[] precReadNotNodes;

ScanEnvironment(Regex regex, Syntax syntax, WarnCallback warnings) {
this.syntax = syntax;
Expand Down
6 changes: 3 additions & 3 deletions src/org/joni/StackMachine.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,18 +78,18 @@ private void doubleStack() {
}

static final ThreadLocal<WeakReference<StackEntry[]>> stacks
= new ThreadLocal<WeakReference<StackEntry[]>>();
= new ThreadLocal<>();

private static StackEntry[] fetchStack() {
WeakReference<StackEntry[]> ref = stacks.get();
StackEntry[] stack;
if (ref == null) {
stacks.set( new WeakReference<StackEntry[]>(stack = allocateStack()) );
stacks.set( new WeakReference<>(stack = allocateStack()) );
}
else {
stack = ref.get();
if (stack == null) {
stacks.set( new WeakReference<StackEntry[]>(stack = allocateStack()) );
stacks.set( new WeakReference<>(stack = allocateStack()) );
}
}
return stack;
Expand Down
2 changes: 1 addition & 1 deletion src/org/joni/ast/BackRefNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
import org.joni.exception.ValueException;

public final class BackRefNode extends StateNode {
public final int back[];
public final int[] back;
public int backNum;
public int nestLevel;

Expand Down
6 changes: 3 additions & 3 deletions src/org/joni/ast/CClassNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ public void or(CClassNode other, ScanEnvironment env) {
}

// add_ctype_to_cc_by_range // Encoding out!
public void addCTypeByRange(int ctype, boolean not, ScanEnvironment env, int sbOut, int mbr[]) {
public void addCTypeByRange(int ctype, boolean not, ScanEnvironment env, int sbOut, int[] mbr) {
int n = mbr[0];
int i;

Expand Down Expand Up @@ -382,13 +382,13 @@ public void addCType(int ctype, boolean not, boolean asciiRange, ScanEnvironment
} // switch
}

public static enum CCVALTYPE {
public enum CCVALTYPE {
SB,
CODE_POINT,
CLASS
}

public static enum CCSTATE {
public enum CCSTATE {
VALUE,
RANGE,
COMPLETE,
Expand Down
6 changes: 3 additions & 3 deletions src/org/joni/ast/QuantifierNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ protected void copy(QuantifierNode other) {
combExpCheckNum = other.combExpCheckNum;
}

static enum ReduceType {
enum ReduceType {
ASIS, /* as is */
DEL, /* delete parent */
A, /* to '*' */
Expand Down Expand Up @@ -199,8 +199,8 @@ public void reduceNestedQuantifier(QuantifierNode other) {
other.target = null; // remove target from reduced quantifier
}

static final String PopularQStr[] = new String[] {"?", "*", "+", "??", "*?", "+?"};
static final String ReduceQStr[] = new String[] {"", "", "*", "*?", "??", "+ and ??", "+? and ?"};
static final String[] PopularQStr = new String[] {"?", "*", "+", "??", "*?", "+?"};
static final String[] ReduceQStr = new String[] {"", "", "*", "*?", "??", "+ and ??", "+? and ?"};

public int setQuantifier(Node tgt, boolean group, ScanEnvironment env, byte[]bytes, int p, int end) {
if (lower == 1 && upper == 1) {
Expand Down
14 changes: 7 additions & 7 deletions src/org/joni/constants/MetaChar.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@
package org.joni.constants;

public interface MetaChar {
final int ESCAPE = 0;
final int ANYCHAR = 1;
final int ANYTIME = 2;
final int ZERO_OR_ONE_TIME = 3;
final int ONE_OR_MORE_TIME = 4;
final int ANYCHAR_ANYTIME = 5;
int ESCAPE = 0;
int ANYCHAR = 1;
int ANYTIME = 2;
int ZERO_OR_ONE_TIME = 3;
int ONE_OR_MORE_TIME = 4;
int ANYCHAR_ANYTIME = 5;

final int INEFFECTIVE_META_CHAR = 0;
int INEFFECTIVE_META_CHAR = 0;
}
Loading

0 comments on commit 2044540

Please sign in to comment.