johngb/langreg — pull request #2 by attilaolah (2014-12-27 14:10:50+00:00)
Title: Use `go generate`

This change makes lookup times about 7× faster, by using a nested `switch` statement instead of one very long `switch`. The outer `switch` looks at the first character, while the inner `switch` looks at the second character.

This avoids lots of redundant comparisons (is it "aa"?, no. is it "ab"?, no. etc. instead we do does it start with "a"?, no. does it start with "b"? etc.) and also this way we can use byte comparison, which is also somewhat faster than the string comparison, since the string headers need not be compared (no need to check if they have the same length, for example).

---

### About `go generate`

I started making these changes by hand, and it was very annoying, so I introduced a small `.py` file that generates the code, and put all the country codes in a CSV file. If you don't like this @johngb, feel free to delete the .py files and remove the `//go:generate` comments from the files, just keep the generated files.