From efb6301f794cc272173fa09ad83293ad3aa79ad4 Mon Sep 17 00:00:00 2001 From: John Denker Date: Tue, 31 Jul 2012 17:47:39 -0700 Subject: smarter utils --- tools/utils.c | 35 +++++++++++++++++++++++------------ 1 file changed, 23 insertions(+), 12 deletions(-) (limited to 'tools/utils.c') diff --git a/tools/utils.c b/tools/utils.c index 602a144..a7c2878 100644 --- a/tools/utils.c +++ b/tools/utils.c @@ -6,26 +6,28 @@ ///// due to lack of interger abs() ///// and ambiguous (and inefficient) promotion #include /* for isalnum() */ +using namespace std; + // strip off the directory part of a path, leaving just // the basic filename -std::string basename(const std::string path){ +string basename(const string path){ size_t where = path.rfind("/"); - if (where != std::string::npos) return path.substr(1+where); + if (where != string::npos) return path.substr(1+where); return path; } //////////////// // little utility to help with argument parsing: // -int prefix(const std::string shorter, const std::string longer){ +int prefix(const string shorter, const string longer){ return shorter == longer.substr(0, shorter.length()); } /////////////// // print a time as (-)hh:mm:ss // -std::string time_out(const int _ttt){ +string time_out(const int _ttt){ using namespace std; int ttt(abs(_ttt)); int sec(ttt % 60); @@ -46,25 +48,34 @@ using namespace std; return foo.str(); } -std::string toLower(const std::string a){ - std::string rslt = a; - std::string::iterator rr; +string toLower(const string a){ + string rslt = a; + string::iterator rr; for (rr = rslt.begin(); rr != rslt.end(); rr++){ *rr = tolower(*rr); } return rslt; } -//////////////// -std::string ltrim(const std::string foo){ - size_t where = foo.find_first_not_of(" \t\r\n"); +string ltrim(const string foo, const string strip){ + size_t where = foo.find_first_not_of(strip); if (where == foo.npos) return foo; return foo.substr(where); } -static const std::string Pure_Enough("+-_.,@%~"); +string rtrim(const string foo, const string strip){ + size_t where = foo.find_last_not_of(strip); + if (where == foo.npos) return ""; + return foo.substr(0, where+1); +} + +string trim(const string foo, const string bar){ + return ltrim(rtrim(foo, bar), bar); +} + +static const string Pure_Enough("+-_.,@%~"); -std::string purify(const std::string arg){ +string purify(const string arg){ using namespace std; string rslt(arg); for (string::iterator ptr = rslt.begin(); -- cgit v1.2.3