Improve byte offset->char offset algorithm