Lumiera  0.pre.03
»edit your freedom«
util.cpp
Go to the documentation of this file.
1 /*
2  util.cpp - helper functions implementation
3 
4  Copyright (C) Lumiera.org
5  2008, Hermann Vosseler <Ichthyostega@web.de>
6 
7  This program is free software; you can redistribute it and/or
8  modify it under the terms of the GNU General Public License as
9  published by the Free Software Foundation; either version 2 of
10  the License, or (at your option) any later version.
11 
12  This program is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU General Public License for more details.
16 
17  You should have received a copy of the GNU General Public License
18  along with this program; if not, write to the Free Software
19  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 
21 * *****************************************************/
22 
23 
32 #include "error.hpp"
33 #include "lib/util.hpp"
34 #include "lib/format-string.hpp"
35 
36 #include <boost/algorithm/string.hpp>
37 #include <functional>
38 #include <boost/bind.hpp> // we need operator! for bind-expressions
39 
40 using boost::algorithm::trim_right_copy_if;
41 using boost::algorithm::is_any_of;
42 using boost::algorithm::is_alnum;
43 using boost::algorithm::is_space;
44 
45 #include <regex>
46 
47 using std::regex;
48 using std::regex_match;
49 
50 using std::function;
51 using util::_Fmt;
52 
53 namespace util {
54 
55 
56  using ChPredicate = function<bool(string::value_type)>;
57  ChPredicate operator! (ChPredicate p) { return not bind(p,_1); }
58 
59  // character classes used for sanitising a string
60  ChPredicate isValid (is_alnum() or is_any_of("-_.+$()@"));
61  ChPredicate isPunct (is_space() or is_any_of(",;:#*~´`?\\=/&%![]{}<>"));
62 
63 
64  string
65  sanitise (string const& org)
66  {
67  string res (trim_right_copy_if(org, !isValid ));
68  string::iterator j = res.begin();
69  string::const_iterator i = org.begin();
70  string::const_iterator e = i + (res.length());
71  while ( i != e )
72  {
73  while ( i != e && !isValid (*i) ) ++i;
74  while ( i != e && isValid (*i) ) *(j++) = *(i++);
75  if ( i != e && isPunct (*i) )
76  {
77  *j++ = '_';
78  do ++i;
79  while ( i != e && isPunct (*i));
80  }
81  }
82  res.erase(j,res.end());
83  return res;
84  }
85 
86 
92  string
93  trim (string const& org)
94  {
95  return boost::algorithm::trim_copy (org);
96  }
97 
98 
99 
100 
101  namespace {
102  regex trueTokens { "\\s*(true|yes|on|1|\\+)\\s*", regex::icase | regex::optimize };
103  regex falseTokens{ "\\s*(false|no|off|0|\\-)\\s*", regex::icase | regex::optimize };
104  }
105 
106  bool
107  boolVal (string const& textForm)
108  {
109  if (regex_match (textForm, trueTokens)) return true;
110  if (regex_match (textForm, falseTokens)) return false;
111  throw lumiera::error::Invalid(_Fmt{"String '%s' can not be interpreted as bool value"} % textForm);
112  }
113 
114 
115  bool
116  isYes (string const& textForm) noexcept
117  {
118  return regex_match (textForm, trueTokens);
119  }
120 
121 
122  bool
123  isNo (string const& textForm) noexcept
124  {
125  return isnil (textForm)
126  or regex_match (textForm, falseTokens);
127  }
128 
129 
130 
131 } // namespace util
132 
ChPredicate isValid(is_alnum() or is_any_of("-_.+$()@"))
characters to be retained
bool isYes(string const &textForm) noexcept
check the given text if it can be interpreted as affirmative answer (bool true).
Definition: util.cpp:116
Front-end for printf-style string template interpolation.
std::string sanitise(std::string const &)
produce an identifier based on the given string.
Definition: util.cpp:65
bool boolVal(string const &textForm)
interpret text representation of a boolean value.
Definition: util.cpp:107
A front-end for using printf-style formatting.
ChPredicate isPunct(is_space() or is_any_of(",;:#*~´`?\/&%![]{}<>"))
punctuation to be replaced by &#39;_&#39;
Derived specific exceptions within Lumiera&#39;s exception hierarchy.
Definition: error.hpp:199
bool isNo(string const &textForm) noexcept
check if the given text is empty or can be interpreted as rejection (bool false)- ...
Definition: util.cpp:123
Tiny helper functions and shortcuts to be used everywhere Consider this header to be effectively incl...
string trim(string const &org)
remove leading and trailing whitespace
Definition: util.cpp:93
Lumiera error handling (C++ interface).