diff --git a/libluna/include/luna/String.h b/libluna/include/luna/String.h index 0bdf7395..c8e7000b 100644 --- a/libluna/include/luna/String.h +++ b/libluna/include/luna/String.h @@ -32,6 +32,8 @@ class String return view().split_once(delim); } + static Result join(const Vector& vec, StringView delim); + void trim(StringView delim); static Result format(const String& fmt, ...); diff --git a/libluna/src/String.cpp b/libluna/src/String.cpp index 1a17bbbe..e8b5c6c2 100644 --- a/libluna/src/String.cpp +++ b/libluna/src/String.cpp @@ -2,6 +2,7 @@ #include #include #include +#include #include String::String() @@ -163,3 +164,20 @@ Result String::from_cstring(const char* str) if (!dup) return err(ENOMEM); return String { dup }; } + +Result String::join(const Vector& vec, StringView delim) +{ + if (vec.size() == 0) return String {}; + if (vec.size() == 1) return vec[0].clone(); + + StringBuilder sb; + TRY(sb.add(vec[0].view())); + + for (usize i = 1; i < vec.size(); i++) + { + TRY(sb.add(delim)); + TRY(sb.add(vec[i].view())); + } + + return sb.string(); +}