As characters are written to a java.io.Writer
I want to apply a regular expression to replace certain characters.
I don't want to buffer the whole thing into memory, it could be very big and it's performance sensitive.
I'm trying with a moving CharBuffer
and a Pattern, but the problem is that any sizing or flushing of the CharBuffer
short of the entire character stream may be incorrect.
Can a Pattern
know what index in a CharSequence
is the latest where it could never find
, even with more input? e.g. given a regex [ab]c
and an input ab
the index of a
is the last spot that could never find, so it should be safe to flush a
, but I still need b
because the next character might be c
. I've played with Matcher.hitEnd()
and Matcher.requireEnd()
to try and achieve this, but they don't give me enough info to know what bit is safe to flush.
I'm wondering if, given the power of regular expressions, this is actually a provably unsolvable problem and I just don't know it...