mirrored from git://gcc.gnu.org/git/gcc.git
-
Notifications
You must be signed in to change notification settings - Fork 4.4k
/
regex.h
3118 lines (2813 loc) · 104 KB
/
regex.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
// class template regex -*- C++ -*-
// Copyright (C) 2010-2024 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library. This library is free
// software; you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the
// Free Software Foundation; either version 3, or (at your option)
// any later version.
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// Under Section 7 of GPL version 3, you are granted additional
// permissions described in the GCC Runtime Library Exception, version
// 3.1, as published by the Free Software Foundation.
// You should have received a copy of the GNU General Public License and
// a copy of the GCC Runtime Library Exception along with this program;
// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
// <http://www.gnu.org/licenses/>.
/**
* @file bits/regex.h
* This is an internal header file, included by other library headers.
* Do not attempt to use it directly. @headername{regex}
*/
#if __cplusplus >= 202002L
# include <bits/iterator_concepts.h> // std::default_sentinel_t
#endif
namespace std _GLIBCXX_VISIBILITY(default)
{
_GLIBCXX_BEGIN_NAMESPACE_VERSION
_GLIBCXX_BEGIN_NAMESPACE_CXX11
template<typename, typename>
class basic_regex;
template<typename _Bi_iter, typename _Alloc>
class match_results;
_GLIBCXX_END_NAMESPACE_CXX11
namespace __detail
{
enum class _RegexExecutorPolicy : int { _S_auto, _S_alternate };
template<typename _BiIter, typename _Alloc,
typename _CharT, typename _TraitsT>
bool
__regex_algo_impl(_BiIter __s, _BiIter __e,
match_results<_BiIter, _Alloc>& __m,
const basic_regex<_CharT, _TraitsT>& __re,
regex_constants::match_flag_type __flags,
_RegexExecutorPolicy __policy,
bool __match_mode);
template<typename, typename, typename, bool>
class _Executor;
template<typename _Tp>
struct __is_contiguous_iter : false_type { };
template<typename _Tp>
struct __is_contiguous_iter<_Tp*> : true_type { };
template<typename _Tp, typename _Cont>
struct __is_contiguous_iter<__gnu_cxx::__normal_iterator<_Tp*, _Cont>>
: true_type { };
}
_GLIBCXX_BEGIN_NAMESPACE_CXX11
/**
* @addtogroup regex
* @{
*/
/**
* @brief Describes aspects of a regular expression.
*
* A regular expression traits class that satisfies the requirements of
* section [28.7].
*
* The class %regex is parameterized around a set of related types and
* functions used to complete the definition of its semantics. This class
* satisfies the requirements of such a traits class.
*
* @headerfile regex
* @since C++11
*/
template<typename _Ch_type>
class regex_traits
{
public:
typedef _Ch_type char_type;
typedef std::basic_string<char_type> string_type;
typedef std::locale locale_type;
private:
struct _RegexMask
{
typedef std::ctype_base::mask _BaseType;
_BaseType _M_base;
unsigned char _M_extended;
static constexpr unsigned char _S_under = 1 << 0;
static constexpr unsigned char _S_valid_mask = 0x1;
constexpr _RegexMask(_BaseType __base = 0,
unsigned char __extended = 0)
: _M_base(__base), _M_extended(__extended)
{ }
constexpr _RegexMask
operator&(_RegexMask __other) const
{
return _RegexMask(_M_base & __other._M_base,
_M_extended & __other._M_extended);
}
constexpr _RegexMask
operator|(_RegexMask __other) const
{
return _RegexMask(_M_base | __other._M_base,
_M_extended | __other._M_extended);
}
constexpr _RegexMask
operator^(_RegexMask __other) const
{
return _RegexMask(_M_base ^ __other._M_base,
_M_extended ^ __other._M_extended);
}
constexpr _RegexMask
operator~() const
{ return _RegexMask(~_M_base, ~_M_extended); }
_RegexMask&
operator&=(_RegexMask __other)
{ return *this = (*this) & __other; }
_RegexMask&
operator|=(_RegexMask __other)
{ return *this = (*this) | __other; }
_RegexMask&
operator^=(_RegexMask __other)
{ return *this = (*this) ^ __other; }
constexpr bool
operator==(_RegexMask __other) const
{
return (_M_extended & _S_valid_mask)
== (__other._M_extended & _S_valid_mask)
&& _M_base == __other._M_base;
}
#if __cpp_impl_three_way_comparison < 201907L
constexpr bool
operator!=(_RegexMask __other) const
{ return !((*this) == __other); }
#endif
};
public:
typedef _RegexMask char_class_type;
public:
/**
* @brief Constructs a default traits object.
*/
regex_traits() { }
/**
* @brief Gives the length of a C-style string starting at @p __p.
*
* @param __p a pointer to the start of a character sequence.
*
* @returns the number of characters between @p *__p and the first
* default-initialized value of type @p char_type. In other words, uses
* the C-string algorithm for determining the length of a sequence of
* characters.
*/
static std::size_t
length(const char_type* __p)
{ return string_type::traits_type::length(__p); }
/**
* @brief Performs the identity translation.
*
* @param __c A character to the locale-specific character set.
*
* @returns __c.
*/
char_type
translate(char_type __c) const
{ return __c; }
/**
* @brief Translates a character into a case-insensitive equivalent.
*
* @param __c A character to the locale-specific character set.
*
* @returns the locale-specific lower-case equivalent of __c.
* @throws std::bad_cast if the imbued locale does not support the ctype
* facet.
*/
char_type
translate_nocase(char_type __c) const
{
typedef std::ctype<char_type> __ctype_type;
const __ctype_type& __fctyp(use_facet<__ctype_type>(_M_locale));
return __fctyp.tolower(__c);
}
/**
* @brief Gets a sort key for a character sequence.
*
* @param __first beginning of the character sequence.
* @param __last one-past-the-end of the character sequence.
*
* Returns a sort key for the character sequence designated by the
* iterator range [F1, F2) such that if the character sequence [G1, G2)
* sorts before the character sequence [H1, H2) then
* v.transform(G1, G2) < v.transform(H1, H2).
*
* What this really does is provide a more efficient way to compare a
* string to multiple other strings in locales with fancy collation
* rules and equivalence classes.
*
* @returns a locale-specific sort key equivalent to the input range.
*
* @throws std::bad_cast if the current locale does not have a collate
* facet.
*/
template<typename _Fwd_iter>
string_type
transform(_Fwd_iter __first, _Fwd_iter __last) const
{
typedef std::collate<char_type> __collate_type;
const __collate_type& __fclt(use_facet<__collate_type>(_M_locale));
string_type __s(__first, __last);
return __fclt.transform(__s.data(), __s.data() + __s.size());
}
/**
* @brief Gets a sort key for a character sequence, independent of case.
*
* @param __first beginning of the character sequence.
* @param __last one-past-the-end of the character sequence.
*
* Effects: if typeid(use_facet<collate<_Ch_type> >) ==
* typeid(collate_byname<_Ch_type>) and the form of the sort key
* returned by collate_byname<_Ch_type>::transform(__first, __last)
* is known and can be converted into a primary sort key
* then returns that key, otherwise returns an empty string.
*
* @todo Implement this function correctly.
*/
template<typename _Fwd_iter>
string_type
transform_primary(_Fwd_iter __first, _Fwd_iter __last) const
{
// TODO : this is not entirely correct.
// This function requires extra support from the platform.
//
// Read http://gcc.gnu.org/ml/libstdc++/2013-09/msg00117.html and
// http://www.open-std.org/Jtc1/sc22/wg21/docs/papers/2003/n1429.htm
// for details.
typedef std::ctype<char_type> __ctype_type;
const __ctype_type& __fctyp(use_facet<__ctype_type>(_M_locale));
_GLIBCXX_STD_C::vector<char_type> __s(__first, __last);
__fctyp.tolower(__s.data(), __s.data() + __s.size());
return this->transform(__s.data(), __s.data() + __s.size());
}
/**
* @brief Gets a collation element by name.
*
* @param __first beginning of the collation element name.
* @param __last one-past-the-end of the collation element name.
*
* @returns a sequence of one or more characters that represents the
* collating element consisting of the character sequence designated by
* the iterator range [__first, __last). Returns an empty string if the
* character sequence is not a valid collating element.
*/
template<typename _Fwd_iter>
string_type
lookup_collatename(_Fwd_iter __first, _Fwd_iter __last) const;
/**
* @brief Maps one or more characters to a named character
* classification.
*
* @param __first beginning of the character sequence.
* @param __last one-past-the-end of the character sequence.
* @param __icase ignores the case of the classification name.
*
* @returns an unspecified value that represents the character
* classification named by the character sequence designated by
* the iterator range [__first, __last). If @p icase is true,
* the returned mask identifies the classification regardless of
* the case of the characters to be matched (for example,
* [[:lower:]] is the same as [[:alpha:]]), otherwise a
* case-dependent classification is returned. The value
* returned shall be independent of the case of the characters
* in the character sequence. If the name is not recognized then
* returns a value that compares equal to 0.
*
* At least the following names (or their wide-character equivalent) are
* supported.
* - d
* - w
* - s
* - alnum
* - alpha
* - blank
* - cntrl
* - digit
* - graph
* - lower
* - print
* - punct
* - space
* - upper
* - xdigit
*/
template<typename _Fwd_iter>
char_class_type
lookup_classname(_Fwd_iter __first, _Fwd_iter __last,
bool __icase = false) const;
/**
* @brief Determines if @p c is a member of an identified class.
*
* @param __c a character.
* @param __f a class type (as returned from lookup_classname).
*
* @returns true if the character @p __c is a member of the classification
* represented by @p __f, false otherwise.
*
* @throws std::bad_cast if the current locale does not have a ctype
* facet.
*/
bool
isctype(_Ch_type __c, char_class_type __f) const;
/**
* @brief Converts a digit to an int.
*
* @param __ch a character representing a digit.
* @param __radix the radix if the numeric conversion (limited to 8, 10,
* or 16).
*
* @returns the value represented by the digit __ch in base radix if the
* character __ch is a valid digit in base radix; otherwise returns -1.
*/
int
value(_Ch_type __ch, int __radix) const;
/**
* @brief Imbues the regex_traits object with a copy of a new locale.
*
* @param __loc A locale.
*
* @returns a copy of the previous locale in use by the regex_traits
* object.
*
* @note Calling imbue with a different locale than the one currently in
* use invalidates all cached data held by *this.
*/
locale_type
imbue(locale_type __loc)
{
std::swap(_M_locale, __loc);
return __loc;
}
/**
* @brief Gets a copy of the current locale in use by the regex_traits
* object.
*/
locale_type
getloc() const
{ return _M_locale; }
protected:
locale_type _M_locale;
};
// [7.8] Class basic_regex
/**
* @brief A regular expression
*
* Specializations of this class template represent regular expressions
* constructed from sequences of character type `_Ch_type`.
* Use the `std::regex` typedef for `std::basic_regex<char>`.
*
* A character sequence passed to the constructor will be parsed according
* to the chosen grammar, and used to create a state machine representing
* the regular expression. The regex object can then be passed to algorithms
* such as `std::regex_match` to match sequences of characters.
*
* The `syntax_option_type` flag passed to the constructor selects from
* one of the supported regular expression grammars. The default is
* `ECMAScript` and the others are `basic`, `extended`, `awk`, `grep`, and
* `egrep`, which are variations on POSIX regular expressions.
*
* @headerfile regex
* @since C++11
*/
template<typename _Ch_type, typename _Rx_traits = regex_traits<_Ch_type>>
class basic_regex
{
public:
static_assert(is_same<_Ch_type, typename _Rx_traits::char_type>::value,
"regex traits class must have the same char_type");
// types:
typedef _Ch_type value_type;
typedef _Rx_traits traits_type;
typedef typename traits_type::string_type string_type;
typedef regex_constants::syntax_option_type flag_type;
typedef typename traits_type::locale_type locale_type;
/**
* @name Constants
* std [28.8.1](1)
*/
///@{
static constexpr flag_type icase = regex_constants::icase;
static constexpr flag_type nosubs = regex_constants::nosubs;
static constexpr flag_type optimize = regex_constants::optimize;
static constexpr flag_type collate = regex_constants::collate;
static constexpr flag_type ECMAScript = regex_constants::ECMAScript;
static constexpr flag_type basic = regex_constants::basic;
static constexpr flag_type extended = regex_constants::extended;
static constexpr flag_type awk = regex_constants::awk;
static constexpr flag_type grep = regex_constants::grep;
static constexpr flag_type egrep = regex_constants::egrep;
#if __cplusplus >= 201703L || !defined __STRICT_ANSI__
static constexpr flag_type multiline = regex_constants::multiline;
#endif
///@}
// [7.8.2] construct/copy/destroy
/**
* Constructs a basic regular expression that does not match any
* character sequence.
*/
basic_regex() noexcept
: _M_flags(ECMAScript), _M_loc(), _M_automaton(nullptr)
{ }
/**
* @brief Constructs a basic regular expression from the
* sequence [__p, __p + char_traits<_Ch_type>::length(__p))
* interpreted according to the flags in @p __f.
*
* @param __p A pointer to the start of a C-style null-terminated string
* containing a regular expression.
* @param __f Flags indicating the syntax rules and options.
*
* @throws regex_error if @p __p is not a valid regular expression.
*/
explicit
basic_regex(const _Ch_type* __p, flag_type __f = ECMAScript)
{ _M_compile(__p, __p + _Rx_traits::length(__p), __f); }
/**
* @brief Constructs a basic regular expression from the sequence
* [p, p + len) interpreted according to the flags in @p f.
*
* @param __p A pointer to the start of a string containing a regular
* expression.
* @param __len The length of the string containing the regular
* expression.
* @param __f Flags indicating the syntax rules and options.
*
* @throws regex_error if @p __p is not a valid regular expression.
*/
basic_regex(const _Ch_type* __p, std::size_t __len,
flag_type __f = ECMAScript)
{
__glibcxx_requires_string_len(__p, __len);
_M_compile(__p, __p + __len, __f);
}
/**
* @brief Copy-constructs a basic regular expression.
*
* @param __rhs A @p regex object.
*/
basic_regex(const basic_regex& __rhs) = default;
/**
* @brief Move-constructs a basic regular expression.
*
* @param __rhs A @p regex object.
*/
basic_regex(basic_regex&& __rhs) noexcept = default;
/**
* @brief Constructs a basic regular expression from the string
* @p s interpreted according to the flags in @p f.
*
* @param __s A string containing a regular expression.
* @param __f Flags indicating the syntax rules and options.
*
* @throws regex_error if @p __s is not a valid regular expression.
*/
template<typename _Ch_traits, typename _Ch_alloc>
explicit
basic_regex(const std::basic_string<_Ch_type, _Ch_traits,
_Ch_alloc>& __s,
flag_type __f = ECMAScript)
{ _M_compile(__s.data(), __s.data() + __s.size(), __f); }
/**
* @brief Constructs a basic regular expression from the range
* [first, last) interpreted according to the flags in @p f.
*
* @param __first The start of a range containing a valid regular
* expression.
* @param __last The end of a range containing a valid regular
* expression.
* @param __f The format flags of the regular expression.
*
* @throws regex_error if @p [__first, __last) is not a valid regular
* expression.
*/
template<typename _FwdIter>
basic_regex(_FwdIter __first, _FwdIter __last,
flag_type __f = ECMAScript)
{ this->assign(__first, __last, __f); }
/**
* @brief Constructs a basic regular expression from an initializer list.
*
* @param __l The initializer list.
* @param __f The format flags of the regular expression.
*
* @throws regex_error if @p __l is not a valid regular expression.
*/
basic_regex(initializer_list<_Ch_type> __l, flag_type __f = ECMAScript)
{ _M_compile(__l.begin(), __l.end(), __f); }
/**
* @brief Destroys a basic regular expression.
*/
~basic_regex()
{ }
/**
* @brief Assigns one regular expression to another.
*/
basic_regex&
operator=(const basic_regex&) = default;
/**
* @brief Move-assigns one regular expression to another.
*/
basic_regex&
operator=(basic_regex&&) = default;
/**
* @brief Replaces a regular expression with a new one constructed from
* a C-style null-terminated string.
*
* @param __p A pointer to the start of a null-terminated C-style string
* containing a regular expression.
*/
basic_regex&
operator=(const _Ch_type* __p)
{ return this->assign(__p); }
/**
* @brief Replaces a regular expression with a new one constructed from
* an initializer list.
*
* @param __l The initializer list.
*
* @throws regex_error if @p __l is not a valid regular expression.
*/
basic_regex&
operator=(initializer_list<_Ch_type> __l)
{ return this->assign(__l); }
/**
* @brief Replaces a regular expression with a new one constructed from
* a string.
*
* @param __s A pointer to a string containing a regular expression.
*/
template<typename _Ch_traits, typename _Alloc>
basic_regex&
operator=(const basic_string<_Ch_type, _Ch_traits, _Alloc>& __s)
{ return this->assign(__s); }
// [7.8.3] assign
/**
* @brief Assigns one regular expression to another.
*
* @param __rhs Another regular expression object.
*/
basic_regex&
assign(const basic_regex& __rhs) noexcept
{ return *this = __rhs; }
/**
* @brief Move-assigns one regular expression to another.
*
* @param __rhs Another regular expression object.
*/
basic_regex&
assign(basic_regex&& __rhs) noexcept
{ return *this = std::move(__rhs); }
/**
* @brief Assigns a new regular expression to a regex object from a
* C-style null-terminated string containing a regular expression
* pattern.
*
* @param __p A pointer to a C-style null-terminated string containing
* a regular expression pattern.
* @param __flags Syntax option flags.
*
* @throws regex_error if __p does not contain a valid regular
* expression pattern interpreted according to @p __flags. If
* regex_error is thrown, *this remains unchanged.
*/
basic_regex&
assign(const _Ch_type* __p, flag_type __flags = ECMAScript)
{
_M_compile(__p, __p + _Rx_traits::length(__p), __flags);
return *this;
}
/**
* @brief Assigns a new regular expression to a regex object from a
* C-style string containing a regular expression pattern.
*
* @param __p A pointer to a C-style string containing a
* regular expression pattern.
* @param __len The length of the regular expression pattern string.
* @param __flags Syntax option flags.
*
* @throws regex_error if p does not contain a valid regular
* expression pattern interpreted according to @p __flags. If
* regex_error is thrown, *this remains unchanged.
*/
// _GLIBCXX_RESOLVE_LIB_DEFECTS
// 3296. Inconsistent default argument for basic_regex<>::assign
basic_regex&
assign(const _Ch_type* __p, size_t __len, flag_type __flags = ECMAScript)
{
_M_compile(__p, __p + __len, __flags);
return *this;
}
/**
* @brief Assigns a new regular expression to a regex object from a
* string containing a regular expression pattern.
*
* @param __s A string containing a regular expression pattern.
* @param __flags Syntax option flags.
*
* @throws regex_error if __s does not contain a valid regular
* expression pattern interpreted according to @p __flags. If
* regex_error is thrown, *this remains unchanged.
*/
template<typename _Ch_traits, typename _Alloc>
basic_regex&
assign(const basic_string<_Ch_type, _Ch_traits, _Alloc>& __s,
flag_type __flags = ECMAScript)
{
_M_compile(__s.data(), __s.data() + __s.size(), __flags);
return *this;
}
/**
* @brief Assigns a new regular expression to a regex object.
*
* @param __first The start of a range containing a valid regular
* expression.
* @param __last The end of a range containing a valid regular
* expression.
* @param __flags Syntax option flags.
*
* @throws regex_error if p does not contain a valid regular
* expression pattern interpreted according to @p __flags. If
* regex_error is thrown, the object remains unchanged.
*/
template<typename _InputIterator>
basic_regex&
assign(_InputIterator __first, _InputIterator __last,
flag_type __flags = ECMAScript)
{
#if __cpp_if_constexpr >= 201606L
using _ValT = typename iterator_traits<_InputIterator>::value_type;
if constexpr (__detail::__is_contiguous_iter<_InputIterator>::value
&& is_same_v<_ValT, value_type>)
{
__glibcxx_requires_valid_range(__first, __last);
if constexpr (is_pointer_v<_InputIterator>)
_M_compile(__first, __last, __flags);
else // __normal_iterator<_T*, C>
_M_compile(__first.base(), __last.base(), __flags);
}
else
#endif
this->assign(string_type(__first, __last), __flags);
return *this;
}
/**
* @brief Assigns a new regular expression to a regex object.
*
* @param __l An initializer list representing a regular expression.
* @param __flags Syntax option flags.
*
* @throws regex_error if @p __l does not contain a valid
* regular expression pattern interpreted according to @p
* __flags. If regex_error is thrown, the object remains
* unchanged.
*/
basic_regex&
assign(initializer_list<_Ch_type> __l, flag_type __flags = ECMAScript)
{
_M_compile(__l.begin(), __l.end(), __flags);
return *this;
}
// [7.8.4] const operations
/**
* @brief Gets the number of marked subexpressions within the regular
* expression.
*/
unsigned int
mark_count() const noexcept
{
if (_M_automaton)
return _M_automaton->_M_sub_count() - 1;
return 0;
}
/**
* @brief Gets the flags used to construct the regular expression
* or in the last call to assign().
*/
flag_type
flags() const noexcept
{ return _M_flags; }
// [7.8.5] locale
/**
* @brief Imbues the regular expression object with the given locale.
*
* @param __loc A locale.
*/
locale_type
imbue(locale_type __loc)
{
std::swap(__loc, _M_loc);
_M_automaton.reset();
return __loc;
}
/**
* @brief Gets the locale currently imbued in the regular expression
* object.
*/
locale_type
getloc() const noexcept
{ return _M_loc; }
// [7.8.6] swap
/**
* @brief Swaps the contents of two regular expression objects.
*
* @param __rhs Another regular expression object.
*/
void
swap(basic_regex& __rhs) noexcept
{
std::swap(_M_flags, __rhs._M_flags);
std::swap(_M_loc, __rhs._M_loc);
std::swap(_M_automaton, __rhs._M_automaton);
}
#ifdef _GLIBCXX_DEBUG
void
_M_dot(std::ostream& __ostr)
{ _M_automaton->_M_dot(__ostr); }
#endif
private:
typedef std::shared_ptr<const __detail::_NFA<_Rx_traits>> _AutomatonPtr;
void
_M_compile(const _Ch_type* __first, const _Ch_type* __last,
flag_type __f)
{
__detail::_Compiler<_Rx_traits> __c(__first, __last, _M_loc, __f);
_M_automaton = __c._M_get_nfa();
_M_flags = __f;
}
template<typename _Bp, typename _Ap, typename _Cp, typename _Rp>
friend bool
__detail::__regex_algo_impl(_Bp, _Bp, match_results<_Bp, _Ap>&,
const basic_regex<_Cp, _Rp>&,
regex_constants::match_flag_type,
__detail::_RegexExecutorPolicy, bool);
template<typename, typename, typename, bool>
friend class __detail::_Executor;
flag_type _M_flags;
locale_type _M_loc;
_AutomatonPtr _M_automaton;
};
#if ! __cpp_inline_variables
template<typename _Ch, typename _Tr>
constexpr regex_constants::syntax_option_type
basic_regex<_Ch, _Tr>::icase;
template<typename _Ch, typename _Tr>
constexpr regex_constants::syntax_option_type
basic_regex<_Ch, _Tr>::nosubs;
template<typename _Ch, typename _Tr>
constexpr regex_constants::syntax_option_type
basic_regex<_Ch, _Tr>::optimize;
template<typename _Ch, typename _Tr>
constexpr regex_constants::syntax_option_type
basic_regex<_Ch, _Tr>::collate;
template<typename _Ch, typename _Tr>
constexpr regex_constants::syntax_option_type
basic_regex<_Ch, _Tr>::ECMAScript;
template<typename _Ch, typename _Tr>
constexpr regex_constants::syntax_option_type
basic_regex<_Ch, _Tr>::basic;
template<typename _Ch, typename _Tr>
constexpr regex_constants::syntax_option_type
basic_regex<_Ch, _Tr>::extended;
template<typename _Ch, typename _Tr>
constexpr regex_constants::syntax_option_type
basic_regex<_Ch, _Tr>::awk;
template<typename _Ch, typename _Tr>
constexpr regex_constants::syntax_option_type
basic_regex<_Ch, _Tr>::grep;
template<typename _Ch, typename _Tr>
constexpr regex_constants::syntax_option_type
basic_regex<_Ch, _Tr>::egrep;
#endif // ! C++17
#if __cpp_deduction_guides >= 201606
template<typename _ForwardIterator>
basic_regex(_ForwardIterator, _ForwardIterator,
regex_constants::syntax_option_type = {})
-> basic_regex<typename iterator_traits<_ForwardIterator>::value_type>;
#endif
/** @brief Standard regular expressions. */
typedef basic_regex<char> regex;
#ifdef _GLIBCXX_USE_WCHAR_T
/** @brief Standard wide-character regular expressions. */
typedef basic_regex<wchar_t> wregex;
#endif
// [7.8.6] basic_regex swap
/**
* @brief Swaps the contents of two regular expression objects.
* @param __lhs First regular expression.
* @param __rhs Second regular expression.
* @relates basic_regex
*/
template<typename _Ch_type, typename _Rx_traits>
inline void
swap(basic_regex<_Ch_type, _Rx_traits>& __lhs,
basic_regex<_Ch_type, _Rx_traits>& __rhs) noexcept
{ __lhs.swap(__rhs); }
// C++11 28.9 [re.submatch] Class template sub_match
/**
* A sequence of characters matched by a particular marked sub-expression.
*
* An object of this class is essentially a pair of iterators marking a
* matched subexpression within a regular expression pattern match. Such
* objects can be converted to and compared with std::basic_string objects
* of the same character type as the pattern matched by the regular
* expression.
*
* A `sub_match<Iter>` has a public base class of type `pair<Iter, Iter>`,
* so inherits pair's data members named `first` and `second`.
* The iterators that make up the pair are the usual half-open interval
* referencing the actual original pattern matched.
*
* @headerfile regex
* @since C++11
*/
template<typename _BiIter>
class sub_match
/// @cond undocumented
: public std::pair<_BiIter, _BiIter>
/// @endcond
{
typedef iterator_traits<_BiIter> __iter_traits;
public:
typedef typename __iter_traits::value_type value_type;
typedef typename __iter_traits::difference_type difference_type;
typedef _BiIter iterator;
typedef basic_string<value_type> string_type;
_GLIBCXX_DOXYGEN_ONLY(iterator first; iterator second;)
bool matched;
constexpr sub_match() noexcept : matched() { }
/// Gets the length of the matching sequence.
difference_type
length() const noexcept
{ return this->matched ? std::distance(this->first, this->second) : 0; }
/**
* @brief Gets the matching sequence as a string.
*
* @returns the matching sequence as a string.
*
* This is the implicit conversion operator. It is identical to the
* str() member function except that it will want to pop up in
* unexpected places and cause a great deal of confusion and cursing
* from the unwary.
*/
operator string_type() const
{ return str(); }
/**
* @brief Gets the matching sequence as a string.
*
* @returns the matching sequence as a string.
*/
string_type
str() const
{
return this->matched
? string_type(this->first, this->second)
: string_type();
}
/**
* @brief Compares this and another matched sequence.
*
* @param __s Another matched sequence to compare to this one.
*
* @retval negative This matched sequence will collate before `__s`.
* @retval zero This matched sequence is equivalent to `__s`.
* @retval positive This matched sequence will collate after `__s`.
*/
int
compare(const sub_match& __s) const
{ return this->_M_str().compare(__s._M_str()); }
/**
* @{
* @brief Compares this `sub_match` to a string.
*
* @param __s A string to compare to this `sub_match`.
*
* @retval negative This matched sequence will collate before `__s`.
* @retval zero This matched sequence is equivalent to `__s`.
* @retval positive This matched sequence will collate after `__s`.
*/
int
compare(const string_type& __s) const
{ return this->_M_str().compare(__s); }
int
compare(const value_type* __s) const
{ return this->_M_str().compare(__s); }
/// @}