Nix 2.93.3
Lix: A modern, delicious implementation of the Nix package manager; unstable internal interfaces
Loading...
Searching...
No Matches
strings.hh
Go to the documentation of this file.
1#pragma once
3
6
7#include <vector>
8#include <boost/container/small_vector.hpp>
9
10namespace nix {
11
15constexpr char treeConn[] = "├───";
16constexpr char treeLast[] = "└───";
17constexpr char treeLine[] = "│ ";
18constexpr char treeNull[] = " ";
19
27std::vector<char *> stringsToCharPtrs(const Strings & ss);
28
29
30MakeError(FormatError, Error);
31
32
36template<class C> C tokenizeString(std::string_view s, std::string_view separators = " \t\n\r");
37
42template<class C>
43std::string concatStringsSep(const std::string_view sep, const C & ss)
44{
45 size_t size = 0;
46 // need a cast to string_view since this is also called with Symbols
47 for (const auto & s : ss) size += sep.size() + std::string_view(s).size();
48 std::string s;
49 s.reserve(size);
50 for (auto & i : ss) {
51 if (s.size() != 0) s += sep;
52 s += i;
53 }
54 return s;
55}
56
57template<class ... Parts>
58auto concatStrings(Parts && ... parts)
59 -> std::enable_if_t<(... && std::is_convertible_v<Parts, std::string_view>), std::string>
60{
61 std::string_view views[sizeof...(parts)] = { parts... };
62 return concatStringsSep({}, views);
63}
64
65
69template<class C, class F>
70std::string concatMapStringsSep(std::string_view separator, const C & iterable, F fn)
71{
72 boost::container::small_vector<std::string, 64> strings;
73 strings.reserve(iterable.size());
74 for (const auto & elem : iterable) {
75 strings.push_back(fn(elem));
76 }
77 return concatStringsSep(separator, strings);
78}
79
80
81
85template<class C> Strings quoteStrings(const C & c)
86{
87 Strings res;
88 for (auto & s : c)
89 res.push_back("'" + s + "'");
90 return res;
91}
92
98std::string chomp(std::string_view s);
99
100
104std::string trim(std::string_view s, std::string_view whitespace = " \n\r\t");
105
106
110std::string replaceStrings(
111 std::string s,
112 std::string_view from,
113 std::string_view to);
114
115
127class Rewriter
128{
129private:
130 std::string initials;
131 std::map<std::string, std::string> rewrites;
132
133public:
134 explicit Rewriter(std::map<std::string, std::string> rewrites);
135
136 std::string operator()(std::string s);
137};
138
139inline std::string rewriteStrings(std::string s, const StringMap & rewrites)
140{
141 return Rewriter(rewrites)(s);
142}
143
144
145
149template<class N>
150std::optional<N> string2Int(const std::string_view s);
151
156template<class N>
157N string2IntWithUnitPrefix(std::string_view s)
158{
159 N multiplier = 1;
160 if (!s.empty()) {
161 char u = std::toupper(*s.rbegin());
162 if (std::isalpha(u)) {
163 if (u == 'K') multiplier = 1ULL << 10;
164 else if (u == 'M') multiplier = 1ULL << 20;
165 else if (u == 'G') multiplier = 1ULL << 30;
166 else if (u == 'T') multiplier = 1ULL << 40;
167 else throw UsageError("invalid unit specifier '%1%'", u);
168 s.remove_suffix(1);
169 }
170 }
171 if (auto n = string2Int<N>(s))
172 return *n * multiplier;
173 throw UsageError("'%s' is not an integer", s);
174}
175
179template<class N>
180std::optional<N> string2Float(const std::string_view s);
181
182
186std::string toLower(const std::string & s);
187
188
192std::string shellEscape(const std::string_view s);
193
197std::string base64Encode(std::string_view s);
198std::string base64Decode(std::string_view s);
199
200
206std::string stripIndentation(std::string_view s);
207
213std::pair<std::string_view, std::string_view> getLine(std::string_view s);
214
215std::string showBytes(uint64_t bytes);
216
217
232inline std::string operator + (const std::string & s1, std::string_view s2)
233{
234 auto s = s1;
235 s.append(s2);
236 return s;
237}
238
239inline std::string operator + (std::string && s, std::string_view s2)
240{
241 s.append(s2);
242 return std::move(s);
243}
244
245inline std::string operator + (std::string_view s1, const char * s2)
246{
247 std::string s;
248 s.reserve(s1.size() + strlen(s2));
249 s.append(s1);
250 s.append(s2);
251 return s;
252}
253
254}
Definition strings.hh:128
This file defines two main structs/classes used in nix error handling.
std::string concatMapStringsSep(std::string_view separator, const C &iterable, F fn)
Definition strings.hh:70
N string2IntWithUnitPrefix(std::string_view s)
Definition strings.hh:157
std::string concatStringsSep(const std::string_view sep, const C &ss)
Definition strings.hh:43
constexpr char treeConn[]
Definition strings.hh:15
Strings quoteStrings(const C &c)
Definition strings.hh:85