File size: 30,112 Bytes
2492322
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
// Copyright (c) 2019, QuantStack and Mamba Contributors
//
// Distributed under the terms of the BSD 3-Clause License.
//
// The full license is in the file LICENSE, distributed with this software.

#ifndef MAMBA_CORE_UTIL_STRING_HPP
#define MAMBA_CORE_UTIL_STRING_HPP

#include <algorithm>
#include <array>
#include <cstdint>
#include <cstring>
#include <optional>
#include <string>
#include <string_view>
#include <tuple>
#include <type_traits>
#include <utility>
#include <vector>

namespace mamba::util
{
    /**
     * Return the string if the pointer is not null, otherwise a pointer to an empty string.
     */
    [[nodiscard]] auto raw_str_or_empty(const char* ptr) -> const char*;

    /**
     * Safe non utf-8 wrapping of <cctype> (see its doc).
     */
    [[nodiscard]] auto is_control(char c) -> bool;
    [[nodiscard]] auto is_control(wchar_t c) -> bool;
    [[nodiscard]] auto is_print(char c) -> bool;
    [[nodiscard]] auto is_print(wchar_t c) -> bool;
    [[nodiscard]] auto is_space(char c) -> bool;
    [[nodiscard]] auto is_space(wchar_t c) -> bool;
    [[nodiscard]] auto is_blank(char c) -> bool;
    [[nodiscard]] auto is_blank(wchar_t c) -> bool;
    [[nodiscard]] auto is_graphic(char c) -> bool;
    [[nodiscard]] auto is_graphic(wchar_t c) -> bool;
    [[nodiscard]] auto is_digit(char c) -> bool;
    [[nodiscard]] auto is_digit(wchar_t c) -> bool;
    [[nodiscard]] auto is_punct(char c) -> bool;
    [[nodiscard]] auto is_punct(wchar_t c) -> bool;
    [[nodiscard]] auto is_alpha(char c) -> bool;
    [[nodiscard]] auto is_alpha(wchar_t c) -> bool;
    [[nodiscard]] auto is_alphanum(char c) -> bool;
    [[nodiscard]] auto is_alphanum(wchar_t c) -> bool;
    [[nodiscard]] auto is_lower(char c) -> bool;
    [[nodiscard]] auto is_lower(wchar_t c) -> bool;
    [[nodiscard]] auto is_upper(char c) -> bool;
    [[nodiscard]] auto is_upper(wchar_t c) -> bool;
    [[nodiscard]] auto to_lower(char c) -> char;
    [[nodiscard]] auto to_lower(wchar_t c) -> wchar_t;

    [[nodiscard]] auto to_lower(std::string_view str) -> std::string;
    [[nodiscard]] auto to_lower(std::wstring_view str) -> std::wstring;
    // The use of a template here serves to exclude the overload for const Char*
    template <typename Char>
    [[nodiscard]] auto to_lower(std::basic_string<Char>&& str) -> std::basic_string<Char>;
    extern template std::string to_lower(std::string&& str);
    extern template std::wstring to_lower(std::wstring&& str);

    [[nodiscard]] auto to_upper(char c) -> char;
    [[nodiscard]] auto to_upper(wchar_t c) -> wchar_t;
    [[nodiscard]] auto to_upper(std::string_view str) -> std::string;
    [[nodiscard]] auto to_upper(std::wstring_view str) -> std::wstring;
    // The use of a template here serves to exclude the overload for const Char*
    template <typename Char>
    [[nodiscard]] auto to_upper(std::basic_string<Char>&& str) -> std::basic_string<Char>;
    extern template std::string to_upper(std::string&& str);
    extern template std::wstring to_upper(std::wstring&& str);

    [[nodiscard]] auto starts_with(std::string_view str, std::string_view prefix) -> bool;
    [[nodiscard]] auto starts_with(std::string_view str, std::string_view::value_type c) -> bool;

    [[nodiscard]] auto ends_with(std::string_view str, std::string_view suffix) -> bool;
    [[nodiscard]] auto ends_with(std::string_view str, std::string_view::value_type c) -> bool;

    [[nodiscard]] auto contains(std::string_view str, std::string_view sub_str) -> bool;
    [[nodiscard]] auto contains(std::string_view str, char c) -> bool;
    [[nodiscard]] auto contains(char c1, char c2) -> bool;

