-
Notifications
You must be signed in to change notification settings - Fork 320
/
Copy pathbdlpcre_regex.cpp
1152 lines (946 loc) · 36.6 KB
/
bdlpcre_regex.cpp
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
// bdlpcre_regex.cpp -*-C++-*-
#include <bdlpcre_regex.h>
#include <bsls_ident.h>
BSLS_IDENT_RCSID(bdlpcre2_regex_cpp,"$Id$ $CSID$")
///IMPLEMENTATION NOTES
///--------------------
// This component depends on the open-source Perl Compatible Regular
// Expressions (PCRE2) library (http://www.pcre.org).
//
// The PCRE2 library used by this component was configured with UTF-8 support.
//
// The 'RegEx_MatchContext' class currently implements the following strategy
// for allocating/deallocating buffers used for pattern matching:
//:
//: o The match context for the main thread (the thread that calls
//: 'initialize') is pre-allocated when the pattern is compiled.
//:
//: o Match contexts for all other threads are allocated and deallocated within
//: each call to 'loadMatchContext' (i.e. when invoked from other thread(s)).
#include <bslma_default.h>
#include <bslmt_threadutil.h>
#include <bsls_assert.h>
#include <bsls_exceptionutil.h>
#include <bsls_platform.h>
#include <bsl_cstring.h>
#include <bsl_new.h> // placement 'new' syntax
extern "C" {
void *bdlpcre_malloc(size_t size, void* context)
{
void *result = 0;
BloombergLP::bslma::Allocator *basicAllocator =
reinterpret_cast<BloombergLP::bslma::Allocator*>(context);
BSLS_TRY {
result = basicAllocator->allocate(size);
} BSLS_CATCH( ... ) {
}
return result;
}
void bdlpcre_free(void* data, void* context)
{
BloombergLP::bslma::Allocator *basicAllocator =
reinterpret_cast<BloombergLP::bslma::Allocator*>(context);
basicAllocator->deallocate(data);
return;
}
} // close extern "C"
namespace BloombergLP {
namespace bdlpcre {
// CONSTANTS
namespace {
static const bool k_IS_JIT_SUPPORTED =
#if defined(BSLS_PLATFORM_CPU_SPARC) && defined (BSLS_PLATFORM_CPU_64_BIT)
false;
#elif defined(BSLS_PLATFORM_CPU_ARM) && defined (BSLS_PLATFORM_CPU_64_BIT)
false;
#else
true;
#endif
// ===================
// class RegEx_ImpUtil
// ===================
struct RegEx_ImpUtil {
// This 'struct' provides a namespace for 'RegEx' implementation utilities.
// TYPES
enum {
// Enumeration used to distinguish among results of match operations.
k_SUCCESS = 0,
k_DEPTH_LIMIT_FAILURE = 1,
k_JIT_STACK_LIMIT_FAILURE = 2,
k_FAILURE = -1
};
// CLASS METHODS
static
void assign(bsl::pair<size_t, size_t> *value,
const char *subject,
size_t offset,
size_t length)
// Assign a newly created pair of the specified 'offset' and
// 'length' to the specified 'value'. Note that the specified
// 'subject' is unused in this overload.
{
(const void*)subject;
*value = bsl::make_pair(offset, length);
}
static
void assign(bsl::string_view *value,
const char *subject,
size_t offset,
size_t length)
// Assign a newly created 'bsl::string_view' object having the
// specified 'subject', 'offset', and 'length' to the specified
// 'value'.
{
*value = (length != 0) ? bsl::string_view(subject + offset, length)
: bsl::string_view();
}
};
// ==========
// struct NOP
// ==========
struct NOP {
// NOP functor for 'RegEx::match' that does not return the result of match.
// ACCESSORS
void operator()(const char *, PCRE2_SIZE *, unsigned int) const
{
}
};
// ===================
// class DataExtractor
// ===================
template <class DATA>
class DataExtractor {
// This functor extracts the result of a match and assigns it to a
// variable of the DATA (template parameter) type.
// DATA
DATA *d_data_p;
public:
// CREATORS
explicit
DataExtractor(DATA *dataPtr)
: d_data_p(dataPtr)
{
BSLS_ASSERT(dataPtr);
}
// ACCESSORS
void operator()(const char *subject,
PCRE2_SIZE *ovector,
unsigned int ovectorCount) const
{
// Number of pairs in the output vector
(void)ovectorCount;
BSLS_ASSERT(1 <= ovectorCount);
size_t offset = ovector[0];
size_t length = ovector[1] - offset;
RegEx_ImpUtil::assign(d_data_p, subject, offset, length);
}
};
// =====================
// class VectorExtractor
// =====================
template <class VECTOR>
class VectorExtractor {
// This functor extracts the result of a match and assigns it to a variable
// of the VECTOR (template parameter) type.
// DATA
VECTOR *d_vector_p;
public:
// CREATORS
explicit
VectorExtractor(VECTOR *vectorPtr)
: d_vector_p(vectorPtr)
{
BSLS_ASSERT(vectorPtr);
}
// ACCESSORS
void operator()(const char *subject,
PCRE2_SIZE *ovector,
unsigned int ovectorCount) const
{
d_vector_p->resize(ovectorCount);
for (unsigned int i = 0, j = 0; i < ovectorCount; ++i, j += 2) {
DataExtractor<typename VECTOR::value_type> extractor(
&(*d_vector_p)[i]);
extractor(subject, ovector + j, 1);
}
}
};
} // close unnamed namespace
// =============================
// struct RegEx_MatchContextData
// =============================
struct RegEx_MatchContextData {
// This is a component-local POD 'struct' that holds the pointers to the
// buffers used by the PCRE2 match API.
public:
// DATA
pcre2_match_context *d_matchContext_p; // PCRE2 match context
pcre2_match_data *d_matchData_p; // PCRE2 match data
pcre2_jit_stack *d_jitStack_p; // PCRE2 JIT stack
};
// ========================
// class RegEx_MatchContext
// ========================
class RegEx_MatchContext {
// This class manages opaque buffers used by PCRE2 match API.
// PRIVATE TYPES
typedef bslmt::ThreadUtil::Handle ThreadHandle;
// Alias for thread handle type.
// DATA
pcre2_general_context *d_pcre2Context_p; // PCRE2 general context
pcre2_code *d_pcre2PatternCode_p; // PCRE2 compiled pattern
int d_depthLimit; // match depth limit
size_t d_jitStackSize; // JIT stack size
ThreadHandle d_mainThread; // main thread ID
RegEx_MatchContextData d_mainThreadMatchData; // main thread match ctx
private:
// PRIVATE ACCESSORS
int allocateMatchContext(RegEx_MatchContextData *matchContextData) const;
// Allocate PCRE2 match data buffers and load them into the specified
// 'matchContextData'.
void deallocateMatchContext(RegEx_MatchContextData *matchContextData)
const;
// Deallocate PCRE2 match data buffers pointed to by the specified
// 'matchContextData'.
public:
// CREATORS
RegEx_MatchContext();
// Create a 'RegEx_MatchContext' object.
~RegEx_MatchContext();
// Destroy this object.
// MANIPULATORS
int initialize(pcre2_general_context *pcre2Context,
pcre2_code *patternCode,
int depthLimit,
size_t jitStackSize);
// Initialize the object to provide data buffers for the PCRE2 match
// API for the specified 'pcre2Context', 'patternCode, 'depthLimit',
// and 'jitStackSize'. Return 0 on success and non-zero value if the
// data buffers cannot be allocated.
void setDepthLimit(int depthLimit);
// Change the match depth limit in the match data buffers to the
// specified 'depthLimit'.
// ACCESSORS
int acquireMatchContext(RegEx_MatchContextData *matchContextData) const;
// Acquire the match data buffers for the current thread and load the
// specified 'matchContextData' with the pointers to the match data
// buffers. The behavior is undefined unless 'matchContextData' is a
// valid pointer.
void releaseMatchContext(RegEx_MatchContextData *matchContextData) const;
// Release the match data buffers pointed to by the specified
// 'matchContextData'. The behavior is undefined unless
// 'matchContextData' is a valid pointer.
};
// ------------------
// RegEx_MatchContext
// ------------------
// CREATORS
RegEx_MatchContext::RegEx_MatchContext()
: d_pcre2Context_p(0)
, d_pcre2PatternCode_p(0)
, d_depthLimit(0)
, d_jitStackSize(0)
, d_mainThread(bslmt::ThreadUtil::invalidHandle())
, d_mainThreadMatchData()
{
}
RegEx_MatchContext::~RegEx_MatchContext()
{
deallocateMatchContext(&d_mainThreadMatchData);
}
// PRIVATE ACCESSORS
int
RegEx_MatchContext::allocateMatchContext(
RegEx_MatchContextData *matchContextData) const
{
BSLS_ASSERT(matchContextData);
BSLS_ASSERT(d_pcre2Context_p);
BSLS_ASSERT(d_pcre2PatternCode_p);
// Match Data
pcre2_match_data *matchData_p = pcre2_match_data_create_from_pattern(
d_pcre2PatternCode_p,
0);
if (0 == matchData_p) {
return RegEx_ImpUtil::k_FAILURE; // RETURN
}
// Match context
pcre2_match_context *matchContext_p = pcre2_match_context_create(
d_pcre2Context_p);
if (0 == matchContext_p) {
pcre2_match_data_free(matchData_p);
return RegEx_ImpUtil::k_FAILURE; // RETURN
}
pcre2_set_match_limit(matchContext_p, d_depthLimit);
// Jit stack
pcre2_jit_stack *jitStack_p = 0;
if (d_jitStackSize) {
jitStack_p =
pcre2_jit_stack_create(d_jitStackSize,
d_jitStackSize,
d_pcre2Context_p);
if (0 == jitStack_p) {
pcre2_match_context_free(matchContext_p);
pcre2_match_data_free(matchData_p);
return RegEx_ImpUtil::k_FAILURE; // RETURN
}
pcre2_jit_stack_assign(matchContext_p, 0, jitStack_p);
}
matchContextData->d_matchData_p = matchData_p;
matchContextData->d_matchContext_p = matchContext_p;
matchContextData->d_jitStack_p = jitStack_p;
return RegEx_ImpUtil::k_SUCCESS;
}
void
RegEx_MatchContext::deallocateMatchContext(
RegEx_MatchContextData *matchContextData) const
{
BSLS_ASSERT(matchContextData);
pcre2_match_data_free(matchContextData->d_matchData_p);
pcre2_jit_stack_free(matchContextData->d_jitStack_p);
pcre2_match_context_free(matchContextData->d_matchContext_p);
}
// MANIPULATORS
int RegEx_MatchContext::initialize(pcre2_general_context *pcre2Context,
pcre2_code *patternCode,
int depthLimit,
size_t jitStackSize)
{
BSLS_ASSERT(pcre2Context);
BSLS_ASSERT(patternCode);
deallocateMatchContext(&d_mainThreadMatchData);
d_mainThread = bslmt::ThreadUtil::self();
d_pcre2Context_p = pcre2Context;
d_pcre2PatternCode_p = patternCode;
d_depthLimit = depthLimit;
d_jitStackSize = jitStackSize;
return allocateMatchContext(&d_mainThreadMatchData);
}
void RegEx_MatchContext::setDepthLimit(int depthLimit)
{
d_depthLimit = depthLimit;
if (d_mainThreadMatchData.d_matchContext_p) {
pcre2_set_match_limit(d_mainThreadMatchData.d_matchContext_p,
d_depthLimit);
}
}
// ACCESSORS
int
RegEx_MatchContext::acquireMatchContext(
RegEx_MatchContextData *matchContextData) const
{
BSLS_ASSERT(matchContextData);
if (bslmt::ThreadUtil::isEqual(d_mainThread, bslmt::ThreadUtil::self())) {
*matchContextData = d_mainThreadMatchData;
return RegEx_ImpUtil::k_SUCCESS; // RETURN
}
return allocateMatchContext(matchContextData);
}
void RegEx_MatchContext::releaseMatchContext(
RegEx_MatchContextData *matchContextData) const
{
BSLS_ASSERT(matchContextData);
if (bslmt::ThreadUtil::isEqual(d_mainThread, bslmt::ThreadUtil::self())) {
return; // RETURN
}
deallocateMatchContext(matchContextData);
}
// -----------
// class RegEx
// -----------
// CLASS DATA
bsls::AtomicOperations::AtomicTypes::Int RegEx::s_depthLimit = {10000000};
// CONSTANTS
const size_t RegEx::k_INVALID_OFFSET = ~(size_t)0;
// PRIVATE MANIPULATORS
int RegEx::prepareImp(char *errorMessage,
size_t errorMessageLength,
size_t *errorOffset,
const char *pattern,
int flags,
size_t jitStackSize)
{
BSLS_ASSERT(errorMessage);
BSLS_ASSERT(errorOffset);
BSLS_ASSERT(pattern);
const int VALID_FLAGS = k_FLAG_CASELESS
| k_FLAG_DOTMATCHESALL
| k_FLAG_MULTILINE
| k_FLAG_UTF8
| k_FLAG_JIT;
(void) VALID_FLAGS;
BSLS_ASSERT(0 == (flags & ~VALID_FLAGS));
// Free resources currently used by this object, if any, and put the object
// into the "unprepared" state.
clear();
d_pattern = pattern;
d_flags = flags;
d_jitStackSize = (flags & k_FLAG_JIT && isJitAvailable()) ? jitStackSize
: 0;
unsigned int pcreFlags = 0;
pcreFlags |= flags & k_FLAG_CASELESS ? PCRE2_CASELESS : 0;
pcreFlags |= flags & k_FLAG_DOTMATCHESALL ? PCRE2_DOTALL : 0;
pcreFlags |= flags & k_FLAG_MULTILINE ? PCRE2_MULTILINE : 0;
pcreFlags |= flags & k_FLAG_UTF8 ? PCRE2_UTF : 0;
// Compile the new pattern.
int errorCodeFromPcre2;
size_t errorOffsetFromPcre2;
pcre2_code *patternCode = pcre2_compile(
reinterpret_cast<const unsigned char*>(pattern),
PCRE2_ZERO_TERMINATED,
pcreFlags,
&errorCodeFromPcre2,
&errorOffsetFromPcre2,
d_compileContext_p);
if (0 == patternCode) {
int result = pcre2_get_error_message(
errorCodeFromPcre2,
reinterpret_cast<unsigned char *>(errorMessage),
errorMessageLength);
if (result < 0 && PCRE2_ERROR_NOMEMORY != result) {
*errorMessage = 0;
}
*errorOffset = errorOffsetFromPcre2;
return RegEx_ImpUtil::k_FAILURE; // RETURN
}
if (flags & k_FLAG_JIT && isJitAvailable()) {
if (0 != pcre2_jit_compile(patternCode, PCRE2_JIT_COMPLETE)) {
pcre2_code_free(patternCode);
bsl::strncpy(errorMessage,
"JIT compilation failed.",
errorMessageLength);
*errorOffset = 0;
return RegEx_ImpUtil::k_FAILURE; // RETURN
}
}
if (RegEx_ImpUtil::k_SUCCESS != d_matchContext->initialize(
d_pcre2Context_p,
patternCode,
d_depthLimit,
d_jitStackSize))
{
pcre2_code_free(patternCode);
bsl::strncpy(errorMessage,
"Unable to create match contexts.",
errorMessageLength);
*errorOffset = 0;
return RegEx_ImpUtil::k_FAILURE; // RETURN
}
// Set the data members and set the object to the "prepared" state.
d_patternCode_p = patternCode;
return RegEx_ImpUtil::k_SUCCESS;
}
// PRIVATE ACCESSORS
template <class RESULT_EXTRACTOR>
int RegEx::matchImp(const RESULT_EXTRACTOR& extractor,
const char *subject,
size_t subjectLength,
size_t subjectOffset,
bool skipUTF8Validation) const
{
BSLS_ASSERT(subject || 0 == subjectLength);
BSLS_ASSERT(subjectOffset <= subjectLength);
BSLS_ASSERT(isPrepared());
RegEx_MatchContextData matchContextData;
if (0 != d_matchContext->acquireMatchContext(&matchContextData)) {
return RegEx_ImpUtil::k_FAILURE; // RETURN
}
const unsigned char *actualSubject =
reinterpret_cast<const unsigned char*>(subject ? subject : "");
int rc = RegEx_ImpUtil::k_SUCCESS;
int rcPcre2;
if (skipUTF8Validation || !(d_flags & k_FLAG_UTF8)) {
if (d_flags & k_FLAG_JIT && isJitAvailable()) {
rcPcre2 = pcre2_jit_match(d_patternCode_p,
actualSubject,
subjectLength,
subjectOffset,
0,
matchContextData.d_matchData_p,
matchContextData.d_matchContext_p);
} else {
rcPcre2 = pcre2_match( d_patternCode_p,
actualSubject,
subjectLength,
subjectOffset,
PCRE2_NO_UTF_CHECK,
matchContextData.d_matchData_p,
matchContextData.d_matchContext_p);
}
} else {
rcPcre2 = pcre2_match( d_patternCode_p,
actualSubject,
subjectLength,
subjectOffset,
0,
matchContextData.d_matchData_p,
matchContextData.d_matchContext_p);
}
if (PCRE2_ERROR_MATCHLIMIT == rcPcre2) {
rc = RegEx_ImpUtil::k_DEPTH_LIMIT_FAILURE;
} else if (PCRE2_ERROR_JIT_STACKLIMIT == rcPcre2) {
rc = RegEx_ImpUtil::k_JIT_STACK_LIMIT_FAILURE;
} else if (0 > rcPcre2) {
rc = RegEx_ImpUtil::k_FAILURE;
}
if (RegEx_ImpUtil::k_SUCCESS == rc) {
extractor(subject,
pcre2_get_ovector_pointer(matchContextData.d_matchData_p),
pcre2_get_ovector_count(matchContextData.d_matchData_p));
}
d_matchContext->releaseMatchContext(&matchContextData);
return rc;
}
template <class STRING>
int RegEx::replaceImp(STRING *result,
int *errorOffset,
const bsl::string_view& subject,
const bsl::string_view& replacement,
size_t options,
bool skipUTF8Validation) const
{
BSLS_ASSERT(result);
BSLS_ASSERT(errorOffset);
BSLS_ASSERT(isPrepared());
RegEx_MatchContextData matchContextData;
if (0 != d_matchContext->acquireMatchContext(&matchContextData)) {
return RegEx_ImpUtil::k_FAILURE; // RETURN
}
unsigned int pcreFlags = 0;
pcreFlags |= options & k_REPLACE_LITERAL ? PCRE2_SUBSTITUTE_LITERAL : 0;
pcreFlags |= options & k_REPLACE_GLOBAL ? PCRE2_SUBSTITUTE_GLOBAL : 0;
pcreFlags |= options & k_REPLACE_EXTENDED ? PCRE2_SUBSTITUTE_EXTENDED : 0;
pcreFlags |= options & k_REPLACE_UNKNOWN_UNSET
? PCRE2_SUBSTITUTE_UNKNOWN_UNSET : 0;
pcreFlags |= options & k_REPLACE_UNSET_EMPTY
? PCRE2_SUBSTITUTE_UNSET_EMPTY : 0;
pcreFlags |= skipUTF8Validation ? PCRE2_NO_UTF_CHECK : 0;
pcreFlags |= PCRE2_SUBSTITUTE_OVERFLOW_LENGTH;
int rcPcre2;
size_t bufferLength = static_cast<size_t>(result->length());
do {
if (bufferLength > result->length()) {
result->resize(bufferLength);
}
rcPcre2 = pcre2_substitute(
d_patternCode_p,
reinterpret_cast<const unsigned char *>(subject.data()),
subject.length(),
0,
pcreFlags,
matchContextData.d_matchData_p,
matchContextData.d_matchContext_p,
reinterpret_cast<const unsigned char *>(replacement.data()),
replacement.length(),
reinterpret_cast<unsigned char *>(&(*result)[0]),
&bufferLength);
} while (PCRE2_ERROR_NOMEMORY == rcPcre2);
int rc = rcPcre2;
if (rcPcre2 >= 0) {
result->resize(bufferLength);
} else {
rc = RegEx_ImpUtil::k_FAILURE;
*errorOffset = PCRE2_UNSET == static_cast<size_t>(rcPcre2)
? -1 : static_cast<int>(bufferLength);
}
d_matchContext->releaseMatchContext(&matchContextData);
return rc;
}
// CLASS METHODS
bool RegEx::isJitAvailable()
{
unsigned int result = 0;
(void) result;
BSLS_ASSERT(0 <= pcre2_config(PCRE2_CONFIG_JIT, &result));
BSLS_ASSERT(k_IS_JIT_SUPPORTED == (0 != result));
return k_IS_JIT_SUPPORTED;
}
// CREATORS
RegEx::RegEx(bslma::Allocator *basicAllocator)
: d_flags(0)
, d_pattern(basicAllocator)
, d_pcre2Context_p(0)
, d_compileContext_p(0)
, d_patternCode_p(0)
, d_depthLimit(RegEx::defaultDepthLimit())
, d_jitStackSize(0)
, d_allocator_p(bslma::Default::allocator(basicAllocator))
{
d_matchContext.load(new (*d_allocator_p) RegEx_MatchContext(),
d_allocator_p);
d_pcre2Context_p = pcre2_general_context_create(
&bdlpcre_malloc,
&bdlpcre_free,
static_cast<void*>(d_allocator_p));
BSLS_ASSERT(0 != d_pcre2Context_p);
// Pre-create compile context
d_compileContext_p = pcre2_compile_context_create(d_pcre2Context_p);
BSLS_ASSERT(0 != d_compileContext_p);
}
// MANIPULATORS
void RegEx::clear()
{
if (isPrepared()) {
pcre2_code_free(d_patternCode_p);
d_patternCode_p = 0;
d_flags = 0;
d_jitStackSize = 0;
d_pattern.clear();
}
}
int RegEx::prepare(bsl::nullptr_t errorMessage,
size_t *errorOffset,
const char *pattern,
int flags,
size_t jitStackSize)
{
(void)errorMessage;
char dummy[1];
size_t offset;
int rc = prepareImp(&dummy[0], 0, &offset, pattern, flags, jitStackSize);
if (rc && errorOffset) {
*errorOffset = offset;
}
return rc;
}
int RegEx::setDepthLimit(int depthLimit)
{
int previous = d_depthLimit;
d_depthLimit = depthLimit;
d_matchContext->setDepthLimit(d_depthLimit);
return previous;
}
// ACCESSORS
int RegEx::match(const bsl::string_view& subject,
size_t subjectOffset) const
{
return match(subject.data(), subject.length(), subjectOffset);
}
int RegEx::match(const char *subject,
size_t subjectLength,
size_t subjectOffset) const
{
return matchImp(NOP(), subject, subjectLength, subjectOffset, false);
}
int RegEx::match(bsl::pair<size_t, size_t> *result,
const char *subject,
size_t subjectLength,
size_t subjectOffset) const
{
BSLS_ASSERT(result);
return matchImp(DataExtractor<bsl::pair<size_t, size_t> >(result),
subject,
subjectLength,
subjectOffset,
false);
}
int RegEx::match(bsl::string_view *result,
const char *subject,
size_t subjectLength,
size_t subjectOffset) const
{
BSLS_ASSERT(result);
return matchImp(DataExtractor<bsl::string_view>(result),
subject,
subjectLength,
subjectOffset,
false);
}
int RegEx::match(bsl::string_view *result,
const bsl::string_view& subject,
size_t subjectOffset) const
{
BSLS_ASSERT(result);
return match(result, subject.data(), subject.length(), subjectOffset);
}
int RegEx::match(bsl::vector<bsl::pair<size_t, size_t> > *result,
const char *subject,
size_t subjectLength,
size_t subjectOffset) const
{
BSLS_ASSERT(result);
typedef VectorExtractor<bsl::vector<bsl::pair<size_t, size_t> > >
Extractor;
return matchImp(Extractor(result),
subject,
subjectLength,
subjectOffset,
false);
}
int RegEx::match(bsl::vector<bslstl::StringRef> *result,
const char *subject,
size_t subjectLength,
size_t subjectOffset) const
{
BSLS_ASSERT(result);
typedef VectorExtractor<bsl::vector<bslstl::StringRef> > Extractor;
return matchImp(Extractor(result),
subject,
subjectLength,
subjectOffset,
false);
}
int RegEx::match(bsl::vector<bsl::string_view> *result,
const bsl::string_view& subject,
size_t subjectOffset) const
{
BSLS_ASSERT(result);
typedef VectorExtractor<bsl::vector<bsl::string_view> > Extractor;
return matchImp(Extractor(result),
subject.data(),
subject.length(),
subjectOffset,
false);
}
int RegEx::match(std::vector<bsl::string_view> *result,
const bsl::string_view& subject,
size_t subjectOffset) const
{
BSLS_ASSERT(result);
typedef VectorExtractor<std::vector<bsl::string_view> > Extractor;
return matchImp(Extractor(result),
subject.data(),
subject.length(),
subjectOffset,
false);
}
#ifdef BSLS_LIBRARYFEATURES_HAS_CPP17_PMR
int RegEx::match(std::pmr::vector<bsl::string_view> *result,
const bsl::string_view& subject,
size_t subjectOffset) const
{
BSLS_ASSERT(result);
typedef VectorExtractor<std::pmr::vector<bsl::string_view> > Extractor;
return matchImp(Extractor(result),
subject.data(),
subject.length(),
subjectOffset,
false);
}
#endif
int RegEx::matchRaw(const char *subject,
size_t subjectLength,
size_t subjectOffset) const
{
return matchImp(NOP(), subject, subjectLength, subjectOffset, true);
}
int RegEx::matchRaw(const bsl::string_view& subject,
size_t subjectOffset) const
{
return matchRaw(subject.data(), subject.length(), subjectOffset);
}
int RegEx::matchRaw(bsl::pair<size_t, size_t> *result,
const char *subject,
size_t subjectLength,
size_t subjectOffset) const
{
BSLS_ASSERT(result);
return matchImp(DataExtractor<bsl::pair<size_t, size_t> >(result),
subject,
subjectLength,
subjectOffset,
true);
}
int RegEx::matchRaw(bsl::string_view *result,
const char *subject,
size_t subjectLength,
size_t subjectOffset) const
{
BSLS_ASSERT(result);
return matchImp(DataExtractor<bsl::string_view>(result),
subject,
subjectLength,
subjectOffset,
true);
}
int RegEx::matchRaw(bsl::string_view *result,
const bsl::string_view& subject,
size_t subjectOffset) const
{
BSLS_ASSERT(result);
return matchRaw(result, subject.data(), subject.length(), subjectOffset);
}
int RegEx::matchRaw(
bsl::vector<bsl::pair<size_t, size_t> > *result,
const char *subject,
size_t subjectLength,
size_t subjectOffset) const
{
BSLS_ASSERT(result);
typedef VectorExtractor<bsl::vector<bsl::pair<size_t, size_t> > >
Extractor;
return matchImp(Extractor(result),
subject,
subjectLength,
subjectOffset,
true);
}
int RegEx::matchRaw(
bsl::vector<bslstl::StringRef> *result,
const char *subject,
size_t subjectLength,
size_t subjectOffset) const
{
BSLS_ASSERT(result);
typedef VectorExtractor<bsl::vector<bslstl::StringRef> > Extractor;
return matchImp(Extractor(result),
subject,
subjectLength,
subjectOffset,
true);
}
int RegEx::matchRaw(bsl::vector<bsl::string_view> *result,
const bsl::string_view& subject,
size_t subjectOffset) const
{
BSLS_ASSERT(result);
typedef VectorExtractor<bsl::vector<bsl::string_view> > Extractor;
return matchImp(Extractor(result),
subject.data(),
subject.length(),
subjectOffset,
true);
}
int RegEx::matchRaw(std::vector<bsl::string_view> *result,
const bsl::string_view& subject,
size_t subjectOffset) const
{
BSLS_ASSERT(result);
typedef VectorExtractor<std::vector<bsl::string_view> > Extractor;
return matchImp(Extractor(result),
subject.data(),
subject.length(),
subjectOffset,
true);