Avoid an IndexError when dealing with empty strings

Fixes #778