Introduce Strings::Joiner
MODIFY
include/vcpkg_Strings.h
include/vcpkg_Strings.h
@@ -68,7 +68,55 @@ namespace vcpkg::Strings
std::string ascii_to_lowercase(const std::string& input);
- std::string join(const std::vector<std::string>& v, const std::string& delimiter);
+ template <class T, class Transformer>
+ static std::string join(const std::vector<T>& v, const std::string& prefix, const std::string& delimiter, const std::string& suffix, Transformer transformer)
+ {
+ if (v.empty())
+ {
+ return std::string();
+ }
+
+ std::string output;
+ size_t size = v.size();
+
+ output.append(prefix);
+ output.append(transformer(v.at(0)));
+
+ for (size_t i = 1; i < size; ++i)
+ {
+ output.append(delimiter);
+ output.append(transformer(v.at(i)));
+ }
+
+ output.append(suffix);
+ return output;
+ }
+
+ static std::string join(const std::vector<std::string>& v, const std::string& prefix, const std::string& delimiter, const std::string& suffix);
+
+ class Joiner
+ {
+ public:
+ static Joiner on(const std::string& delimiter);
+
+ Joiner& prefix(const std::string& prefix);
+ Joiner& suffix(const std::string& suffix);
+
+ std::string join(const std::vector<std::string>& v) const;
+
+ template <class T, class Transformer>
+ std::string join(const std::vector<T>& v, Transformer transformer) const
+ {
+ return Strings::join(v, this->m_prefix, this->m_delimiter, this->m_suffix, transformer);
+ }
+
+ private:
+ explicit Joiner(const std::string& delimiter);
+
+ std::string m_prefix;
+ std::string m_delimiter;
+ std::string m_suffix;
+ };
void trim(std::string* s);

MODIFY
src/SourceParagraph.cpp
src/SourceParagraph.cpp
@@ -53,8 +53,8 @@ namespace vcpkg
const std::vector<std::string> remaining_fields = Maps::extract_keys(fields);
const std::vector<std::string>& valid_fields = get_list_of_valid_fields();
- const std::string remaining_fields_as_string = Strings::join(remaining_fields, "\n ");
- const std::string valid_fields_as_string = Strings::join(valid_fields, "\n ");
+ const std::string remaining_fields_as_string = Strings::Joiner::on("\n ").join(remaining_fields);
+ const std::string valid_fields_as_string = Strings::Joiner::on("\n ").join(valid_fields);
System::println(System::color::error, "Error: There are invalid fields in the Source Paragraph of %s", this->name);
System::println("The following fields were not expected:\n\n %s\n\n", remaining_fields_as_string);

MODIFY
src/vcpkg_Strings.cpp
src/vcpkg_Strings.cpp
@@ -67,25 +67,38 @@ namespace vcpkg::Strings
return output;
}
- std::string join(const std::vector<std::string>& v, const std::string& delimiter)
+ std::string join(const std::vector<std::string>& v, const std::string& prefix, const std::string& delimiter, const std::string& suffix)
{
- if (v.empty())
+ return join(v, prefix, delimiter, suffix, [](const std::string& i) -> std::string
{
- return std::string();
+ return i;
+ });
}
- std::string output;
- size_t size = v.size();
+ Joiner Joiner::on(const std::string& delimiter)
+ {
+ return Joiner(delimiter);
+ }
+
+ Joiner& Joiner::prefix(const std::string& prefix)
+ {
+ this->m_prefix = prefix;
+ return *this;
+ }
- output.append(v.at(0));
+ Joiner& Joiner::suffix(const std::string& suffix)
+ {
+ this->m_suffix = suffix;
+ return *this;
+ }
- for (size_t i = 1; i < size; ++i)
+ std::string Joiner::join(const std::vector<std::string>& v) const
{
- output.append(delimiter);
- output.append(v.at(i));
+ return Strings::join(v, this->m_prefix, this->m_delimiter, this->m_suffix);
}
- return output;
+ Joiner::Joiner(const std::string& delimiter) : m_prefix(""), m_delimiter(delimiter), m_suffix("")
+ {
}
void trim(std::string* s)

Introduce Strings::Joiner