mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-25 00:50:22 +00:00
Add a Chomp feature to String construction that removes a trailing newline.
This will be useful in many situations.
This commit is contained in:
parent
90bab5ea71
commit
8135952832
Notes:
sideshowbarker
2024-07-19 18:32:52 +09:00
Author: https://github.com/awesomekling Commit: https://github.com/SerenityOS/serenity/commit/81359528321
4 changed files with 19 additions and 9 deletions
|
@ -65,6 +65,8 @@ Vector<String> String::split(const char separator) const
|
||||||
size_t taillen = length() - substart;
|
size_t taillen = length() - substart;
|
||||||
if (taillen != 0)
|
if (taillen != 0)
|
||||||
v.append(substring(substart, taillen));
|
v.append(substring(substart, taillen));
|
||||||
|
if (characters()[length() - 1] == separator)
|
||||||
|
v.append(empty());
|
||||||
return v;
|
return v;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -24,13 +24,13 @@ public:
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
String(const char* cstring)
|
String(const char* cstring, ShouldChomp shouldChomp = NoChomp)
|
||||||
: m_impl(StringImpl::create(cstring))
|
: m_impl(StringImpl::create(cstring, shouldChomp))
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
String(const char* cstring, size_t length)
|
String(const char* cstring, size_t length, ShouldChomp shouldChomp = NoChomp)
|
||||||
: m_impl(StringImpl::create(cstring, length))
|
: m_impl(StringImpl::create(cstring, length, shouldChomp))
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -42,7 +42,7 @@ RetainPtr<StringImpl> StringImpl::createUninitialized(size_t length, char*& buff
|
||||||
return newStringImpl;
|
return newStringImpl;
|
||||||
}
|
}
|
||||||
|
|
||||||
RetainPtr<StringImpl> StringImpl::create(const char* cstring, size_t length)
|
RetainPtr<StringImpl> StringImpl::create(const char* cstring, size_t length, ShouldChomp shouldChomp)
|
||||||
{
|
{
|
||||||
if (!cstring)
|
if (!cstring)
|
||||||
return nullptr;
|
return nullptr;
|
||||||
|
@ -54,15 +54,20 @@ RetainPtr<StringImpl> StringImpl::create(const char* cstring, size_t length)
|
||||||
auto newStringImpl = createUninitialized(length, buffer);
|
auto newStringImpl = createUninitialized(length, buffer);
|
||||||
memcpy(buffer, cstring, length * sizeof(char));
|
memcpy(buffer, cstring, length * sizeof(char));
|
||||||
|
|
||||||
|
if (shouldChomp && buffer[length - 1] == '\n') {
|
||||||
|
buffer[length - 1] = '\0';
|
||||||
|
--newStringImpl->m_length;
|
||||||
|
}
|
||||||
|
|
||||||
return newStringImpl;
|
return newStringImpl;
|
||||||
}
|
}
|
||||||
|
|
||||||
RetainPtr<StringImpl> StringImpl::create(const char* cstring)
|
RetainPtr<StringImpl> StringImpl::create(const char* cstring, ShouldChomp shouldChomp)
|
||||||
{
|
{
|
||||||
if (!cstring)
|
if (!cstring)
|
||||||
return nullptr;
|
return nullptr;
|
||||||
|
|
||||||
return create(cstring, strlen(cstring));
|
return create(cstring, strlen(cstring), shouldChomp);
|
||||||
}
|
}
|
||||||
|
|
||||||
static inline bool isASCIILowercase(char c)
|
static inline bool isASCIILowercase(char c)
|
||||||
|
|
|
@ -6,11 +6,13 @@
|
||||||
|
|
||||||
namespace AK {
|
namespace AK {
|
||||||
|
|
||||||
|
enum ShouldChomp { NoChomp, Chomp };
|
||||||
|
|
||||||
class StringImpl : public Retainable<StringImpl> {
|
class StringImpl : public Retainable<StringImpl> {
|
||||||
public:
|
public:
|
||||||
static RetainPtr<StringImpl> createUninitialized(size_t length, char*& buffer);
|
static RetainPtr<StringImpl> createUninitialized(size_t length, char*& buffer);
|
||||||
static RetainPtr<StringImpl> create(const char* cstring);
|
static RetainPtr<StringImpl> create(const char* cstring, ShouldChomp = NoChomp);
|
||||||
static RetainPtr<StringImpl> create(const char* cstring, size_t length);
|
static RetainPtr<StringImpl> create(const char* cstring, size_t length, ShouldChomp = NoChomp);
|
||||||
RetainPtr<StringImpl> toLowercase() const;
|
RetainPtr<StringImpl> toLowercase() const;
|
||||||
RetainPtr<StringImpl> toUppercase() const;
|
RetainPtr<StringImpl> toUppercase() const;
|
||||||
|
|
||||||
|
@ -50,3 +52,4 @@ private:
|
||||||
}
|
}
|
||||||
|
|
||||||
using AK::StringImpl;
|
using AK::StringImpl;
|
||||||
|
using AK::Chomp;
|
||||||
|
|
Loading…
Reference in a new issue