libluna: Let String::join take a vector of StringViews instead of Strings

This commit is contained in:
apio 2023-05-20 12:46:33 +02:00
parent 1db60b5a82
commit 30d1dad149
Signed by: apio
GPG Key ID: B8A7D06E42258954
2 changed files with 20 additions and 0 deletions

View File

@ -55,6 +55,9 @@ class String
/* Creates a single String consisting of a list of strings separated by a delimiter. */ /* Creates a single String consisting of a list of strings separated by a delimiter. */
static Result<String> join(const Vector<String>& vec, StringView delim); static Result<String> join(const Vector<String>& vec, StringView delim);
/* Creates a single String consisting of a list of strings separated by a delimiter. */
static Result<String> join(const Vector<StringView>& vec, StringView delim);
/* Removes all trailing characters contained in delim. */ /* Removes all trailing characters contained in delim. */
void trim(StringView delim); void trim(StringView delim);

View File

@ -181,6 +181,23 @@ Result<String> String::join(const Vector<String>& vec, StringView delim)
return sb.string(); return sb.string();
} }
Result<String> String::join(const Vector<StringView>& vec, StringView delim)
{
if (vec.size() == 0) return String {};
if (vec.size() == 1) return from_string_view(vec[0]);
StringBuilder sb;
TRY(sb.add(vec[0]));
for (usize i = 1; i < vec.size(); i++)
{
TRY(sb.add(delim));
TRY(sb.add(vec[i]));
}
return sb.string();
}
int String::compare(const String* a, const String* b) int String::compare(const String* a, const String* b)
{ {
return strcmp(a->chars(), b->chars()); return strcmp(a->chars(), b->chars());