Fix count_chars return value for empty strings

Closes #4395.