Logo

Programming-Idioms

History of Idiom 41 > diff from v57 to v58

Edit summary for version 58 by daxim:
[Perl] complete overhaul

Version 57

2019-09-27, 20:51:02

Version 58

2019-09-29, 18:43:33

Idiom #41 Reverse a string

Create string t containing the same characters as string s, in reverse order.
Original string s must remain unaltered. Each character must be handled correctly regardless its number of bytes in memory.

Illustration

Idiom #41 Reverse a string

Create string t containing the same characters as string s, in reverse order.
Original string s must remain unaltered. Each character must be handled correctly regardless its number of bytes in memory.

Illustration
Code
my $t = scalar reverse $s;
Code
my $s = 'cafe' . "\N{COMBINING ACUTE ACCENT}";
my $t = join '', reverse $s =~ /\X/g;
Comments bubble
**DOESN'T WORK WITH MULTI-BYTE CHARACTERS**

This snippet will be deleted soon
Comments bubble
Bet your programming language is not Unicode compliant. Perl, in general, is. Sad state of affairs: https://www.azabani.com/pages/gbu/

As of 2019-09-29, all other solutions are wrong AFAICT with respect to Unicode marks. (Dart has a disclaimer.)

Correct result: "e\N{U+0301}fac"
Wrong result: "\N{U+0301}efac"
Doc URL
http://p3rl.org/unicook#℞-32:-Reverse-string-by-grapheme