    /**
     * Check if any of the strings starts with the prefix.
     */
    template <typename StrRange>
    [[nodiscard]] auto any_starts_with(const StrRange& strs, std::string_view prefix) -> bool;
    template <typename StrRange>
    [[nodiscard]] auto any_starts_with(const StrRange& strs, std::wstring_view prefix) -> bool;

    /**
     * Check if the string starts with any of the prefix.
     */
    template <typename StrRange>
    [[nodiscard]] auto starts_with_any(std::string_view str, const StrRange& prefix) -> bool;
    template <typename StrRange>
    [[nodiscard]] auto starts_with_any(std::wstring_view str, const StrRange& prefix) -> bool;

    /**
     * Return a view to the input without the prefix if present.
     */
    [[nodiscard]] auto remove_prefix(std::string_view str, std::string_view prefix)
        -> std::string_view;
    [[nodiscard]] auto remove_prefix(std::string_view str, std::string_view::value_type c)
        -> std::string_view;

    /**
     * Return a view to prefix if present, and a view to the rest of the input.
     */
    [[nodiscard]] auto split_prefix(std::string_view str, std::string_view prefix)
        -> std::array<std::string_view, 2>;
    [[nodiscard]] auto split_prefix(std::string_view str, std::string_view::value_type c)
        -> std::array<std::string_view, 2>;

    /**
     * Return a view to the input without the suffix if present.
     */
    [[nodiscard]] auto remove_suffix(std::string_view str, std::string_view suffix)
        -> std::string_view;
    [[nodiscard]] auto remove_suffix(std::string_view str, std::string_view::value_type c)
        -> std::string_view;

    /**
     * Return a view to the head of the input, and a view to suffix if present.
     */
    [[nodiscard]] auto split_suffix(std::string_view str, std::string_view suffix)
        -> std::array<std::string_view, 2>;
    [[nodiscard]] auto split_suffix(std::string_view str, std::string_view::value_type c)
        -> std::array<std::string_view, 2>;

    [[nodiscard]] auto lstrip(std::string_view input, char c) -> std::string_view;
    [[nodiscard]] auto lstrip(std::wstring_view input, wchar_t c) -> std::wstring_view;
    [[nodiscard]] auto lstrip(std::string_view input, std::string_view chars) -> std::string_view;
    [[nodiscard]] auto lstrip(std::wstring_view input, std::wstring_view chars) -> std::wstring_view;
    [[nodiscard]] auto lstrip(std::string_view input) -> std::string_view;
    [[nodiscard]] auto lstrip(std::wstring_view input) -> std::wstring_view;

    [[nodiscard]] auto lstrip_parts(std::string_view input, char c)
        -> std::array<std::string_view, 2>;
    [[nodiscard]] auto lstrip_parts(std::wstring_view input, wchar_t c)
        -> std::array<std::wstring_view, 2>;
    [[nodiscard]] auto lstrip_parts(std::string_view input, std::string_view chars)
        -> std::array<std::string_view, 2>;
    [[nodiscard]] auto lstrip_parts(std::wstring_view input, std::wstring_view chars)
        -> std::array<std::wstring_view, 2>;

    template <typename UnaryFunc>
    [[nodiscard]] auto lstrip_if(std::string_view input, UnaryFunc should_strip) -> std::string_view;
    template <typename UnaryFunc>
    [[nodiscard]] auto lstrip_if(std::wstring_view input, UnaryFunc should_strip)
        -> std::wstring_view;

    template <typename UnaryFunc>
    [[nodiscard]] auto lstrip_if_parts(std::string_view input, UnaryFunc should_strip)
        -> std::array<std::string_view, 2>;
    template <typename UnaryFunc>
    [[nodiscard]] auto lstrip_if_parts(std::wstring_view input, UnaryFunc should_strip)
        -> std::array<std::wstring_view, 2>;

    [[nodiscard]] auto rstrip(std::string_view input, char c) -> std::string_view;
    [[nodiscard]] auto rstrip(std::wstring_view input, wchar_t c) -> std::wstring_view;
    [[nodiscard]] auto rstrip(std::string_view input, std::string_view chars) -> std::string_view;
    [[nodiscard]] auto rstrip(std::wstring_view input, std::wstring_view chars) -> std::wstring_view;
    [[nodiscard]] auto rstrip(std::string_view input) -> std::string_view;
    [[nodiscard]] auto rstrip(std::wstring_view input) -> std::wstring_view;

