MarginaliaSearch/code/libraries/guarded-regex
Viktor Lofgren 1d34224416 (refac) Remove src/main from all source code paths.
Look, this will make the git history look funny, but trimming unnecessary depth from the source tree is a very necessary sanity-preserving measure when dealing with a super-modularized codebase like this one.

While it makes the project configuration a bit less conventional, it will save you several clicks every time you jump between modules.  Which you'll do a lot, because it's *modul*ar.  The src/main/java convention makes a lot of sense for a non-modular project though.  This ain't that.
2024-02-23 16:13:40 +01:00
..
java/nu/marginalia/gregex (refac) Remove src/main from all source code paths. 2024-02-23 16:13:40 +01:00
build.gradle (refac) Remove src/main from all source code paths. 2024-02-23 16:13:40 +01:00
readme.md Move all code to a code directory. 2023-03-07 17:14:32 +01:00

Guarded Regex

This is a simple library for creating guarded regular expressions. Pattern matching in Java is pretty slow even with compiled regular expressions.

Guarding them with a startsWith(), endsWith() or contains() can be an order of magnitude faster, but leads to an unfortunate spreading out of the logic across the pattern and the guard condition.

Guarded regexes aims to fix this. Instead of code like

Pattern pattern = Pattern.compile("[123]?foo(bar|baz){2,5}");

void ifTheThingDoTheThing(String str) {
  if (str.contains("foo") && pattern.matcher(str).matches()) {
    doTheThing();
  }
}

you get the more expressive variant

GuardedRegex thingPredicate = 
    GuardedRegexFactory.contains("foo", "[123]?foo(bar|baz){2,5}");

void ifTheThingDoTheThing(String str) {
    if (thingPredicate.test(str)) {
      doTheThing();
    }
}

Central Classes