mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
PHP Fast injection regex patterns
This commit is contained in:
@@ -17,6 +17,10 @@ package com.intellij.patterns;
|
||||
|
||||
import com.intellij.openapi.util.text.StringUtil;
|
||||
import com.intellij.util.ProcessingContext;
|
||||
import dk.brics.automaton.Automaton;
|
||||
import dk.brics.automaton.DatatypesAutomatonProvider;
|
||||
import dk.brics.automaton.RegExp;
|
||||
import dk.brics.automaton.RunAutomaton;
|
||||
import org.jetbrains.annotations.NonNls;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
@@ -91,6 +95,7 @@ public class StringPattern extends ObjectPattern<String, StringPattern> {
|
||||
return equalTo(s);
|
||||
}
|
||||
final Pattern pattern = Pattern.compile(s);
|
||||
|
||||
return with(new ValuePatternCondition<String>("matches") {
|
||||
public boolean accepts(@NotNull final String str, final ProcessingContext context) {
|
||||
return pattern.matcher(str).matches();
|
||||
@@ -103,6 +108,44 @@ public class StringPattern extends ObjectPattern<String, StringPattern> {
|
||||
});
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public StringPattern matchesBrics(@NonNls @NotNull final String s) {
|
||||
final String escaped = StringUtil.escapeToRegexp(s);
|
||||
if (escaped.equals(s)) {
|
||||
return equalTo(s);
|
||||
}
|
||||
|
||||
StringBuilder sb = new StringBuilder(s.length()*5);
|
||||
for (int i = 0; i < s.length(); i++) {
|
||||
final char c = s.charAt(i);
|
||||
if(c == ' ') {
|
||||
sb.append("<whitespace>");
|
||||
}
|
||||
else
|
||||
if(Character.isUpperCase(c)) {
|
||||
sb.append('[').append(Character.toUpperCase(c)).append(Character.toLowerCase(c)).append(']');
|
||||
}
|
||||
else
|
||||
{
|
||||
sb.append(c);
|
||||
}
|
||||
}
|
||||
final RegExp regExp = new RegExp(sb.toString());
|
||||
final Automaton automaton = regExp.toAutomaton(new DatatypesAutomatonProvider());
|
||||
final RunAutomaton runAutomaton = new RunAutomaton(automaton, true);
|
||||
|
||||
return with(new ValuePatternCondition<String>("matchesBrics") {
|
||||
public boolean accepts(@NotNull final String str, final ProcessingContext context) {
|
||||
return runAutomaton.run(str);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection<String> getValues() {
|
||||
return Collections.singleton(s);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public StringPattern contains(@NonNls @NotNull final ElementPattern<Character> pattern) {
|
||||
return with(new PatternCondition<String>("contains") {
|
||||
|
||||
Reference in New Issue
Block a user