A JNI wrapper for the Oniguruma regular expression library, with Rust implementation using the onig crate.
This library is primarily designed to support syntax highlighting in IntelliJ-based IDEs through the textmate-core
library.
This project provides Java Native Interface (JNI) bindings for TextMate grammar pattern matching,
implemented in Rust using the onig
crate which provides the native bindings to the Oniguruma regular expression library.
Add the following dependency to your project:
dependencies {
implementation("com.github.zolotov:oniguruma-jni:0.1")
}
// Load the library from bundled resources
// Note: This method has performance overhead during instantiation (unpacking library from jar)
// and JVM shutdown (cleanup hook to remove the unpacked file)
val oniguruma = Oniguruma.createFromResources()
// Or load from a specific file path (preferred for better performance)
val oniguruma = Oniguruma.createFromFile(Path.of("/path/to/library"))
val oniguruma = Oniguruma.createFromResources()
// Create pattern and string handles
val pattern = "pattern".toByteArray()
val text = "text to match".toByteArray()
val textPtr = oniguruma.createString(text)
try {
val regexPtr = oniguruma.createRegex(pattern)
try {
val result = oniguruma.match(
regexPtr = regexPtr,
textPtr = textPtr,
byteOffset = 0,
matchBeginPosition = true,
matchBeginString = false
)
// Process results
result?.let {
// Match found, process the integer array of positions
it.asSequence()?.windowed(size = 2, step = 2, partialWindows = false) { (startByteOffset, endByteOffset) ->
}
}
} finally {
// Clean up native regex
oniguruma.freeRegex(regexPtr)
}
} finally {
// Clean up native string
oniguruma.freeString(textPtr)
}
- Clone the repository
- Ensure you have the following prerequisites:
- JDK 8 or later
- Rust toolchain
- Cross tool installed:
cargo install cross
- Build the project using Gradle:
./gradlew build
Contributions are welcome! Please feel free to submit pull requests.
- Oniguruma library developers
- onig-rs crate maintainers
This library is primarily intended for use with the textmate-core
library in IntelliJ-based IDEs. While it can be used independently, the API is designed with this specific use case in mind.