Optimise Regex with Mehdi H.

This commit is contained in:
jeffcheasey88 2023-06-09 22:44:04 +02:00
parent 9b41348588
commit b82e91fefa
11 changed files with 48 additions and 48 deletions

View file

@ -5,10 +5,10 @@ import java.util.regex.Pattern;
public class Import { public class Import {
private static Pattern PATTERN = Pattern.compile("^\\s*(import\\s+([^;]*);).*$"); private static Pattern PATTERN = Pattern.compile("^\\s*(import\\s+([^;]*);)");
public static boolean isImport(String content){ public static boolean isImport(String content){
return PATTERN.matcher(content).matches(); return PATTERN.matcher(content).lookingAt();
} }
private String name; private String name;
@ -17,7 +17,7 @@ public class Import {
public int parse(String content) throws Exception{ public int parse(String content) throws Exception{
Matcher matcher = PATTERN.matcher(content); Matcher matcher = PATTERN.matcher(content);
matcher.matches(); matcher.lookingAt();
this.name = matcher.group(2); this.name = matcher.group(2);
return matcher.group(1).length(); return matcher.group(1).length();
} }

View file

@ -5,7 +5,7 @@ import java.util.regex.Pattern;
public class Package { public class Package {
private static Pattern PATTERN = Pattern.compile("^(\\s*package\\s+([^;]*);).*$"); private static Pattern PATTERN = Pattern.compile("^(\\s*package\\s+([^;]*);)");
private String name; private String name;
@ -13,7 +13,7 @@ public class Package {
public int parse(String content) throws Exception{ public int parse(String content) throws Exception{
Matcher matcher = PATTERN.matcher(content); Matcher matcher = PATTERN.matcher(content);
matcher.matches(); matcher.lookingAt();
this.name = matcher.group(2); this.name = matcher.group(2);
return matcher.group(1).length(); return matcher.group(1).length();
} }

View file

@ -11,7 +11,7 @@ import be.jeffcheasey88.peeratcode.parser.java.JavaElement;
public class AssigmentOperation extends JavaElement{ public class AssigmentOperation extends JavaElement{
private static Pattern PATTERN = Pattern.compile("^(\\s*([^\\s]+)\\s*=\\s*([^;]+);).*$"); private static Pattern PATTERN = Pattern.compile("^(\\s*([^\\s]+)\\s*=\\s*([^;]+);)");
private String variable; private String variable;
private String value; private String value;
@ -21,7 +21,7 @@ public class AssigmentOperation extends JavaElement{
@Override @Override
public int parse(String content, CleanerPool global, CleanerPool local) throws Exception{ public int parse(String content, CleanerPool global, CleanerPool local) throws Exception{
Matcher matcher = PATTERN.matcher(content); Matcher matcher = PATTERN.matcher(content);
matcher.matches(); matcher.lookingAt();
this.variable = local.unzip(matcher.group(2), (s,p) -> s); this.variable = local.unzip(matcher.group(2), (s,p) -> s);
this.value = local.unzip(matcher.group(3), (s,p) -> s); this.value = local.unzip(matcher.group(3), (s,p) -> s);

View file

@ -9,8 +9,8 @@ import be.jeffcheasey88.peeratcode.parser.java.JavaElement;
public class ConditionalOperation extends OperationContainer{ public class ConditionalOperation extends OperationContainer{
private static Pattern PATTERN = Pattern.compile("^(\\^GENERIC_FUNCTION\\d+).*$"); private static Pattern PATTERN = Pattern.compile("^(\\^GENERIC_FUNCTION\\d+)");
private static Pattern PATTERN_SPACE = Pattern.compile("^(\\s+).*$"); private static Pattern PATTERN_SPACE = Pattern.compile("^(\\s+)");
private Pattern pattern; private Pattern pattern;
private String condition; private String condition;
@ -22,7 +22,7 @@ public class ConditionalOperation extends OperationContainer{
@Override @Override
public int parse(String content, CleanerPool global, CleanerPool local) throws Exception{ public int parse(String content, CleanerPool global, CleanerPool local) throws Exception{
Matcher matcher = this.pattern.matcher(content); Matcher matcher = this.pattern.matcher(content);
matcher.matches(); matcher.lookingAt();
this.condition = local.unzipOne(matcher.group(2), (s,p) -> s); this.condition = local.unzipOne(matcher.group(2), (s,p) -> s);
System.out.println("CONDITION "+condition); System.out.println("CONDITION "+condition);
@ -31,7 +31,7 @@ public class ConditionalOperation extends OperationContainer{
content = content.substring(index); content = content.substring(index);
matcher = PATTERN_SPACE.matcher(content); matcher = PATTERN_SPACE.matcher(content);
if(matcher.matches()){ if(matcher.lookingAt()){
index+=matcher.group(1).length(); index+=matcher.group(1).length();
content = content.substring(matcher.group(1).length()); content = content.substring(matcher.group(1).length());
} }
@ -39,7 +39,7 @@ public class ConditionalOperation extends OperationContainer{
int bodysize; int bodysize;
if(content.startsWith("^")){ if(content.startsWith("^")){
matcher = PATTERN.matcher(content); matcher = PATTERN.matcher(content);
matcher.matches(); matcher.lookingAt();
content = matcher.group(1); content = matcher.group(1);
@ -84,7 +84,7 @@ public class ConditionalOperation extends OperationContainer{
public static class IfOperation extends ConditionalOperation{ public static class IfOperation extends ConditionalOperation{
private static Pattern PATTERN = Pattern.compile("^(\\s*if\\s*(\\^GENERIC_PARENTHESIS\\d+)).*$"); private static Pattern PATTERN = Pattern.compile("^(\\s*if\\s*(\\^GENERIC_PARENTHESIS\\d+))");
public IfOperation(){ public IfOperation(){
super(PATTERN); super(PATTERN);
@ -104,7 +104,7 @@ public class ConditionalOperation extends OperationContainer{
public static class ForOperation extends ConditionalOperation{ public static class ForOperation extends ConditionalOperation{
private static Pattern PATTERN = Pattern.compile("^(\\s*for\\s*(\\^GENERIC_PARENTHESIS\\d+)).*$"); private static Pattern PATTERN = Pattern.compile("^(\\s*for\\s*(\\^GENERIC_PARENTHESIS\\d+))");
public ForOperation(){ public ForOperation(){
super(PATTERN); super(PATTERN);
@ -123,7 +123,7 @@ public class ConditionalOperation extends OperationContainer{
public static class WhileOperation extends ConditionalOperation{ public static class WhileOperation extends ConditionalOperation{
private static Pattern PATTERN = Pattern.compile("^(\\s*while\\s*(\\^GENERIC_PARENTHESIS\\d+)).*$"); private static Pattern PATTERN = Pattern.compile("^(\\s*while\\s*(\\^GENERIC_PARENTHESIS\\d+))");
public WhileOperation(){ public WhileOperation(){
super(PATTERN); super(PATTERN);

View file

@ -9,21 +9,21 @@ import be.jeffcheasey88.peeratcode.parser.java.JavaElement;
public class DoOperation extends OperationContainer{ public class DoOperation extends OperationContainer{
private static Pattern PATTERN = Pattern.compile("^(\\s*do\\s*).*$"); private static Pattern PATTERN = Pattern.compile("^(\\s*do\\s*)");
private static Pattern PATTERN_CLEANER = Pattern.compile("^(\\^GENERIC_FUNCTION\\d+).*$"); private static Pattern PATTERN_CLEANER = Pattern.compile("^(\\^GENERIC_FUNCTION\\d+)");
public DoOperation(){} public DoOperation(){}
@Override @Override
public int parse(String content, CleanerPool global, CleanerPool local) throws Exception{ public int parse(String content, CleanerPool global, CleanerPool local) throws Exception{
Matcher matcher = PATTERN.matcher(content); Matcher matcher = PATTERN.matcher(content);
matcher.matches(); matcher.lookingAt();
int index = matcher.group(1).length(); int index = matcher.group(1).length();
content = content.substring(index); content = content.substring(index);
matcher = PATTERN_CLEANER.matcher(content); matcher = PATTERN_CLEANER.matcher(content);
matcher.matches(); matcher.lookingAt();
content = matcher.group(1); content = matcher.group(1);

View file

@ -9,15 +9,15 @@ import be.jeffcheasey88.peeratcode.parser.java.JavaElement;
public class ElseOperation extends OperationContainer{ public class ElseOperation extends OperationContainer{
private static Pattern PATTERN = Pattern.compile("^(\\s*else\\s*).*$"); private static Pattern PATTERN = Pattern.compile("^(\\s*else\\s*)");
private static Pattern PATTERN_CLEANER = Pattern.compile("^(\\^GENERIC_FUNCTION\\d+).*$"); private static Pattern PATTERN_CLEANER = Pattern.compile("^(\\^GENERIC_FUNCTION\\d+)");
public ElseOperation(){} public ElseOperation(){}
@Override @Override
public int parse(String content, CleanerPool global, CleanerPool local) throws Exception{ public int parse(String content, CleanerPool global, CleanerPool local) throws Exception{
Matcher matcher = PATTERN.matcher(content); Matcher matcher = PATTERN.matcher(content);
matcher.matches(); matcher.lookingAt();
int index = matcher.group(1).length(); int index = matcher.group(1).length();
content = content.substring(index); content = content.substring(index);
@ -25,7 +25,7 @@ public class ElseOperation extends OperationContainer{
int bodysize; int bodysize;
if(content.startsWith("^")){ if(content.startsWith("^")){
matcher = PATTERN_CLEANER.matcher(content); matcher = PATTERN_CLEANER.matcher(content);
matcher.matches(); matcher.lookingAt();
content = matcher.group(1); content = matcher.group(1);

View file

@ -20,7 +20,7 @@ public class LoopAffectOperation extends JavaElement{
@Override @Override
public int parse(String content, CleanerPool global, CleanerPool local) throws Exception{ public int parse(String content, CleanerPool global, CleanerPool local) throws Exception{
Matcher matcher = this.pattern.matcher(content); Matcher matcher = this.pattern.matcher(content);
matcher.matches(); matcher.lookingAt();
return matcher.group(1).length(); return matcher.group(1).length();
} }
@ -39,7 +39,7 @@ public class LoopAffectOperation extends JavaElement{
public static class ContinueOperation extends LoopAffectOperation{ public static class ContinueOperation extends LoopAffectOperation{
private static Pattern PATTERN = Pattern.compile("^(\\s*continue\\s*;).*$"); private static Pattern PATTERN = Pattern.compile("^(\\s*continue\\s*;)");
public ContinueOperation(){ public ContinueOperation(){
super(PATTERN); super(PATTERN);
@ -56,7 +56,7 @@ public class LoopAffectOperation extends JavaElement{
public static class BreakOperation extends LoopAffectOperation{ public static class BreakOperation extends LoopAffectOperation{
private static Pattern PATTERN = Pattern.compile("^(\\s*break\\s*;).*$"); private static Pattern PATTERN = Pattern.compile("^(\\s*break\\s*;)");
public BreakOperation(){ public BreakOperation(){
super(PATTERN); super(PATTERN);

View file

@ -11,7 +11,7 @@ import be.jeffcheasey88.peeratcode.parser.java.JavaElement;
public class MethodCallOperation extends JavaElement{ public class MethodCallOperation extends JavaElement{
private static Pattern PATTERN = Pattern.compile("^(\\s*([^\\^\\s]+\\^GENERIC_PARENTHESIS\\d+);).*$"); private static Pattern PATTERN = Pattern.compile("^(\\s*([^\\^\\s]+\\^GENERIC_PARENTHESIS\\d+);)");
private String value; private String value;
@ -22,7 +22,7 @@ public class MethodCallOperation extends JavaElement{
content = local.clean(content); content = local.clean(content);
Matcher matcher = PATTERN.matcher(content); Matcher matcher = PATTERN.matcher(content);
matcher.matches(); matcher.lookingAt();
this.value = local.unzip(matcher.group(2), (s,p) -> s); this.value = local.unzip(matcher.group(2), (s,p) -> s);

View file

@ -28,23 +28,23 @@ public class OperationFactory{
private OperationFactory(){ private OperationFactory(){
this.patterns = new LinkedHashMap<>(); this.patterns = new LinkedHashMap<>();
this.patterns.put(Pattern.compile("^\\s*return\\s*[^;]*;.*$"), ReturnOperation.class); this.patterns.put(Pattern.compile("^\\s*return\\s*[^;]*;"), ReturnOperation.class);
this.patterns.put(Pattern.compile("^\\s*if\\s*\\^GENERIC_PARENTHESIS\\d+.*$"), IfOperation.class); this.patterns.put(Pattern.compile("^\\s*if\\s*\\^GENERIC_PARENTHESIS\\d+"), IfOperation.class);
this.patterns.put(Pattern.compile("^\\s*for\\s*\\^GENERIC_PARENTHESIS\\d+.*$"), ForOperation.class); this.patterns.put(Pattern.compile("^\\s*for\\s*\\^GENERIC_PARENTHESIS\\d+"), ForOperation.class);
this.patterns.put(Pattern.compile("^\\s*while\\s*\\^GENERIC_PARENTHESIS\\d+.*$"), WhileOperation.class); this.patterns.put(Pattern.compile("^\\s*while\\s*\\^GENERIC_PARENTHESIS\\d+"), WhileOperation.class);
this.patterns.put(Pattern.compile("^\\s*else\\s*.*$"), ElseOperation.class); this.patterns.put(Pattern.compile("^\\s*else\\s*"), ElseOperation.class);
this.patterns.put(Pattern.compile("^\\s*do\\s*\\^GENERIC_FUNCTION\\d+.*$"), DoOperation.class); this.patterns.put(Pattern.compile("^\\s*do\\s*\\^GENERIC_FUNCTION\\d+"), DoOperation.class);
this.patterns.put(Pattern.compile("^\\s*synchronized\\s*\\^GENERIC_PARENTHESIS\\d+.*$"), SynchronizedOperation.class); this.patterns.put(Pattern.compile("^\\s*synchronized\\s*\\^GENERIC_PARENTHESIS\\d+"), SynchronizedOperation.class);
this.patterns.put(Pattern.compile("^\\s*continue\\s*;.*$"), ContinueOperation.class); this.patterns.put(Pattern.compile("^\\s*continue\\s*;"), ContinueOperation.class);
this.patterns.put(Pattern.compile("^\\s*break\\s*;.*$"), BreakOperation.class); this.patterns.put(Pattern.compile("^\\s*break\\s*;"), BreakOperation.class);
this.patterns.put(Pattern.compile("^\\s*[^\\^\\s]+\\^GENERIC_PARENTHESIS\\d+;.*$"), MethodCallOperation.class); this.patterns.put(Pattern.compile("^\\s*[^\\^\\s]+\\^GENERIC_PARENTHESIS\\d+;"), MethodCallOperation.class);
this.patterns.put(Pattern.compile("^\\s*[^\\s]+\\s+[^\\s]+\\s*=\\s*[^;]+;.*$"), Variable.class); this.patterns.put(Pattern.compile("^\\s*[^\\s]+\\s+[^\\s]+\\s*=\\s*[^;]+;"), Variable.class);
this.patterns.put(Pattern.compile("^\\s*[^\\s]+\\s*=\\s*[^;]+;.*$"), AssigmentOperation.class); this.patterns.put(Pattern.compile("^\\s*[^\\s]+\\s*=\\s*[^;]+;"), AssigmentOperation.class);
this.patterns.put(Pattern.compile("^\\s*[^;]+;.*$"), Variable.class); this.patterns.put(Pattern.compile("^\\s*[^;]+;"), Variable.class);
} }
/* /*
@ -81,7 +81,7 @@ public class OperationFactory{
content = generiqueTypes(content, generic); content = generiqueTypes(content, generic);
for(Entry<Pattern, Class<? extends JavaElement>> entries : this.patterns.entrySet()){ for(Entry<Pattern, Class<? extends JavaElement>> entries : this.patterns.entrySet()){
if(entries.getKey().matcher(content).matches()) return entries.getValue().newInstance(); if(entries.getKey().matcher(content).lookingAt()) return entries.getValue().newInstance();
} }
return null; return null;
} }

View file

@ -11,7 +11,7 @@ import be.jeffcheasey88.peeratcode.parser.java.JavaElement;
public class ReturnOperation extends JavaElement{ public class ReturnOperation extends JavaElement{
private static Pattern PATTERN = Pattern.compile("^(\\s*return\\s*([^;]*);).*$"); private static Pattern PATTERN = Pattern.compile("^(\\s*return\\s*([^;]*);)");
private static OperationFactory FACTORY = OperationFactory.getFactory(); private static OperationFactory FACTORY = OperationFactory.getFactory();
@ -23,7 +23,7 @@ public class ReturnOperation extends JavaElement{
@Override @Override
public int parse(String content, CleanerPool global, CleanerPool local) throws Exception{ public int parse(String content, CleanerPool global, CleanerPool local) throws Exception{
Matcher matcher = PATTERN.matcher(content); Matcher matcher = PATTERN.matcher(content);
matcher.matches(); matcher.lookingAt();
//To update for native obj //To update for native obj
this.toChange = matcher.group(2); this.toChange = matcher.group(2);

View file

@ -9,15 +9,15 @@ import be.jeffcheasey88.peeratcode.parser.java.JavaElement;
public class SynchronizedOperation extends OperationContainer{ public class SynchronizedOperation extends OperationContainer{
private static Pattern PATTERN = Pattern.compile("^(\\s*synchronized\\s*)(\\^GENERIC_PARENTHESIS\\d+)(\\s*).*$"); private static Pattern PATTERN = Pattern.compile("^(\\s*synchronized\\s*)(\\^GENERIC_PARENTHESIS\\d+)(\\s*)");
private static Pattern PATTERN_CLEANER = Pattern.compile("^(\\^GENERIC_FUNCTION\\d+).*$"); private static Pattern PATTERN_CLEANER = Pattern.compile("^(\\^GENERIC_FUNCTION\\d+)");
private String include; private String include;
@Override @Override
public int parse(String content, CleanerPool global, CleanerPool local) throws Exception{ public int parse(String content, CleanerPool global, CleanerPool local) throws Exception{
Matcher matcher = PATTERN.matcher(content); Matcher matcher = PATTERN.matcher(content);
matcher.matches(); matcher.lookingAt();
this.include = local.unzip(matcher.group(2), (s,p) -> s); this.include = local.unzip(matcher.group(2), (s,p) -> s);
@ -25,7 +25,7 @@ public class SynchronizedOperation extends OperationContainer{
content = content.substring(index); content = content.substring(index);
matcher = PATTERN_CLEANER.matcher(content); matcher = PATTERN_CLEANER.matcher(content);
matcher.matches(); matcher.lookingAt();
content = matcher.group(1); content = matcher.group(1);