File stringtrimmer.h
File List > code_source > templat > include > TempLat > util > stringtrimmer.h
Go to the documentation of this file
#ifndef TEMPLAT_UTIL_STRINGTRIMMER_H
#define TEMPLAT_UTIL_STRINGTRIMMER_H
/* This file is part of TempLat, available at https://cosmolattice.github.io/templat .
Copyright 2021-2026 The TempLat authors, see AUTHORS.md.
Released under the MIT license, see LICENSE.md. */
// File info: Main contributor(s): Wessel Valkenburg, Year: 2019
/* from http://stackoverflow.com/a/217605 */
// 2022: Adapted to lambdas.
#include <algorithm>
#include <functional>
#include <cctype>
#include <locale>
namespace TempLat
{
class StringTrimmer
{
public:
// Put public methods here. These should change very little over time.
static inline void ltrim(std::string &s)
{
s.erase(s.begin(), std::find_if(s.begin(), s.end(), [](int c) { return !std::isspace(c); }));
}
// trim from end (in place)
static inline void rtrim(std::string &s)
{
s.erase(std::find_if(s.rbegin(), s.rend(), [](int c) { return !std::isspace(c); }).base(), s.end());
}
// trim from both ends (in place)
static inline void trim(std::string &s)
{
ltrim(s);
rtrim(s);
}
// trim from start (copying)
static inline std::string ltrimmed(std::string s)
{
ltrim(s);
return s;
}
// trim from end (copying)
static inline std::string rtrimmed(std::string s)
{
rtrim(s);
return s;
}
// trim from both ends (copying)
static inline std::string trimmed(std::string s)
{
trim(s);
return s;
}
private:
/* Put all member variables and private methods here. These may change arbitrarily. */
};
} // namespace TempLat
#endif