libluna: Let String::join take a vector of StringViews instead of Strings
This commit is contained in:
parent
1db60b5a82
commit
30d1dad149
@ -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);
|
||||||
|
|
||||||
|
@ -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());
|
||||||
|
Loading…
Reference in New Issue
Block a user