- if (a.empty() || !*p) return a;
- string tmp(a);
- string::size_type i = tmp.find_last_not_of(p);
- if (i == a.length() - 1) return tmp; // no c's at end of a
- if (i != string::npos)
- tmp.erase(i + 1, string::npos);
-#if !defined(USE_INCLUDED_STRING) && !defined(STD_STRING_IS_GOOD)
- // Ok This code is now suspect... (Lgb)
- /// Needed for broken string::find_last_not_of
- else if (tmp[0] != p[0]) {
- if (a.length() == 1) return tmp;
- tmp.erase(1, string::npos);
- }
-#endif
- else
- tmp.erase(); // only chars from p in the whole string
- return tmp;
+ if (a.empty() || !*p)
+ return a;
+
+ string::size_type r = a.find_last_not_of(p);
+ string::size_type l = a.find_first_not_of(p);
+
+ // Is this the minimal test? (lgb)
+ if (r == string::npos && l == string::npos)
+ return string();
+
+ return a.substr(l, r - l + 1);