| 47 | } |
| 48 | |
| 49 | std::string Trim(const std::string& s) { |
| 50 | std::string result; |
| 51 | |
| 52 | if (s.size() == 0) { |
| 53 | return result; |
| 54 | } |
| 55 | |
| 56 | size_t start_index = 0; |
| 57 | size_t end_index = s.size() - 1; |
| 58 | |
| 59 | // Skip initial whitespace. |
| 60 | while (start_index < s.size()) { |
| 61 | if (!isspace(s[start_index])) { |
| 62 | break; |
| 63 | } |
| 64 | start_index++; |
| 65 | } |
| 66 | |
| 67 | // Skip terminating whitespace. |
| 68 | while (end_index >= start_index) { |
| 69 | if (!isspace(s[end_index])) { |
| 70 | break; |
| 71 | } |
| 72 | end_index--; |
| 73 | } |
| 74 | |
| 75 | // All spaces, no beef. |
| 76 | if (end_index < start_index) { |
| 77 | return ""; |
| 78 | } |
| 79 | // Start_index is the first non-space, end_index is the last one. |
| 80 | return s.substr(start_index, end_index - start_index + 1); |
| 81 | } |
| 82 | |
| 83 | // These cases are probably the norm, so we mark them extern in the header to |
| 84 | // aid compile time and binary size. |