From 81359528321490740c6e7e69cab07076c505972b Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Wed, 7 Nov 2018 00:19:35 +0100 Subject: [PATCH] Add a Chomp feature to String construction that removes a trailing newline. This will be useful in many situations. --- AK/String.cpp | 2 ++ AK/String.h | 8 ++++---- AK/StringImpl.cpp | 11 ++++++++--- AK/StringImpl.h | 7 +++++-- 4 files changed, 19 insertions(+), 9 deletions(-) diff --git a/AK/String.cpp b/AK/String.cpp index ccc61dd08f..bf8339e449 100644 --- a/AK/String.cpp +++ b/AK/String.cpp @@ -65,6 +65,8 @@ Vector String::split(const char separator) const size_t taillen = length() - substart; if (taillen != 0) v.append(substring(substart, taillen)); + if (characters()[length() - 1] == separator) + v.append(empty()); return v; } diff --git a/AK/String.h b/AK/String.h index b639fe478c..a8e7e07818 100644 --- a/AK/String.h +++ b/AK/String.h @@ -24,13 +24,13 @@ public: { } - String(const char* cstring) - : m_impl(StringImpl::create(cstring)) + String(const char* cstring, ShouldChomp shouldChomp = NoChomp) + : m_impl(StringImpl::create(cstring, shouldChomp)) { } - String(const char* cstring, size_t length) - : m_impl(StringImpl::create(cstring, length)) + String(const char* cstring, size_t length, ShouldChomp shouldChomp = NoChomp) + : m_impl(StringImpl::create(cstring, length, shouldChomp)) { } diff --git a/AK/StringImpl.cpp b/AK/StringImpl.cpp index c89d080b13..6a3b9767a5 100644 --- a/AK/StringImpl.cpp +++ b/AK/StringImpl.cpp @@ -42,7 +42,7 @@ RetainPtr StringImpl::createUninitialized(size_t length, char*& buff return newStringImpl; } -RetainPtr StringImpl::create(const char* cstring, size_t length) +RetainPtr StringImpl::create(const char* cstring, size_t length, ShouldChomp shouldChomp) { if (!cstring) return nullptr; @@ -54,15 +54,20 @@ RetainPtr StringImpl::create(const char* cstring, size_t length) auto newStringImpl = createUninitialized(length, buffer); memcpy(buffer, cstring, length * sizeof(char)); + if (shouldChomp && buffer[length - 1] == '\n') { + buffer[length - 1] = '\0'; + --newStringImpl->m_length; + } + return newStringImpl; } -RetainPtr StringImpl::create(const char* cstring) +RetainPtr StringImpl::create(const char* cstring, ShouldChomp shouldChomp) { if (!cstring) return nullptr; - return create(cstring, strlen(cstring)); + return create(cstring, strlen(cstring), shouldChomp); } static inline bool isASCIILowercase(char c) diff --git a/AK/StringImpl.h b/AK/StringImpl.h index b52a252277..c5402a2121 100644 --- a/AK/StringImpl.h +++ b/AK/StringImpl.h @@ -6,11 +6,13 @@ namespace AK { +enum ShouldChomp { NoChomp, Chomp }; + class StringImpl : public Retainable { public: static RetainPtr createUninitialized(size_t length, char*& buffer); - static RetainPtr create(const char* cstring); - static RetainPtr create(const char* cstring, size_t length); + static RetainPtr create(const char* cstring, ShouldChomp = NoChomp); + static RetainPtr create(const char* cstring, size_t length, ShouldChomp = NoChomp); RetainPtr toLowercase() const; RetainPtr toUppercase() const; @@ -50,3 +52,4 @@ private: } using AK::StringImpl; +using AK::Chomp;