Ways to Reverse String.kmmacros (18.5 KB) (The AppleScriptObjC method works correctly with all.)įor ease of testing, here a macro with all posted solutions so far: (The diacritics ́̂ and ̛̣ get detached from the letters they belong to.) It works correctly with the “simple” kind of strings like résumé, but fails with the Rượu đế test string from above. Hey That method is fully unicode compliment Once you paste the string into a Unicode-compliant editor (and using a compliant font) on your computer it will again look like “ Rượu đế ”, but with the correct composition.Īlternatively you can copy the string from inside this macro. If you want to test a script with the Vietnamese test string, you have to copy it from the following line (do not copy one of the inline instances in the text!) : Rượu đếĪlthough the string looks different here, this will maintain the original composition of the characters. You should also set the output to a (non-local) variable, because the KM Shell Script Results window doesn’t show all characters correctly. ́̂eđ ựơuR when only setting the binmode to utf8.Īll the scripts are written to be used in a KM Shell Script action with input from a variable (stdin), as shown in the screenshot at the top.ÌÌeÄ u£ÌÌoÌuR when using the multibyte incompatible script at the top of this post, and.$_ = join '', reverse $str->as_arrayref } Īs test case I have used the nice Vietnamese word Rượu đế, found in a related discussion on .ĭepending on the used technique, I’ve seen two different wrong results: So far the only solutions I have found to work with any kind of strings are these:ġ) - Splitting the string with the \X special (“Match Unicode eXtended grapheme cluster”): #!/usr/bin/env perlĢ) - Or using the Unicode::GCString module and reversing the result as list: #!/usr/bin/env perl With this modification, the Perl script from above produces correctly esäK and émusér.īut with more “sophisticated” strings like Rượu đế this is not enough. it seems to be sufficient to explicitly set the stdin mode to “utf8” with binmode STDIN, ':utf8' The pitfall (or: “what I have learned today ) is that this fails to a different degree if the string contains multibyte UTF characters like ä, é, ợ, đ etc.įor the more simple things like Käse, résumé etc. Or as a command-line one-liner: perl -pe '$_ = reverse' If you only have singlebyte characters in your input (for example ) you can also get away with a very nice and short Perl solution:
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |