2007-09-14 19:18:11 +02:00
|
|
|
|
/*
|
|
|
|
|
|
util.cpp - helper functions implementation
|
2010-12-17 23:28:49 +01:00
|
|
|
|
|
2008-03-10 04:25:03 +01:00
|
|
|
|
Copyright (C) Lumiera.org
|
|
|
|
|
|
2008, Hermann Vosseler <Ichthyostega@web.de>
|
2010-12-17 23:28:49 +01:00
|
|
|
|
|
2007-09-14 19:18:11 +02:00
|
|
|
|
This program is free software; you can redistribute it and/or
|
|
|
|
|
|
modify it under the terms of the GNU General Public License as
|
2010-12-17 23:28:49 +01:00
|
|
|
|
published by the Free Software Foundation; either version 2 of
|
|
|
|
|
|
the License, or (at your option) any later version.
|
|
|
|
|
|
|
2007-09-14 19:18:11 +02:00
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
|
GNU General Public License for more details.
|
2010-12-17 23:28:49 +01:00
|
|
|
|
|
2007-09-14 19:18:11 +02:00
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
|
|
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
2010-12-17 23:28:49 +01:00
|
|
|
|
|
2007-09-14 19:18:11 +02:00
|
|
|
|
* *****************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-11-03 18:22:31 +01:00
|
|
|
|
/** @file util.cpp
|
|
|
|
|
|
** TODO util.cpp
|
2016-11-03 18:20:10 +01:00
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
|
2008-12-17 17:53:32 +01:00
|
|
|
|
#include "lib/util.hpp"
|
2007-09-14 19:18:11 +02:00
|
|
|
|
|
|
|
|
|
|
#include <boost/algorithm/string.hpp>
|
2014-04-03 22:42:48 +02:00
|
|
|
|
#include <functional>
|
2009-07-19 06:24:36 +02:00
|
|
|
|
#include <boost/bind.hpp> // we need operator! for bind-expressions
|
2007-09-14 19:18:11 +02:00
|
|
|
|
|
|
|
|
|
|
using boost::algorithm::trim_right_copy_if;
|
|
|
|
|
|
using boost::algorithm::is_any_of;
|
|
|
|
|
|
using boost::algorithm::is_alnum;
|
|
|
|
|
|
using boost::algorithm::is_space;
|
|
|
|
|
|
|
|
|
|
|
|
|
2009-07-19 05:47:36 +02:00
|
|
|
|
namespace util {
|
2007-09-14 19:18:11 +02:00
|
|
|
|
|
2014-04-03 22:42:48 +02:00
|
|
|
|
using std::function;
|
2009-07-19 05:47:36 +02:00
|
|
|
|
|
|
|
|
|
|
typedef function<bool(string::value_type)> ChPredicate;
|
2015-09-25 02:38:59 +02:00
|
|
|
|
ChPredicate operator! (ChPredicate p) { return not bind(p,_1); }
|
2007-09-14 19:18:11 +02:00
|
|
|
|
|
2009-07-19 05:47:36 +02:00
|
|
|
|
// character classes used for sanitising a string
|
2016-01-10 02:02:18 +01:00
|
|
|
|
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 '_'
|
2007-09-14 19:18:11 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
string
|
2016-01-08 08:20:59 +01:00
|
|
|
|
sanitise (string const& org)
|
2007-09-14 19:18:11 +02:00
|
|
|
|
{
|
|
|
|
|
|
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 )
|
|
|
|
|
|
{
|
|
|
|
|
|
while ( i != e && !isValid (*i) ) ++i;
|
|
|
|
|
|
while ( i != e && isValid (*i) ) *(j++) = *(i++);
|
|
|
|
|
|
if ( i != e && isPunct (*i) )
|
|
|
|
|
|
{
|
|
|
|
|
|
*j++ = '_';
|
|
|
|
|
|
do ++i;
|
|
|
|
|
|
while ( i != e && isPunct (*i));
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
res.erase(j,res.end());
|
|
|
|
|
|
return res;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2015-08-17 01:22:01 +02:00
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* @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);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2007-09-14 19:18:11 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} // namespace util
|
|
|
|
|
|
|