Skip to content
Snippets Groups Projects
Commit 3ee8758a authored by Jorrit Fahlke's avatar Jorrit Fahlke
Browse files

Methods for determining whether a given character container has a given prefix

or suffix.  This is in particular applicable to strings.

[[Imported from SVN: r6038]]
parent e0fbf206
No related branches found
No related tags found
No related merge requests found
......@@ -7,8 +7,12 @@
\brief Miscellaneous helper stuff
*/
#include <algorithm>
#include <cstddef>
#include <cstring>
#include <iostream>
#include <iomanip>
#include <iterator>
#include <sstream>
#include <complex>
#include "exceptions.hh"
......@@ -123,6 +127,41 @@ namespace Dune {
return name.str();
}
//********************************************************************
//
// check whether a given container has a prefix/suffix
//
//********************************************************************
//! check whether a character container has a given prefix
/**
* The container must support the the begin() and size() methods.
*/
template<typename C>
bool hasPrefix(const C& c, const char* prefix) {
std::size_t len = std::strlen(prefix);
return c.size() >= len &&
std::equal(prefix, prefix+len, c.begin());
}
//! check whether a character container has a given suffix
/**
* The container must support the the begin() and size() methods and the
* const_iterator member type.
*
* \note This is slow for containers which don't have random access iterators.
* In the case of containers with bidirectional iterators, this
* slow-ness is unecessary.
*/
template<typename C>
bool hasSuffix(const C& c, const char* suffix) {
std::size_t len = std::strlen(suffix);
if(c.size() < len) return false;
typename C::const_iterator it = c.begin();
std::advance(it, c.size() - len);
return std::equal(suffix, suffix+len, it);
}
/** @} */
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment