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)
5  2008, Hermann Vosseler <Ichthyostega@web.de>
6 
7   **Lumiera** is free software; you can redistribute it and/or modify it
8   under the terms of the GNU General Public License as published by the
9   Free Software Foundation; either version 2 of the License, or (at your
10   option) any later version. See the file COPYING for further details.
11 
12 * *****************************************************************/
13 
14 
23 #include "error.hpp"
24 #include "lib/util.hpp"
25 #include "lib/format-string.hpp"
26 
27 #include <boost/algorithm/string.hpp>
28 #include <functional>
29 #include <boost/bind.hpp> // we need operator! for bind-expressions
30 
31 using boost::algorithm::trim_right_copy_if;
32 using boost::algorithm::is_any_of;
33 using boost::algorithm::is_alnum;
34 using boost::algorithm::is_space;
35 
36 #include <regex>
37 
38 using std::regex;
39 using std::regex_match;
40 
41 using std::function;
42 using util::_Fmt;
43 
44 namespace util {
45 
46 
47  using ChPredicate = function<bool(string::value_type)>;
48  ChPredicate operator! (ChPredicate p) { return not bind(p,_1); }
49 
50  // character classes used for sanitising a string
51  ChPredicate isValid (is_alnum() or is_any_of("-_.+$()@"));
52  ChPredicate isPunct (is_space() or is_any_of(",;:#*~´`?\\=/&%![]{}<>"));
53 
54 
55  string
56  sanitise (string const& org)
57  {
58  string res (trim_right_copy_if(org, !isValid ));
59  string::iterator j = res.begin();
60  string::const_iterator i = org.begin();
61  string::const_iterator e = i + (res.length());
62  while ( i != e )
63  {
64  while ( i != e && !isValid (*i) ) ++i;
65  while ( i != e && isValid (*i) ) *(j++) = *(i++);
66  if ( i != e && isPunct (*i) )
67  {
68  *j++ = '_';
69  do ++i;
70  while ( i != e && isPunct (*i));
71  }
72  }
73  res.erase(j,res.end());
74  return res;
75  }
76 
77 
83  string
84  trim (string const& org)
85  {
86  return boost::algorithm::trim_copy (org);
87  }
88 
89 
90 
91 
92  namespace {
93  regex trueTokens { "\\s*(true|yes|on|1|\\+)\\s*", regex::icase | regex::optimize };
94  regex falseTokens{ "\\s*(false|no|off|0|\\-)\\s*", regex::icase | regex::optimize };
95  }
96 
97  bool
98  boolVal (string const& textForm)
99  {
100  if (regex_match (textForm, trueTokens)) return true;
101  if (regex_match (textForm, falseTokens)) return false;
102  throw lumiera::error::Invalid(_Fmt{"String '%s' can not be interpreted as bool value"} % textForm);
103  }
104 
105 
106  bool
107  isYes (string const& textForm) noexcept
108  {
109  return regex_match (textForm, trueTokens);
110  }
111 
112 
113  bool
114  isNo (string const& textForm) noexcept
115  {
116  return isnil (textForm)
117  or regex_match (textForm, falseTokens);
118  }
119 
120 
121 
122 } // namespace util
123 
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:107
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:56
bool boolVal(string const &textForm)
interpret text representation of a boolean value.
Definition: util.cpp:98
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:190
bool isNo(string const &textForm) noexcept
check if the given text is empty or can be interpreted as rejection (bool false)- ...
Definition: util.cpp:114
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:84
Lumiera error handling (C++ interface).