    [[nodiscard]] auto rstrip_parts(std::string_view input, char c)
        -> std::array<std::string_view, 2>;
    [[nodiscard]] auto rstrip_parts(std::wstring_view input, wchar_t c)
        -> std::array<std::wstring_view, 2>;
    [[nodiscard]] auto rstrip_parts(std::string_view input, std::string_view chars)
        -> std::array<std::string_view, 2>;
    [[nodiscard]] auto rstrip_parts(std::wstring_view input, std::wstring_view chars)
        -> std::array<std::wstring_view, 2>;

    template <typename UnaryFunc>
    [[nodiscard]] auto rstrip_if(std::string_view input, UnaryFunc should_strip) -> std::string_view;
    template <typename UnaryFunc>
    [[nodiscard]] auto rstrip_if(std::wstring_view input, UnaryFunc should_strip)
        -> std::wstring_view;

    template <typename UnaryFunc>
    [[nodiscard]] auto rstrip_if_parts(std::string_view input, UnaryFunc should_strip)
        -> std::array<std::string_view, 2>;
    template <typename UnaryFunc>
    [[nodiscard]] auto rstrip_if_parts(std::wstring_view input, UnaryFunc should_strip)
        -> std::array<std::wstring_view, 2>;

    [[nodiscard]] auto strip(std::string_view input, char c) -> std::string_view;
    [[nodiscard]] auto strip(std::wstring_view input, wchar_t c) -> std::wstring_view;
    [[nodiscard]] auto strip(std::string_view input, std::string_view chars) -> std::string_view;
    [[nodiscard]] auto strip(std::wstring_view input, std::wstring_view chars) -> std::wstring_view;
    [[nodiscard]] auto strip(std::string_view input) -> std::string_view;
    [[nodiscard]] auto strip(std::wstring_view input) -> std::wstring_view;

    /**
     * Dedicated implementation for inplace stripping of `std::string` to avoid copies
     */
    void inplace_strip(std::string& input);

    [[nodiscard]] auto strip_parts(std::string_view input, char c) -> std::array<std::string_view, 3>;
    [[nodiscard]] auto strip_parts(std::wstring_view input, wchar_t c)
        -> std::array<std::wstring_view, 3>;
    [[nodiscard]] auto strip_parts(std::string_view input, std::string_view chars)
        -> std::array<std::string_view, 3>;
    [[nodiscard]] auto strip_parts(std::wstring_view input, std::wstring_view chars)
        -> std::array<std::wstring_view, 3>;

    template <typename UnaryFunc>
    [[nodiscard]] auto strip_if(std::string_view input, UnaryFunc should_strip) -> std::string_view;
    template <typename UnaryFunc>
    [[nodiscard]] auto strip_if(std::wstring_view input, UnaryFunc should_strip)
        -> std::wstring_view;

    template <typename UnaryFunc>
    [[nodiscard]] auto strip_if_parts(std::string_view input, UnaryFunc should_strip)
        -> std::array<std::string_view, 3>;
    template <typename UnaryFunc>
    [[nodiscard]] auto strip_if_parts(std::wstring_view input, UnaryFunc should_strip)
        -> std::array<std::wstring_view, 3>;

    [[nodiscard]] auto split_once(std::string_view str, char sep)
        -> std::tuple<std::string_view, std::optional<std::string_view>>;
    [[nodiscard]] auto split_once(std::string_view str, std::string_view sep)
        -> std::tuple<std::string_view, std::optional<std::string_view>>;

    [[nodiscard]] auto rsplit_once(std::string_view str, char sep)
        -> std::tuple<std::optional<std::string_view>, std::string_view>;
    [[nodiscard]] auto rsplit_once(std::string_view str, std::string_view sep)
        -> std::tuple<std::optional<std::string_view>, std::string_view>;

    [[nodiscard]] auto split_once_on_any(std::string_view str, std::string_view many_seps)
        -> std::tuple<std::string_view, std::optional<std::string_view>>;
    template <std::size_t N>
    [[nodiscard]] auto split_once_on_any(std::string_view str, std::array<char, N> many_seps)
        -> std::tuple<std::string_view, std::optional<std::string_view>>;

