SIRIUS 7.5.0
Electronic structure library and applications
string_tools.hpp
Go to the documentation of this file.
1// Copyright (c) 2013-2023 Anton Kozhevnikov, Thomas Schulthess
2// All rights reserved.
3//
4// Redistribution and use in source and binary forms, with or without modification, are permitted provided that
5// the following conditions are met:
6//
7// 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the
8// following disclaimer.
9// 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions
10// and the following disclaimer in the documentation and/or other materials provided with the distribution.
11//
12// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
13// WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
14// PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
15// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
16// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
17// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
18// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
19
20/** \file string_tools.hpp
21 *
22 * \brief Extra functions to work with std::strings
23 */
24
25#ifndef __STRING_TOOLS_HPP__
26#define __STRING_TOOLS_HPP__
27
28namespace sirius {
29
30/// Split multi-line string into a list of strings.
31inline auto split(std::string const str__, char delim__)
32{
33 std::istringstream iss(str__);
34 std::vector<std::string> result;
35
36 while (iss.good()) {
37 std::string s;
38 std::getline(iss, s, delim__);
39 result.push_back(s);
40 }
41 return result;
42}
43
44inline std::string& ltrim(std::string& str, const std::string& chars = "\t\n\v\f\r ")
45{
46 str.erase(0, str.find_first_not_of(chars));
47 return str;
48}
49
50inline std::string& rtrim(std::string& str, const std::string& chars = "\t\n\v\f\r ")
51{
52 str.erase(str.find_last_not_of(chars) + 1);
53 return str;
54}
55
56inline std::string& trim(std::string& str, const std::string& chars = "\t\n\v\f\r ")
57{
58 return ltrim(rtrim(str, chars), chars);
59}
60
61}
62#endif
Namespace of the SIRIUS library.
Definition: sirius.f90:5
auto split(std::string const str__, char delim__)
Split multi-line string into a list of strings.