refactor: Replace StringFinder/Joiner.h with find/join.h
Also move to "algorithm" dir
This commit is contained in:
1 parent
a26918efe9
commit
e1553f1b04
6 files changed
+131
-217
No files matched your search
@@ -1,8 +1,8 @@
|
||||
#pragma once
|
||||
|
||||
#include "spark/algorithm/string/find.h"
|
||||
#include "spark/algorithm/string/join.h"
|
||||
#include "spark/core/FMT_IMPL.h"
|
||||
#include "spark/core/StringFinder.h"
|
||||
#include "spark/core/StringJoiner.h"
|
||||
#include "spark/utils/bts_assert.h"
|
||||
|
||||
#include <boost/algorithm/string/split.hpp>
|
||||
@@ -119,7 +119,7 @@ inline CCApiCorrelationID CCApiCorrelationID::from_string(
|
||||
template <class... Parts>
|
||||
inline CCApiCorrelationID CCApiCorrelationID::from_parts(Parts... parts) {
|
||||
// TODO: create the parts vector/string simultaneously (more efficient)
|
||||
auto parts_str = core::StringJoiner::join(delimiter(), parts...);
|
||||
auto parts_str = bts::algorithm::join(delimiter(), parts...);
|
||||
return from_string(parts_str);
|
||||
}
|
||||
|
||||
@@ -231,7 +231,7 @@ inline void CCApiCorrelationID::advance_(std::size_t len) {
|
||||
return;
|
||||
}
|
||||
cursor_ += len;
|
||||
auto idx = core::StringFinder::find(str_, '.', len);
|
||||
auto idx = bts::algorithm::find_nth(str_, '.', len);
|
||||
str_ = str_.substr(idx);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
#include "CCApiMessageLogger.h"
|
||||
#include "spark/core/StringJoiner.h"
|
||||
#include "spark/algorithm/string/join.h"
|
||||
#include "spark/core/time_stamp.h"
|
||||
|
||||
#include <fmt/format.h>
|
||||
@@ -9,7 +9,7 @@
|
||||
|
||||
namespace bts::md {
|
||||
|
||||
using bts::core::StringJoiner;
|
||||
using bts::algorithm::join;
|
||||
using std::string;
|
||||
|
||||
CCApiMessageLogger::CCApiMessageLogger(std::string name)
|
||||
@@ -24,7 +24,7 @@ void CCApiMessageLogger::on_messages(
|
||||
auto msg_strs = msgs | ranges::views::transform([](const ccapi::Message *m) {
|
||||
return m->toStringPretty();
|
||||
});
|
||||
auto msgs_str = StringJoiner::join(msg_strs.begin(), msg_strs.end(), "\n");
|
||||
auto msgs_str = join(msg_strs.begin(), msg_strs.end(), "\n");
|
||||
LOG(md, Info, "Received CCApi messages. name: {}; corr_id: {}; msgs:\n{}",
|
||||
name_, corr_id, msgs_str);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
|
||||
namespace bts::algorithm {
|
||||
|
||||
std::size_t find_nth(std::string str, char ch, int n);
|
||||
|
||||
//////////////////////////////////////////////////
|
||||
// Definition
|
||||
//////////////////////////////////////////////////
|
||||
|
||||
inline std::size_t find_nth(std::string str, char ch, int n) {
|
||||
if (n == 0) {
|
||||
return std::string::npos;
|
||||
}
|
||||
std::size_t i;
|
||||
std::size_t end;
|
||||
int incr;
|
||||
if (n < 0) {
|
||||
n = -n;
|
||||
i = str.length() - 1;
|
||||
incr = -1;
|
||||
end = 0;
|
||||
} else {
|
||||
i = 0;
|
||||
incr = 1;
|
||||
end = str.length() - 1;
|
||||
}
|
||||
if (str.length() < static_cast<std::size_t>(n)) {
|
||||
return std::string::npos;
|
||||
}
|
||||
while (i != end && (str[i] != ch || --n > 0)) {
|
||||
i += incr;
|
||||
}
|
||||
return n == 0 ? i : std::string::npos;
|
||||
}
|
||||
|
||||
} // namespace bts::algorithm
|
||||
@@ -0,0 +1,85 @@
|
||||
#pragma once
|
||||
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
|
||||
namespace bts::algorithm {
|
||||
|
||||
template <class Iter>
|
||||
static std::ostream &join(
|
||||
Iter first, Iter last, const std::string &delimiter, std::ostream &out);
|
||||
|
||||
template <class Iter>
|
||||
static std::string join(Iter first, Iter last, const std::string &delimiter);
|
||||
|
||||
template <class Arg>
|
||||
static std::ostream &join(
|
||||
const std::string &delimiter, std::ostream &out, Arg arg);
|
||||
|
||||
template <class Arg, class... Args>
|
||||
static std::ostream &join(
|
||||
const std::string &delimiter, std::ostream &out, Arg arg, Args... args);
|
||||
|
||||
template <class Arg>
|
||||
static std::string join(const std::string &delimiter, Arg arg);
|
||||
|
||||
template <class Arg, class... Args>
|
||||
static std::string join(const std::string &delimiter, Arg arg, Args... args);
|
||||
|
||||
//////////////////////////////////////////////////
|
||||
// Definition
|
||||
//////////////////////////////////////////////////
|
||||
|
||||
template <class Iter>
|
||||
std::ostream &join(
|
||||
Iter first, Iter last, const std::string &delimiter, std::ostream &out) {
|
||||
if (first == last) {
|
||||
return out;
|
||||
}
|
||||
out << *first;
|
||||
first++;
|
||||
for (; first != last; first++) {
|
||||
out << delimiter << *first;
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
template <class Iter>
|
||||
std::string join(Iter first, Iter last, const std::string &delimiter) {
|
||||
std::stringstream out;
|
||||
join(first, last, delimiter, out);
|
||||
return out.str();
|
||||
}
|
||||
|
||||
template <class Arg>
|
||||
std::ostream &join(
|
||||
const std::string & /*delimiter*/, std::ostream &out, Arg arg) {
|
||||
// special case for single item (don't use delimiter)
|
||||
out << arg;
|
||||
return out;
|
||||
}
|
||||
|
||||
template <class Arg>
|
||||
std::string join(const std::string & /*delimiter*/, Arg arg) {
|
||||
// special case for single item (don't use delimiter)
|
||||
std::stringstream out;
|
||||
out << arg;
|
||||
return out.str();
|
||||
}
|
||||
|
||||
template <class Arg, class... Args>
|
||||
std::ostream &join(
|
||||
const std::string &delimiter, std::ostream &out, Arg arg, Args... args) {
|
||||
out << arg << delimiter;
|
||||
return join(delimiter, out, args...);
|
||||
}
|
||||
|
||||
template <class Arg, class... Args>
|
||||
std::string join(const std::string &delimiter, Arg arg, Args... args) {
|
||||
std::stringstream out;
|
||||
out << arg << delimiter;
|
||||
join<Args...>(delimiter, out, args...);
|
||||
return out.str();
|
||||
}
|
||||
|
||||
} // namespace bts::algorithm
|
||||
@@ -1,32 +0,0 @@
|
||||
#pragma once
|
||||
#include <cstdint>
|
||||
#include <string>
|
||||
|
||||
namespace bts::core {
|
||||
|
||||
class StringFinder {
|
||||
public:
|
||||
static std::size_t find(std::string str, char ch, int occurrence = 1);
|
||||
};
|
||||
|
||||
//////////////////////////////////////////////////
|
||||
// Definition
|
||||
//////////////////////////////////////////////////
|
||||
|
||||
inline std::size_t StringFinder::find(
|
||||
std::string str, char ch, int occurrence) {
|
||||
int occur = 0;
|
||||
|
||||
// Loop to find the Nth
|
||||
// occurrence of the character
|
||||
for (std::size_t i = 0; i < str.length(); i++) {
|
||||
if (str[i] == ch) {
|
||||
occur += 1;
|
||||
}
|
||||
if (occur == occurrence)
|
||||
return i;
|
||||
}
|
||||
return std::string::npos;
|
||||
}
|
||||
|
||||
} // namespace bts::core
|
||||
@@ -1,178 +0,0 @@
|
||||
#pragma once
|
||||
#include <fmt/format.h>
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
|
||||
namespace bts::core {
|
||||
|
||||
class StringJoiner {
|
||||
public:
|
||||
template <class Arg>
|
||||
static std::ostream &join(
|
||||
const std::string &delimiter, std::ostream &out, Arg arg);
|
||||
|
||||
template <class Arg>
|
||||
static std::string join(const std::string &delimiter, Arg arg);
|
||||
|
||||
template <class Arg, class... Args>
|
||||
static std::ostream &join(
|
||||
const std::string &delimiter, std::ostream &out, Arg arg, Args... args);
|
||||
|
||||
template <class Arg, class... Args>
|
||||
static std::string join(const std::string &delimiter, Arg arg, Args... args);
|
||||
|
||||
template <class Iter>
|
||||
static std::ostream &join(
|
||||
Iter first, Iter last, const std::string &delimiter, std::ostream &out);
|
||||
|
||||
template <class Iter>
|
||||
static std::string join(Iter first, Iter last, const std::string &delimiter);
|
||||
|
||||
/**
|
||||
* Converts the args to strings (using the standard output operator), then
|
||||
* joins the non-empty ones with the specified delimiter.
|
||||
*/
|
||||
template <class Arg>
|
||||
static std::string join_non_empties(const std::string &delimiter, Arg arg);
|
||||
|
||||
/**
|
||||
* Converts the args to strings (using the standard output operator), then
|
||||
* joins the non-empty ones with the specified delimiter.
|
||||
*/
|
||||
template <class Arg, class... Args>
|
||||
static std::string join_non_empties(
|
||||
const std::string &delimiter, Arg arg, Args... args);
|
||||
|
||||
/**
|
||||
* Converts the args to strings (using the standard output operator), then
|
||||
* joins the non-empty ones with the specified delimiter.
|
||||
*/
|
||||
template <class Arg>
|
||||
static std::ostream &join_non_empties(
|
||||
const std::string &delimiter, std::ostream &out, Arg arg);
|
||||
|
||||
/**
|
||||
* Converts the args to strings (using the standard output operator), then
|
||||
* joins the non-empty ones with the specified delimiter.
|
||||
*/
|
||||
template <class Arg, class... Args>
|
||||
static std::ostream &join_non_empties(
|
||||
const std::string &delimiter, std::ostream &out, Arg arg, Args... args);
|
||||
|
||||
private:
|
||||
template <class Arg, class... Args>
|
||||
static std::ostream &join_non_empties_(
|
||||
const std::string &delimiter, std::ostream &out, Arg arg, Args... args);
|
||||
};
|
||||
|
||||
//////////////////////////////////////////////////
|
||||
// Definition
|
||||
//////////////////////////////////////////////////
|
||||
|
||||
template <class Arg>
|
||||
std::ostream &StringJoiner::join(
|
||||
const std::string & /*delimiter*/, std::ostream &out, Arg arg) {
|
||||
// special case for single item (don't use delimiter)
|
||||
out << arg;
|
||||
return out;
|
||||
}
|
||||
|
||||
template <class Arg>
|
||||
std::string StringJoiner::join(const std::string & /*delimiter*/, Arg arg) {
|
||||
// special case for single item (don't use delimiter)
|
||||
std::stringstream out;
|
||||
out << arg;
|
||||
return out.str();
|
||||
}
|
||||
|
||||
template <class Arg, class... Args>
|
||||
std::ostream &StringJoiner::join(
|
||||
const std::string &delimiter, std::ostream &out, Arg arg, Args... args) {
|
||||
out << arg << delimiter;
|
||||
return join(delimiter, out, args...);
|
||||
}
|
||||
|
||||
template <class Arg, class... Args>
|
||||
std::string StringJoiner::join(
|
||||
const std::string &delimiter, Arg arg, Args... args) {
|
||||
std::stringstream out;
|
||||
out << arg << delimiter;
|
||||
join<Args...>(delimiter, out, args...);
|
||||
return out.str();
|
||||
}
|
||||
|
||||
template <class Iter>
|
||||
std::ostream &StringJoiner::join(
|
||||
Iter it, Iter last, const std::string &delimiter, std::ostream &out) {
|
||||
if (it == last) {
|
||||
return out;
|
||||
}
|
||||
out << *it;
|
||||
it++;
|
||||
for (; it != last; it++) {
|
||||
out << delimiter << *it;
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
template <class Iter>
|
||||
std::string StringJoiner::join(
|
||||
Iter it, Iter last, const std::string &delimiter) {
|
||||
std::stringstream out;
|
||||
join(it, last, delimiter, out);
|
||||
return out.str();
|
||||
}
|
||||
|
||||
template <class Arg>
|
||||
std::string StringJoiner::join_non_empties(
|
||||
const std::string & /*delimiter*/, Arg arg) {
|
||||
std::stringstream ss;
|
||||
ss << arg;
|
||||
return ss.str();
|
||||
}
|
||||
|
||||
template <class Arg, class... Args>
|
||||
std::string StringJoiner::join_non_empties(
|
||||
const std::string &delimiter, Arg arg, Args... args) {
|
||||
std::stringstream ss;
|
||||
std::ostream &out = ss;
|
||||
join_non_empties(delimiter, out, arg, args...);
|
||||
return ss.str();
|
||||
}
|
||||
|
||||
template <class Arg>
|
||||
std::ostream &StringJoiner::join_non_empties(
|
||||
const std::string & /*delimiter*/, std::ostream &out, Arg arg) {
|
||||
out << arg;
|
||||
return out;
|
||||
}
|
||||
|
||||
template <class Arg, class... Args>
|
||||
std::ostream &StringJoiner::join_non_empties(
|
||||
const std::string &delimiter, std::ostream &out, Arg arg, Args... args) {
|
||||
std::stringstream ss;
|
||||
ss << arg;
|
||||
auto arg_str = ss.str();
|
||||
if (arg_str.empty()) {
|
||||
return join_non_empties(delimiter, out, args...);
|
||||
}
|
||||
out << arg;
|
||||
return join_non_empties_(delimiter, out, args...);
|
||||
}
|
||||
|
||||
template <class Arg, class... Args>
|
||||
std::ostream &StringJoiner::join_non_empties_(
|
||||
const std::string &delimiter, std::ostream &out, Arg arg, Args... args) {
|
||||
std::stringstream ss;
|
||||
ss << arg;
|
||||
auto arg_str = ss.str();
|
||||
if (arg_str.empty()) {
|
||||
return join_non_empties_(delimiter, out, args...);
|
||||
}
|
||||
out << delimiter << arg_str;
|
||||
join(delimiter, out, args...);
|
||||
return out;
|
||||
}
|
||||
|
||||
} // namespace bts::core
|
||||
Reference in new issue
Block a user