Lumiera 0.pre.04~rc.1
»edit your freedom«
Loading...
Searching...
No Matches
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
30using boost::algorithm::trim_right_copy_if;
31using boost::algorithm::is_any_of;
32using boost::algorithm::is_alnum;
33using boost::algorithm::is_space;
34
35#include <regex>
36
37using std::regex;
38using std::regex_match;
39
40using std::string;
41using std::function;
42using util::_Fmt;
43
44namespace util {
45
46
47 using Cha = string::value_type;
48 using ChPredicate = function<bool(Cha)>;
49 ChPredicate operator! (ChPredicate p) { return [p](Cha c){ return not p(c); }; }
50
51 // character classes used for sanitising a string
52 ChPredicate isValid (is_alnum() or is_any_of("-_.+$()@"));
53 ChPredicate isPunct (is_space() or is_any_of(",;:#*~´`?\\=/&%![]{}<>"));
54
55
56 string
57 sanitise (string const& org)
58 {
59 string res (trim_right_copy_if(org, !isValid ));
60 string::iterator j = res.begin();
61 string::const_iterator i = org.begin();
62 string::const_iterator e = i + (res.length());
63 while ( i != e )
64 {
65 while ( i != e and !isValid (*i) ) ++i;
66 while ( i != e and isValid (*i) ) *(j++) = *(i++);
67 if ( i != e and isPunct (*i) )
68 {
69 *j++ = '_';
70 do ++i;
71 while ( i != e and isPunct (*i));
72 }
73 }
74 res.erase(j,res.end());
75 return res;
76 }
77
78
84 string
85 trim (string const& org)
86 {
87 return boost::algorithm::trim_copy (org);
88 }
89
90
91
92
93 namespace {
94 regex trueTokens { "\\s*(true|yes|on|1|\\+)\\s*", regex::icase | regex::optimize };
95 regex falseTokens{ "\\s*(false|no|off|0|\\-)\\s*", regex::icase | regex::optimize };
96 }
97
98 bool
99 boolVal (string const& textForm)
100 {
101 if (regex_match (textForm, trueTokens)) return true;
102 if (regex_match (textForm, falseTokens)) return false;
103 throw lumiera::error::Invalid(_Fmt{"String '%s' can not be interpreted as bool value"} % textForm);
104 }
105
106
107 bool
108 isYes (string const& textForm) noexcept
109 {
110 return regex_match (textForm, trueTokens);
111 }
112
113
114 bool
115 isNo (string const& textForm) noexcept
116 {
117 return isnil (textForm)
118 or regex_match (textForm, falseTokens);
119 }
120
121
122
123} // namespace util
124
A front-end for using printf-style formatting.
Lumiera error handling (C++ interface).
Front-end for printf-style string template interpolation.
LumieraError< LERR_(INVALID)> Invalid
Definition error.hpp:211
bool isNo(string const &textForm) noexcept
check if the given text is empty or can be interpreted as rejection (bool false)-
Definition util.cpp:115
std::string sanitise(std::string const &)
produce an identifier based on the given string.
Definition util.cpp:57
ChPredicate isValid(is_alnum() or is_any_of("-_.+$()@"))
characters to be retained
string::value_type Cha
Definition util.cpp:47
ChPredicate operator!(ChPredicate p)
Definition util.cpp:49
ChPredicate isPunct(is_space() or is_any_of(",;:#*~´`?\\=/&%![]{}<>"))
punctuation to be replaced by '_'
string trim(string const &org)
remove leading and trailing whitespace
Definition util.cpp:85
bool boolVal(string const &textForm)
interpret text representation of a boolean value.
Definition util.cpp:99
bool isYes(string const &textForm) noexcept
check the given text if it can be interpreted as affirmative answer (bool true).
Definition util.cpp:108
function< bool(Cha)> ChPredicate
Definition util.cpp:48
bool isnil(lib::time::Duration const &dur)
Tiny helper functions and shortcuts to be used everywhere Consider this header to be effectively incl...