See my comment above : "tested this to actually be quite faster than the same with a regex, so let's keep it this way, even if it's pretty ugly" ;-P

And it's not that hard to follow once you've figured it out what we're trying to do...