Use multibyte substr and strlen functions

Fixes spans being positioned incorrectly after multibyte characters
Merge branch 'bugfix/handle-multibyte-characters' into patched