1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 23:07:35 +00:00

AK: Made Strings reversible

`AK::String` can now be reversed via AK::String::reverse(). This makes
life a lot easier for functions like `itoa()`, where the output
ends up being backwards. Very much not like the normal STL
(which requires an `std::reverse` object) way of doing things.

A call to reverse returns a new `AK::String` so as to not upset any
of the possible references to the same `StringImpl` shared between
Strings.
This commit is contained in:
Jesse Buhagiar 2019-09-13 16:00:36 +10:00 committed by Andreas Kling
parent 093961d2d9
commit 26e81ad574
4 changed files with 28 additions and 4 deletions

10
AK/String.h Executable file → Normal file
View file

@ -110,6 +110,13 @@ public:
return m_impl->to_uppercase();
}
String reversed() const
{
if (!m_impl)
return String();
return m_impl->reversed();
}
Vector<String> split_limit(char separator, int limit) const;
Vector<String> split(char separator) const;
String substring(int start, int length) const;
@ -227,7 +234,6 @@ struct Traits<String> : public GenericTraits<String> {
struct CaseInsensitiveStringTraits : public AK::Traits<String> {
static unsigned hash(const String& s) { return s.impl() ? s.to_lowercase().impl()->hash() : 0; }
static bool equals(const String& a, const String& b) { return a.to_lowercase() == b.to_lowercase(); }
};
inline bool operator<(const char* characters, const String& string)
@ -264,5 +270,5 @@ inline bool operator<=(const char* characters, const String& string)
}
using AK::String;
using AK::CaseInsensitiveStringTraits;
using AK::String;