libluna: Add String::trim

This commit is contained in:
apio 2023-04-22 15:19:07 +02:00
parent 257c2ffd0a
commit dcc6bbf055
Signed by: apio
GPG Key ID: B8A7D06E42258954
2 changed files with 21 additions and 0 deletions

View File

@ -32,6 +32,8 @@ class String
return view().split_once(delim);
}
void trim(StringView delim);
static Result<String> format(const String& fmt, ...);
static Result<String> format(StringView fmt, ...);

View File

@ -80,6 +80,25 @@ Result<Vector<String>> String::split(StringView delim) const
return view().split(delim);
}
void String::trim(StringView delim)
{
isize i = (isize)m_length;
while (i--)
{
char c = chars()[i];
if (!strchr(delim.chars(), c)) break;
}
i++;
if (m_inline) m_inline_storage[i] = '\0';
else
m_string[i] = '\0';
m_length = (usize)i;
}
const char& String::operator[](usize index) const
{
expect(index < m_length, "index out of range");