summaryrefslogtreecommitdiff
path: root/decoder/stringlib.h
diff options
context:
space:
mode:
authorgraehl <graehl@ec762483-ff6d-05da-a07a-a48fb63a330f>2010-08-10 02:29:56 +0000
committergraehl <graehl@ec762483-ff6d-05da-a07a-a48fb63a330f>2010-08-10 02:29:56 +0000
commitac58a78e4c2c129c838927b1479c31788fd9bd5f (patch)
treec74908f34d681995ea53faa138219a95fe3674f7 /decoder/stringlib.h
parentae6e9133a743e824fb2671505d5dcaa6469f887e (diff)
parse trule(string) using lexer - needs testing, affects earley_composer
git-svn-id: https://ws10smt.googlecode.com/svn/trunk@497 ec762483-ff6d-05da-a07a-a48fb63a330f
Diffstat (limited to 'decoder/stringlib.h')
-rw-r--r--decoder/stringlib.h28
1 files changed, 28 insertions, 0 deletions
diff --git a/decoder/stringlib.h b/decoder/stringlib.h
index 53e6fe50..84e95d44 100644
--- a/decoder/stringlib.h
+++ b/decoder/stringlib.h
@@ -20,6 +20,34 @@
#include <sstream>
#include <algorithm>
+inline std::size_t skip_ws(std::string const& s,std::size_t starting=0,char const* ws=" \t\n\r") {
+ 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") {
+ 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
+}
+
+// is_single_line(strip_ws(line))
+inline bool is_single_line_stripped(std::string const& line) {
+ std::size_t b=skip_ws(line),e=trailing_ws(line);
+ std::size_t n=line.find('\n',b);
+ return n==std::string::npos || n>=e;
+}
+
struct toupperc {
inline char operator()(char c) const {
return std::toupper(c);