From 734d2f82f79c3bfa4ee8d57d0ab6c1eafa6b91cc Mon Sep 17 00:00:00 2001 From: graehl Date: Thu, 12 Aug 2010 01:19:27 +0000 Subject: fidget git-svn-id: https://ws10smt.googlecode.com/svn/trunk@532 ec762483-ff6d-05da-a07a-a48fb63a330f --- utils/stringlib.h | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) (limited to 'utils/stringlib.h') diff --git a/utils/stringlib.h b/utils/stringlib.h index 84e95d44..4f79eb31 100644 --- a/utils/stringlib.h +++ b/utils/stringlib.h @@ -20,22 +20,22 @@ #include #include -inline std::size_t skip_ws(std::string const& s,std::size_t starting=0,char const* ws=" \t\n\r") { +namespace { +const char c_isspace[]=" \t\n\r\f\v"; // somewhat ridiculous, including characters nobody uses. +const char common_isspace[]=" \t\n\r"; // even \n\r is borderline, but maybe you pass multiline DOS format text. +} + +inline std::size_t skip_ws(std::string const& s,std::size_t starting=0,char const* ws=common_isspace) { return s.find_first_not_of(ws,starting); } // returns position of end of all non-ws chars before ending, i.e. string(s.begin()+skip_ws(s),s.begin()+trailing_ws(s)) strips both ends -inline std::size_t trailing_ws(std::string const& s,std::size_t ending=std::string::npos,char const* ws=" \t\n\r") { +inline std::size_t trailing_ws(std::string const& s,std::size_t ending=std::string::npos,char const* ws=common_isspace) { std::size_t n=s.find_last_not_of(ws,ending); if (n==std::string::npos) return n; else return n+1; } -//TEST: if string is all whitespace, make sure that string(a+npos,a+npos) can't segfault (i.e. won't access any memory because begin==end) -inline std::string strip_ws(std::string const& s) { - return std::string(s.begin()+skip_ws(s),s.begin()+trailing_ws(s)); -} - inline bool is_single_line(std::string const& line) { return std::count(line.begin(),line.end(),'\n')==0; // but we want to allow terminal newlines/blanks -- cgit v1.2.3