4 ms·
It’s not clear to me why reversing a string is dangerous or pointless.
by togs 7y ago
It’s not clear to me why reversing a string is dangerous or pointless.
- bastawhiz 7y agoAn array of characters reversed loses the original meaning of the characters. Other than looking for palindromes, there's almost nothing in the way of practical uses of a reversed string that can't be accomplished by subscripting the string and iterating from the end to the beginning.
- PeCaN 7y agoIterating from back to front doesn't work either; you probably (depending on what you're doing) still have to segment into grapheme clusters—which is stateful as of unicode 9, so you have to start from the beginning of the string. And then god forbid you get U+202C POP DIRECTIONAL FORMATTING CODE....
- catlifeonmars 7y agoI found this little gem on Software engineering StackExchange: https://softwareengineering.stackexchange.com/questions/24691/what-do-you-use-string-reversal-for https://softwareengineering.stackexchange.com/questions/2469... Suffice it to say, there are many interesting uses for string reversal :)
- bastawhiz 7y agoInteresting, but questionably useful. If you're converting integer bases or obfuscating email addresses, there are much better approaches that don't involve a bad hack
- jodrellblank 7y ago"lol :Man facepalming: :medium light skintone:" becomes the skintone applying to nothing (which might crash?) and the wrong coloured man. (e+accent a) making éa becomes (a+accent e) incorrectly making áe - or possibly invalidly making an error combination. Right-to-left markers[1] and left-to-right markers will change which sections of the text are reversed unless you swap them over. Codepoints can combine more than once, to the point where if you're too nitpicky you can't validly substring either, you can only read a string from the first codepoint onwards; they could become invalid sequences if reversed, possibly? [1] https://en.wikipedia.org/wiki/Right-to-left_mark https://en.wikipedia.org/wiki/Right-to-left_mark
- rlayton2 7y agoAgree. I think reversing in non-ascii should always be thought of as "per-token", where English is character-as-token. So the reverse of what you gave would be: ":medium light skintone: :Man facepalming: lol" (with the lol reversed). In this problem, it is a much harder problem than, say in python, mystring[::-1]. Therefore, it is a different problem "reverse a string" than to "reverse an array". Accented characters would be kept as is in my scenario.
- PeCaN 7y agoThe "tokens" you're thinking of are "grapheme clusters" in Unicode. Unfortunately just reversing by grapheme clusters doesn't solve the problem because of directional formatting codes; if you have e.g. a right-to-left embedding followed by a pop directional formatting you can't naively reverse them.
- naniwaduni 7y agoGrapheme clusters are a poor approximation of the vaguely-defined linguistic-level concept you're groping for.
- PeCaN 7y agoWell, yes, but we gotta stop somewhere or just give up any hope of computers operating on text. Although I think grapheme clusters are a pretty good approximation in that it's usually what you want to backspace in a word processor.
- diegoperini 7y agoIs there a better approximation?
- deleted 7y ago[deleted]
- tragomaskhalos 7y agoThere'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...