Make it possible to also get the 'previous' match.

This is either the match before the 'current' one, or when that is
None, the match before the 'next' one. Add helper functions to avoid
lots of repeat bounded checks or 'next' lookup logic and replace the
existing functions with these.

Change-Id: I41498113d2b3e9ec073dd3e53c8c220fe39993bf