    [[nodiscard]] auto rsplit_once_on_any(std::string_view str, std::string_view many_seps)
        -> std::tuple<std::optional<std::string_view>, std::string_view>;
    template <std::size_t N>
    [[nodiscard]] auto rsplit_once_on_any(std::string_view str, std::array<char, N> many_seps)
        -> std::tuple<std::optional<std::string_view>, std::string_view>;

    [[nodiscard]] auto
    split(std::string_view input, std::string_view sep, std::size_t max_split = SIZE_MAX)
        -> std::vector<std::string>;
    [[nodiscard]] auto split(std::string_view input, char sep, std::size_t max_split = SIZE_MAX)
        -> std::vector<std::string>;
    [[nodiscard]] auto
    split(std::wstring_view input, std::wstring_view sep, std::size_t max_split = SIZE_MAX)
        -> std::vector<std::wstring>;
    [[nodiscard]] auto split(std::wstring_view input, wchar_t sep, std::size_t max_split = SIZE_MAX)
        -> std::vector<std::wstring>;

    [[nodiscard]] auto
    rsplit(std::string_view input, std::string_view sep, std::size_t max_split = SIZE_MAX)
        -> std::vector<std::string>;
    [[nodiscard]] auto rsplit(std::string_view input, char sep, std::size_t max_split = SIZE_MAX)
        -> std::vector<std::string>;
    [[nodiscard]] auto
    rsplit(std::wstring_view input, std::wstring_view sep, std::size_t max_split = SIZE_MAX)
        -> std::vector<std::wstring>;
    [[nodiscard]] auto rsplit(std::wstring_view input, wchar_t sep, std::size_t max_split = SIZE_MAX)
        -> std::vector<std::wstring>;

    /**
     * Concatenate string while removing the suffix of the first that may be prefix of second.
     *
     * Comparison are done as if comparing elements in a split given by @p sep.
     * For instance "private/channel" and "channel/label/foo" with separator "/"
     * would return "private/channel/label/foo", but "private/chan" and "channel/label/foo"
     * would return the "private/chan/channel/label/foo".
     */
    [[nodiscard]] auto concat_dedup_splits(std::string_view str1, std::string_view str2, char sep)
        -> std::string;
    [[nodiscard]] auto
    concat_dedup_splits(std::string_view str1, std::string_view str2, std::string_view sep)
        -> std::string;

    void replace_all(std::string& data, std::string_view search, std::string_view replace);
    void replace_all(std::wstring& data, std::wstring_view search, std::wstring_view replace);

    namespace detail
    {
        struct PlusEqual
        {
            template <typename T, typename U>
            auto operator()(T& left, const U& right);
        };
    }

    /**
     * Execute the function @p func on each element of a join iteration.
     *
     * The join iteration of an iterator pair (@p first, @p last) with a separator @p sep is
     * defined by iterating through the ``n`` elements of the iterator pair, interleaving the
     * separator in between the elements (thus appearing ``n-1`` times).
     */
    template <typename InputIt, typename UnaryFunction, typename Value>
    auto join_for_each(InputIt first, InputIt last, UnaryFunction func, const Value& sep)
        -> UnaryFunction;

    /**
     * Concatenate the elements of the container @p container by interleaving a separator.
     *
     * Joining is done by successively joining (using the provided @p joiner) the aggregate with
     * element of the container and the separator, such that the separator only appears
     * in-between two elements of the range.
     *
     * @see join_for_each
     */
    template <class Range, class Value, class Joiner = detail::PlusEqual>
    auto join(const Value& sep, const Range& container, Joiner joiner = detail::PlusEqual{}) ->
        typename Range::value_type;

    /**
     * Execute the function @p func on each element of a tuncated join iteration.
     *
     * The join iteration of an iterator pair (@p first, @p last) with a separator @p sep
     * and a truncation symbol @p etc is define by the join iteration of either all the elements
     * in the iterator pair if they are less than @p threshold, a limited number of elements, with
     * middle elements represented by @p etc.
     * defined by iterating through the ``n`` elements of the iterator pair, interleaving the
     * separator in between the elements (thus appearing ``n-1`` times).
     *
     * @param first The iterator pointing to the beginning of the range of elements to join.
     * @param last The iterator pointing to past the end of the range of elements to join.
     * @param func The unary function to apply to all elements (separation and truncation included).
     * @param sep The separator used in between elements.
     * @param etc The value used to represent the truncation of the elements.
     * @param threshold Distance between the iterator pair beyond which truncation is preformed.
     * @param show Number of elements to keep at the begining/end when truncation is preformed.
     *
     * @see join_for_each
     */
    template <typename InputIt, typename UnaryFunction, typename Value>
    auto join_trunc_for_each(
        InputIt first,
        InputIt last,
        UnaryFunction func,
        const Value& sep,
        const Value& etc,
        std::size_t threshold = 5,
        std::pair<std::size_t, std::size_t> show = { 2, 1 }
    ) -> UnaryFunction;

