Nix 2.36.0
Nix, the purely functional package manager; unstable internal interfaces
Loading...
Searching...
No Matches
strings-inline.hh
1#pragma once
2
3#include "nix/util/strings.hh"
4
5namespace nix {
6
7template<class C, class CharT>
8C basicTokenizeString(std::basic_string_view<CharT> s, std::basic_string_view<CharT> separators)
9{
10 C result;
11 auto pos = s.find_first_not_of(separators, 0);
12 while (pos != s.npos) {
13 auto end = s.find_first_of(separators, pos + 1);
14 if (end == s.npos)
15 end = s.size();
16 result.insert(result.end(), std::basic_string<CharT>(s, pos, end - pos));
17 pos = s.find_first_not_of(separators, end);
18 }
19 return result;
20}
21
22template<class C>
23C tokenizeString(std::string_view s, std::string_view separators)
24{
25 return basicTokenizeString<C, char>(s, separators);
26}
27
28template<class C, class CharT>
29void basicSplitStringInto(C & accum, std::basic_string_view<CharT> s, std::basic_string_view<CharT> separators)
30{
31 size_t pos = 0;
32 while (pos <= s.size()) {
33 auto end = s.find_first_of(separators, pos);
34 if (end == s.npos)
35 end = s.size();
36 accum.insert(accum.end(), typename C::value_type{s.substr(pos, end - pos)});
37 pos = end + 1;
38 }
39}
40
41template<typename C>
42void splitStringInto(C & accum, std::string_view s, std::string_view separators)
43{
44 basicSplitStringInto<C, char>(accum, s, separators);
45}
46
47template<class C, class CharT>
48C basicSplitString(std::basic_string_view<CharT> s, std::basic_string_view<CharT> separators)
49{
50 C result;
51 basicSplitStringInto(result, s, separators);
52 return result;
53}
54
55template<class C>
56C splitString(std::string_view s, std::string_view separators)
57{
58 return basicSplitString<C, char>(s, separators);
59}
60
61template<class CharT, class C>
62std::basic_string<CharT> basicConcatStringsSep(const std::basic_string_view<CharT> sep, const C & ss)
63{
64 size_t size = 0;
65 bool tail = false;
66 // need a cast to string_view since this is also called with Symbols
67 for (const auto & s : ss) {
68 if (tail)
69 size += sep.size();
70 size += std::basic_string_view<CharT>{s}.size();
71 tail = true;
72 }
73 std::basic_string<CharT> s;
74 s.reserve(size);
75 tail = false;
76 for (auto & i : ss) {
77 if (tail)
78 s += sep;
79 s += i;
80 tail = true;
81 }
82 return s;
83}
84
85template<class C>
86std::string concatStringsSep(const std::string_view sep, const C & ss)
87{
88 return basicConcatStringsSep<char, C>(sep, ss);
89}
90
91template<class C>
92std::string dropEmptyInitThenConcatStringsSep(const std::string_view sep, const C & ss)
93{
94 size_t size = 0;
95
96 // TODO? remove to make sure we don't rely on the empty item ignoring behavior,
97 // or just get rid of this function by understanding the remaining calls.
98 // for (auto & i : ss) {
99 // // Make sure we don't rely on the empty item ignoring behavior
100 // assert(!i.empty());
101 // break;
102 // }
103
104 // need a cast to string_view since this is also called with Symbols
105 for (const auto & s : ss)
106 size += sep.size() + std::string_view(s).size();
107 std::string s;
108 s.reserve(size);
109 for (auto & i : ss) {
110 if (s.size() != 0)
111 s += sep;
112 s += i;
113 }
114 return s;
115}
116
117} // namespace nix