Introduce Strings::Joiner

This commit is contained in:
Alexander Karatarakis 2017-01-27 20:09:40 -08:00
parent 4059d4a6b9
commit 0f0698dc18
3 changed files with 78 additions and 17 deletions

View File

@ -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);

View File

@ -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);

View File

@ -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);
}
output.append(v.at(0));
Joiner& Joiner::prefix(const std::string& prefix)
{
this->m_prefix = prefix;
return *this;
}
for (size_t i = 1; i < size; ++i)
{
output.append(delimiter);
output.append(v.at(i));
}
Joiner& Joiner::suffix(const std::string& suffix)
{
this->m_suffix = suffix;
return *this;
}
return output;
std::string Joiner::join(const std::vector<std::string>& v) const
{
return Strings::join(v, this->m_prefix, this->m_delimiter, this->m_suffix);
}
Joiner::Joiner(const std::string& delimiter) : m_prefix(""), m_delimiter(delimiter), m_suffix("")
{
}
void trim(std::string* s)
@ -119,7 +132,7 @@ namespace vcpkg::Strings
std::vector<std::string> output;
size_t i = 0;
for (size_t pos = s.find(delimiter); pos != std::string::npos; pos = s.find(delimiter, pos))
for (size_t pos = s.find(delimiter); pos != std::string::npos; pos = s.find(delimiter, pos))
{
output.push_back(s.substr(i, pos - i));
i = ++pos;