-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstring_split.hpp
952 lines (932 loc) · 40.8 KB
/
string_split.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
/*=============================================================================
Copyright (C) 2016-2018 yumetodo
Distributed under the Boost Software License, Version 1.0.
(See http://www.boost.org/LICENSE_1_0.txt)
=============================================================================*/
#pragma once
#include <string>
#include <vector>
#include <array>
#include <type_traits>
#include <limits>
#include <stdexcept>
#if defined(_MSC_VER)
# if _MSC_VER >= 1910 && _MSVC_LANG >= 201703
# define STRING_SPLIT_HAS_CXX17_STRING_VIEW 1
# endif
#elif defined(__clang__)
# if __clang_major__ >= 4 && __cplusplus >= 201703
# define STRING_SPLIT_HAS_CXX17_STRING_VIEW 1
# endif
#elif defined(__GNUC__)
# if (__GNUC__ > 7 || (__GNUC__ == 7 && __GNUC_MINOR__ >= 1)) && __cplusplus >= 201703
# define STRING_SPLIT_HAS_CXX17_STRING_VIEW 1
# endif
#endif
#ifdef STRING_SPLIT_HAS_CXX17_STRING_VIEW
# include <string_view>
#endif
//Windows.hなどからくる min max マクロがdefineされるのを防ぐ
#ifndef NOMINMAX
#define NOMINMAX
#endif
#ifdef max
#undef max
#endif
#ifdef min
#undef min
#endif
namespace detail {
using std::vector;
template<typename CharType> using b_str = std::basic_string<CharType>;
#ifdef STRING_SPLIT_HAS_CXX17_STRING_VIEW
template<typename CharType> using b_str_view = std::basic_string_view<CharType>;
#endif
using std::is_same;
using std::size_t;
using std::is_void;
template<bool con, typename T>
using enable_if_t = typename std::enable_if<con, T>::type;
template<bool con, typename T1, typename T2>
using conditional_t = typename std::conditional<con, T1, T2>::type;
template<typename T>
using remove_pointer_t = typename std::remove_pointer<T>::type;
template<typename T>
using remove_reference_t = typename std::remove_reference<T>::type;
template<typename T>
using remove_cv_t = typename std::remove_cv<T>::type;
namespace type_traits {
//
// is_char_type
//
template<typename T> struct is_char_type : public std::false_type {};
template<typename T> struct is_char_type<T const> : public type_traits::is_char_type<T> {};
template<typename T> struct is_char_type<T volatile> : public type_traits::is_char_type<T> {};
template<typename T> struct is_char_type<T const volatile> : public type_traits::is_char_type<T> {};
template<> struct is_char_type<char> : public std::true_type {};
template<> struct is_char_type<wchar_t> : public std::true_type {};
template<> struct is_char_type<char16_t> : public std::true_type {};
template<> struct is_char_type<char32_t> : public std::true_type {};
//
// is_c_str
//
template<typename T>
struct is_c_str : std::integral_constant<bool, std::is_pointer<T>::value && is_char_type<remove_pointer_t<T>>::value> {};
//
// is_stl_string
//
template<typename T>
struct is_stl_string : conditional_t<std::is_reference<T>::value, is_stl_string<remove_reference_t<T>>, std::false_type> {};
template<typename T> struct is_stl_string<T const> : is_stl_string<T> {};
template<typename T> struct is_stl_string<T volatile> : is_stl_string<T> {};
template<typename T> struct is_stl_string<T const volatile> : is_stl_string<T> {};
template<typename CharType>
struct is_stl_string<b_str<CharType>> : std::integral_constant<bool, is_char_type<remove_cv_t<CharType>>::value> {};
namespace detail {
template <class T>
struct is_reference_wrapper : std::false_type {};
template <class U>
struct is_reference_wrapper<std::reference_wrapper<U>> : std::true_type {};
template<class T>
struct invoke_impl {
template<class F, class... Args>
static auto call(F&& f, Args&&... args)
-> decltype(std::forward<F>(f)(std::forward<Args>(args)...));
};
template<class B, class MT>
struct invoke_impl<MT B::*> {
template<class T, class Td = typename std::decay<T>::type,
class = typename std::enable_if<std::is_base_of<B, Td>::value>::type
>
static auto get(T&& t)->T&&;
template<class T, class Td = typename std::decay<T>::type,
class = typename std::enable_if<is_reference_wrapper<Td>::value>::type
>
static auto get(T&& t) -> decltype(t.get());
template<class T, class Td = typename std::decay<T>::type,
class = typename std::enable_if<!std::is_base_of<B, Td>::value>::type,
class = typename std::enable_if<!is_reference_wrapper<Td>::value>::type
>
static auto get(T&& t) -> decltype(*std::forward<T>(t));
template<class T, class... Args, class MT1,
class = typename std::enable_if<std::is_function<MT1>::value>::type
>
static auto call(MT1 B::*pmf, T&& t, Args&&... args)
-> decltype((invoke_impl::get(std::forward<T>(t)).*pmf)(std::forward<Args>(args)...));
template<class T>
static auto call(MT B::*pmd, T&& t)
-> decltype(invoke_impl::get(std::forward<T>(t)).*pmd);
};
template<class F, class... Args, class Fd = typename std::decay<F>::type>
auto INVOKE(F&& f, Args&&... args)
-> decltype(invoke_impl<Fd>::call(std::forward<F>(f), std::forward<Args>(args)...));
} // namespace detail
// Conforming C++14 implementation (is also a valid C++11 implementation):
namespace detail {
template <typename AlwaysVoid, typename, typename...>
struct invoke_result { };
template <typename F, typename...Args>
struct invoke_result<decltype(void(detail::INVOKE(std::declval<F>(), std::declval<Args>()...))),
F, Args...> {
using type = decltype(detail::INVOKE(std::declval<F>(), std::declval<Args>()...));
};
} // namespace detail
template <class F, class... ArgTypes>
struct invoke_result : detail::invoke_result<void, F, ArgTypes...> {};
template< class F, class... ArgTypes>
using invoke_result_t = typename invoke_result<F, ArgTypes...>::type;
template<class...> struct conjunction : std::true_type { };
template<class B1> struct conjunction<B1> : B1 { };
template<class B1, class... Bn>
struct conjunction<B1, Bn...>
: conditional_t<bool(B1::value), conjunction<Bn...>, B1> {};
template<class...> struct disjunction : std::false_type { };
template<class B1> struct disjunction<B1> : B1 { };
template<class B1, class... Bn>
struct disjunction<B1, Bn...>
: conditional_t<bool(B1::value), B1, disjunction<Bn...>> { };
template <bool B>
using bool_constant = std::integral_constant<bool, B>;
template<class B>
struct negation : bool_constant<!bool(B::value)> { };
#ifdef STRING_SPLIT_HAS_CXX17_STRING_VIEW
//
// is_stl_string_view
//
template<typename T>
struct is_stl_string_view : conditional_t<std::is_reference<T>::value, is_stl_string_view<remove_reference_t<T>>, std::false_type> {};
template<typename T> struct is_stl_string_view<T const> : is_stl_string_view<T> {};
template<typename T> struct is_stl_string_view<T volatile> : is_stl_string_view<T> {};
template<typename T> struct is_stl_string_view<T const volatile> : is_stl_string_view<T> {};
template<typename CharType>
struct is_stl_string_view<b_str_view<CharType>> : std::integral_constant<bool, is_char_type<remove_cv_t<CharType>>::value> {};
template<typename T> constexpr bool is_stl_string_view_v = is_stl_string_view<T>::value;
//
// is_stl_string_or_stl_string_view
//
template<typename T> struct is_stl_string_or_stl_string_view : std::disjunction<
type_traits::is_stl_string<T>,
type_traits::is_stl_string_view<T>
> {};
//
// contract_str_type
//
template<typename StrType, typename CharType> struct contract_str_type : std::conjunction<
type_traits::is_stl_string_or_stl_string_view<StrType>,
is_same<CharType, typename StrType::value_type>
> {};
template<typename StrType, typename CharType> constexpr bool contract_str_type_v = contract_str_type<StrType, CharType>::value;
#endif
}
#ifdef STRING_SPLIT_HAS_CXX17_STRING_VIEW
using std::conjunction_v;
#endif
using type_traits::invoke_result_t;
struct subroutine_base {};
template<typename T> struct is_subroutine : std::is_base_of<subroutine_base, T> {};
struct get_front : subroutine_base {};
struct get_back : subroutine_base {};
struct split_at_first_back : subroutine_base {};
struct split_at_first : subroutine_base {
constexpr get_front front() const { return{}; }
constexpr split_at_first_back back() const { return{}; }
};
struct split_at_last_front : subroutine_base {};
struct split_at_last : subroutine_base {
constexpr split_at_last_front front() const { return{}; }
constexpr get_back back() const { return{}; }
};
template<typename Subroutine, typename DelimType, typename CharType>
struct split_helper_subroutine {
using char_type = CharType;
DelimType delim;
};
template<typename DelimType, typename CharType>
struct split_helper_get_back {
using char_type = CharType;
DelimType delim;
};
template<typename DelimType, typename CharType>
struct split_helper_split_at_first_back {
using char_type = CharType;
DelimType delim;
};
template<typename DelimType, typename CharType>
struct split_helper_split_at_first {
using char_type = CharType;
DelimType delim;
};
template<typename DelimType, typename CharType>
struct split_helper_split_at_last_front {
using char_type = CharType;
DelimType delim;
};
template<typename DelimType, typename CharType>
struct split_helper_split_at_last {
using char_type = CharType;
DelimType delim;
};
template<typename DelimType, bool is_single_char, bool is_c_str, bool is_stl_string>
struct split_helper_index;
template<typename CharType>
struct split_helper_index<CharType, true, false, false> {
using char_type = CharType;
CharType delim; size_t index;
};
template<typename CharType>
struct split_helper_get_front : split_helper_index<CharType, true, false, false> {
constexpr split_helper_get_front(CharType d) : split_helper_index<CharType, true, false, false>({d, 0}) {}
};
template<typename CStr>
struct split_helper_index<CStr, false, true, false> {
using char_type = remove_cv_t<remove_pointer_t<CStr>>;
const char_type* delim; size_t index;
};
template<typename StlString>
struct split_helper_index<StlString, false, false, true> {
using char_type = typename StlString::value_type;
b_str<char_type> delim; size_t index;
};
#ifdef STRING_SPLIT_HAS_CXX17_STRING_VIEW
template<typename StlStringView>
struct split_helper_index<StlStringView, false, false, false> {
using char_type = typename StlStringView::value_type;
b_str_view<char_type> delim; size_t index;
};
#endif
template<typename DelimType, typename FuncType, bool is_single_char, bool is_c_str, bool is_stl_string>
struct split_helper_conv_func;
template<typename CharType, typename FuncType>
struct split_helper_conv_func<CharType, FuncType, true, false, false> {
using char_type = CharType;
CharType delim; FuncType f;
};
template<typename CStr, typename FuncType>
struct split_helper_conv_func<CStr, FuncType, false, true, false> {
using char_type = remove_cv_t<remove_pointer_t<CStr>>;
const char_type* delim; FuncType f;
};
template<typename StlString, typename FuncType>
struct split_helper_conv_func<StlString, FuncType, false, false, true> {
using char_type = typename StlString::value_type;
b_str<char_type> delim; FuncType f;
};
#ifdef STRING_SPLIT_HAS_CXX17_STRING_VIEW
template<typename StlStringView, typename FuncType>
struct split_helper_conv_func<StlStringView, FuncType, false, false, false> {
using char_type = typename StlStringView::value_type;
b_str_view<char_type> delim; FuncType f;
};
#endif
template<typename DelimType, bool is_single_char, bool is_c_str, bool is_stl_string>
struct split_helper;
template<typename CharType>
struct split_helper<CharType, true, false, false> {
using char_type = CharType;
CharType delim;
constexpr split_helper_index<CharType, true, false, false> operator[](size_t n) const noexcept { return{ delim, n }; }
template<typename FuncType, enable_if_t<!is_subroutine<FuncType>::value, std::nullptr_t> = nullptr>
constexpr split_helper_conv_func<CharType, FuncType, true, false, false> operator>> (FuncType&& f) const { return{ delim, std::forward<FuncType>(f) }; }
constexpr split_helper_get_front<CharType> operator>>(get_front) const noexcept { return{ delim }; }
template<typename Subroutine, enable_if_t<is_subroutine<Subroutine>::value, std::nullptr_t> = nullptr>
constexpr split_helper_subroutine<Subroutine, CharType, CharType> operator>> (Subroutine) const noexcept { return{ delim }; }
};
template<typename CStr>
struct split_helper<CStr, false, true, false> {
using char_type = remove_cv_t<remove_pointer_t<CStr>>;
const char_type* delim;
constexpr split_helper_index<CStr, false, true, false> operator[](size_t n) const noexcept { return{ delim, n }; }
template<typename FuncType, enable_if_t<!is_subroutine<FuncType>::value, std::nullptr_t> = nullptr>
constexpr split_helper_conv_func<CStr, FuncType, false, true, false> operator >> (FuncType&& f) const { return{ delim, std::forward<FuncType>(f) }; }
constexpr split_helper_index<CStr, false, true, false> operator>>(get_front) const noexcept { return{ delim, 0 }; }
template<typename Subroutine, enable_if_t<is_subroutine<Subroutine>::value, std::nullptr_t> = nullptr>
constexpr split_helper_subroutine<Subroutine, CStr, char_type> operator>> (Subroutine) const noexcept { return{ delim }; }
};
template<typename StlString>
struct split_helper<StlString, false, false, true> {
using char_type = typename StlString::value_type;
b_str<char_type> delim;
split_helper_index<StlString, false, false, true> operator[](size_t n) const noexcept { return{ std::move(delim), n }; }
template<typename FuncType, enable_if_t<!is_subroutine<FuncType>::value, std::nullptr_t> = nullptr>
split_helper_conv_func<StlString, FuncType, false, false, true> operator >> (FuncType&& f) const { return{ std::move(delim), std::forward<FuncType>(f) }; }
split_helper_index<StlString, false, false, true> operator>>(get_front) const noexcept { return{ std::move(delim), 0 }; }
template<typename Subroutine, enable_if_t<is_subroutine<Subroutine>::value, std::nullptr_t> = nullptr>
split_helper_subroutine<Subroutine, StlString, char_type> operator>> (Subroutine) const noexcept { return{ std::move(delim) }; }
};
#ifdef STRING_SPLIT_HAS_CXX17_STRING_VIEW
template<typename StlStringView>
struct split_helper<StlStringView, false, false, false> {
using char_type = typename StlStringView::value_type;
b_str_view<char_type> delim;
split_helper_index<StlStringView, false, false, false> operator[](size_t n) const noexcept { return{ std::move(delim), n }; }
template<typename FuncType, enable_if_t<!is_subroutine<FuncType>::value, std::nullptr_t> = nullptr>
split_helper_conv_func<StlStringView, FuncType, false, false, false> operator >> (FuncType&& f) const { return{ std::move(delim), std::forward<FuncType>(f) }; }
split_helper_index<StlStringView, false, false, false> operator>>(get_front) const noexcept { return{ std::move(delim), 0 }; }
template<typename Subroutine, enable_if_t<is_subroutine<Subroutine>::value, std::nullptr_t> = nullptr>
split_helper_subroutine<Subroutine, StlStringView, char_type> operator>> (Subroutine) const noexcept { return{ std::move(delim) }; }
};
namespace type_traits {
//
//contract_str_type_and_delim_type_without_single_char
//
//is_c_str: true && is_stl_string: true => impossible!!!
//is_c_str: true && is_stl_string: false => const T*
//is_c_str: false && is_stl_string: true => std::basic_string
//is_c_str: false && is_stl_string: false => std::basic_string_view
template<typename StrType, typename DelimType, bool is_c_str, bool is_stl_string>
struct contract_str_type_and_delim_type_without_single_char : conjunction<
contract_str_type<StrType, typename split_helper<DelimType, false, is_c_str, is_stl_string>::char_type>,
bool_constant<!(is_c_str && is_stl_string)>
>
{};
template<typename StrType, typename DelimType, bool is_c_str, bool is_stl_string>
constexpr bool contract_str_type_and_delim_type_without_single_char_v = contract_str_type_and_delim_type_without_single_char<StrType, DelimType, is_c_str, is_stl_string>::value;
}
#endif
namespace type_traits {
template<typename CharType, typename DelimType, bool is_c_str, bool is_stl_string>
struct contract_delim_type_without_single_char : conjunction<
is_same<CharType, typename split_helper<DelimType, false, is_c_str, is_stl_string>::char_type>,
bool_constant<(is_c_str || is_stl_string)>
>
{};
}
//back()の時
#ifdef STRING_SPLIT_HAS_CXX17_STRING_VIEW
template<
typename StrType, typename DelimType, typename CharType,
enable_if_t<type_traits::contract_str_type_v<StrType, CharType>, std::nullptr_t> = nullptr
>
StrType operator| (const StrType& str, const split_helper_subroutine<get_back, DelimType, CharType>& info) noexcept(type_traits::is_stl_string_view_v<StrType>)
{
#else
template<typename CharType, typename DelimType>
b_str<CharType> operator| (const b_str<CharType>& str, const split_helper_subroutine<get_back, DelimType, CharType>& info)
{
using StrType = b_str<CharType>;
#endif
const auto pos = str.find_first_of(info.delim);
return (StrType::npos == pos) ? str : str.substr(str.find_last_of(info.delim) + 1);
}
//at_first().back()の時
#ifdef STRING_SPLIT_HAS_CXX17_STRING_VIEW
template<
typename StrType, typename DelimType, typename CharType,
enable_if_t<type_traits::contract_str_type_v<StrType, CharType>, std::nullptr_t> = nullptr
>
StrType operator| (const StrType& str, const split_helper_subroutine<split_at_first_back, DelimType, CharType>& info) noexcept(type_traits::is_stl_string_view_v<StrType>)
{
#else
template<typename CharType, typename DelimType>
b_str<CharType> operator| (const b_str<CharType>& str, const split_helper_subroutine<split_at_first_back, DelimType, CharType>& info)
{
using StrType = b_str<CharType>;
#endif
const auto pos = str.find_first_of(info.delim);
if (StrType::npos == pos) return {};
return str.substr(str.find_first_not_of(info.delim, pos));
}
//at_first()の時
#ifdef STRING_SPLIT_HAS_CXX17_STRING_VIEW
template<
typename StrType, typename DelimType, typename CharType,
enable_if_t<type_traits::contract_str_type_v<StrType, CharType>, std::nullptr_t> = nullptr
>
std::array<StrType, 2> operator| (const StrType& str, const split_helper_subroutine<split_at_first, DelimType, CharType>& info) noexcept(type_traits::is_stl_string_view_v<StrType>)
{
#else
template<typename CharType, typename DelimType>
std::array<b_str<CharType>, 2> operator| (const b_str<CharType>& str, const split_helper_subroutine<split_at_first, DelimType, CharType>& info)
{
using StrType = b_str<CharType>;
#endif
const auto pos = str.find_first_of(info.delim);
if (StrType::npos == pos) return{ { str, {} } };
return{ {str.substr(0, pos), str.substr(str.find_first_not_of(info.delim, pos))} };
}
//at_last().front()の時
#ifdef STRING_SPLIT_HAS_CXX17_STRING_VIEW
template<
typename StrType, typename DelimType, typename CharType,
enable_if_t<type_traits::contract_str_type_v<StrType, CharType>, std::nullptr_t> = nullptr
>
StrType operator| (const StrType& str, const split_helper_subroutine<split_at_last_front, DelimType, CharType>& info) noexcept(type_traits::is_stl_string_view_v<StrType>)
{
#else
template<typename CharType, typename DelimType>
b_str<CharType> operator| (const b_str<CharType>& str, const split_helper_subroutine<split_at_last_front, DelimType, CharType>& info)
{
using StrType = b_str<CharType>;
#endif
const auto pos = str.find_last_of(info.delim);
if (StrType::npos == pos) return {};
return str.substr(0, str.find_last_not_of(info.delim, pos) + 1);
}
//at_last()の時
#ifdef STRING_SPLIT_HAS_CXX17_STRING_VIEW
template<
typename StrType, typename DelimType, typename CharType,
enable_if_t<type_traits::contract_str_type_v<StrType, CharType>, std::nullptr_t> = nullptr
>
std::array<StrType, 2> operator| (const StrType& str, const split_helper_subroutine<split_at_last, DelimType, CharType>& info) noexcept(type_traits::is_stl_string_view_v<StrType>)
{
#else
template<typename CharType, typename DelimType>
std::array<b_str<CharType>, 2> operator| (const b_str<CharType>& str, const split_helper_subroutine<split_at_last, DelimType, CharType>& info)
{
using StrType = b_str<CharType>;
#endif
const auto pos = str.find_last_of(info.delim);
if (StrType::npos == pos) return{ { {}, str } };
return { { str.substr(0, str.find_last_not_of(info.delim, pos) + 1), str.substr(pos + 1) } };
}
//区切り文字1文字, operator[] or front()の時
#ifdef STRING_SPLIT_HAS_CXX17_STRING_VIEW
template<
typename StrType, typename CharType,
enable_if_t<type_traits::contract_str_type_v<StrType, CharType>, std::nullptr_t> = nullptr
>
StrType operator| (const StrType& str, const split_helper_index<CharType, true, false, false>& info)
{
#else
template<typename CharType>
b_str<CharType> operator| (const b_str<CharType>& str, const split_helper_index<CharType, true, false, false>& info)
{
using StrType = b_str<CharType>;
#endif
size_t pre = 0, pos = 0, i;
for (i = 0; i < info.index + 1; ++i) {
pre = pos;
pos = str.find_first_of(info.delim, pos);
if (StrType::npos == pos) break;
++pos;
}
if(i < info.index) throw std::out_of_range("index(" + std::to_string(info.index) + ") is too big.");
return str.substr(pre, pos - pre - 1);
}
#ifdef STRING_SPLIT_HAS_CXX17_STRING_VIEW
//区切り文字1文字, front()の時
template<typename CharType>
b_str_view<CharType> operator| (const b_str_view<CharType>& str, const split_helper_get_front<CharType>& info) noexcept
{
return str.substr(0, str.find_first_of(info.delim));
}
#endif
//区切り文字複数, operator[] or front()の時
#ifdef STRING_SPLIT_HAS_CXX17_STRING_VIEW
template<
typename StrType, typename DelimType, bool is_c_str, bool is_stl_string,
enable_if_t<type_traits::contract_str_type_and_delim_type_without_single_char_v<StrType, DelimType, is_c_str, is_stl_string>, std::nullptr_t> = nullptr
>
auto operator| (const StrType& str, const split_helper_index<DelimType, false, is_c_str, is_stl_string>& info) -> StrType
{
#else
template<typename CharType, typename DelimType, bool is_c_str, bool is_stl_string>
auto operator| (const b_str<CharType>& str, const split_helper_index<DelimType, false, is_c_str, is_stl_string>& info)
-> enable_if_t<
type_traits::contract_delim_type_without_single_char<CharType, DelimType, is_c_str, is_stl_string>::value,
b_str<CharType>
>
{
using StrType = b_str<CharType>;
#endif
size_t pre = 0, pos = 0, i;
for (i = 0; i < info.index + 1; ++i) {
if(pos) pre = pos = str.find_first_not_of(info.delim, pos);
pos = str.find_first_of(info.delim, pos);
if (StrType::npos == pos) break;
}
if (i < info.index) throw std::out_of_range("index(" + std::to_string(info.index) + ") is too big.");
return str.substr(pre, pos - pre);
}
//区切り文字1文字, has chain funcの時
#ifdef STRING_SPLIT_HAS_CXX17_STRING_VIEW
template<
typename StrType, typename CharType, typename FuncType,
enable_if_t<conjunction_v<
type_traits::contract_str_type<StrType, CharType>,
is_void<invoke_result_t<FuncType, StrType>>
>, std::nullptr_t> = nullptr
>
void operator| (const StrType& str, const split_helper_conv_func<CharType, FuncType, true, false, false>& info)
{
#else
template<typename CharType, typename FuncType>
auto operator| (const b_str<CharType>& str, const split_helper_conv_func<CharType, FuncType, true, false, false>& info)
-> enable_if_t<
is_void<invoke_result_t<FuncType, b_str<CharType>>>::value,
void
>
{
using StrType = b_str<CharType>;
#endif
size_t current = 0;
for (size_t found; (found = str.find_first_of(info.delim, current)) != StrType::npos; current = found + 1) {
info.f(str.substr(current, found - current));
}
info.f(str.substr(current, str.size() - current));
}
//区切り文字1文字, has chain convert funcの時
#ifdef STRING_SPLIT_HAS_CXX17_STRING_VIEW
template<
typename StrType, typename CharType, typename FuncType,
enable_if_t<conjunction_v<
type_traits::contract_str_type<StrType, CharType>,
type_traits::negation<is_void<invoke_result_t<FuncType, StrType>>>
>, std::nullptr_t> = nullptr
>
auto operator| (const StrType& str, const split_helper_conv_func<CharType, FuncType, true, false, false>& info)
-> vector<invoke_result_t<FuncType, StrType>>
{
#else
template<typename CharType, typename FuncType>
auto operator| (const b_str<CharType>& str, const split_helper_conv_func<CharType, FuncType, true, false, false>& info)
-> enable_if_t<
!is_void<invoke_result_t<FuncType, b_str<CharType>>>::value,
vector<invoke_result_t<FuncType, b_str<CharType>>>
>
{
using StrType = b_str<CharType>;
#endif
vector<invoke_result_t<FuncType, StrType>> re;
size_t current = 0;
for (size_t found; (found = str.find_first_of(info.delim, current)) != StrType::npos; current = found + 1) {
if (re.capacity() < re.size() + 1) re.reserve((std::numeric_limits<size_t>::max() / 2 < re.size()) ? std::numeric_limits<size_t>::max() : re.size() * 2);
re.emplace_back(info.f(str.substr(current, found - current)));
}
re.emplace_back(info.f(str.substr(current, str.size() - current)));
return re;
}
#ifdef STRING_SPLIT_HAS_CXX17_STRING_VIEW
template<typename CharType>
void vector_emplace_make_substr(vector<b_str_view<CharType>>& re, const b_str_view<CharType>& str, size_t pos, size_t n)
{
re.emplace_back(str.substr(pos, n));
}
#endif
template<typename CharType>
void vector_emplace_make_substr(vector<b_str<CharType>>& re, const b_str<CharType>& str, size_t pos, size_t n)
{
re.emplace_back(str, pos, n);
}
//区切り文字1文字の時
#ifdef STRING_SPLIT_HAS_CXX17_STRING_VIEW
template<
typename StrType, typename CharType,
enable_if_t<type_traits::contract_str_type_v<StrType, CharType>, std::nullptr_t> = nullptr
>
vector<StrType> operator| (const StrType& str, const split_helper<CharType, true, false, false>& info)
{
#else
template<typename CharType>
vector<b_str<CharType>> operator| (const b_str<CharType>& str, const split_helper<CharType, true, false, false>& info)
{
using StrType = b_str<CharType>;
#endif
vector<StrType> re;
size_t current = 0;
for (size_t found; (found = str.find_first_of(info.delim, current)) != StrType::npos; current = found + 1) {
if (re.capacity() < re.size() + 1) re.reserve((std::numeric_limits<size_t>::max() / 2 < re.size()) ? std::numeric_limits<size_t>::max() : re.size() * 2);
vector_emplace_make_substr(re, str, current, found - current);
}
vector_emplace_make_substr(re, str, current, str.size() - current);
return re;
}
//区切り文字複数, has chain funcの時
#ifdef STRING_SPLIT_HAS_CXX17_STRING_VIEW
template<
typename StrType, typename DelimType, typename FuncType,
bool is_c_str, bool is_stl_string,
enable_if_t<conjunction_v<
type_traits::contract_str_type_and_delim_type_without_single_char<StrType, DelimType, is_c_str, is_stl_string>,
is_void<invoke_result_t<FuncType, StrType>>
>, std::nullptr_t> = nullptr
>
void operator| (const StrType& str, const split_helper_conv_func<DelimType, FuncType, false, is_c_str, is_stl_string>& info)
{
#else
template<
typename CharType, typename DelimType, typename FuncType,
bool is_c_str, bool is_stl_string
>
auto operator| (const b_str<CharType>& str, const split_helper_conv_func<DelimType, FuncType, false, is_c_str, is_stl_string>& info)
-> enable_if_t<
type_traits::conjunction<
type_traits::contract_delim_type_without_single_char<CharType, DelimType, is_c_str, is_stl_string>,
is_void<invoke_result_t<FuncType, b_str<CharType>>>
>::value,
void
>
{
using StrType = b_str<CharType>;
#endif
size_t current = 0;
for (
size_t found = str.find_first_of(info.delim, current);
current != StrType::npos && found != StrType::npos;
current = str.find_first_not_of(info.delim, found + 1), found = str.find_first_of(info.delim, current)
) {
info.f(str.substr(current, found - current));
}
info.f(str.substr(current, str.size() - current));
}
//区切り文字複数, has chain convert funcの時
#ifdef STRING_SPLIT_HAS_CXX17_STRING_VIEW
template<
typename StrType, typename DelimType, typename FuncType,
bool is_c_str, bool is_stl_string,
enable_if_t<conjunction_v<
type_traits::contract_str_type_and_delim_type_without_single_char<StrType, DelimType, is_c_str, is_stl_string>,
type_traits::negation<is_void<invoke_result_t<FuncType, StrType>>>
>, std::nullptr_t> = nullptr
>
auto operator| (const StrType& str, const split_helper_conv_func<DelimType, FuncType, false, is_c_str, is_stl_string>& info)
-> vector<invoke_result_t<FuncType, StrType>>
{
#else
template<
typename CharType, typename DelimType, typename FuncType,
bool is_c_str, bool is_stl_string
>
auto operator| (const b_str<CharType>& str, const split_helper_conv_func<DelimType, FuncType, false, is_c_str, is_stl_string>& info)
-> enable_if_t<
type_traits::conjunction<
type_traits::contract_delim_type_without_single_char<CharType, DelimType, is_c_str, is_stl_string>,
type_traits::negation<is_void<invoke_result_t<FuncType, b_str<CharType>>>>
>::value,
vector<invoke_result_t<FuncType, b_str<CharType>>>
>
{
using StrType = b_str<CharType>;
#endif
vector<invoke_result_t<FuncType, StrType>> re;
size_t current = 0;
for (
size_t found = str.find_first_of(info.delim, current);
current != StrType::npos && found != StrType::npos;
current = str.find_first_not_of(info.delim, found + 1), found = str.find_first_of(info.delim, current)
) {
if (re.capacity() < re.size() + 1) re.reserve((std::numeric_limits<size_t>::max() / 2 < re.size()) ? std::numeric_limits<size_t>::max() : re.size() * 2);
re.emplace_back(info.f(str.substr(current, found - current)));
}
re.emplace_back(info.f(str.substr(current, str.size() - current)));
return re;
}
//区切り文字複数の時
#ifdef STRING_SPLIT_HAS_CXX17_STRING_VIEW
template<
typename StrType, typename DelimType, bool is_c_str, bool is_stl_string,
enable_if_t<type_traits::contract_str_type_and_delim_type_without_single_char_v<StrType, DelimType, is_c_str, is_stl_string>, std::nullptr_t> = nullptr
>
auto operator| (const StrType& str, const split_helper<DelimType, false, is_c_str, is_stl_string>& info) -> vector<StrType>
{
#else
template<typename CharType, typename DelimType, bool is_c_str, bool is_stl_string>
auto operator| (const b_str<CharType>& str, const split_helper<DelimType, false, is_c_str, is_stl_string>& info)
-> enable_if_t<
type_traits::contract_delim_type_without_single_char<CharType, DelimType, is_c_str, is_stl_string>::value,
vector<b_str<CharType>>
>
{
using StrType = b_str<CharType>;
#endif
vector<StrType> re;
size_t current = 0;
for (
size_t found = str.find_first_of(info.delim, current);
current != StrType::npos && found != StrType::npos;
current = str.find_first_not_of(info.delim, found + 1), found = str.find_first_of(info.delim, current)
) {
if (re.capacity() < re.size() + 1) re.reserve((std::numeric_limits<size_t>::max() / 2 < re.size()) ? std::numeric_limits<size_t>::max() : re.size() * 2);
vector_emplace_make_substr(re, str, current, found - current);
}
vector_emplace_make_substr(re, str, current, str.size() - current);
return re;
}
//back()の時
template<typename CharType, typename DelimType>
b_str<CharType> operator| (b_str<CharType>&& str, const split_helper_subroutine<get_back, DelimType, CharType>& info)
{
const auto pos = str.find_first_of(info.delim);
if (b_str<CharType>::npos != pos) str.erase(0, str.find_last_of(info.delim) + 1);
return std::move(str);
}
//at_first().back()の時
template<typename CharType, typename DelimType>
b_str<CharType> operator| (b_str<CharType>&& str, const split_helper_subroutine<split_at_first_back, DelimType, CharType>& info)
{
const std::size_t pos = str.find_first_of(info.delim);
if (b_str<CharType>::npos == pos) return {};
str.erase(0, str.find_first_not_of(info.delim, pos));
return std::move(str);
}
//at_first()の時
template<typename CharType, typename DelimType>
std::array<b_str<CharType>, 2> operator| (b_str<CharType>&& str, const split_helper_subroutine<split_at_first, DelimType, CharType>& info)
{
const std::size_t pos = str.find_first_of(info.delim);
if (b_str<CharType>::npos == pos) return{ { std::move(str),{} } };
std::array<b_str<CharType>, 2> re;
re[1] = str.substr(str.find_first_not_of(info.delim, pos));
str.erase(pos);
re[0] = std::move(str);
return re;
}
//at_last().front()の時
template<typename CharType, typename DelimType>
b_str<CharType> operator| (b_str<CharType>&& str, const split_helper_subroutine<split_at_last_front, DelimType, CharType>& info)
{
const std::size_t pos = str.find_last_of(info.delim);
if (b_str<CharType>::npos == pos) return {};
str.erase(str.find_last_not_of(info.delim, pos) + 1);
return std::move(str);
}
//at_last()の時
template<typename CharType, typename DelimType>
std::array<b_str<CharType>, 2> operator| (b_str<CharType>&& str, const split_helper_subroutine<split_at_last, DelimType, CharType>& info)
{
const std::size_t pos = str.find_last_of(info.delim);
if (b_str<CharType>::npos == pos) return{ { {}, std::move(str) } };
std::array<b_str<CharType>, 2> re;
re[1] = str.substr(pos + 1);
str.erase(str.find_last_not_of(info.delim, pos) + 1);
re[0] = std::move(str);
return re;
}
//区切り文字1文字, operator[]の時
template<typename CharType>
b_str<CharType> operator| (b_str<CharType>&& str, const split_helper_index<CharType, true, false, false>& info)
{
size_t pre = 0, pos = 0, i;
for (i = 0; i < info.index + 1; ++i) {
pre = pos;
pos = str.find_first_of(info.delim, pos);
if (b_str<CharType>::npos == pos) break;
++pos;
}
if (i < info.index) throw std::out_of_range("index(" + std::to_string(info.index) + ") is too big.");
--pos;
if(pos <= str.size()) str.erase(pos);
str.erase(0, pre);
return std::move(str);
}
//区切り文字複数, operator[]の時
template<typename CharType, typename DelimType, bool is_c_str, bool is_stl_string>
auto operator| (b_str<CharType>&& str, const split_helper_index<DelimType, false, is_c_str, is_stl_string>& info)
-> enable_if_t<
type_traits::contract_delim_type_without_single_char<CharType, DelimType, is_c_str, is_stl_string>::value,
b_str<CharType>
>
{
size_t pre = 0, pos = 0, i;
for (i = 0; i < info.index + 1; ++i) {
if (pos) pre = pos = str.find_first_not_of(info.delim, pos);
pos = str.find_first_of(info.delim, pos);
if (b_str<CharType>::npos == pos) break;
}
if (i < info.index) throw std::out_of_range("index(" + std::to_string(info.index) + ") is too big.");
if (pos <= str.size()) str.erase(pos);
str.erase(0, pre);
return std::move(str);
}
//区切り文字1文字, has chain funcの時
template<typename CharType, typename FuncType>
auto operator| (b_str<CharType>&& str, const split_helper_conv_func<CharType, FuncType, true, false, false>& info)
-> enable_if_t<
is_void<invoke_result_t<FuncType, b_str<CharType>>>::value,
void
>
{
size_t current = 0;
for (size_t found; (found = str.find_first_of(info.delim, current)) != b_str<CharType>::npos; current = found + 1) {
info.f(std::basic_string<CharType>(str, current, found - current));
}
str.erase(0, current);
info.f(std::move(str));
}
//区切り文字1文字, has chain convert funcの時
template<
typename CharType, typename FuncType,
typename SplitHelperConvFunc = split_helper_conv_func<CharType, FuncType, true, false, false>
>
auto operator| (b_str<CharType>&& str, const split_helper_conv_func<CharType, FuncType, true, false, false>& info)
-> enable_if_t<
!is_void<invoke_result_t<FuncType, b_str<CharType>>>::value,
vector<invoke_result_t<FuncType, b_str<CharType>>>
>
{
vector<invoke_result_t<FuncType, b_str<CharType>>> re;
size_t current = 0;
for (size_t found; (found = str.find_first_of(info.delim, current)) != b_str<CharType>::npos; current = found + 1) {
if (re.capacity() < re.size() + 1) re.reserve((std::numeric_limits<size_t>::max() / 2 < re.size()) ? std::numeric_limits<size_t>::max() : re.size() * 2);
re.push_back(info.f(std::basic_string<CharType>(str, current, found - current)));
}
str.erase(0, current);
re.push_back(info.f(std::move(str)));
return re;
}
//区切り文字1文字の時
template<typename CharType>
vector<b_str<CharType>> operator| (b_str<CharType>&& str, const split_helper<CharType, true, false, false>& info)
{
vector<b_str<CharType>> re;
size_t current = 0;
for (size_t found; (found = str.find_first_of(info.delim, current)) != b_str<CharType>::npos; current = found + 1) {
if (re.capacity() < re.size() + 1) re.reserve((std::numeric_limits<size_t>::max() / 2 < re.size()) ? std::numeric_limits<size_t>::max() : re.size() * 2);
re.emplace_back(str, current, found - current);
}
str.erase(0, current);
re.emplace_back(std::move(str));
return re;
}
//区切り文字複数, has chain funcの時
template<
typename CharType, typename DelimType, typename FuncType,
bool is_c_str, bool is_stl_string
>
auto operator| (b_str<CharType>&& str, const split_helper_conv_func<DelimType, FuncType, false, is_c_str, is_stl_string>& info)
-> enable_if_t<
type_traits::conjunction<
type_traits::contract_delim_type_without_single_char<CharType, DelimType, is_c_str, is_stl_string>,
is_void<invoke_result_t<FuncType, b_str<CharType>>>
>::value,
void
>
{
size_t current = 0;
for (
size_t found = str.find_first_of(info.delim, current);
current != b_str<CharType>::npos && found != b_str<CharType>::npos;
current = str.find_first_not_of(info.delim, found + 1), found = str.find_first_of(info.delim, current)
) {
info.f(std::basic_string<CharType>(str, current, found - current));
}
str.erase(0, current);
info.f(std::move(str));
}
//区切り文字複数, has chain convert funcの時
template<
typename CharType, typename DelimType, typename FuncType,
bool is_c_str, bool is_stl_string,
typename SplitHelperConvFunc = split_helper_conv_func<DelimType, FuncType, false, is_c_str, is_stl_string>
>
auto operator| (b_str<CharType>&& str, const split_helper_conv_func<DelimType, FuncType, false, is_c_str, is_stl_string>& info)
-> enable_if_t<
type_traits::conjunction<
type_traits::contract_delim_type_without_single_char<CharType, DelimType, is_c_str, is_stl_string>,
type_traits::negation<is_void<invoke_result_t<FuncType, b_str<CharType>>>>
>::value,
vector<invoke_result_t<FuncType, b_str<CharType>>>
>
{
vector<invoke_result_t<FuncType, b_str<CharType>>> re;
size_t current = 0;
for (
size_t found = str.find_first_of(info.delim, current);
current != b_str<CharType>::npos && found != b_str<CharType>::npos;
current = str.find_first_not_of(info.delim, found + 1), found = str.find_first_of(info.delim, current)
) {
if (re.capacity() < re.size() + 1) re.reserve((std::numeric_limits<size_t>::max() / 2 < re.size()) ? std::numeric_limits<size_t>::max() : re.size() * 2);
re.push_back(info.f(std::basic_string<CharType>(str, current, found - current)));
}
str.erase(0, current);
re.push_back(info.f(std::move(str)));
return re;
}
//区切り文字複数の時
template<typename CharType, typename DelimType, bool is_c_str, bool is_stl_string>
auto operator| (b_str<CharType>&& str, const split_helper<DelimType, false, is_c_str, is_stl_string>& info)
-> enable_if_t<
type_traits::contract_delim_type_without_single_char<CharType, DelimType, is_c_str, is_stl_string>::value,
vector<b_str<CharType>>
>
{
vector<b_str<CharType>> re;
size_t current = 0;
for (
size_t found = str.find_first_of(info.delim, current);
current != b_str<CharType>::npos && found != b_str<CharType>::npos;
current = str.find_first_not_of(info.delim, found + 1), found = str.find_first_of(info.delim, current)
) {
if (re.capacity() < re.size() + 1) re.reserve((std::numeric_limits<size_t>::max() / 2 < re.size()) ? std::numeric_limits<size_t>::max() : re.size() * 2);
re.emplace_back(str, current, found - current);
}
str.erase(0, current);
re.emplace_back(std::move(str));
return re;
}
}
template<typename CharType, typename std::enable_if<detail::type_traits::is_char_type<CharType>::value, std::nullptr_t>::type = nullptr>
constexpr detail::split_helper<CharType, true, false, false> split(CharType delim) noexcept { return{ delim }; }
template<typename CStr, typename std::enable_if<detail::type_traits::is_c_str<CStr>::value, std::nullptr_t>::type = nullptr>
constexpr detail::split_helper<CStr, false, true, false> split(CStr delim) noexcept { return{ delim }; }
template<typename StlString, typename std::enable_if<detail::type_traits::is_stl_string<StlString>::value, std::nullptr_t>::type = nullptr>
detail::split_helper<StlString, false, false, true> split(StlString delim) noexcept { return{ std::move(delim) }; }
#ifdef STRING_SPLIT_HAS_CXX17_STRING_VIEW
template<typename StlStringView, typename std::enable_if<detail::type_traits::is_stl_string_view<StlStringView>::value, std::nullptr_t>::type = nullptr>
detail::split_helper<StlStringView, false, false, false> split(StlStringView delim) noexcept { return{ delim }; }
#endif
constexpr detail::get_front front() noexcept { return{}; }
constexpr detail::get_back back() noexcept { return{}; }
constexpr detail::split_at_first at_first() noexcept { return{}; }
constexpr detail::split_at_last at_last() noexcept { return{}; }