arecarn/crunch — pull request #25 by mMontu (2013-10-03 20:16:09+00:00)
Title: Suffix refactor

While refactoring the code I entered github to check for an older version of s:ValidLine(), and only then I noticed you already did it, mostly in the same lines I was about to propose :D

I just added a few more changes, but I think it is already clean on your last commit.

I thought it would be possible to simplify more the code by removing the prefix and suffix at the beginning of CrunchLine(), and restoring them only at the end. But unfortunately they are also used at GetVariableValue().

It would be possible to decouple from GetVariableValue() only for CrunchBlock(): instead of backward searching for variable values, one could save any variables defined in the block as each line is read, and then GetVariableValue() would simple read from a dictionary.
But when it comes to CrunchLine() usage I don't know how this can be avoided.

I've changed s:prefixRegex/s:suffixRegex to a dictionary with an entry per filetype, but I'm not sure if this is really an improvement - I'd like to hear your thoughts. With the script variables both regex are recalculated for every line that is parsed. As these regex are expected to be the same for a given filetype they can be saved, but this performance gain increases the complexity of the code.
Since it is possible for a buffer to have an empty filetype but an empty string is an invalid key for dictionary I've prefixed it with an '_' -- this was the best I could think now to overcome this unexpected limitation.

Another small change: replaced '=' by '?' in some regexes, because they meaning are the same, but as '\=' is also used on many regex and '?' has the same meaning on perl regex I think it can make code easier to read.