MarginaliaSearch/code/libraries/guarded-regex
Viktor Lofgren 4668b1ddcb (build) Java 22 and its consequences has been a disaster for Marginalia Search
Roll back to JDK 21 for now, and make Java version configurable in the root build.gradle

The project has run into no less than three distinct show-stopping bugs in JDK22, across multiple vendors, and gradle still doesn't fully support it, meaning you need multiple JDK versions installed.
2024-04-24 13:54:04 +02:00
..
java/nu/marginalia/gregex (refac) Remove src/main from all source code paths. 2024-02-23 16:13:40 +01:00
build.gradle (build) Java 22 and its consequences has been a disaster for Marginalia Search 2024-04-24 13:54:04 +02:00
readme.md (docs) Begin un-fucking the docs after refactoring 2024-02-27 21:22:21 +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