tisonkun commented on code in PR #644:
URL: https://github.com/apache/incubator-kvrocks/pull/644#discussion_r902807971
##########
src/util.cc:
##########
@@ -359,42 +359,54 @@ std::string ToLower(std::string in) {
return in;
}
-void Trim(const std::string &in, const std::string &chars, std::string *out) {
- out->clear();
- if (in.empty()) return;
- out->assign(in);
- out->erase(0, out->find_first_not_of(chars));
- out->erase(out->find_last_not_of(chars)+1);
+std::string Trim(std::string in, const std::string &chars) {
+ if (in.empty()) return in;
+
+ in.erase(0, in.find_first_not_of(chars));
+ in.erase(in.find_last_not_of(chars) + 1);
+
+ return in;
}
-void Split(std::string in, std::string delim, std::vector<std::string> *out) {
- if (in.empty() || !out) return;
- out->clear();
+std::vector<std::string> Split(const std::string &in, const std::string
&delim) {
+ std::vector<std::string> out;
+
+ if (in.empty()) {
+ return out;
+ }
+
+ if (delim.empty()) {
+ out.resize(in.size());
+ std::transform(in.begin(), in.end(), out.begin(),
+ [](char c) -> std::string { return {c}; });
+ return out;
+ }
- std::string::size_type pos = 0;
- std::string elem, trimed_elem;
+ size_t begin = 0, end = in.find_first_of(delim);
do {
- pos = in.find_first_of(delim);
- elem = in.substr(0, pos);
- Trim(elem, delim, &trimed_elem);
- if (!trimed_elem.empty()) out->push_back(trimed_elem);
- in = in.substr(pos+1);
- } while (pos != std::string::npos);
+ std::string elem = in.substr(begin, end - begin);
+ if (!elem.empty()) out.push_back(std::move(elem));
Review Comment:
`Split("1 2", " ")` should normally return `{"1", "", "2"}`. But with this
condition, it returns `{"1", "2"}`.
I don't know if kvrocks depends on this behavior but point out it's diverged
from other libraries.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]