lumiera_documentation/src/lib/util.cpp

125 lines
3.3 KiB
C++
Raw Normal View History

/*
util.cpp - helper functions implementation
2010-12-17 23:28:49 +01:00
Copyright: clarify and simplify the file headers * Lumiera source code always was copyrighted by individual contributors * there is no entity "Lumiera.org" which holds any copyrights * Lumiera source code is provided under the GPL Version 2+ == Explanations == Lumiera as a whole is distributed under Copyleft, GNU General Public License Version 2 or above. For this to become legally effective, the ''File COPYING in the root directory is sufficient.'' The licensing header in each file is not strictly necessary, yet considered good practice; attaching a licence notice increases the likeliness that this information is retained in case someone extracts individual code files. However, it is not by the presence of some text, that legally binding licensing terms become effective; rather the fact matters that a given piece of code was provably copyrighted and published under a license. Even reformatting the code, renaming some variables or deleting parts of the code will not alter this legal situation, but rather creates a derivative work, which is likewise covered by the GPL! The most relevant information in the file header is the notice regarding the time of the first individual copyright claim. By virtue of this initial copyright, the first author is entitled to choose the terms of licensing. All further modifications are permitted and covered by the License. The specific wording or format of the copyright header is not legally relevant, as long as the intention to publish under the GPL remains clear. The extended wording was based on a recommendation by the FSF. It can be shortened, because the full terms of the license are provided alongside the distribution, in the file COPYING.
2024-11-17 23:42:55 +01:00
Copyright (C)
2008, Hermann Vosseler <Ichthyostega@web.de>
2010-12-17 23:28:49 +01:00
Copyright: clarify and simplify the file headers * Lumiera source code always was copyrighted by individual contributors * there is no entity "Lumiera.org" which holds any copyrights * Lumiera source code is provided under the GPL Version 2+ == Explanations == Lumiera as a whole is distributed under Copyleft, GNU General Public License Version 2 or above. For this to become legally effective, the ''File COPYING in the root directory is sufficient.'' The licensing header in each file is not strictly necessary, yet considered good practice; attaching a licence notice increases the likeliness that this information is retained in case someone extracts individual code files. However, it is not by the presence of some text, that legally binding licensing terms become effective; rather the fact matters that a given piece of code was provably copyrighted and published under a license. Even reformatting the code, renaming some variables or deleting parts of the code will not alter this legal situation, but rather creates a derivative work, which is likewise covered by the GPL! The most relevant information in the file header is the notice regarding the time of the first individual copyright claim. By virtue of this initial copyright, the first author is entitled to choose the terms of licensing. All further modifications are permitted and covered by the License. The specific wording or format of the copyright header is not legally relevant, as long as the intention to publish under the GPL remains clear. The extended wording was based on a recommendation by the FSF. It can be shortened, because the full terms of the license are provided alongside the distribution, in the file COPYING.
2024-11-17 23:42:55 +01:00
  **Lumiera** is free software; you can redistribute it and/or modify it
  under the terms of the GNU General Public License as published by the
  Free Software Foundation; either version 2 of the License, or (at your
  option) any later version. See the file COPYING for further details.
2010-12-17 23:28:49 +01:00
Copyright: clarify and simplify the file headers * Lumiera source code always was copyrighted by individual contributors * there is no entity "Lumiera.org" which holds any copyrights * Lumiera source code is provided under the GPL Version 2+ == Explanations == Lumiera as a whole is distributed under Copyleft, GNU General Public License Version 2 or above. For this to become legally effective, the ''File COPYING in the root directory is sufficient.'' The licensing header in each file is not strictly necessary, yet considered good practice; attaching a licence notice increases the likeliness that this information is retained in case someone extracts individual code files. However, it is not by the presence of some text, that legally binding licensing terms become effective; rather the fact matters that a given piece of code was provably copyrighted and published under a license. Even reformatting the code, renaming some variables or deleting parts of the code will not alter this legal situation, but rather creates a derivative work, which is likewise covered by the GPL! The most relevant information in the file header is the notice regarding the time of the first individual copyright claim. By virtue of this initial copyright, the first author is entitled to choose the terms of licensing. All further modifications are permitted and covered by the License. The specific wording or format of the copyright header is not legally relevant, as long as the intention to publish under the GPL remains clear. The extended wording was based on a recommendation by the FSF. It can be shortened, because the full terms of the license are provided alongside the distribution, in the file COPYING.
2024-11-17 23:42:55 +01:00
* *****************************************************************/
/** @file util.cpp
** 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).
*/
#include "error.hpp"
#include "lib/util.hpp"
#include "lib/format-string.hpp"
#include <boost/algorithm/string.hpp>
#include <functional>
using boost::algorithm::trim_right_copy_if;
using boost::algorithm::is_any_of;
using boost::algorithm::is_alnum;
using boost::algorithm::is_space;
#include <regex>
using std::regex;
using std::regex_match;
using std::string;
using std::function;
using util::_Fmt;
namespace util {
using Cha = string::value_type;
using ChPredicate = function<bool(Cha)>;
ChPredicate operator! (ChPredicate p) { return [p](Cha c){ return not p(c); }; }
// character classes used for sanitising a string
ChPredicate isValid (is_alnum() or is_any_of("-_.+$()@")); ///< characters to be retained
ChPredicate isPunct (is_space() or is_any_of(",;:#*~´`?\\=/&%![]{}<>")); ///< punctuation to be replaced by '_'
string
sanitise (string const& org)
{
string res (trim_right_copy_if(org, !isValid ));
string::iterator j = res.begin();
string::const_iterator i = org.begin();
string::const_iterator e = i + (res.length());
while ( i != e )
{
2025-06-07 23:59:57 +02:00
while ( i != e and !isValid (*i) ) ++i;
while ( i != e and isValid (*i) ) *(j++) = *(i++);
if ( i != e and isPunct (*i) )
{
*j++ = '_';
do ++i;
2025-06-07 23:59:57 +02:00
while ( i != e and isPunct (*i));
}
}
res.erase(j,res.end());
return res;
}
/**
* @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.
*/
string
trim (string const& org)
{
return boost::algorithm::trim_copy (org);
}
namespace {
regex trueTokens { "\\s*(true|yes|on|1|\\+)\\s*", regex::icase | regex::optimize };
regex falseTokens{ "\\s*(false|no|off|0|\\-)\\s*", regex::icase | regex::optimize };
}
bool
boolVal (string const& textForm)
{
if (regex_match (textForm, trueTokens)) return true;
if (regex_match (textForm, falseTokens)) return false;
throw lumiera::error::Invalid(_Fmt{"String '%s' can not be interpreted as bool value"} % textForm);
}
bool
isYes (string const& textForm) noexcept
{
return regex_match (textForm, trueTokens);
}
bool
isNo (string const& textForm) noexcept
{
return isnil (textForm)
or regex_match (textForm, falseTokens);
}
} // namespace util