Lumiera  0.pre.03
»edit your freedom«
util.cpp File Reference

Go to the source code of this file.

Description

Implementation of widely used helper functions.

The helpers implemented here are typically accessed by including util.hpp, so the reason of pushing down the implementation into a separate translation unit (this one here) is to avoid more heavyweight includes (e.g. boost).

Definition in file util.cpp.

#include "error.hpp"
#include "lib/util.hpp"
#include "lib/format-string.hpp"
#include <boost/algorithm/string.hpp>
#include <functional>
#include <boost/bind.hpp>
#include <regex>

Typedefs

using ChPredicate = function< bool(string::value_type)>
 

Functions

bool boolVal (string const &)
 interpret text representation of a boolean value. More...
 
bool isNo (string const &) noexcept
 check if the given text is empty or can be interpreted as rejection (bool false)- More...
 
ChPredicate isPunct (is_space() or is_any_of(",;:#*~´`?\/&%![]{}<>"))
 punctuation to be replaced by '_'
 
ChPredicate isValid (is_alnum() or is_any_of("-_.+$()@"))
 characters to be retained
 
bool isYes (string const &) noexcept
 check the given text if it can be interpreted as affirmative answer (bool true). More...
 
ChPredicate operator! (ChPredicate p)
 
std::string sanitise (string const &org)
 produce an identifier based on the given string. More...
 
string trim (string const &org)
 remove leading and trailing whitespace More...
 

Variables

regex falseTokens { "\\s*(false|no|off|0|\\-)\\s*", regex::icase | regex::optimize }
 
regex trueTokens { "\\s*(true|yes|on|1|\\+)\\s*", regex::icase | regex::optimize }
 

Namespaces

Function Documentation

◆ sanitise()

string sanitise ( string const &  org)

produce an identifier based on the given string.

remove non-standard-chars, reduce sequences of punctuation and whitespace to single underscores. The sanitised string will start with an alphanumeric character.

Example Conversions
   "Word"                             --> "Word"
   "a Sentence"                       --> "a_Sentence"
   "trailing Withespace  \t \n"       --> "trailing_Withespace"
   "with    a   lot  \nof Whitespace" --> "with_a_lot_of_Whitespace"
   "@with\".\'much ($punctuation)[]!" --> "@with.much_($punctuation)"
   "§&Ω%€  leading garbage"           --> "leading_garbage"
   "mixed    Ω   garbage"             --> "mixed_garbage"
   "Bääääh!!"                         --> "Bh"
See also
UtilSanitizedIdentifier_test
lib::meta::sanitisedSymbol()

Definition at line 56 of file util.cpp.

References util::isValid(), and util::sanitise().

Referenced by util::sanitise().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ trim()

string trim ( string const &  org)

remove leading and trailing whitespace

Remarks
this function just forwards to boost::algorithm::trim_copy. Use this call when boost header inclusion is an issue, otherwise a direct invocation is likely to perform better, due to inlining.
Returns
a trimmed copy (default locale)

Definition at line 84 of file util.cpp.

References util::trim().

Referenced by util::trim().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ boolVal()

bool boolVal ( string const &  )

interpret text representation of a boolean value.

Remarks
this function detects the relevant token rather strict....
  • yields true for the tokens "true", "True", "TRUE", "yes", "Yes", "YES", "1", "+"
  • yields false for the tokens "false", "False", "FALSE", "no", "No, "NO", "0", "-"
  • leading and trailing whitespace is ignored
Exceptions
lumiera::error::Invalidfor any other text content

Definition at line 98 of file util.cpp.

References util::boolVal().

Referenced by util::boolVal().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ isYes()

bool isYes ( string const &  )
noexcept

check the given text if it can be interpreted as affirmative answer (bool true).

Remarks
this function just fishes for the known true tokens and interprets all other content as false, including empty strings. Never throws.

Definition at line 107 of file util.cpp.

References util::isYes().

Referenced by util::isYes().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ isNo()

bool isNo ( string const &  )
noexcept

check if the given text is empty or can be interpreted as rejection (bool false)-

Remarks
this function fishes for the known false tokens; any other non-empty content counts as not no.

Definition at line 114 of file util.cpp.

References util::isNo().

Referenced by util::isNo().

+ Here is the call graph for this function:
+ Here is the caller graph for this function: