Speed up #build_formats (changes its API)

Rewrites build_formats for more speed.

1. Stops once best match is found and skips tests that won't match
1. Performs count directly instead of temporarily storing a value for every row
1. Uses symbols to be nice to garbage collector

My understanding is that `#build_formats` is only used internally, i.e. not part of a public API.