This patch fixes a few problems with the FormatAnsiTerminalCodes function:
- It does an infinite loop on an unknown color value.
- It crashes when the color value is at the end of the string.
- It deletes the first character behind the color token.
Also added a few tests that reproduce those problems (and test some other corner cases).