Optimise Regex with Mehdi H.
This commit is contained in:
parent
9b41348588
commit
b82e91fefa
11 changed files with 48 additions and 48 deletions
|
@ -5,10 +5,10 @@ import java.util.regex.Pattern;
|
|||
|
||||
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){
|
||||
return PATTERN.matcher(content).matches();
|
||||
return PATTERN.matcher(content).lookingAt();
|
||||
}
|
||||
|
||||
private String name;
|
||||
|
@ -17,7 +17,7 @@ public class Import {
|
|||
|
||||
public int parse(String content) throws Exception{
|
||||
Matcher matcher = PATTERN.matcher(content);
|
||||
matcher.matches();
|
||||
matcher.lookingAt();
|
||||
this.name = matcher.group(2);
|
||||
return matcher.group(1).length();
|
||||
}
|
||||
|
|
|
@ -5,7 +5,7 @@ import java.util.regex.Pattern;
|
|||
|
||||
public class Package {
|
||||
|
||||
private static Pattern PATTERN = Pattern.compile("^(\\s*package\\s+([^;]*);).*$");
|
||||
private static Pattern PATTERN = Pattern.compile("^(\\s*package\\s+([^;]*);)");
|
||||
|
||||
private String name;
|
||||
|
||||
|
@ -13,7 +13,7 @@ public class Package {
|
|||
|
||||
public int parse(String content) throws Exception{
|
||||
Matcher matcher = PATTERN.matcher(content);
|
||||
matcher.matches();
|
||||
matcher.lookingAt();
|
||||
this.name = matcher.group(2);
|
||||
return matcher.group(1).length();
|
||||
}
|
||||
|
|
|
@ -11,7 +11,7 @@ import be.jeffcheasey88.peeratcode.parser.java.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 value;
|
||||
|
@ -21,7 +21,7 @@ public class AssigmentOperation extends JavaElement{
|
|||
@Override
|
||||
public int parse(String content, CleanerPool global, CleanerPool local) throws Exception{
|
||||
Matcher matcher = PATTERN.matcher(content);
|
||||
matcher.matches();
|
||||
matcher.lookingAt();
|
||||
|
||||
this.variable = local.unzip(matcher.group(2), (s,p) -> s);
|
||||
this.value = local.unzip(matcher.group(3), (s,p) -> s);
|
||||
|
|
|
@ -9,8 +9,8 @@ import be.jeffcheasey88.peeratcode.parser.java.JavaElement;
|
|||
|
||||
public class ConditionalOperation extends OperationContainer{
|
||||
|
||||
private static Pattern PATTERN = Pattern.compile("^(\\^GENERIC_FUNCTION\\d+).*$");
|
||||
private static Pattern PATTERN_SPACE = Pattern.compile("^(\\s+).*$");
|
||||
private static Pattern PATTERN = Pattern.compile("^(\\^GENERIC_FUNCTION\\d+)");
|
||||
private static Pattern PATTERN_SPACE = Pattern.compile("^(\\s+)");
|
||||
|
||||
private Pattern pattern;
|
||||
private String condition;
|
||||
|
@ -22,7 +22,7 @@ public class ConditionalOperation extends OperationContainer{
|
|||
@Override
|
||||
public int parse(String content, CleanerPool global, CleanerPool local) throws Exception{
|
||||
Matcher matcher = this.pattern.matcher(content);
|
||||
matcher.matches();
|
||||
matcher.lookingAt();
|
||||
|
||||
this.condition = local.unzipOne(matcher.group(2), (s,p) -> s);
|
||||
System.out.println("CONDITION "+condition);
|
||||
|
@ -31,7 +31,7 @@ public class ConditionalOperation extends OperationContainer{
|
|||
content = content.substring(index);
|
||||
|
||||
matcher = PATTERN_SPACE.matcher(content);
|
||||
if(matcher.matches()){
|
||||
if(matcher.lookingAt()){
|
||||
index+=matcher.group(1).length();
|
||||
content = content.substring(matcher.group(1).length());
|
||||
}
|
||||
|
@ -39,7 +39,7 @@ public class ConditionalOperation extends OperationContainer{
|
|||
int bodysize;
|
||||
if(content.startsWith("^")){
|
||||
matcher = PATTERN.matcher(content);
|
||||
matcher.matches();
|
||||
matcher.lookingAt();
|
||||
|
||||
content = matcher.group(1);
|
||||
|
||||
|
@ -84,7 +84,7 @@ public class ConditionalOperation extends OperationContainer{
|
|||
|
||||
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(){
|
||||
super(PATTERN);
|
||||
|
@ -104,7 +104,7 @@ public class ConditionalOperation extends OperationContainer{
|
|||
|
||||
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(){
|
||||
super(PATTERN);
|
||||
|
@ -123,7 +123,7 @@ public class ConditionalOperation extends OperationContainer{
|
|||
|
||||
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(){
|
||||
super(PATTERN);
|
||||
|
|
|
@ -9,21 +9,21 @@ import be.jeffcheasey88.peeratcode.parser.java.JavaElement;
|
|||
|
||||
public class DoOperation extends OperationContainer{
|
||||
|
||||
private static Pattern PATTERN = Pattern.compile("^(\\s*do\\s*).*$");
|
||||
private static Pattern PATTERN_CLEANER = Pattern.compile("^(\\^GENERIC_FUNCTION\\d+).*$");
|
||||
private static Pattern PATTERN = Pattern.compile("^(\\s*do\\s*)");
|
||||
private static Pattern PATTERN_CLEANER = Pattern.compile("^(\\^GENERIC_FUNCTION\\d+)");
|
||||
|
||||
public DoOperation(){}
|
||||
|
||||
@Override
|
||||
public int parse(String content, CleanerPool global, CleanerPool local) throws Exception{
|
||||
Matcher matcher = PATTERN.matcher(content);
|
||||
matcher.matches();
|
||||
matcher.lookingAt();
|
||||
|
||||
int index = matcher.group(1).length();
|
||||
content = content.substring(index);
|
||||
|
||||
matcher = PATTERN_CLEANER.matcher(content);
|
||||
matcher.matches();
|
||||
matcher.lookingAt();
|
||||
|
||||
content = matcher.group(1);
|
||||
|
||||
|
|
|
@ -9,15 +9,15 @@ import be.jeffcheasey88.peeratcode.parser.java.JavaElement;
|
|||
|
||||
public class ElseOperation extends OperationContainer{
|
||||
|
||||
private static Pattern PATTERN = Pattern.compile("^(\\s*else\\s*).*$");
|
||||
private static Pattern PATTERN_CLEANER = Pattern.compile("^(\\^GENERIC_FUNCTION\\d+).*$");
|
||||
private static Pattern PATTERN = Pattern.compile("^(\\s*else\\s*)");
|
||||
private static Pattern PATTERN_CLEANER = Pattern.compile("^(\\^GENERIC_FUNCTION\\d+)");
|
||||
|
||||
public ElseOperation(){}
|
||||
|
||||
@Override
|
||||
public int parse(String content, CleanerPool global, CleanerPool local) throws Exception{
|
||||
Matcher matcher = PATTERN.matcher(content);
|
||||
matcher.matches();
|
||||
matcher.lookingAt();
|
||||
|
||||
int index = matcher.group(1).length();
|
||||
content = content.substring(index);
|
||||
|
@ -25,7 +25,7 @@ public class ElseOperation extends OperationContainer{
|
|||
int bodysize;
|
||||
if(content.startsWith("^")){
|
||||
matcher = PATTERN_CLEANER.matcher(content);
|
||||
matcher.matches();
|
||||
matcher.lookingAt();
|
||||
|
||||
content = matcher.group(1);
|
||||
|
||||
|
|
|
@ -20,7 +20,7 @@ public class LoopAffectOperation extends JavaElement{
|
|||
@Override
|
||||
public int parse(String content, CleanerPool global, CleanerPool local) throws Exception{
|
||||
Matcher matcher = this.pattern.matcher(content);
|
||||
matcher.matches();
|
||||
matcher.lookingAt();
|
||||
|
||||
return matcher.group(1).length();
|
||||
}
|
||||
|
@ -39,7 +39,7 @@ public class LoopAffectOperation extends JavaElement{
|
|||
|
||||
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(){
|
||||
super(PATTERN);
|
||||
|
@ -56,7 +56,7 @@ public class LoopAffectOperation extends JavaElement{
|
|||
|
||||
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(){
|
||||
super(PATTERN);
|
||||
|
|
|
@ -11,7 +11,7 @@ import be.jeffcheasey88.peeratcode.parser.java.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;
|
||||
|
||||
|
@ -22,7 +22,7 @@ public class MethodCallOperation extends JavaElement{
|
|||
content = local.clean(content);
|
||||
|
||||
Matcher matcher = PATTERN.matcher(content);
|
||||
matcher.matches();
|
||||
matcher.lookingAt();
|
||||
|
||||
this.value = local.unzip(matcher.group(2), (s,p) -> s);
|
||||
|
||||
|
|
|
@ -28,23 +28,23 @@ public class OperationFactory{
|
|||
private OperationFactory(){
|
||||
this.patterns = new LinkedHashMap<>();
|
||||
|
||||
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*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*else\\s*.*$"), ElseOperation.class);
|
||||
this.patterns.put(Pattern.compile("^\\s*do\\s*\\^GENERIC_FUNCTION\\d+.*$"), DoOperation.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*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*else\\s*"), ElseOperation.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*break\\s*;.*$"), BreakOperation.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*[^\\^\\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*[^;]+;.*$"), AssigmentOperation.class);
|
||||
this.patterns.put(Pattern.compile("^\\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*[^;]+;"), Variable.class);
|
||||
}
|
||||
|
||||
/*
|
||||
|
@ -81,7 +81,7 @@ public class OperationFactory{
|
|||
content = generiqueTypes(content, generic);
|
||||
|
||||
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;
|
||||
}
|
||||
|
|
|
@ -11,7 +11,7 @@ import be.jeffcheasey88.peeratcode.parser.java.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();
|
||||
|
||||
|
@ -23,7 +23,7 @@ public class ReturnOperation extends JavaElement{
|
|||
@Override
|
||||
public int parse(String content, CleanerPool global, CleanerPool local) throws Exception{
|
||||
Matcher matcher = PATTERN.matcher(content);
|
||||
matcher.matches();
|
||||
matcher.lookingAt();
|
||||
|
||||
//To update for native obj
|
||||
this.toChange = matcher.group(2);
|
||||
|
|
|
@ -9,15 +9,15 @@ import be.jeffcheasey88.peeratcode.parser.java.JavaElement;
|
|||
|
||||
public class SynchronizedOperation extends OperationContainer{
|
||||
|
||||
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 = Pattern.compile("^(\\s*synchronized\\s*)(\\^GENERIC_PARENTHESIS\\d+)(\\s*)");
|
||||
private static Pattern PATTERN_CLEANER = Pattern.compile("^(\\^GENERIC_FUNCTION\\d+)");
|
||||
|
||||
private String include;
|
||||
|
||||
@Override
|
||||
public int parse(String content, CleanerPool global, CleanerPool local) throws Exception{
|
||||
Matcher matcher = PATTERN.matcher(content);
|
||||
matcher.matches();
|
||||
matcher.lookingAt();
|
||||
|
||||
this.include = local.unzip(matcher.group(2), (s,p) -> s);
|
||||
|
||||
|
@ -25,7 +25,7 @@ public class SynchronizedOperation extends OperationContainer{
|
|||
content = content.substring(index);
|
||||
|
||||
matcher = PATTERN_CLEANER.matcher(content);
|
||||
matcher.matches();
|
||||
matcher.lookingAt();
|
||||
|
||||
content = matcher.group(1);
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue