Search in sources :

Example 1 with DatatypesAutomatonProvider

use of dk.brics.automaton.DatatypesAutomatonProvider in project intellij-community by JetBrains.

the class StringPattern method matchesBrics.

@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("<whitespacechar>");
        } else //This is really stupid and inconvenient builder - it breaks any normal pattern with uppercase
        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") {

        @Override
        public boolean accepts(@NotNull String str, final ProcessingContext context) {
            return runAutomaton.run(str);
        }

        @Override
        public Collection<String> getValues() {
            return Collections.singleton(s);
        }
    });
}
Also used : ProcessingContext(com.intellij.util.ProcessingContext) RunAutomaton(dk.brics.automaton.RunAutomaton) Automaton(dk.brics.automaton.Automaton) RegExp(dk.brics.automaton.RegExp) DatatypesAutomatonProvider(dk.brics.automaton.DatatypesAutomatonProvider) Collection(java.util.Collection) RunAutomaton(dk.brics.automaton.RunAutomaton) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

ProcessingContext (com.intellij.util.ProcessingContext)1 Automaton (dk.brics.automaton.Automaton)1 DatatypesAutomatonProvider (dk.brics.automaton.DatatypesAutomatonProvider)1 RegExp (dk.brics.automaton.RegExp)1 RunAutomaton (dk.brics.automaton.RunAutomaton)1 Collection (java.util.Collection)1 NotNull (org.jetbrains.annotations.NotNull)1