AK: Improve StringImpl chomping a bit.

Chomp off any number of trailing [\0\n\r] characters and trim the allocation
to fit instead of keeping the original size.
This commit is contained in:
Andreas Kling 2019-04-07 20:19:16 +02:00
parent 31b9d8354e
commit f93b3b7806

View file

@ -66,7 +66,7 @@ Retained<StringImpl> StringImpl::create_uninitialized(ssize_t length, char*& buf
return new_stringimpl;
}
RetainPtr<StringImpl> StringImpl::create(const char* cstring, ssize_t length, ShouldChomp shouldChomp)
RetainPtr<StringImpl> StringImpl::create(const char* cstring, ssize_t length, ShouldChomp should_chomp)
{
if (!cstring)
return nullptr;
@ -74,6 +74,16 @@ RetainPtr<StringImpl> StringImpl::create(const char* cstring, ssize_t length, Sh
if (!*cstring)
return the_empty_stringimpl();
if (should_chomp) {
while (length) {
char last_ch = cstring[length - 1];
if (!last_ch || last_ch == '\n' || last_ch == '\r')
--length;
else
break;
}
}
if (!length)
return the_empty_stringimpl();
@ -81,11 +91,6 @@ RetainPtr<StringImpl> StringImpl::create(const char* cstring, ssize_t length, Sh
auto new_stringimpl = create_uninitialized(length, buffer);
memcpy(buffer, cstring, length * sizeof(char));
if (shouldChomp && buffer[length - 1] == '\n') {
buffer[length - 1] = '\0';
--new_stringimpl->m_length;
}
return new_stringimpl;
}