-
Notifications
You must be signed in to change notification settings - Fork 151
/
clipp.h
7024 lines (5864 loc) · 212 KB
/
clipp.h
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
/*****************************************************************************
* ___ _ _ ___ ___
* | _|| | | | | _ \ _ \ CLIPP - command line interfaces for modern C++
* | |_ | |_ | | | _/ _/ version 1.2.3
* |___||___||_| |_| |_| https://github.com/muellan/clipp
*
* Licensed under the MIT License <http://opensource.org/licenses/MIT>.
* Copyright (c) 2017-2018 André Müller <foss@andremueller-online.de>
*
* ---------------------------------------------------------------------------
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*
*****************************************************************************/
#ifndef AM_CLIPP_H__
#define AM_CLIPP_H__
#include <cstring>
#include <string>
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <memory>
#include <vector>
#include <limits>
#include <stack>
#include <algorithm>
#include <sstream>
#include <utility>
#include <iterator>
#include <functional>
/*************************************************************************//**
*
* @brief primary namespace
*
*****************************************************************************/
namespace clipp {
/*****************************************************************************
*
* basic constants and datatype definitions
*
*****************************************************************************/
using arg_index = int;
using arg_string = std::string;
using doc_string = std::string;
using arg_list = std::vector<arg_string>;
/*************************************************************************//**
*
* @brief tristate
*
*****************************************************************************/
enum class tri : char { no, yes, either };
inline constexpr bool operator == (tri t, bool b) noexcept {
return b ? t != tri::no : t != tri::yes;
}
inline constexpr bool operator == (bool b, tri t) noexcept { return (t == b); }
inline constexpr bool operator != (tri t, bool b) noexcept { return !(t == b); }
inline constexpr bool operator != (bool b, tri t) noexcept { return !(t == b); }
/*************************************************************************//**
*
* @brief (start,size) index range
*
*****************************************************************************/
class subrange {
public:
using size_type = arg_string::size_type;
/** @brief default: no match */
explicit constexpr
subrange() noexcept :
at_{arg_string::npos}, length_{0}
{}
/** @brief match length & position within subject string */
explicit constexpr
subrange(size_type pos, size_type len) noexcept :
at_{pos}, length_{len}
{}
/** @brief position of the match within the subject string */
constexpr size_type at() const noexcept { return at_; }
/** @brief length of the matching subsequence */
constexpr size_type length() const noexcept { return length_; }
/** @brief returns true, if query string is a prefix of the subject string */
constexpr bool prefix() const noexcept {
return at_ == 0;
}
/** @brief returns true, if query is a substring of the query string */
constexpr explicit operator bool () const noexcept {
return at_ != arg_string::npos;
}
private:
size_type at_;
size_type length_;
};
/*************************************************************************//**
*
* @brief match predicates
*
*****************************************************************************/
using match_predicate = std::function<bool(const arg_string&)>;
using match_function = std::function<subrange(const arg_string&)>;
/*************************************************************************//**
*
* @brief type traits (NOT FOR DIRECT USE IN CLIENT CODE!)
* no interface guarantees; might be changed or removed in the future
*
*****************************************************************************/
namespace traits {
/*************************************************************************//**
*
* @brief function (class) signature type trait
*
*****************************************************************************/
template<class Fn, class Ret, class... Args>
constexpr auto
check_is_callable(int) -> decltype(
std::declval<Fn>()(std::declval<Args>()...),
std::integral_constant<bool,
std::is_same<Ret,typename std::result_of<Fn(Args...)>::type>::value>{} );
template<class,class,class...>
constexpr auto
check_is_callable(long) -> std::false_type;
template<class Fn, class Ret>
constexpr auto
check_is_callable_without_arg(int) -> decltype(
std::declval<Fn>()(),
std::integral_constant<bool,
std::is_same<Ret,typename std::result_of<Fn()>::type>::value>{} );
template<class,class>
constexpr auto
check_is_callable_without_arg(long) -> std::false_type;
template<class Fn, class... Args>
constexpr auto
check_is_void_callable(int) -> decltype(
std::declval<Fn>()(std::declval<Args>()...), std::true_type{});
template<class,class,class...>
constexpr auto
check_is_void_callable(long) -> std::false_type;
template<class Fn>
constexpr auto
check_is_void_callable_without_arg(int) -> decltype(
std::declval<Fn>()(), std::true_type{});
template<class>
constexpr auto
check_is_void_callable_without_arg(long) -> std::false_type;
template<class Fn, class Ret>
struct is_callable;
template<class Fn, class Ret, class... Args>
struct is_callable<Fn, Ret(Args...)> :
decltype(check_is_callable<Fn,Ret,Args...>(0))
{};
template<class Fn, class Ret>
struct is_callable<Fn,Ret()> :
decltype(check_is_callable_without_arg<Fn,Ret>(0))
{};
template<class Fn, class... Args>
struct is_callable<Fn, void(Args...)> :
decltype(check_is_void_callable<Fn,Args...>(0))
{};
template<class Fn>
struct is_callable<Fn,void()> :
decltype(check_is_void_callable_without_arg<Fn>(0))
{};
/*************************************************************************//**
*
* @brief input range type trait
*
*****************************************************************************/
template<class T>
constexpr auto
check_is_input_range(int) -> decltype(
begin(std::declval<T>()), end(std::declval<T>()),
std::true_type{});
template<class T>
constexpr auto
check_is_input_range(char) -> decltype(
std::begin(std::declval<T>()), std::end(std::declval<T>()),
std::true_type{});
template<class>
constexpr auto
check_is_input_range(long) -> std::false_type;
template<class T>
struct is_input_range :
decltype(check_is_input_range<T>(0))
{};
/*************************************************************************//**
*
* @brief size() member type trait
*
*****************************************************************************/
template<class T>
constexpr auto
check_has_size_getter(int) ->
decltype(std::declval<T>().size(), std::true_type{});
template<class>
constexpr auto
check_has_size_getter(long) -> std::false_type;
template<class T>
struct has_size_getter :
decltype(check_has_size_getter<T>(0))
{};
} // namespace traits
/*************************************************************************//**
*
* @brief helpers (NOT FOR DIRECT USE IN CLIENT CODE!)
* no interface guarantees; might be changed or removed in the future
*
*****************************************************************************/
namespace detail {
/*************************************************************************//**
* @brief forwards string to first non-whitespace char;
* std string -> unsigned conv yields max value, but we want 0;
* also checks for nullptr
*****************************************************************************/
inline bool
fwd_to_unsigned_int(const char*& s)
{
if(!s) return false;
for(; std::isspace(*s); ++s);
if(!s[0] || s[0] == '-') return false;
if(s[0] == '-') return false;
return true;
}
/*************************************************************************//**
*
* @brief value limits clamping
*
*****************************************************************************/
template<class T, class V, bool = (sizeof(V) > sizeof(T))>
struct limits_clamped {
static T from(const V& v) {
if(v >= V(std::numeric_limits<T>::max())) {
return std::numeric_limits<T>::max();
}
if(v <= V(std::numeric_limits<T>::lowest())) {
return std::numeric_limits<T>::lowest();
}
return T(v);
}
};
template<class T, class V>
struct limits_clamped<T,V,false> {
static T from(const V& v) { return T(v); }
};
/*************************************************************************//**
*
* @brief returns value of v as a T, clamped at T's maximum
*
*****************************************************************************/
template<class T, class V>
inline T clamped_on_limits(const V& v) {
return limits_clamped<T,V>::from(v);
}
/*************************************************************************//**
*
* @brief type conversion helpers
*
*****************************************************************************/
template<class T>
struct make {
static inline T from(const char* s) {
if(!s) return false;
//a conversion from const char* to / must exist
return static_cast<T>(s);
}
};
template<>
struct make<bool> {
static inline bool from(const char* s) {
if(!s) return false;
return static_cast<bool>(s);
}
};
template<>
struct make<unsigned char> {
static inline unsigned char from(const char* s) {
if(!fwd_to_unsigned_int(s)) return (0);
return clamped_on_limits<unsigned char>(std::strtoull(s,nullptr,10));
}
};
template<>
struct make<unsigned short int> {
static inline unsigned short int from(const char* s) {
if(!fwd_to_unsigned_int(s)) return (0);
return clamped_on_limits<unsigned short int>(std::strtoull(s,nullptr,10));
}
};
template<>
struct make<unsigned int> {
static inline unsigned int from(const char* s) {
if(!fwd_to_unsigned_int(s)) return (0);
return clamped_on_limits<unsigned int>(std::strtoull(s,nullptr,10));
}
};
template<>
struct make<unsigned long int> {
static inline unsigned long int from(const char* s) {
if(!fwd_to_unsigned_int(s)) return (0);
return clamped_on_limits<unsigned long int>(std::strtoull(s,nullptr,10));
}
};
template<>
struct make<unsigned long long int> {
static inline unsigned long long int from(const char* s) {
if(!fwd_to_unsigned_int(s)) return (0);
return clamped_on_limits<unsigned long long int>(std::strtoull(s,nullptr,10));
}
};
template<>
struct make<char> {
static inline char from(const char* s) {
//parse as single character?
const auto n = std::strlen(s);
if(n == 1) return s[0];
//parse as integer
return clamped_on_limits<char>(std::strtoll(s,nullptr,10));
}
};
template<>
struct make<short int> {
static inline short int from(const char* s) {
return clamped_on_limits<short int>(std::strtoll(s,nullptr,10));
}
};
template<>
struct make<int> {
static inline int from(const char* s) {
return clamped_on_limits<int>(std::strtoll(s,nullptr,10));
}
};
template<>
struct make<long int> {
static inline long int from(const char* s) {
return clamped_on_limits<long int>(std::strtoll(s,nullptr,10));
}
};
template<>
struct make<long long int> {
static inline long long int from(const char* s) {
return (std::strtoll(s,nullptr,10));
}
};
template<>
struct make<float> {
static inline float from(const char* s) {
return (std::strtof(s,nullptr));
}
};
template<>
struct make<double> {
static inline double from(const char* s) {
return (std::strtod(s,nullptr));
}
};
template<>
struct make<long double> {
static inline long double from(const char* s) {
return (std::strtold(s,nullptr));
}
};
template<>
struct make<std::string> {
static inline std::string from(const char* s) {
return std::string(s);
}
};
/*************************************************************************//**
*
* @brief assigns boolean constant to one or multiple target objects
*
*****************************************************************************/
template<class T, class V = T>
class assign_value
{
public:
template<class X>
explicit constexpr
assign_value(T& target, X&& value) noexcept :
t_{std::addressof(target)}, v_{std::forward<X>(value)}
{}
void operator () () const {
if(t_) *t_ = v_;
}
private:
T* t_;
V v_;
};
/*************************************************************************//**
*
* @brief flips bools
*
*****************************************************************************/
class flip_bool
{
public:
explicit constexpr
flip_bool(bool& target) noexcept :
b_{&target}
{}
void operator () () const {
if(b_) *b_ = !*b_;
}
private:
bool* b_;
};
/*************************************************************************//**
*
* @brief increments using operator ++
*
*****************************************************************************/
template<class T>
class increment
{
public:
explicit constexpr
increment(T& target) noexcept : t_{std::addressof(target)} {}
void operator () () const {
if(t_) ++(*t_);
}
private:
T* t_;
};
/*************************************************************************//**
*
* @brief decrements using operator --
*
*****************************************************************************/
template<class T>
class decrement
{
public:
explicit constexpr
decrement(T& target) noexcept : t_{std::addressof(target)} {}
void operator () () const {
if(t_) --(*t_);
}
private:
T* t_;
};
/*************************************************************************//**
*
* @brief increments by a fixed amount using operator +=
*
*****************************************************************************/
template<class T>
class increment_by
{
public:
explicit constexpr
increment_by(T& target, T by) noexcept :
t_{std::addressof(target)}, by_{std::move(by)}
{}
void operator () () const {
if(t_) (*t_) += by_;
}
private:
T* t_;
T by_;
};
/*************************************************************************//**
*
* @brief makes a value from a string and assigns it to an object
*
*****************************************************************************/
template<class T>
class map_arg_to
{
public:
explicit constexpr
map_arg_to(T& target) noexcept : t_{std::addressof(target)} {}
void operator () (const char* s) const {
if(t_ && s) *t_ = detail::make<T>::from(s);
}
private:
T* t_;
};
//-------------------------------------------------------------------
/**
* @brief specialization for vectors: append element
*/
template<class T>
class map_arg_to<std::vector<T>>
{
public:
map_arg_to(std::vector<T>& target): t_{std::addressof(target)} {}
void operator () (const char* s) const {
if(t_ && s) t_->push_back(detail::make<T>::from(s));
}
private:
std::vector<T>* t_;
};
//-------------------------------------------------------------------
/**
* @brief specialization for bools:
* set to true regardless of string content
*/
template<>
class map_arg_to<bool>
{
public:
map_arg_to(bool& target): t_{&target} {}
void operator () (const char* s) const {
if(t_ && s) *t_ = true;
}
private:
bool* t_;
};
} // namespace detail
/*************************************************************************//**
*
* @brief string matching and processing tools
*
*****************************************************************************/
namespace str {
/*************************************************************************//**
*
* @brief converts string to value of target type 'T'
*
*****************************************************************************/
template<class T>
T make(const arg_string& s)
{
return detail::make<T>::from(s);
}
/*************************************************************************//**
*
* @brief removes trailing whitespace from string
*
*****************************************************************************/
template<class C, class T, class A>
inline void
trimr(std::basic_string<C,T,A>& s)
{
if(s.empty()) return;
s.erase(
std::find_if_not(s.rbegin(), s.rend(),
[](char c) { return std::isspace(c);} ).base(),
s.end() );
}
/*************************************************************************//**
*
* @brief removes leading whitespace from string
*
*****************************************************************************/
template<class C, class T, class A>
inline void
triml(std::basic_string<C,T,A>& s)
{
if(s.empty()) return;
s.erase(
s.begin(),
std::find_if_not(s.begin(), s.end(),
[](char c) { return std::isspace(c);})
);
}
/*************************************************************************//**
*
* @brief removes leading and trailing whitespace from string
*
*****************************************************************************/
template<class C, class T, class A>
inline void
trim(std::basic_string<C,T,A>& s)
{
triml(s);
trimr(s);
}
/*************************************************************************//**
*
* @brief removes all whitespaces from string
*
*****************************************************************************/
template<class C, class T, class A>
inline void
remove_ws(std::basic_string<C,T,A>& s)
{
if(s.empty()) return;
s.erase(std::remove_if(s.begin(), s.end(),
[](char c) { return std::isspace(c); }),
s.end() );
}
/*************************************************************************//**
*
* @brief returns true, if the 'prefix' argument
* is a prefix of the 'subject' argument
*
*****************************************************************************/
template<class C, class T, class A>
inline bool
has_prefix(const std::basic_string<C,T,A>& subject,
const std::basic_string<C,T,A>& prefix)
{
if(prefix.size() > subject.size()) return false;
return subject.find(prefix) == 0;
}
/*************************************************************************//**
*
* @brief returns true, if the 'postfix' argument
* is a postfix of the 'subject' argument
*
*****************************************************************************/
template<class C, class T, class A>
inline bool
has_postfix(const std::basic_string<C,T,A>& subject,
const std::basic_string<C,T,A>& postfix)
{
if(postfix.size() > subject.size()) return false;
return (subject.size() - postfix.size()) == subject.find(postfix);
}
/*************************************************************************//**
*
* @brief returns longest common prefix of several
* sequential random access containers
*
* @details InputRange require begin and end (member functions or overloads)
* the elements of InputRange require a size() member
*
*****************************************************************************/
template<class InputRange>
auto
longest_common_prefix(const InputRange& strs)
-> typename std::decay<decltype(*begin(strs))>::type
{
static_assert(traits::is_input_range<InputRange>(),
"parameter must satisfy the InputRange concept");
static_assert(traits::has_size_getter<
typename std::decay<decltype(*begin(strs))>::type>(),
"elements of input range must have a ::size() member function");
using std::begin;
using std::end;
using item_t = typename std::decay<decltype(*begin(strs))>::type;
using str_size_t = typename std::decay<decltype(begin(strs)->size())>::type;
const auto n = size_t(distance(begin(strs), end(strs)));
if(n < 1) return item_t("");
if(n == 1) return *begin(strs);
//length of shortest string
auto m = std::min_element(begin(strs), end(strs),
[](const item_t& a, const item_t& b) {
return a.size() < b.size(); })->size();
//check each character until we find a mismatch
for(str_size_t i = 0; i < m; ++i) {
for(str_size_t j = 1; j < n; ++j) {
if(strs[j][i] != strs[j-1][i])
return strs[0].substr(0, i);
}
}
return strs[0].substr(0, m);
}
/*************************************************************************//**
*
* @brief returns longest substring range that could be found in 'arg'
*
* @param arg string to be searched in
* @param substrings range of candidate substrings
*
*****************************************************************************/
template<class C, class T, class A, class InputRange>
subrange
longest_substring_match(const std::basic_string<C,T,A>& arg,
const InputRange& substrings)
{
using string_t = std::basic_string<C,T,A>;
static_assert(traits::is_input_range<InputRange>(),
"parameter must satisfy the InputRange concept");
static_assert(std::is_same<string_t,
typename std::decay<decltype(*begin(substrings))>::type>(),
"substrings must have same type as 'arg'");
auto i = string_t::npos;
auto n = string_t::size_type(0);
for(const auto& s : substrings) {
auto j = arg.find(s);
if(j != string_t::npos && s.size() > n) {
i = j;
n = s.size();
}
}
return subrange{i,n};
}
/*************************************************************************//**
*
* @brief returns longest prefix range that could be found in 'arg'
*
* @param arg string to be searched in
* @param prefixes range of candidate prefix strings
*
*****************************************************************************/
template<class C, class T, class A, class InputRange>
subrange
longest_prefix_match(const std::basic_string<C,T,A>& arg,
const InputRange& prefixes)
{
using string_t = std::basic_string<C,T,A>;
using s_size_t = typename string_t::size_type;
static_assert(traits::is_input_range<InputRange>(),
"parameter must satisfy the InputRange concept");
static_assert(std::is_same<string_t,
typename std::decay<decltype(*begin(prefixes))>::type>(),
"prefixes must have same type as 'arg'");
auto i = string_t::npos;
auto n = s_size_t(0);
for(const auto& s : prefixes) {
auto j = arg.find(s);
if(j == 0 && s.size() > n) {
i = 0;
n = s.size();
}
}
return subrange{i,n};
}
/*************************************************************************//**
*
* @brief returns the first occurrence of 'query' within 'subject'
*
*****************************************************************************/
template<class C, class T, class A>
inline subrange
substring_match(const std::basic_string<C,T,A>& subject,
const std::basic_string<C,T,A>& query)
{
if(subject.empty() && query.empty()) return subrange(0,0);
if(subject.empty() || query.empty()) return subrange{};
auto i = subject.find(query);
if(i == std::basic_string<C,T,A>::npos) return subrange{};
return subrange{i,query.size()};
}
/*************************************************************************//**
*
* @brief returns first substring match (pos,len) within the input string
* that represents a number
* (with at maximum one decimal point and digit separators)
*
*****************************************************************************/
template<class C, class T, class A>
subrange
first_number_match(std::basic_string<C,T,A> s,
C digitSeparator = C(','),
C decimalPoint = C('.'),
C exponential = C('e'))
{
using string_t = std::basic_string<C,T,A>;
str::trim(s);
if(s.empty()) return subrange{};
auto i = s.find_first_of("0123456789+-");
if(i == string_t::npos) {
i = s.find(decimalPoint);
if(i == string_t::npos) return subrange{};
}
bool point = false;
bool sep = false;
auto exp = string_t::npos;
auto j = i + 1;
for(; j < s.size(); ++j) {
if(s[j] == digitSeparator) {
if(!sep) sep = true; else break;
}
else {
sep = false;
if(s[j] == decimalPoint) {
//only one decimal point before exponent allowed
if(!point && exp == string_t::npos) point = true; else break;
}
else if(std::tolower(s[j]) == std::tolower(exponential)) {
//only one exponent separator allowed
if(exp == string_t::npos) exp = j; else break;
}
else if(exp != string_t::npos && (exp+1) == j) {
//only sign or digit after exponent separator
if(s[j] != '+' && s[j] != '-' && !std::isdigit(s[j])) break;
}
else if(!std::isdigit(s[j])) {
break;
}
}
}
//if length == 1 then must be a digit
if(j-i == 1 && !std::isdigit(s[i])) return subrange{};
return subrange{i,j-i};
}
/*************************************************************************//**
*
* @brief returns first substring match (pos,len)
* that represents an integer (with optional digit separators)
*
*****************************************************************************/
template<class C, class T, class A>
subrange
first_integer_match(std::basic_string<C,T,A> s,
C digitSeparator = C(','))
{
using string_t = std::basic_string<C,T,A>;
str::trim(s);
if(s.empty()) return subrange{};