1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-14 09:24:57 +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:
Andreas Kling 2018-11-07 00:19:35 +01:00
parent 90bab5ea71
commit 8135952832
4 changed files with 19 additions and 9 deletions

View file

@ -42,7 +42,7 @@ RetainPtr<StringImpl> StringImpl::createUninitialized(size_t length, char*& buff
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)
return nullptr;
@ -54,15 +54,20 @@ RetainPtr<StringImpl> 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> StringImpl::create(const char* cstring)
RetainPtr<StringImpl> 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)