almondtools / stringsearchalgorithms

String matching algorithms for searching a single or multiple strings in large texts
http://stringsearchalgorithms.amygdalum.net/
GNU Lesser General Public License v3.0
44 stars 4 forks source link

Find previous #12

Closed thomasa299792 closed 3 years ago

thomasa299792 commented 3 years ago

Consider supporting the very common need of searching backwards in a string.

For example, consider adding a method findPrevious() as a sibling to StringFinder.findNext().

StringFinder {

    public abstract StringMatch findNext();
    public abstract StringMatch findPrevious();
almondtools commented 3 years ago

StringSearchAlgorithms is about efficient matching of strings. Especially it can find strings in InputStreams or Readers. And InputStreams/Readers can only be traversed in one direction (forwards) efficiently.

Adding a feature Backwards Search would be technically limited to the search in strings - so we could not provide this feature in the already existing StringFinder class.

Besides I am quite sceptical about the benefit of allwoing findNext() and findPrevious() simultaneously: If applied on a forward search calling findPrevious means that I search an already found match. Just storing the matches in a list and return the last stored stringmatch would be far more efficient than searching backwards.

If you just want to search backwards through a string efficiently:

Search using a new ReverseCharProvider(new StringCharProvider("hay needle hay", 14)) with the reverted search word (´"eldeen" instead of needle).