2 ms·
There's actually two levels of jeopardy here: 1/ Treating a string as an array of bytes will give an invalid result if the string unless the string is simple a
by tragomaskhalos 7y ago
There's actually two levels of jeopardy here:
1/ Treating a string as an array of bytes will give an invalid result if the string unless the string is simple ascii (or an equivalent encoding where each byte has a clearly defined standalone meaning); in particular, just reversing a UTF-8 string in this way will give an invalid answer - ie a string that isn't even valid UTF-8.
2/ The fix for (1) is to convert your string into an array of Unicode code points and reverse that … except that is also broken, because combining characters will now not associate correctly, as per other answers in this thread.
Coding your way out of problem (2) in a robust and sensible way is, I suggest, a significant challenge.
- to11mtm 7y agoOf all things, VB.Net actually has a string reverse that handles unicode cases. I'm not quite sure how to view it in referencesource, but it is on Github. [1] The result is less than 100 lines of VB so I don't think it's -that- hard. There's certainly some clever index manipulation going on but nothing that looks too crazy. [1] - https://github.com/microsoft/referencesource/blob/master/Microsoft.VisualBasic/runtime/msvbalib/Strings.vb#L1043-L1134 https://github.com/microsoft/referencesource/blob/master/Mic...