feat add StrJoin
This commit is contained in:
parent
4b68474fe5
commit
3dccac62b7
@ -28,6 +28,7 @@ target_sources(
|
||||
src/network/socket_address.cc
|
||||
src/network/socket_server.cc
|
||||
src/strings/base64.cc
|
||||
src/strings/utils.cc
|
||||
src/synchronization/event.cc
|
||||
src/synchronization/mutex.cc
|
||||
src/synchronization/sequence_checker_internal.cc
|
||||
|
@ -1,10 +1,15 @@
|
||||
#ifndef SLED_STRINGS_UTILS_H
|
||||
#define SLED_STRINGS_UTILS_H
|
||||
#include <string>
|
||||
|
||||
namespace sled {
|
||||
|
||||
std::string ToLower(const std::string &str);
|
||||
std::string ToUpper(const std::string &str);
|
||||
std::string ToHex(const std::string &str);
|
||||
std::string StrJoin(const std::vector<std::string> &strings,
|
||||
const std::string &delim,
|
||||
bool skip_empty = false);
|
||||
|
||||
} // namespace sled
|
||||
#endif // SLED_STRINGS_UTILS_H
|
||||
}// namespace sled
|
||||
#endif// SLED_STRINGS_UTILS_H
|
||||
|
23
src/strings/utils.cc
Normal file
23
src/strings/utils.cc
Normal 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
|
Loading…
Reference in New Issue
Block a user