Redmine#6725: refactor the local class matching functions and add tag support to them

* removes lots of duplicated code, always a good thing
* slightly inefficient: builds a StringSet of all the matches, but that can be easily optimized after it's tested to be correct
* many tests fail for unrelated reasons so I tested this as much as I could

Have fun...