diff options
author | Jesse Buhagiar <jesse.buhagiar@student.rmit.edu.au> | 2019-09-13 16:00:36 +1000 |
---|---|---|
committer | Andreas Kling <awesomekling@gmail.com> | 2019-09-13 13:54:07 +0200 |
commit | 26e81ad574d463faee19f5973108f80d0e02aaf6 (patch) | |
tree | abbbd81218782429505e60cae6db3df4dd187690 | |
parent | 093961d2d935b189861f86d2f805ac31c23377f9 (diff) | |
download | serenity-26e81ad574d463faee19f5973108f80d0e02aaf6.zip |
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.
-rw-r--r--[-rwxr-xr-x] | AK/String.h | 10 | ||||
-rw-r--r-- | AK/StringImpl.cpp | 17 | ||||
-rw-r--r-- | AK/StringImpl.h | 4 | ||||
-rw-r--r-- | AK/Tests/TestString.cpp | 1 |
4 files changed, 28 insertions, 4 deletions
diff --git a/AK/String.h b/AK/String.h index 2b299841b9..fd0b707a42 100755..100644 --- a/AK/String.h +++ b/AK/String.h @@ -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; diff --git a/AK/StringImpl.cpp b/AK/StringImpl.cpp index 5e8c6d9090..a036ef250c 100644 --- a/AK/StringImpl.cpp +++ b/AK/StringImpl.cpp @@ -4,7 +4,7 @@ #include "kmalloc.h" #ifndef __serenity__ -#include <new> +# include <new> #endif //#define DEBUG_STRINGIMPL @@ -172,4 +172,19 @@ void StringImpl::compute_hash() const m_has_hash = true; } +NonnullRefPtr<StringImpl> StringImpl::reversed() const +{ + if (m_length == 0) + return the_empty_stringimpl(); + + char* buffer; + const char* pos = &m_inline_buffer[m_length - 1]; + auto new_impl = create_uninitialized(m_length, buffer); + + for (int i = 0; i < m_length; i++) + buffer[i] = *pos--; + + return new_impl; +} + } diff --git a/AK/StringImpl.h b/AK/StringImpl.h index b2b085f81d..d00a8dc857 100644 --- a/AK/StringImpl.h +++ b/AK/StringImpl.h @@ -1,7 +1,7 @@ #pragma once -#include <AK/RefPtr.h> #include <AK/RefCounted.h> +#include <AK/RefPtr.h> #include <AK/Types.h> #include <AK/kmalloc.h> @@ -44,6 +44,8 @@ public: return m_hash; } + NonnullRefPtr<StringImpl> reversed() const; + private: enum ConstructTheEmptyStringImplTag { ConstructTheEmptyStringImpl diff --git a/AK/Tests/TestString.cpp b/AK/Tests/TestString.cpp index 9a427d302e..03fbb51641 100644 --- a/AK/Tests/TestString.cpp +++ b/AK/Tests/TestString.cpp @@ -41,6 +41,7 @@ TEST_CASE(compare) EXPECT(!("a" >= String("b"))); EXPECT("a" <= String("a")); EXPECT(!("b" <= String("a"))); + EXPECT(!strcmp(test_string.reversed().characters(), "FEDCBA")); } TEST_CASE(index_access) |