feat add StrJoin

This commit is contained in:
tqcq
2024-03-03 00:46:50 +08:00
parent 4b68474fe5
commit 3dccac62b7
3 changed files with 31 additions and 2 deletions

23
src/strings/utils.cc Normal file
View File

@@ -0,0 +1,23 @@
#include "sled/strings/utils.h"
#include <sstream>
namespace sled {
std::string
StrJoin(const std::vector<std::string> &strings,
const std::string &delim,
bool skip_empty)
{
if (strings.empty()) { return ""; }
std::stringstream ss;
size_t i = 0;
while (skip_empty && i < strings.size() && strings[i].empty()) { ++i; }
if (i < strings.size()) { ss << strings[i++]; }
for (; i < strings.size(); ++i) {
if (skip_empty && strings[i].empty()) { continue; }
ss << delim << strings[i];
}
return ss.str();
}
}// namespace sled