diff --git a/libluna/include/luna/String.h b/libluna/include/luna/String.h index 84f7d3ab..99aa7854 100644 --- a/libluna/include/luna/String.h +++ b/libluna/include/luna/String.h @@ -32,6 +32,8 @@ class String return view().split_once(delim); } + void trim(StringView delim); + static Result format(const String& fmt, ...); static Result format(StringView fmt, ...); diff --git a/libluna/src/String.cpp b/libluna/src/String.cpp index d3a1c2d5..8d5a6595 100644 --- a/libluna/src/String.cpp +++ b/libluna/src/String.cpp @@ -80,6 +80,25 @@ Result> 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");