mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-25 00:50:22 +00:00
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:
parent
31b9d8354e
commit
f93b3b7806
Notes:
sideshowbarker
2024-07-19 14:47:47 +09:00
Author: https://github.com/awesomekling Commit: https://github.com/SerenityOS/serenity/commit/f93b3b78068
1 changed files with 11 additions and 6 deletions
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue