|
|
|
|
|
by pwinnski
1469 days ago
|
|
I think most people are expecting something along these lines: C++: reverse(str.begin(), str.end()); Dart: str.split('').reversed.join(); Java: new StringBuilder().append(str).reverse().toString(); JavaScript: str.split('').reverse().join(''); PHP: strrev($str) Python: ".join(reversed(str)) Rust: str.chars().rev().collect() |
|
(What do I mean by wildly different things?
C++: Swaps the string's contents in-place, and probably breaks any multi-byte code units unless you've got a parameterized std::string at hand.
Dart: Makes a new string but has to round-trip via an array, because... it doesn't have a string reverse? This seems like a really bad argument for your side!
Java: Reverses codepoints, but the fact you have to round-trip through a StringBuilder to handle this is also telling.
JavaScript: Same comments as Dart, but I believe this is broken, it will reverse surrogate pairs incorrectly.
PHP: Good luck figuring out what this does depending on your platform, locale, and moon phase.
Python: Another codepoint reverse, again not via strings but a lazy sequence, and also not even idiomatic - use `str[::-1]`.
Rust: And finally again... not a string reverse.
You want a Go slice reverse? You can get a perfect one post-generics.)