3.5-git
DUNE for Multi-{Phase, Component, Scale, Physics, ...} flow and transport in porous media
stringutilities.hh
Go to the documentation of this file.
1// -*- mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2// vi: set et ts=4 sw=4 sts=4:
3/*****************************************************************************
4 * See the file COPYING for full copying permissions. *
5 * *
6 * This program is free software: you can redistribute it and/or modify *
7 * it under the terms of the GNU General Public License as published by *
8 * the Free Software Foundation, either version 3 of the License, or *
9 * (at your option) any later version. *
10 * *
11 * This program is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU General Public License *
17 * along with this program. If not, see <http://www.gnu.org/licenses/>. *
18 *****************************************************************************/
19
26#ifndef DUMUX_COMMON_STRING_UTILITIES_HH
27#define DUMUX_COMMON_STRING_UTILITIES_HH
28
29#include <vector>
30#include <string>
31#include <string_view>
32#include <tuple>
33
34namespace Dumux {
35
36/*
37 * \brief Tokenize a string splitting at a given delimiter
38 * \ingroup Common
39 * \param str a string to be tokenized
40 * \param delim a set of delimiter characters separating the tokens
41 * \note this works without copying the original string, make sure that string
42 * does not go out of scope!
43 * \note The delimiter characters cannot appear within any of the tokens
44 *
45 * Examples:
46 * - tokenize("bla&foo&&&bar", "&") -> {"bla", "foo", "bar"}
47 * - tokenize(" a b ", " ") -> {"a", "b"}
48 * - tokenize(str, " \n\t") -> split at whitespace (and removes all whitespace)
49 */
50std::vector<std::string_view> tokenize(std::string_view str, std::string_view delim)
51{
52 const auto token = [&](std::size_t pos){
53 const auto start = str.find_first_not_of(delim, pos);
54 const auto end = str.find_first_of(delim, start);
55 return std::make_pair(start, end);
56 };
57
58 std::vector<std::string_view> tokens;
59 for (auto [start, end] = token(0); start != end; std::tie(start, end) = token(end))
60 tokens.emplace_back(str.substr(start, end-start));
61 return tokens;
62}
63
64/*
65 * \brief Split a string at a given seperator string
66 * \ingroup Common
67 * \param str a string to be split
68 * \param delim a set of delimiter characters separating the tokens
69 * \param removeEmpty if empty string between two separator should be removed from the result
70 * \note this works without copying the original string, make sure that string
71 * does not go out of scope!
72 *
73 * Examples:
74 * - split("| Hello | world |", "|") -> {"", " Hello ", " world ", ""}
75 * - split("| Hello | world |", "|", true) -> {" Hello ", " world "}
76 * - split("blafoobla", "foo") -> {"bla", "bla"}
77 * - split("blafoobla", "bla") -> {"", "foo", ""}
78 * - split("blafoobla", "bla", true) -> {"foo"}
79 */
80std::vector<std::string_view> split(std::string_view str, std::string_view delim, bool removeEmpty = false)
81{
82 const auto token = [&](std::size_t pos){
83 const auto end = str.find(delim, pos);
84 return std::make_pair(pos, end);
85 };
86
87 std::vector<std::string_view> split;
88 for (auto [start, end] = token(0); start != std::string::npos; std::tie(start, end) = token(start))
89 {
90 if (auto&& sub = str.substr(start, end-start); !removeEmpty || !sub.empty())
91 split.emplace_back(std::move(sub));
92
93 // skip to after the delimiter
94 start = (end != std::string::npos) ? end + delim.size() : end;
95 }
96 return split;
97}
98
99} // end namespace Dumux
100
101#endif
Definition: adapt.hh:29
std::vector< std::string_view > split(std::string_view str, std::string_view delim, bool removeEmpty=false)
Definition: stringutilities.hh:80
std::vector< std::string_view > tokenize(std::string_view str, std::string_view delim)
Definition: stringutilities.hh:50