Why length(str) != sizeof(str)?

Well, that’s because your last character isn’t a unicode. If you want to get the last character, use lastindex.