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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to