Nix 2.36.0
Nix, the purely functional package manager; unstable internal interfaces
Loading...
Searching...
No Matches
strings.hh
1#pragma once
2
3#include "nix/util/types.hh"
4
5#include <list>
6#include <optional>
7#include <set>
8#include <string_view>
9#include <string>
10#include <vector>
11
12#include <boost/container/small_vector.hpp>
13
14namespace nix {
15
21template<class C, class CharT = char>
22C basicTokenizeString(std::basic_string_view<CharT> s, std::basic_string_view<CharT> separators);
23
27template<class C>
28C tokenizeString(std::string_view s, std::string_view separators = " \t\n\r");
29
30extern template std::list<std::string> tokenizeString(std::string_view s, std::string_view separators);
31extern template StringSet tokenizeString(std::string_view s, std::string_view separators);
32extern template std::vector<std::string> tokenizeString(std::string_view s, std::string_view separators);
33
39template<class C, class CharT = char>
40C basicSplitString(std::basic_string_view<CharT> s, std::basic_string_view<CharT> separators);
41template<typename C>
42C splitString(std::string_view s, std::string_view separators);
43
44extern template std::list<std::string> splitString(std::string_view s, std::string_view separators);
45extern template StringSet splitString(std::string_view s, std::string_view separators);
46extern template std::vector<std::string> splitString(std::string_view s, std::string_view separators);
47
51template<class C>
52std::string concatStringsSep(const std::string_view sep, const C & ss);
53
54extern template std::string concatStringsSep(std::string_view, const std::list<std::string> &);
55extern template std::string concatStringsSep(std::string_view, const StringSet &);
56extern template std::string concatStringsSep(std::string_view, const std::vector<std::string> &);
57extern template std::string concatStringsSep(std::string_view, const boost::container::small_vector<std::string, 64> &);
58
62template<class C, class F>
63std::string concatMapStringsSep(std::string_view separator, const C & iterable, F fn)
64{
65 boost::container::small_vector<std::string, 64> strings;
66 strings.reserve(iterable.size());
67 for (const auto & elem : iterable) {
68 strings.push_back(fn(elem));
69 }
70 return concatStringsSep(separator, strings);
71}
72
80template<class C>
81[[deprecated(
82 "Consider removing the empty string dropping behavior. If acceptable, use concatStringsSep instead.")]] std::string
83dropEmptyInitThenConcatStringsSep(const std::string_view sep, const C & ss);
84
85extern template std::string dropEmptyInitThenConcatStringsSep(std::string_view, const std::list<std::string> &);
86extern template std::string dropEmptyInitThenConcatStringsSep(std::string_view, const StringSet &);
87extern template std::string dropEmptyInitThenConcatStringsSep(std::string_view, const std::vector<std::string> &);
88
95std::list<std::string> shellSplitString(std::string_view s);
96
112std::string optionalBracket(std::string_view prefix, std::string_view content, std::string_view suffix);
113
125template<typename T>
126 requires std::convertible_to<T, std::string_view>
127std::string optionalBracket(std::string_view prefix, const std::optional<T> & content, std::string_view suffix)
128{
129 if (!content || std::string_view(*content).empty()) {
130 return "";
131 }
132 return optionalBracket(prefix, std::string_view(*content), suffix);
133}
134
142{
143private:
144 using HashType = std::hash<std::string_view>;
145
146public:
147 using is_transparent = void;
148
149 auto operator()(const char * str) const
150 {
151 /* This has a slight overhead due to an implicit strlen, but there isn't
152 a good way around it because the hash value of all overloads must be
153 consistent. Delegating to string_view is the solution initially proposed
154 in P0919R3. */
155 return HashType{}(std::string_view{str});
156 }
157
158 auto operator()(std::string_view str) const
159 {
160 return HashType{}(str);
161 }
162
163 auto operator()(const std::string & str) const
164 {
165 return HashType{}(std::string_view{str});
166 }
167};
168
173const char * requireCString(const std::string & str);
174
175} // namespace nix
Definition strings.hh:142
std::set< std::string, std::less<> > StringSet
Definition types.hh:45