You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@pegasus.apache.org by GitBox <gi...@apache.org> on 2022/11/14 07:30:34 UTC

[GitHub] [incubator-pegasus] empiredan opened a new issue, #1239: Refactor: improve split_args for strings

empiredan opened a new issue, #1239:
URL: https://github.com/apache/incubator-pegasus/issues/1239

   While analyzing the code of `split_args` and `trim_string`, some problems could be found.
   
   - Both `std::string v(args)` and `v.substr(...)` will copy string, which will lead to overhead for performance.
   - In `trim_string`, all trailing spaces will be updated with `\0`, which is unnecessary.
   
   Based on the above problems, I think we can improve `split_args`.
   
   The code of current `split_args` and `trim_string` are listed as below:
   
   ```c++
   void split_args(const char *args,
                   /*out*/ std::vector<std::string> &sargs,
                   char splitter,
                   bool keep_place_holder)
   {
       sargs.clear();
       std::string v(args);
       uint64_t last_pos = 0;
       while (true) {
           auto pos = v.find(splitter, last_pos);
           if (pos != std::string::npos) {
               std::string s = trim_string((char *)v.substr(last_pos, pos - last_pos).c_str());
               if (!s.empty()) {
                   sargs.push_back(s);
               } else if (keep_place_holder) {
                   sargs.emplace_back("");
               }
               last_pos = pos + 1;
           } else {
               std::string s = trim_string((char *)v.substr(last_pos).c_str());
               if (!s.empty()) {
                   sargs.push_back(s);
               } else if (keep_place_holder) {
                   sargs.emplace_back("");
               }
               break;
           }
       }
   }
   ```
   
   ```c++
   char *trim_string(char *s)
   {
       while (*s != '\0' && (*s == ' ' || *s == '\t')) {
           s++;
       }
       char *r = s;
       s += strlen(s);
       while (s >= r && (*s == '\0' || *s == ' ' || *s == '\t' || *s == '\r' || *s == '\n')) {
           *s = '\0';
           s--;
       }
       return r;
   }
   ```


-- 
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: dev-unsubscribe@pegasus.apache.org.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@pegasus.apache.org
For additional commands, e-mail: dev-help@pegasus.apache.org


[GitHub] [incubator-pegasus] empiredan closed issue #1239: Refactor: improve split_args for strings

Posted by GitBox <gi...@apache.org>.
empiredan closed issue #1239: Refactor: improve split_args for strings
URL: https://github.com/apache/incubator-pegasus/issues/1239


-- 
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: dev-unsubscribe@pegasus.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@pegasus.apache.org
For additional commands, e-mail: dev-help@pegasus.apache.org