1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
#include "lix/libutil/c-calls.hh"
#include "lix/libutil/strings.hh"
#include "lix/libutil/references.hh"
#include <boost/lexical_cast.hpp>
#include <ranges>
#include <stdint.h>

namespace nix {

std::vector<char *> stringsToCharPtrs(const Strings & ss)
{
    std::vector<char *> res;
    // This is const cast since this exists for OS APIs that want char *
    for (auto & s : ss) {
        res.push_back(const_cast<char *>(requireCString(s).asCStr()));
    }
    res.push_back(0);
    return res;
}


template<class C> C tokenizeString(std::string_view s, std::string_view separators)
{
    C result;
    auto pos = s.find_first_not_of(separators, 0);
    while (pos != std::string_view::npos) {
        auto end = s.find_first_of(separators, pos + 1);
        if (end == std::string_view::npos) end = s.size();
        result.insert(result.end(), std::string(s, pos, end - pos));
        pos = s.find_first_not_of(separators, end);
    }
    return result;
}

template Strings tokenizeString(std::string_view s, std::string_view separators);
template StringSet tokenizeString(std::string_view s, std::string_view separators);
template std::vector<std::string> tokenizeString(std::string_view s, std::string_view separators);

std::pair<std::string_view, std::optional<std::string_view>>
partitionString(std::string_view s, char separator)
{
    auto pos = s.find_first_of(separator);
    if (pos == std::string::npos) {
        return {s, std::nullopt};
    } else {
        return {s.substr(0, pos), s.substr(pos + 1)};
    }
}

std::string chomp(std::string_view s)
{
    size_t i = s.find_last_not_of(" \n\r\t");
    return i == std::string_view::npos ? "" : std::string(s, 0, i + 1);
}


std::string trim(std::string_view s, std::string_view whitespace)
{
    auto i = s.find_first_not_of(whitespace);
    if (i == s.npos) return "";
    auto j = s.find_last_not_of(whitespace);
    return std::string(s, i, j == s.npos ? j : j - i + 1);
}


std::string replaceStrings(
    std::string res,
    std::string_view from,
    std::string_view to)
{
    if (from.empty()) return res;
    size_t pos = 0;
    while ((pos = res.find(from, pos)) != std::string::npos) {
        res.replace(pos, from.size(), to);
        pos += to.size();
    }
    return res;
}


Rewriter::Rewriter(std::map<std::string, std::string> rewrites)
    : rewrites(std::move(rewrites))
{
}

std::string Rewriter::operator()(std::string s)
{
    StringSource src{s};
    RewritingSource inner{RewritingSource::may_change_size, rewrites, src};
    return inner.drain();
}

template<class N>
std::optional<N> string2Int(const std::string_view s)
{
    if (s.substr(0, 1) == "-" && !std::numeric_limits<N>::is_signed)
        return std::nullopt;
    try {
        return boost::lexical_cast<N>(s.data(), s.size());
    } catch (const boost::bad_lexical_cast &) { // NOLINT(lix-foreign-exceptions)
        return std::nullopt;
    }
}

// Explicitly instantiated in one place for faster compilation
template std::optional<unsigned char>  string2Int<unsigned char>(const std::string_view s);
template std::optional<unsigned short> string2Int<unsigned short>(const std::string_view s);
template std::optional<unsigned int> string2Int<unsigned int>(const std::string_view s);
template std::optional<unsigned long> string2Int<unsigned long>(const std::string_view s);
template std::optional<unsigned long long> string2Int<unsigned long long>(const std::string_view s);
template std::optional<signed char> string2Int<signed char>(const std::string_view s);
template std::optional<signed short> string2Int<signed short>(const std::string_view s);
template std::optional<signed int> string2Int<signed int>(const std::string_view s);
template std::optional<signed long> string2Int<signed long>(const std::string_view s);
template std::optional<signed long long> string2Int<signed long long>(const std::string_view s);

template<class N>
std::optional<N> string2Float(const std::string_view s)
{
    try {
        return boost::lexical_cast<N>(s.data(), s.size());
    } catch (const boost::bad_lexical_cast &) { // NOLINT(lix-foreign-exceptions)
        return std::nullopt;
    }
}

template std::optional<double> string2Float<double>(const std::string_view s);
template std::optional<float> string2Float<float>(const std::string_view s);


std::string toLower(const std::string & s)
{
    std::string r(s);
    for (auto & c : r)
        c = std::tolower(c);
    return r;
}


std::string shellEscape(const std::string_view s)
{
    std::string r;
    r.reserve(s.size() + 2);
    r += "'";
    for (auto & i : s) {
        if (i == '\'') {
            // End the single quote, add a single backslash-escaped single quote,
            // then start a single quote again.
            // i.e., `I didn't know` becomes `'I didn'\''t know'`.
            r += "'\\''";
        } else {
            r += i;
        }
    }

    r += '\'';
    return r;
}

std::string bashEscape(const std::string_view s)
{
    std::string r;
    r.reserve(s.size() + 2);
    r += "'";
    for (auto & i : s) {
        if (!std::isprint(i)) {
            // Close the single quote, start an "ANSI-C Quote" ($'foo'), add `\xXX`,
            // close the ANSI-C Quote, and finally start a normal single quote again.
            r += fmt("'$'\\x%02x''", static_cast<unsigned int>(static_cast<unsigned char>(i)));
        } else if (i == '\'') {
            // End the single quote, add a single backslash-escaped single quote,
            // then start a single quote again.
            // i.e., `I didn't know` becomes `'I didn'\''t know'`.
            r += "'\\''";
        } else {
            r += i;
        }
    }

    r += '\'';
    return r;
}

constexpr char base64Chars[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";

std::string base64Encode(std::string_view s)
{
    std::string res;
    res.reserve((s.size() + 2) / 3 * 4);
    int data = 0, nbits = 0;

    for (char c : s) {
        data = data << 8 | (unsigned char) c;
        nbits += 8;
        while (nbits >= 6) {
            nbits -= 6;
            res.push_back(base64Chars[data >> nbits & 0x3f]);
        }
    }

    if (nbits) res.push_back(base64Chars[data << (6 - nbits) & 0x3f]);
    while (res.size() % 4) res.push_back('=');

    return res;
}


std::string base64Decode(std::string_view s)
{
    constexpr char npos = -1;
    constexpr std::array<char, 256> base64DecodeChars = [&]() {
        std::array<char, 256>  result{};
        for (auto& c : result)
            c = npos;
        for (int i = 0; i < 64; i++)
            result[base64Chars[i]] = i;
        return result;
    }();

    std::string res;
    // Some sequences are missing the padding consisting of up to two '='.
    //                    vvv
    res.reserve((s.size() + 2) / 4 * 3);
    unsigned int d = 0, bits = 0;

    for (char c : s) {
        if (c == '=') break;
        if (c == '\n') continue;

        char digit = base64DecodeChars[(unsigned char) c];
        if (digit == npos)
            throw Error("invalid character in Base64 string: '%c'", c);

        bits += 6;
        d = d << 6 | digit;
        if (bits >= 8) {
            res.push_back(d >> (bits - 8) & 0xff);
            bits -= 8;
        }
    }

    return res;
}

// omitted: E O U T
const std::string base32Chars = "0123456789abcdfghijklmnpqrsvwxyz";

std::string base32EncodeStr(std::string_view s)
{
    std::span<std::byte const> sp = std::as_bytes(std::span(s));
    return base32Encode(sp);
}

std::string base32Encode(std::span<std::byte const> bytes)
{
    // log2(32) == 5.
    constexpr int B32_BITS_PER_DIGIT = 5;

    // We need to do arithmetic.
    auto const s = std::views::transform(bytes, std::to_integer<std::uint32_t>);

    if (s.empty()) {
        return "";
    }

    ssize_t len = (s.size() * CHAR_BIT - 1) / B32_BITS_PER_DIGIT + 1;

    std::string res;
    res.reserve(len);

    for (ssize_t const n : std::views::iota(0, len) | std::views::reverse) {
        unsigned int b = n * B32_BITS_PER_DIGIT;
        unsigned int i = b / CHAR_BIT;
        unsigned int j = b % CHAR_BIT;

        auto const curChar = s[i];
        auto const second = i >= s.size() - 1 ? 0 : s[i + 1] << (CHAR_BIT - j);
        auto const c = (curChar >> j) | second;

        res.push_back(base32Chars[c & 0x1f]);
    }

    return res;
}

std::string base32Decode(std::string_view s)
{
    if (s.empty()) {
        return "";
    }

    std::string res(((s.size() - 1) * 5) / 8 + 1, 0);

    for (unsigned int n = 0; n < s.size(); ++n) {
        char c = s[s.size() - n - 1];
        size_t digit;
        for (digit = 0; digit < base32Chars.size(); ++digit) /* !!! slow */ {
            if (base32Chars[digit] == c) {
                break;
            }
        }
        if (digit >= 32) {
            throw Error("invalid character in base-32 string '%s'", s);
        }
        unsigned int b = n * 5;
        unsigned int i = b / 8;
        unsigned int j = b % 8;
        res[i] |= digit << j;

        if (i < res.size() - 1) {
            res[i + 1] |= digit >> (8 - j);
        } else {
            if (digit >> (8 - j)) {
                throw Error("invalid base-32 string '%s'", s);
            }
        }
    }

    return res;
}

std::string stripIndentation(std::string_view s)
{
    size_t minIndent = 10000;
    size_t curIndent = 0;
    bool atStartOfLine = true;

    for (auto & c : s) {
        if (atStartOfLine && c == ' ')
            curIndent++;
        else if (c == '\n') {
            if (atStartOfLine)
                minIndent = std::max(minIndent, curIndent);
            curIndent = 0;
            atStartOfLine = true;
        } else {
            if (atStartOfLine) {
                minIndent = std::min(minIndent, curIndent);
                atStartOfLine = false;
            }
        }
    }

    std::string res;

    size_t pos = 0;
    while (pos < s.size()) {
        auto eol = s.find('\n', pos);
        if (eol == s.npos) eol = s.size();
        if (eol - pos > minIndent)
            res.append(s.substr(pos + minIndent, eol - pos - minIndent));
        res.push_back('\n');
        pos = eol + 1;
    }

    return res;
}


std::pair<std::string_view, std::string_view> getLine(std::string_view s)
{
    auto newline = s.find('\n');

    if (newline == s.npos) {
        return {s, ""};
    } else {
        auto line = s.substr(0, newline);
        if (!line.empty() && line[line.size() - 1] == '\r')
            line = line.substr(0, line.size() - 1);
        return {line, s.substr(newline + 1)};
    }
}

std::string showBytes(uint64_t bytes)
{
    return fmt("%.2f MiB", bytes / (1024.0 * 1024.0));
}

std::string escapeNul(const std::string & in)
{
    using std::operator""sv;
    return replaceStrings(replaceStrings(std::move(in), R"(\)", R"(\\)"), "\0"sv, R"(\0)");
}

std::string unescapeNul(const std::string & in)
{
    std::string result;
    result.reserve(in.size());
    bool inSequence = false;
    for (auto c : in) {
        if (inSequence) {
            result += c == '0' ? '\0' : c;
            inSequence = false;
        } else if (c == '\\') {
            inSequence = true;
        } else {
            result += c;
        }
    }
    return result;
}
}