Home | History | Annotate | Download | only in performanced
      1 #ifndef ANDROID_DVR_PERFORMANCED_STRING_TRIM_H_
      2 #define ANDROID_DVR_PERFORMANCED_STRING_TRIM_H_
      3 
      4 #include <functional>
      5 #include <locale>
      6 #include <string>
      7 
      8 namespace android {
      9 namespace dvr {
     10 
     11 // Trims whitespace from the left side of |subject| and returns the result as a
     12 // new string.
     13 inline std::string LeftTrim(std::string subject) {
     14   subject.erase(subject.begin(),
     15                 std::find_if(subject.begin(), subject.end(),
     16                              std::not1(std::ptr_fun<int, int>(std::isspace))));
     17   return subject;
     18 }
     19 
     20 // Trims whitespace from the right side of |subject| and returns the result as a
     21 // new string.
     22 inline std::string RightTrim(std::string subject) {
     23   subject.erase(std::find_if(subject.rbegin(), subject.rend(),
     24                              std::not1(std::ptr_fun<int, int>(std::isspace)))
     25                     .base(),
     26                 subject.end());
     27   return subject;
     28 }
     29 
     30 // Trims whitespace from the both sides of |subject| and returns the result as a
     31 // new string.
     32 inline std::string Trim(std::string subject) {
     33   subject.erase(subject.begin(),
     34                 std::find_if(subject.begin(), subject.end(),
     35                              std::not1(std::ptr_fun<int, int>(std::isspace))));
     36   subject.erase(std::find_if(subject.rbegin(), subject.rend(),
     37                              std::not1(std::ptr_fun<int, int>(std::isspace)))
     38                     .base(),
     39                 subject.end());
     40   return subject;
     41 }
     42 
     43 }  // namespace dvr
     44 }  // namespace android
     45 
     46 #endif  // ANDROID_DVR_PERFORMANCED_STRING_TRIM_H_
     47