Nix 2.26.3
Nix, the purely functional package manager; unstable internal interfaces
 
Loading...
Searching...
No Matches
strings-inline.hh
1#pragma once
2
3#include "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>
29C basicSplitString(std::basic_string_view<CharT> s, std::basic_string_view<CharT> separators)
30{
31 C result;
32 size_t pos = 0;
33 while (pos <= s.size()) {
34 auto end = s.find_first_of(separators, pos);
35 if (end == s.npos)
36 end = s.size();
37 result.insert(result.end(), std::basic_string<CharT>(s, pos, end - pos));
38 pos = end + 1;
39 }
40
41 return result;
42}
43
44template<class C>
45C splitString(std::string_view s, std::string_view separators)
46{
47 return basicSplitString<C, char>(s, separators);
48}
49
50template<class CharT, class C>
51std::basic_string<CharT> basicConcatStringsSep(const std::basic_string_view<CharT> sep, const C & ss)
52{
53 size_t size = 0;
54 bool tail = false;
55 // need a cast to string_view since this is also called with Symbols
56 for (const auto & s : ss) {
57 if (tail)
58 size += sep.size();
59 size += std::basic_string_view<CharT>{s}.size();
60 tail = true;
61 }
62 std::basic_string<CharT> s;
63 s.reserve(size);
64 tail = false;
65 for (auto & i : ss) {
66 if (tail)
67 s += sep;
68 s += i;
69 tail = true;
70 }
71 return s;
72}
73
74template<class C>
75std::string concatStringsSep(const std::string_view sep, const C & ss)
76{
77 return basicConcatStringsSep<char, C>(sep, ss);
78}
79
80template<class C>
81std::string dropEmptyInitThenConcatStringsSep(const std::string_view sep, const C & ss)
82{
83 size_t size = 0;
84
85 // TODO? remove to make sure we don't rely on the empty item ignoring behavior,
86 // or just get rid of this function by understanding the remaining calls.
87 // for (auto & i : ss) {
88 // // Make sure we don't rely on the empty item ignoring behavior
89 // assert(!i.empty());
90 // break;
91 // }
92
93 // need a cast to string_view since this is also called with Symbols
94 for (const auto & s : ss)
95 size += sep.size() + std::string_view(s).size();
96 std::string s;
97 s.reserve(size);
98 for (auto & i : ss) {
99 if (s.size() != 0)
100 s += sep;
101 s += i;
102 }
103 return s;
104}
105
106} // namespace nix
PosIdx end
Definition lexer.l:5814
auto i
Definition lexer.l:2745
return s
Definition lexer.l:459