You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@kvrocks.apache.org by GitBox <gi...@apache.org> on 2022/06/22 01:49:11 UTC

[GitHub] [incubator-kvrocks] git-hulk commented on a diff in pull request #644: Refactor Trim/Split/Split2KV functions in util.h

git-hulk commented on code in PR #644:
URL: https://github.com/apache/incubator-kvrocks/pull/644#discussion_r903205197


##########
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:
   Yes, this `split` function has a bit different from most languages
   since we want to ignore the empty value.



-- 
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: issues-unsubscribe@kvrocks.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org