-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils_string.hpp
1004 lines (912 loc) · 36 KB
/
utils_string.hpp
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
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#ifndef UTILS_STRING_HPP
#define UTILS_STRING_HPP
#include "utils_exceptions.hpp"
#include "utils_compiler.hpp"
#include "utils_traits.hpp"
#include <string>
#include <string_view>
#include <locale>
#include <codecvt>
#include <cstring>
#include <algorithm>
#include <functional>
#include <sstream>
#include <vector>
#include <array>
namespace utils::string {
/**
* Crate a facet for the current locale.
*/
template<class CharT> ATTR_MAYBE_UNUSED
static const auto& facet = std::use_facet<std::ctype<CharT>>(std::locale());
ATTR_MAYBE_UNUSED
static const auto& facetch = facet<char>;
ATTR_MAYBE_UNUSED
static const auto& facetwch = facet<wchar_t>;
/**
* \brief Struct to wrap a char in a std::string_view,
* and act as a string_view in all other cases.
*/
template<typename _CharT, typename _Traits = std::char_traits<_CharT>>
struct basic_string_view : public std::basic_string_view<_CharT, _Traits> {
public:
using traits_type = _Traits;
using value_type = _CharT;
using pointer = const _CharT*;
using const_pointer = const _CharT*;
using reference = const _CharT&;
using const_reference = const _CharT&;
using const_iterator = const _CharT*;
using iterator = const_iterator;
using const_reverse_iterator = std::reverse_iterator<const_iterator>;
using reverse_iterator = const_reverse_iterator;
using size_type = size_t;
using difference_type = ptrdiff_t;
static inline constexpr size_type npos = size_type(-1);
constexpr basic_string_view(const _CharT& __ch) noexcept
: std::basic_string_view<_CharT>{&__ch, 1} { }
constexpr basic_string_view(const basic_string_view<_CharT>& __str) noexcept
: std::basic_string_view<_CharT>{__str} { }
constexpr basic_string_view() noexcept
: std::basic_string_view<_CharT>{} { }
constexpr basic_string_view(const std::basic_string_view<_CharT>& sv) noexcept
: std::basic_string_view<_CharT>{sv} { }
constexpr basic_string_view(const _CharT* __str) noexcept
: std::basic_string_view<_CharT>{__str} { }
constexpr basic_string_view(const _CharT* __str, size_t __len) noexcept
: std::basic_string_view<_CharT>{__str, __len} { }
constexpr basic_string_view(const std::basic_string<_CharT>& __str) noexcept
: std::basic_string_view<_CharT>{__str} { }
template <size_t N>
constexpr basic_string_view(const _CharT (&__str)[N])
: std::basic_string_view<_CharT>{__str, N - 1} { }
constexpr basic_string_view&
operator=(const basic_string_view&) noexcept = default;
template <size_t... N>
static inline constexpr std::array<std::basic_string_view<_CharT>, sizeof...(N)>
array_from_args(const char (&...__strs)[N]) {
return {{basic_string_view{__strs}...}};
}
};
using string_view = basic_string_view<char>;
using wstring_view = basic_string_view<wchar_t>;
/**
* \brief Test if the given string \p str contains the other string \p part.
*
* \param str
* The std::string to check.
* \param part
* The std::string to look for.
* \param start
* The starting offset to start looking. (default: 0)
* \return Returns an `std:optional<size_t>` holding the offset from
* `str.begin()` if the part was found, or nothing if not.
*/
ATTR_MAYBE_UNUSED ATTR_NODISCARD
static inline constexpr utils::traits::found_t
contains(const std::string_view str,
const utils::string::string_view part,
const size_t start = 0)
{
if (const size_t pos = str.find(part, start);
pos != std::string::npos)
{
return { pos };
}
return std::nullopt;
}
/**
* \brief Test if the given string \p str contains the other string \p part.
*
* \param str
* The std::string to check.
* \param part
* The std::string to look for.
* \param start
* The starting offset to start looking. (default: 0)
* \return Returns an `std:optional<size_t>` holding the offset from
* `str.begin()` if the part was found, or nothing if not.
*/
ATTR_MAYBE_UNUSED ATTR_NODISCARD
static inline constexpr utils::traits::found_t
rcontains(const std::string_view str,
const utils::string::string_view part,
const size_t start = std::string_view::npos)
{
if (const size_t pos = str.rfind(part, start);
pos != std::string::npos)
{
return { pos };
}
return std::nullopt;
}
/**
* \brief Check if \p str starts with the given string.
*
* \param str
* The std::string to check.
* \param start
* The string to look for.
* \return Returns true if str[0:start.size()] == start.
*/
ATTR_MAYBE_UNUSED ATTR_NODISCARD
static inline constexpr bool starts_with(const utils::string::string_view str,
const utils::string::string_view start)
{
#if UTILS_CPP_LANG_CHECK(UTILS_CPP_VERSION_20)
return str.starts_with(start);
#else
return !start.size() || str.size() < start.size()
? false
: std::equal(start.begin(), start.end(),
str.begin());
#endif
}
/**
* \brief Check if \p str ends with the given string.
*
* \param str
* The std::string to check.
* \param end
* The char* to look for.
* \return Returns true if str[-end.size():] == end.
*/
ATTR_MAYBE_UNUSED ATTR_NODISCARD
static inline constexpr bool ends_with(const utils::string::string_view str,
const utils::string::string_view end)
{
#if UTILS_CPP_LANG_CHECK(UTILS_CPP_VERSION_20)
return str.ends_with(end);
#else
return !end.size() || str.size() < end.size()
? false
: std::equal(end.begin(), end.end(),
str.end() - static_cast<std::string::difference_type>(end.size()));
#endif
}
/** \brief Trim whitespace from the start of the given string (in-place).
*
* \param s
* A reference to the string to perform the operation.
*/
ATTR_MAYBE_UNUSED
static inline void ltrim(std::string& s) {
s.erase(s.begin(), std::find_if(s.begin(), s.end(),
[](const char& c) { return !facetch.is(facetch.space, c); }));
}
/** \brief Trim whitespace from the end of the given string (in-place).
*
* \param s
* A reference to the string to perform the operation.
*/
ATTR_MAYBE_UNUSED
static inline void rtrim(std::string& s) {
s.erase(std::find_if(s.rbegin(), s.rend(),
[](const char& c) { return !facetch.is(facetch.space, c); }).base(), s.end());
}
/** \brief Trim whitespace from both start and end of the given string (in-place).
*
* \param s
* A reference to the string to perform the operation.
*/
ATTR_MAYBE_UNUSED
static inline void trim(std::string& s) {
ltrim(s);
rtrim(s);
}
/** \brief Trim whitespace from both start and end of the given string (copy).
*
* \param s
* A string to perform the operation.
*/
ATTR_MAYBE_UNUSED ATTR_NODISCARD
static inline std::string trimmed(std::string s) {
trim(s);
return s;
}
/**
* \brief Erase everything starting from \p erasefrom (inclusive; in place).
*
* \param str
* The string to erase characters from.
* \param erasefrom
* The substring to look for and start erasing from.
*/
ATTR_MAYBE_UNUSED
static inline void erase_from(std::string& str, const utils::string::string_view erasefrom) {
if (const auto found = utils::string::contains(str, erasefrom)) {
str = str.substr(0, found.value());
}
}
/**
* \brief Erase everything starting from the beginning of the string
* to \p eraseto (exclusive; in place).
*
* \param str
* The string to erase characters from.
* \param eraseto
* The substring to look for and erase to.
*/
ATTR_MAYBE_UNUSED
static inline void erase_to(std::string& str, const utils::string::string_view eraseto) {
if (const auto found = utils::string::contains(str, eraseto)) {
str = str.substr(found.value());
}
}
/**
* \brief Erase everything starting from \p erasefrom (inclusive; copy).
*
* \param str
* The string to erase characters from.
* \param erasefrom
* The substring to look for and start erasing from.
* \return Returns a copy where the appropriate parts were erased.
*/
ATTR_MAYBE_UNUSED ATTR_NODISCARD
static inline std::string erased_from(std::string str, const utils::string::string_view erasefrom) {
erase_from(str, erasefrom);
return str;
}
/**
* \brief Erase everything starting from the beginning of the string
* to \p eraseto (exclusive; copy).
*
* \param str
* The string to erase characters from.
* \param eraseto
* The substring to look for and erase to.
*/
ATTR_MAYBE_UNUSED ATTR_NODISCARD
static inline std::string erased_to(std::string str, const utils::string::string_view eraseto) {
erase_to(str, eraseto);
return str;
}
/** \brief Transform the string contents to uppercase (within the current locale) (in-place).
*
* \param str
* A reference to the string to perform the operation.
*/
template<typename CharT> ATTR_MAYBE_UNUSED
static inline void to_upper(std::basic_string<CharT>& str) {
std::transform(str.begin(), str.end(), str.begin(),
[](const CharT& ch) { return facet<CharT>.toupper(ch); });
}
/** \brief Transform the string contents to uppercase (within the current locale) (copying).
*
* \param str
* A copy of the string to perform the operation.
*/
template<typename CharT> ATTR_MAYBE_UNUSED ATTR_NODISCARD
static inline std::basic_string<CharT> to_uppercase(std::basic_string<CharT> str) {
to_upper(str);
return str;
}
/** \brief Transform the string contents to lowercase (within the current locale) (in-place).
*
* \param str
* A reference to the string to perform the operation.
*/
template<typename CharT> ATTR_MAYBE_UNUSED
static inline void to_lower(std::basic_string<CharT>& str) {
std::transform(str.begin(), str.end(), str.begin(),
[](const CharT& ch) { return facet<CharT>.tolower(ch); });
}
/** \brief Transform the string contents to lowercase (within the current locale) (copying).
*
* \param str
* A copy of the string to perform the operation.
*/
template<typename CharT> ATTR_MAYBE_UNUSED ATTR_NODISCARD
static inline std::basic_string<CharT> to_lowercase(std::basic_string<CharT> str) {
to_lower(str);
return str;
}
/** \brief Erase all consecutive occurrences of the given char
* within the given string (in-place).
* Only erase consecutive and repeating chars, keep unique.
*
* \param str
* A reference to the string to replace the character.
* \param ch
* The characters to replace.
*/
ATTR_MAYBE_UNUSED
static inline void erase_consecutive(std::string& str, const char ch) {
str.erase(std::unique(str.begin(), str.end(),
[&](const char lhs, const char rhs) {
return (lhs == ch) && (lhs == rhs);
}
), str.end());
}
/** \brief Replace all occurrences of from with to in the given
* std::string str.
*
* \param str
* A reference to the string to replace a substring.
* \param from
* A reference to a string to replace.
* \param to
* A reference to a string to replace with.
*/
ATTR_MAYBE_UNUSED
static inline void replace_all(std::string& str,
const utils::string::string_view from,
const utils::string::string_view to = "")
{
if (HEDLEY_UNLIKELY(from.size() == 0)) return;
utils::traits::found_t found = 0ull;
while ((found = utils::string::contains(str, from, *found))) {
str.replace(*found, from.size(), to);
*found += to.size();
}
}
/** \brief Erase all occurrences of erase in the given std::string str.
*
* \param str
* A reference to the string to erase a string.
* \param erase
* A string to erase.
*/
ATTR_MAYBE_UNUSED
static inline void erase_all(std::string& str,
const utils::string::string_view erase)
{
if (HEDLEY_UNLIKELY(erase.size() == 0)) return;
utils::traits::found_t found = 0ull;
while ((found = utils::string::contains(str, erase, *found))) {
str.erase(*found, erase.length());
}
}
/**
* \brief Convert the given string to an std::wstring.
*
* \param str
* The string to convert.
* \return
* Returns the converted data as an std::wstring.
*/
ATTR_MAYBE_UNUSED ATTR_NODISCARD
static inline std::wstring to_wstring(const std::string_view str) {
return std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>, wchar_t>().from_bytes(str.data());
}
/**
* \brief Convert the given wide string to an std::string.
*
* \param str
* The character buffer to convert.
* \return
* Returns the converted data as an std::string.
*/
ATTR_MAYBE_UNUSED ATTR_NODISCARD
static inline std::string to_string(const std::wstring_view& str) {
return std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>, wchar_t>().to_bytes(str.data());
}
/**
* \brief Surround the given string with the \p quote char. (in-place)
* If it is already surrounded, do nothing.
*
* \param str
* The string to surround.
* \param quote
* The character to surround the string with.
*/
ATTR_MAYBE_UNUSED
static inline void quote(std::string& str, const
utils::string::string_view quote='\"')
{
if (HEDLEY_UNLIKELY(utils::string::starts_with(str, quote)
&& utils::string::ends_with(str, quote)))
{
return;
}
std::string out(str.size() + 2 * quote.size(), '\0');
str = out.replace(0, quote.size(), quote)
.replace(quote.size(), str.size(), str)
.replace(out.size() - quote.size(), quote.size(), quote);
}
/**
* \brief Surround the given string with the \p quote char. (copy)
* If it is already surrounded, do nothing.
*
* \param str
* The string to surround.
* \param quote
* The character to surround the string with.
*/
ATTR_MAYBE_UNUSED ATTR_NODISCARD
static inline std::string quoted(std::string str,
const utils::string::string_view quote='\"')
{
utils::string::quote(str, quote);
return str;
}
/**
* \brief Invoke \p callback on each substring in \p s that has been quoted
* with char/string \p quote.
*
* e.g. 'str1', 'str2' => extract every string between 2 `'`
* => performs callback("str1"); callback("str2");
*
* \param callback
* The function to call with an std::string_view argument.
* \param s
* The string to look for quoted strings in.
* \param quote
* The char the strings are quoted in.
*/
template<typename F> ATTR_MAYBE_UNUSED
static void for_each_quoted(F&& callback,
const std::string_view s,
const utils::string::string_view quote='\"')
{
static_assert(utils::traits::is_invocable_v<F, const std::string_view>,
"utils::string::for_each_quoted: Callable function required.");
const size_t len = s.length();
if (HEDLEY_UNLIKELY(len < quote.size() * 2)) return;
utils::traits::found_t found_start = 0, found_end = 0;
do {
if ((found_start = utils::string::contains(s, quote, *found_start))) {
if ((found_end = utils::string::contains(s, quote, ++(*found_start)))) {
std::invoke<F>(std::forward<F>(callback),
std::string_view{s.data() + *found_start, *found_end - *found_start});
found_start = *found_end + quote.size();
}
}
} while (found_start && *found_start < len);
}
/**
* \brief Extract a vector of string views from the given string \p s that have
* been quoted with char \p quote.
*
* e.g. 'str1', 'str2' => extract every string between 2 `'`
* => results in v = [ "str1", "str2" ]
*
* \param s
* The string to look for quoted strings in.
* \param quote
* The char the strings are quoted in.
* \return A vector with string views for each quoted string from \p s.
*/
ATTR_MAYBE_UNUSED
static auto extract_quoted(const std::string_view s,
const utils::string::string_view quote='\"')
{
std::vector<std::string_view> extracted;
utils::string::for_each_quoted([&](const std::string_view sv){
extracted.emplace_back(sv);
}, s, quote);
return extracted;
}
/**
* \brief Join the strings in \p v with the string \p join_with between them.
*
* \param v
* The list of strings to join.
* \param join_with
* The string to join with.
* \return Returns one string containing all the strings in \p v appended
* to eachother, joined by \p join_with.
*/
ATTR_MAYBE_UNUSED ATTR_NODISCARD
static std::string join(const std::vector<std::string>& v,
const utils::string::string_view join_with = ",")
{
std::string joined;
const auto end = v.end();
if (auto start = v.begin(); start != end) {
// Calculate final size beforehand
size_t size = (*start).size() + ((v.size() - 1) * join_with.size());
while (++start != end) {
size += (*start).size();
}
joined.reserve(size);
// Append strings and delimiter
start = v.begin();
joined += *start;
while (++start != end) {
joined += join_with;
joined += *start;
}
}
return joined;
}
/**
* \brief Join the chars in \p v with the char \p join_with between them.
*
* \param v
* The list of chars to join (a compatible char type).
* \param join_with
* The char to join with. If \p join_with == '\0', just concatenate all chars.
* \return Returns one string containing all the chars in \p v appended
* to eachother, joined by \p join_with.
*/
template<
class CharT,
typename std::enable_if_t< std::is_same<CharT, char>::value
|| std::is_same<CharT, int8_t>::value
|| std::is_same<CharT, uint8_t>::value
|| std::is_same<CharT, wchar_t>::value
|| std::is_same<CharT, char16_t>::value
|| std::is_same<CharT, char32_t>::value,
int> = 0
> ATTR_MAYBE_UNUSED ATTR_NODISCARD
static std::string join(const std::vector<CharT>& v,
const char join_with = '\0')
{
std::string joined;
const auto end = v.end();
if (auto start = v.begin(); start != end) {
if (join_with == '\0') {
joined.reserve(v.size());
joined.assign(v.begin(), v.end());
} else {
joined.reserve(v.size() * 2 - 1);
joined.push_back(*start);
while(++start != end) {
joined.push_back(join_with);
joined.push_back(*start);
}
}
}
return joined;
}
/**
* \brief Split the given string \p s into parts delimited by \p delim,
* and invoke \p callback on each of them.
*
* \param callback
* The function to call with each splitted std::string_view.
* \param s
* The string to split.
* \param delim
* The string delimiter.
* \param max_splits
* The maximum amount of splits to make.
* `-1` split and call on all delimiters
* `0` don't split: call will be made with the original string \p s
* `1` call will be made with the first splitted element,
* and the rest of the string as second call.
*/
template<typename F> ATTR_MAYBE_UNUSED
static void for_each_splitted(F&& callback,
const std::string_view s,
const utils::string::string_view delim = ',',
int max_splits = -1)
{
static_assert(utils::traits::is_invocable_v<F, const std::string_view>,
"utils::string::for_each_splitted: Callable function required.");
utils::traits::found_t found = 0ull;
size_t prev_end = 0ull;
if (max_splits != 0 && (found = utils::string::contains(s, delim, 0))) {
std::invoke<F>(std::forward<F>(callback),
std::string_view{s.data(), *found});
max_splits--;
prev_end = *found + delim.size();
while (max_splits != 0 && (found = utils::string::contains(s, delim, prev_end))) {
std::invoke<F>(std::forward<F>(callback),
std::string_view{s.data() + prev_end, *found - prev_end});
max_splits--;
prev_end = *found + delim.size();
}
}
if (prev_end < s.size() + delim.size() || max_splits == 0) {
std::invoke<F>(std::forward<F>(callback),
std::string_view{s.data() + prev_end, s.size() - prev_end});
}
}
/**
* \brief Split the given string \p s into parts delimited by \p delim,
* and put them in \p v as std::string_views.
*
* \param s
* The string to split.
* \param delim
* The string delimiter.
* \param max_splits
* The maximum amount of splits to make.
* `-1` split on all delimiters
* `0` don't split: vector will contain the original string \p s
* `1` vector has the first splitted element,
* and the rest of the string as second element.
* \return A vector with string views for each splitted string from \p s.
*/
ATTR_MAYBE_UNUSED
static auto split(const std::string_view s,
const utils::string::string_view delim = ',',
int max_splits = -1)
{
std::vector<std::string_view> splitted;
utils::string::for_each_splitted([&](const std::string_view sv){
splitted.emplace_back(sv);
}, s, delim, max_splits);
return splitted;
}
/**
* \brief Split the given string \p s into parts delimited by \p delim,
* but start from the end, i.e. the first time \p callback is invoked,
* it will be on the right-most splitted substring.
*
* \param callback
* The function to call with each splitted std::string_view.
* \param s
* The string to split.
* \param delim
* The string delimiter.
* \param max_splits
* The maximum amount of splits to make.
* `-1` split and call on all delimiters
* `0` don't split: call will be made with the original string \p s
* `1` call will be made with the first splitted element
* (starting from the right), and the rest of the string (left)
* as second call.
*/
template<typename F> ATTR_MAYBE_UNUSED
static void for_each_rsplitted(F&& callback,
const std::string_view s,
const utils::string::string_view delim = ',',
int max_splits = -1)
{
static_assert(utils::traits::is_invocable_v<F, const std::string_view>,
"utils::string::for_each_rsplitted: Callable function required.");
utils::traits::found_t found = 0ull;
const size_t delim_len = delim.size();
size_t prev_start = s.size() - delim_len;
while (max_splits != 0 && (found = utils::string::rcontains(s, delim, prev_start))) {
const size_t size = prev_start - *found;
std::invoke<F>(std::forward<F>(callback),
std::string_view{ s.data() + *found + delim_len, size });
max_splits--;
prev_start = *found;
if (*found < delim_len) break;
prev_start -= delim_len;
}
if ((!found && prev_start+delim_len > 0) || (found && max_splits == 0)) {
std::invoke<F>(std::forward<F>(callback),
std::string_view{ s.data(), prev_start + delim_len });
} else if (found && *found < delim_len) {
std::invoke<F>(std::forward<F>(callback),
std::string_view{ s.data(), prev_start });
} else if (prev_start > delim_len) {
std::invoke<F>(std::forward<F>(callback), s);
}
}
/**
* \brief Split the given string \p s into parts delimited by \p delim,
* but start from the end.
*
* \param s
* The string to split.
* \param delim
* The string delimiter.
* \param max_splits
* The maximum amount of splits to make.
* `-1` split on all delimiters
* `0` don't split: vector will contain the original string \p s
* `1` vector has the first splitted element (at its end),
* and the rest of the string as previous elements.
* \return A vector with string views for each splitted string from \p s.
*/
ATTR_MAYBE_UNUSED
static auto rsplit(const std::string_view s,
const utils::string::string_view delim = ',',
int max_splits = -1)
{
std::vector<std::string_view> splitted;
utils::string::for_each_rsplitted([&](const std::string_view sv){
splitted.emplace_back(sv);
}, s, delim, max_splits);
std::reverse(splitted.begin(), splitted.end());
return splitted;
}
/**
* \brief Format the given args into the format string.
* If no arguments are given, the given format string
* will be returned as-is.
*
* \tparam ...Type
* Variable argument list of params to expand in the format string.
* \param format
* The format string to expand.
* \param args
* The args tro fill in.
* \return
* Returns the format expanded with the args.
*/
template<typename ... Type> ATTR_MAYBE_UNUSED ATTR_NODISCARD
static std::string format(const std::string_view format, Type&& ...args) {
if constexpr (sizeof...(Type) != 0) {
const size_t size = std::snprintf(nullptr, 0, format.data(), args...); // Extra space for '\0'
std::string out; out.resize(size);
std::snprintf(out.data(), size + 1, format.data(), args...);
return out;
} else {
return std::string(format);
}
}
/**
* \brief Static buffer with all valid base64 chars.
*/
static constexpr std::string_view _base64_chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz"
"0123456789+/";
/**
* \brief Check if given char is a valid base64 char (without `=` pad).
*
* \param c
* The character to check.
* \return
* Returns true if _base64_chars.find(c) != npos
*/
ATTR_MAYBE_UNUSED ATTR_NODISCARD
static inline bool is_base64(uint8_t c) {
return (facetch.is(facetch.alnum, char(c)) || (c == '+') || (c == '/'));
}
/**
* \brief Check if given buffer contains a valid base64
* encoded string.
*
* \param buffer
* The buffer to check.
* \param length
* The length of the buffer.
* \return
* Returns true if buffer contains a valid base64 encoded string.
*/
ATTR_MAYBE_UNUSED ATTR_NODISCARD
static bool is_base64(const uint8_t *buffer, size_t length) {
if (HEDLEY_UNLIKELY(length % 4)) return false;
const uint8_t *end = --buffer + length + 1;
while (++buffer != end) {
if (HEDLEY_UNLIKELY(*buffer == '=')) {
break;
} else if (!utils::string::is_base64(*buffer)) {
return false;
}
}
switch (end - buffer) {
case 0:
return true;
case 1:
return buffer[0] == '=';
case 2:
return buffer[0] == '=' && buffer[1] == '=';
default:
return false;
}
}
/**
* \brief Check if given string contains is a valid base64
* encoded string.
*
* \param str
* The string to check.
* \return
* Returns true if string contains a valid base64 encoded string.
*/
ATTR_MAYBE_UNUSED ATTR_NODISCARD
static inline bool is_base64(const std::string_view str) {
return utils::string::is_base64(reinterpret_cast<const uint8_t*>(str.data()), str.length());
}
/**
* \brief Encode the given buffer with base64.
*
* \param buffer
* The buffer to encode.
* \param length
* The length of the buffer to encode.
* \return Return an std::string containing the encoded buffer.
*/
ATTR_MAYBE_UNUSED ATTR_NODISCARD
static std::string to_base64(const uint8_t *buffer, size_t length) {
std::string encoded;
const uint8_t mod = length % 3;
encoded.reserve(((length / 3) + (mod > 0)) * 4);
uint32_t temp;
for (const uint8_t *end = --buffer + length - mod; buffer != end;) {
temp = (uint32_t(buffer[1]) << 16) | (uint32_t(buffer[2]) << 8) | (buffer[3]);
buffer += 3;
encoded.push_back(utils::string::_base64_chars[(temp & 0x00FC0000) >> 18]);
encoded.push_back(utils::string::_base64_chars[(temp & 0x0003F000) >> 12]);
encoded.push_back(utils::string::_base64_chars[(temp & 0x00000FC0) >> 6]);
encoded.push_back(utils::string::_base64_chars[(temp & 0x0000003F)]);
}
switch (mod) {
case 1:
temp = (uint32_t(buffer[1]) << 16);
encoded.push_back(utils::string::_base64_chars[(temp & 0x00FC0000) >> 18]);
encoded.push_back(utils::string::_base64_chars[(temp & 0x0003F000) >> 12]);
encoded.push_back('=');
encoded.push_back('=');
break;
case 2:
temp = (uint32_t(buffer[1]) << 16) | (uint32_t(buffer[2]) << 8);
encoded.push_back(utils::string::_base64_chars[(temp & 0x00FC0000) >> 18]);
encoded.push_back(utils::string::_base64_chars[(temp & 0x0003F000) >> 12]);
encoded.push_back(utils::string::_base64_chars[(temp & 0x00000FC0) >> 6]);
encoded.push_back('=');
break;
}
return encoded;
}
/**
* \brief Encode the given string with base64.
*
* \param str
* The string to encode.
* \return Return an std::string containing the encoded string.
*/
ATTR_MAYBE_UNUSED ATTR_NODISCARD
static inline std::string to_base64(const std::string_view str) {
return utils::string::to_base64(reinterpret_cast<const uint8_t*>(str.data()), str.length());
}
/**
* \brief Decode the given buffer with base64.
*
* \param buffer
* The buffer to decode.
* \param length
* The length of the buffer to decode.
* \return Return an std::string containing the decoded buffer.
*/
ATTR_MAYBE_UNUSED ATTR_NODISCARD
static std::string from_base64(const uint8_t *buffer, size_t length) {
if (HEDLEY_UNLIKELY(length % 4)) {
throw utils::exceptions::ConversionException("utils::string::base64_decode (invalid size)");
}
const uint8_t *const buffer_end = buffer + length;
size_t decoded_length = (length / 4) * 3;
if (HEDLEY_LIKELY(length)) {
decoded_length -= (*(buffer_end - 2) == '=') + (*(buffer_end - 1) == '=');
}
std::string decoded;
decoded.resize(decoded_length);
char *iter = decoded.data() - 1;
while (buffer < buffer_end) {
int32_t temp = 0;
for (uint8_t i = 0; i < 4; ++i, ++buffer) {
temp <<= 6;
if (*buffer >= 'A' && *buffer <= 'Z') {
temp |= *buffer - 'A';
} else if (*buffer >= 'a' && *buffer <= 'z') {
temp |= *buffer - 'a' + 26;
} else if (*buffer >= '0' && *buffer <= '9') {
temp |= *buffer - '0' + 2 * 26;
} else if (*buffer == '+') {
temp |= 2 * 26 + 10;
} else if (*buffer == '/') {
temp |= 2 * 26 + 10 + 1;
} else if (*buffer == '=') {
switch (buffer_end - buffer) {
case 1:
*++iter = char((temp >> 16) & 0xFF);
*++iter = char((temp >> 8) & 0xFF);
return decoded;
case 2:
*++iter = char((temp >> 10) & 0xFF);
return decoded;
default:
throw utils::exceptions::ConversionException("utils::string::base64_decode (invalid padding)");
}
} else {
throw utils::exceptions::ConversionException("utils::string::base64_decode (invalid character)");
}
}
*++iter = char((temp >> 16) & 0xFF);
*++iter = char((temp >> 8) & 0xFF);
*++iter = char((temp ) & 0xFF);
}
return decoded;
}
/**
* \brief Decode the given string with base64.
*
* \param str
* The string to decode.
* \return Return an std::string containing the decoded string.
*/
ATTR_MAYBE_UNUSED ATTR_NODISCARD
static inline std::string from_base64(const std::string_view str) {
return utils::string::from_base64(reinterpret_cast<const uint8_t*>(str.data()), str.length());