]> git.proxmox.com Git - ceph.git/blame - ceph/src/include/str_list.h
update ceph source to reef 18.1.2
[ceph.git] / ceph / src / include / str_list.h
CommitLineData
7c673cae
FG
1#ifndef CEPH_STRLIST_H
2#define CEPH_STRLIST_H
3
4#include <list>
5#include <set>
6#include <string>
11fdf7f2 7#include <string_view>
7c673cae
FG
8#include <vector>
9
11fdf7f2
TL
10namespace ceph {
11
12/// Split a string using the given delimiters, passing each piece as a
13/// (non-null-terminated) std::string_view to the callback.
14template <typename Func> // where Func(std::string_view) is a valid call
15void for_each_substr(std::string_view s, const char *delims, Func&& f)
16{
17 auto pos = s.find_first_not_of(delims);
18 while (pos != s.npos) {
19 s.remove_prefix(pos); // trim delims from the front
20 auto end = s.find_first_of(delims);
21 f(s.substr(0, end));
22 pos = s.find_first_not_of(delims, end);
23 }
24}
25
26} // namespace ceph
27
7c673cae
FG
28/**
29 * Split **str** into a list of strings, using the ";,= \t" delimiters and output the result in **str_list**.
30 *
31 * @param [in] str String to split and save as list
32 * @param [out] str_list List modified containing str after it has been split
33**/
34extern void get_str_list(const std::string& str,
35 std::list<std::string>& str_list);
36
37/**
38 * Split **str** into a list of strings, using the **delims** delimiters and output the result in **str_list**.
39 *
40 * @param [in] str String to split and save as list
41 * @param [in] delims characters used to split **str**
42 * @param [out] str_list List modified containing str after it has been split
43**/
44extern void get_str_list(const std::string& str,
45 const char *delims,
46 std::list<std::string>& str_list);
47
11fdf7f2
TL
48std::list<std::string> get_str_list(const std::string& str,
49 const char *delims = ";,= \t");
50
7c673cae 51/**
f67539c2 52 * Split **str** into a vector of strings, using the ";,= \t" delimiters and output the result in **str_vec**.
7c673cae
FG
53 *
54 * @param [in] str String to split and save as Vector
55 * @param [out] str_vec Vector modified containing str after it has been split
56**/
1e59de90 57void get_str_vec(std::string_view str, std::vector<std::string>& str_vec);
7c673cae
FG
58
59/**
f67539c2 60 * Split **str** into a vector of strings, using the **delims** delimiters and output the result in **str_vec**.
7c673cae
FG
61 *
62 * @param [in] str String to split and save as Vector
63 * @param [in] delims characters used to split **str**
64 * @param [out] str_vec Vector modified containing str after it has been split
65**/
1e59de90
TL
66void get_str_vec(std::string_view str,
67 const char *delims,
68 std::vector<std::string>& str_vec);
7c673cae 69
1e59de90 70std::vector<std::string> get_str_vec(std::string_view str,
11fdf7f2 71 const char *delims = ";,= \t");
7c673cae
FG
72
73/**
74 * Return a String containing the vector **v** joined with **sep**
75 *
76 * If **v** is empty, the function returns an empty string
77 * For each element in **v**,
78 * it will concatenate this element and **sep** with result
79 *
80 * @param [in] v Vector to join as a String
81 * @param [in] sep String used to join each element from **v**
82 * @return empty string if **v** is empty or concatenated string
83**/
11fdf7f2 84inline std::string str_join(const std::vector<std::string>& v, const std::string& sep)
7c673cae
FG
85{
86 if (v.empty())
87 return std::string();
f67539c2 88 auto i = v.cbegin();
7c673cae 89 std::string r = *i;
f67539c2 90 for (++i; i != v.cend(); ++i) {
7c673cae
FG
91 r += sep;
92 r += *i;
93 }
94 return r;
95}
96
7c673cae 97#endif