    /**
     * Join elements of a range, with possible truncation.
     *
     * @param range Elements to join.
     * @param sep The separator used in between elements.
     * @param etc The value used to represent the truncation of the elements.
     * @param threshold Distance between the iterator pair beyond which truncation is preformed.
     * @param show Number of elements to keep at the begining/end when truncation is preformed.
     *
     * @see join_trunc_for_each
     * @see join
     */
    template <typename Range, typename Joiner = detail::PlusEqual>
    auto join_trunc(
        const Range& range,
        std::string_view sep = ", ",
        std::string_view etc = "...",
        std::size_t threshold = 5,
        std::pair<std::size_t, std::size_t> show = { 2, 1 },
        Joiner joiner = detail::PlusEqual{}
    ) -> typename Range::value_type;
    ;

    /************************
     *  Implementation misc *
     ************************/

    inline auto raw_str_or_empty(const char* ptr) -> const char*
    {
        return ptr ? ptr : "";
    }

    /********************************************
     *  Implementation of start_with functions  *
     ********************************************/

    template <typename StrRange, typename Char>
    auto any_starts_with(const StrRange& strs, std::basic_string_view<Char> prefix) -> bool
    {
        return std::any_of(
            strs.cbegin(),
            strs.cend(),
            [&prefix](const auto& s) { return starts_with(s, prefix); }
        );
    }

    template <typename StrRange>
    auto any_starts_with(const StrRange& strs, std::string_view prefix) -> bool
    {
        return any_starts_with<StrRange, decltype(prefix)::value_type>(strs, prefix);
    }

    template <typename StrRange>
    auto any_starts_with(const StrRange& strs, std::wstring_view prefix) -> bool
    {
        return any_starts_with<StrRange, decltype(prefix)::value_type>(strs, prefix);
    }

    extern template bool any_starts_with(const std::vector<std::string>&, std::string_view);
    extern template bool any_starts_with(const std::vector<std::string_view>&, std::string_view);

    template <typename StrRange, typename Char>
    auto starts_with_any(std::basic_string_view<Char> str, const StrRange& prefix) -> bool
    {
        return std::any_of(
            prefix.cbegin(),
            prefix.cend(),
            [&str](const auto& p) { return starts_with(str, p); }
        );
    }

    template <typename StrRange>
    auto starts_with_any(std::string_view str, const StrRange& prefix) -> bool
    {
        return starts_with_any<StrRange, char>(str, prefix);
    }

    template <typename StrRange>
    auto starts_with_any(std::wstring_view str, const StrRange& prefix) -> bool
    {
        return starts_with_any<StrRange, wchar_t>(str, prefix);
    }

    extern template bool starts_with_any(std::string_view, const std::vector<std::string>&);
    extern template bool starts_with_any(std::string_view, const std::vector<std::string_view>&);

    /***************************************
     *  Implementation of strip functions  *
     ***************************************/

    namespace detail
    {
        template <typename Char, typename UnaryFunc>
        auto lstrip_if_parts_impl(std::basic_string_view<Char> input, UnaryFunc should_strip)
            -> std::array<std::basic_string_view<Char>, 2>
        {
            const auto start_iter = std::find_if(
                input.cbegin(),
                input.cend(),
                [&should_strip](Char c) -> bool { return !should_strip(c); }
            );
            const auto start_idx = static_cast<std::size_t>(start_iter - input.cbegin());
            return { input.substr(0, start_idx), input.substr(start_idx) };
        }
    }

    template <typename UnaryFunc>
    auto lstrip_if(std::string_view input, UnaryFunc should_strip) -> std::string_view
    {
        return lstrip_if_parts(input, std::move(should_strip))[1];
    }

    template <typename UnaryFunc>
    auto lstrip_if(std::wstring_view input, UnaryFunc should_strip) -> std::wstring_view
    {
        return lstrip_if_parts(input, std::move(should_strip))[1];
    }

    template <typename UnaryFunc>
    auto lstrip_if_parts(std::string_view input, UnaryFunc should_strip)
        -> std::array<std::string_view, 2>
    {
        return detail::lstrip_if_parts_impl(input, std::move(should_strip));
    }

    template <typename UnaryFunc>
    auto lstrip_if_parts(std::wstring_view input, UnaryFunc should_strip)
        -> std::array<std::wstring_view, 2>
    {
        return detail::lstrip_if_parts_impl(input, std::move(should_strip));
    }

    namespace detail
    {
        template <typename Char, typename UnaryFunc>
        auto rstrip_if_parts_impl(std::basic_string_view<Char> input, UnaryFunc should_strip)
            -> std::array<std::basic_string_view<Char>, 2>
        {
            const auto rstart_iter = std::find_if(
                input.crbegin(),
                input.crend(),
                [&should_strip](Char c) -> bool { return !should_strip(c); }
            );
            const auto past_end_idx = static_cast<std::size_t>(input.crend() - rstart_iter);
            return { input.substr(0, past_end_idx), input.substr(past_end_idx) };
        }
    }

    template <typename UnaryFunc>
    auto rstrip_if(std::string_view input, UnaryFunc should_strip) -> std::string_view
    {
        return rstrip_if_parts(input, std::move(should_strip))[0];
    }

    template <typename UnaryFunc>
    auto rstrip_if(std::wstring_view input, UnaryFunc should_strip) -> std::wstring_view
    {
        return rstrip_if_parts(input, std::move(should_strip))[0];
    }

    template <typename UnaryFunc>
    auto rstrip_if_parts(std::string_view input, UnaryFunc should_strip)
        -> std::array<std::string_view, 2>
    {
        return detail::rstrip_if_parts_impl(input, std::move(should_strip));
    }

    template <typename UnaryFunc>
    auto rstrip_if_parts(std::wstring_view input, UnaryFunc should_strip)
        -> std::array<std::wstring_view, 2>
    {
        return detail::rstrip_if_parts_impl(input, std::move(should_strip));
    }

    namespace detail
    {
        template <typename Char, typename UnaryFunc>
        auto strip_if_parts_impl(std::basic_string_view<Char> input, UnaryFunc should_strip)
            -> std::array<std::basic_string_view<Char>, 3>
        {
            const auto [head, not_head] = lstrip_if_parts(input, should_strip);
            const auto [body, tail] = rstrip_if_parts(not_head, std::move(should_strip));
            return { head, body, tail };
        }
    }

    template <typename UnaryFunc>
    auto strip_if(std::string_view input, UnaryFunc should_strip) -> std::string_view
    {
        return strip_if_parts(input, std::move(should_strip))[1];
    }

    template <typename UnaryFunc>
    auto strip_if(std::wstring_view input, UnaryFunc should_strip) -> std::wstring_view
    {
        return strip_if_parts(input, std::move(should_strip))[1];
    }

    template <typename UnaryFunc>
    auto strip_if_parts(std::string_view input, UnaryFunc should_strip)
        -> std::array<std::string_view, 3>
    {
        return detail::strip_if_parts_impl(input, std::move(should_strip));
    }

    template <typename UnaryFunc>
    auto strip_if_parts(std::wstring_view input, UnaryFunc should_strip)
        -> std::array<std::wstring_view, 3>
    {
        return detail::strip_if_parts_impl(input, std::move(should_strip));
    }

    template <std::size_t N>
    auto split_once_on_any(std::string_view str, std::array<char, N> many_seps)
        -> std::tuple<std::string_view, std::optional<std::string_view>>
    {
        return split_once_on_any(str, std::string_view{ many_seps.data(), many_seps.size() });
    }

    template <std::size_t N>
    auto rsplit_once_on_any(std::string_view str, std::array<char, N> many_seps)
        -> std::tuple<std::optional<std::string_view>, std::string_view>
    {
        return rsplit_once_on_any(str, std::string_view{ many_seps.data(), many_seps.size() });
    }

    /**************************************
     *  Implementation of join functions  *
     **************************************/

