Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Why do you want a string reverse?

- Do you want to reverse bytes, or codepoints?

- Do you want to reveres codepoints, or grapheme clusters?

- Do you really want to reverse grapheme clusters, or do you want to reverse some grapheme clusters while leaving e.g. sequences of control characters in the same order?

- Do you really want to reify any of this rather than iterate backwards in the existing memory?



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()


Yes but why? Those expressions do wildly different things both in terms of language semantics and in terms of observable behavior and most of them haul in some heavy additional machinery from the language. Perhaps where it is present, this is a case where stdlibs have implemented it because it's easy to implement, and not because it's actually useful.

(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.)


And reversing by codepoints is still wrong. It wrecks multi-codepoint sequences like flags.

https://go.dev/play/p/IsZBLqi7--1




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: