From 9b93eabcc5b8ebe5bcda37414e2740345d55b91c Mon Sep 17 00:00:00 2001 From: Maarten ter Huurne Date: Sat, 19 Jul 2014 02:32:30 +0200 Subject: [PATCH] Clean up trim() utility function --- src/utilities.cpp | 11 +++-------- src/utilities.h | 2 ++ 2 files changed, 5 insertions(+), 8 deletions(-) diff --git a/src/utilities.cpp b/src/utilities.cpp index 22c76d2..23ca7cf 100644 --- a/src/utilities.cpp +++ b/src/utilities.cpp @@ -40,15 +40,10 @@ bool case_less::operator()(const string &left, const string &right) const { return strcasecmp(left.c_str(), right.c_str()) < 0; } -// General tool to strip spaces from both ends: string trim(const string& s) { - if(s.length() == 0) - return s; - int b = s.find_first_not_of(" \t\r"); - int e = s.find_last_not_of(" \t\r"); - if(b == -1) // No non-spaces - return ""; - return string(s, b, e - b + 1); + auto b = s.find_first_not_of(" \t\r"); + auto e = s.find_last_not_of(" \t\r"); + return b == string::npos ? "" : string(s, b, e + 1 - b); } bool fileExists(const string &file) { diff --git a/src/utilities.h b/src/utilities.h index 7a9d038..11ea6eb 100644 --- a/src/utilities.h +++ b/src/utilities.h @@ -35,7 +35,9 @@ public: bool operator()(const std::string &left, const std::string &right) const; }; +/** Returns the string with whitespace stripped from both ends. */ std::string trim(const std::string& s); + std::string strreplace(std::string orig, const std::string &search, const std::string &replace); std::string cmdclean(std::string cmdline);