    namespace detail
    {
        template <typename T, typename U>
        auto PlusEqual::operator()(T& left, const U& right)
        {
            left += right;
        }

        template <class T, class = void>
        struct has_reserve : std::false_type
        {
        };

        template <class T>
        struct has_reserve<T, std::void_t<decltype(std::declval<T>().reserve(std::size_t()))>>
            : std::true_type
        {
        };

        template <typename T>
        inline constexpr bool has_reserve_v = has_reserve<T>::value;

        auto length(const char* s) -> std::size_t;
        auto length(const wchar_t* s) -> std::size_t;
        auto length(const char c) -> std::size_t;
        auto length(const wchar_t c) -> std::size_t;

        template <class T>
        auto length(const T& s) -> std::size_t
        {
            return s.length();
        }
    }

    // TODO(C++20) Use ``std::ranges::join_view`` (or ``std::ranges::join``)
    template <typename InputIt, typename UnaryFunction, typename Value>
    auto join_for_each(InputIt first, InputIt last, UnaryFunction func, const Value& sep)
        -> UnaryFunction
    {
        if (first < last)
        {
            func(*(first++));
            for (; first < last; ++first)
            {
                func(sep);
                func(*first);
            }
        }
        return func;
    }

    template <class Range, class Value, class Joiner>
    auto join(const Value& sep, const Range& container, Joiner joiner) -> typename Range::value_type
    {
        using Result = typename Range::value_type;
        Result out{};
        if constexpr (detail::has_reserve_v<Result>)
        {
            std::size_t final_size = 0;
            auto inc_size = [&final_size](const auto& val) { final_size += detail::length(val); };
            join_for_each(container.begin(), container.end(), inc_size, sep);
            out.reserve(final_size);
        }
        auto out_joiner = [&](auto&& val) { joiner(out, std::forward<decltype(val)>(val)); };
        join_for_each(container.begin(), container.end(), out_joiner, sep);
        return out;
    }

    /********************************************
     *  Implementation of join_trunc functions  *
     ********************************************/

    // TODO(C++20) Take an input range and return a range
    template <typename InputIt, typename UnaryFunction, typename Value>
    auto join_trunc_for_each(
        InputIt first,
        InputIt last,
        UnaryFunction func,
        const Value& sep,
        const Value& etc,
        std::size_t threshold,
        std::pair<std::size_t, std::size_t> show
    ) -> UnaryFunction
    {
        if (std::cmp_less_equal(last - first, threshold))
        {
            return join_for_each(first, last, std::move(func), sep);
        }

        // Working around non-assignable function types, such as lambda with references.
        auto join_for_each_func = [&func](auto f, auto l, auto val)
        {
            if constexpr (std::is_assignable_v<UnaryFunction, UnaryFunction>)
            {
                func = join_for_each(f, l, std::move(func), val);
            }
            else
            {
                join_for_each(f, l, func, val);
            }
        };

        const auto [show_head, show_tail] = show;
        if (show_head > 0)
        {
            join_for_each_func(first, first + static_cast<std::ptrdiff_t>(show_head), sep);
            func(sep);
        }
        func(etc);
        if (show_tail)
        {
            func(sep);
            join_for_each_func(last - static_cast<std::ptrdiff_t>(show_tail), last, sep);
        }
        return func;
    }

    template <typename Range, typename Joiner>
    auto join_trunc(
        const Range& range,
        std::string_view sep,
        std::string_view etc,
        std::size_t threshold,
        std::pair<std::size_t, std::size_t> show,
        Joiner joiner
    ) -> typename Range::value_type
    {
        using Result = typename Range::value_type;
        Result out{};
        if constexpr (detail::has_reserve_v<Result>)
        {
            std::size_t final_size = 0;
            auto inc_size = [&final_size](const auto& val) { final_size += detail::length(val); };
            join_trunc_for_each(range.begin(), range.end(), inc_size, sep, etc, threshold, show);
            out.reserve(final_size);
        }

        auto out_joiner = [&](auto&& val) { joiner(out, std::forward<decltype(val)>(val)); };
        join_trunc_for_each(range.begin(), range.end(), out_joiner, sep, etc, threshold, show);
        return out;
    }

    template <typename... Args>
    auto concat(const Args&... args) -> std::string
    {
        std::string result;
        result.reserve((detail::length(args) + ...));
        ((result += args), ...);
        return result;
    }
}

#endif