In Kotlin/JVM, is there a fast way to check if a b...
# getting-started
k
In Kotlin/JVM, is there a fast way to check if a byte array contains a given subarray? This is for a high-throughput filtering requirement. The subarray is known at design time. The byte array is UTF-8 encoded. I can convert the byte array to String and just
String.contains
(or
a in b
), but if I can do a byte-array search instead it would be preferable as it would save the overhead of converting to String. I don't want to write a Boyer-Moore (or similar fast algorithm) implementation for this myself, as (1) I might get it wrong and (2) there may already be one and I don't want to reinvent the wheel. I'm hoping there's something already there in the stdlib.
p
there’s a boyer-moore impl in Java on wikipedia; could just let intellij convert it over and then make any manual improvements: https://en.wikipedia.org/wiki/Boyer%E2%80%93Moore_string-search_algorithm#Java_implementation
🙏 1
if you care about speed, I would certainly keep things as ByteArrays over any conversion
140 Views