forked from tindy2013/subconverter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjpcre2.hpp
5119 lines (4568 loc) · 214 KB
/
jpcre2.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* *****************************************************************************
* ******************* C++ wrapper for PCRE2 Library ****************************
* *****************************************************************************
* Copyright (c) Md. Jahidul Hamid
*
* -----------------------------------------------------------------------------
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* * The names of its contributors may not be used to endorse or promote
* products derived from this software without specific prior written
* permission.
*
* Disclaimer:
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
* */
/** @file jpcre2.hpp
* @brief Main header file for JPCRE2 library to be included by programs that uses its functionalities.
* It includes the `pcre2.h` header, therefore you shouldn't include `pcre2.h`, neither should you define `PCRE2_CODE_UNIT_WIDTH` before including
* `jpcre2.hpp`.
* If your `pcre2.h` header is not in standard include paths, you may include `pcre2.h` with correct path before including `jpcre2.hpp`
* manually. In this case you will have to define `PCRE2_CODE_UNIT_WIDTH` before including `pcre2.h`.
* Make sure to link required PCRE2 libraries when compiling.
*
* @author [Md Jahidul Hamid](https://github.com/neurobin)
*/
#ifndef JPCRE2_HPP
#define JPCRE2_HPP
#ifndef PCRE2_CODE_UNIT_WIDTH
///@def PCRE2_CODE_UNIT_WIDTH
///This macro does not have any significance in JPCRE2 context.
///It is defined as 0 by default. Defining it before including jpcre2.hpp
///will override the default (discouraged as it will make it harder for you to detect problems),
///but still it will have no effect in a JPCRE2 perspective.
///Defining it with an invalid value will yield to compile error.
#define PCRE2_CODE_UNIT_WIDTH 0
#endif
//previous inclusion of pcre2.h will be respected and we won't try to include it twice.
//Thus one can pre-include pcre2.h from an arbitrary/non-standard path.
#ifndef PCRE2_MAJOR
#include <pcre2.h> // pcre2 header
#endif
#include <string> // std::string, std::wstring
#include <vector> // std::vector
#include <map> // std::map
#include <cstdio> // std::fprintf
#include <climits> // CHAR_BIT
#include <cstdlib> // std::abort()
#if __cplusplus >= 201103L || _MSVC_LANG >= 201103L
#define JPCRE2_USE_MINIMUM_CXX_11 1
#include <utility>
#ifndef JPCRE2_USE_FUNCTION_POINTER_CALLBACK
#include <functional> // std::function
#endif
#else
#define JPCRE2_USE_MINIMUM_CXX_11 0
#endif
#define JPCRE2_UNUSED(x) ((void)(x))
#if defined(NDEBUG) || defined(JPCRE2_NDEBUG)
#define JPCRE2_ASSERT(cond, msg) ((void)0)
#define JPCRE2_VECTOR_DATA_ASSERT(cond, name) ((void)0)
#else
#define JPCRE2_ASSERT(cond, msg) jpcre2::jassert(cond, msg, __FILE__, __LINE__)
#define JPCRE2_VECTOR_DATA_ASSERT(cond, name) jpcre2::_jvassert(cond, name, __FILE__, __LINE__)
#endif
// In Windows, Windows.h defines ERROR macro
// It conflicts with our jpcre2::ERROR namespace
#ifdef ERROR
#undef ERROR
#endif
/** @namespace jpcre2
* Top level namespace of JPCRE2.
*
* All functions, classes/structs, constants, enums that are provided by JPCRE2 belong to this namespace while
* **PCRE2** structs, functions, constants remain outside of its scope.
*
* If you want to use any PCRE2 functions or constants,
* remember that they are in the global scope and should be used as such.
*/
namespace jpcre2 {
///Define for JPCRE2 version.
///It can be used to support changes in different versions of the lib.
#define JPCRE2_VERSION 103201L
/** @namespace jpcre2::INFO
* Namespace to provide information about JPCRE2 library itself.
* Contains constant Strings with version info.
*/
namespace INFO {
static const char NAME[] = "JPCRE2"; ///< Name of the project
static const char FULL_VERSION[] = "10.32.01"; ///< Full version string
static const char VERSION_GENRE[] = "10"; ///< Generation, depends on original PCRE2 version
static const char VERSION_MAJOR[] = "32"; ///< Major version, updated when API change is made
static const char VERSION_MINOR[] = "01"; ///< Minor version, includes bug fix or minor feature upgrade
static const char VERSION_PRE_RELEASE[] = ""; ///< Alpha or beta (testing) release version
}
typedef PCRE2_SIZE SIZE_T; ///< Used for match count and vector size
typedef uint32_t Uint; ///< Used for options (bitwise operation)
typedef uint8_t Ush; ///< 8 bit unsigned integer.
typedef std::vector<SIZE_T> VecOff; ///< vector of size_t.
typedef std::vector<Uint> VecOpt; ///< vector for Uint option values.
/// @namespace jpcre2::ERROR
/// Namespace for error codes.
namespace ERROR {
/** Error numbers for JPCRE2.
* JPCRE2 error numbers are positive integers while
* PCRE2 error numbers are negative integers.
*/
enum {
INVALID_MODIFIER = 2, ///< Invalid modifier was detected
INSUFFICIENT_OVECTOR = 3 ///< Ovector was not big enough during a match
};
}
/** These constants provide JPCRE2 options.
*/
enum {
NONE = 0x0000000u, ///< Option 0 (zero)
FIND_ALL = 0x0000002u, ///< Find all during match (global match)
JIT_COMPILE = 0x0000004u ///< Perform JIT compilation for optimization
};
//enableif and is_same implementation
template<bool B, typename T = void>
struct EnableIf{};
template<typename T>
struct EnableIf<true, T>{typedef T Type;};
template<typename T1, typename T2>
struct IsSame{ static const bool value = false; };
template<typename T>
struct IsSame<T,T>{ static const bool value = true; };
///JPCRE2 assert function.
///Aborts with an error message if condition fails.
///@param cond boolean condition
///@param msg message (std::string)
///@param f file where jassert was called.
///@param line line number where jassert was called.
static inline void jassert(bool cond, const char* msg, const char* f, size_t line){
if(!cond) {
std::fprintf(stderr,"\n\tE: AssertionFailure\n%s\nAssertion failed in file: %s\t at line: %u\n", msg, f, (unsigned)line);
std::abort();
}
}
static inline void _jvassert(bool cond, char const * name, const char* f, size_t line){
jassert(cond, (std::string("ValueError: \n\
Required data vector of type ")+std::string(name)+" is empty.\n\
Your MatchEvaluator callback function is not\n\
compatible with existing data!!\n\
You are trying to use a vector that does not\n\
have any match data. Either call nreplace() or replace()\n\
with true or perform a match with appropriate\n\
callback function. For more details, refer to\n\
the doc in MatchEvaluator section.").c_str(), f, line);
}
static inline std::string _tostdstring(unsigned x){
char buf[128];
int written = std::sprintf(buf, "%u", x);
return (written > 0) ? std::string(buf, buf + written) : std::string();
}
////////////////////////// The following are type and function mappings from PCRE2 interface to JPCRE2 interface /////////////////////////
//forward declaration
template<Ush BS> struct Pcre2Type;
template<Ush BS> struct Pcre2Func;
//PCRE2 types
//These templated types will be used in place of actual types
template<Ush BS> struct Pcre2Type {};
template<> struct Pcre2Type<8>{
//typedefs used
typedef PCRE2_UCHAR8 Pcre2Uchar;
typedef PCRE2_SPTR8 Pcre2Sptr;
typedef pcre2_code_8 Pcre2Code;
typedef pcre2_compile_context_8 CompileContext;
typedef pcre2_match_data_8 MatchData;
typedef pcre2_general_context_8 GeneralContext;
typedef pcre2_match_context_8 MatchContext;
typedef pcre2_jit_callback_8 JitCallback;
typedef pcre2_jit_stack_8 JitStack;
};
template<> struct Pcre2Type<16>{
//typedefs used
typedef PCRE2_UCHAR16 Pcre2Uchar;
typedef PCRE2_SPTR16 Pcre2Sptr;
typedef pcre2_code_16 Pcre2Code;
typedef pcre2_compile_context_16 CompileContext;
typedef pcre2_match_data_16 MatchData;
typedef pcre2_general_context_16 GeneralContext;
typedef pcre2_match_context_16 MatchContext;
typedef pcre2_jit_callback_16 JitCallback;
typedef pcre2_jit_stack_16 JitStack;
};
template<> struct Pcre2Type<32>{
//typedefs used
typedef PCRE2_UCHAR32 Pcre2Uchar;
typedef PCRE2_SPTR32 Pcre2Sptr;
typedef pcre2_code_32 Pcre2Code;
typedef pcre2_compile_context_32 CompileContext;
typedef pcre2_match_data_32 MatchData;
typedef pcre2_general_context_32 GeneralContext;
typedef pcre2_match_context_32 MatchContext;
typedef pcre2_jit_callback_32 JitCallback;
typedef pcre2_jit_stack_32 JitStack;
};
//wrappers for PCRE2 functions
template<Ush BS> struct Pcre2Func{};
//8-bit version
template<> struct Pcre2Func<8> {
static Pcre2Type<8>::CompileContext* compile_context_create(Pcre2Type<8>::GeneralContext *gcontext){
return pcre2_compile_context_create_8(gcontext);
}
static void compile_context_free(Pcre2Type<8>::CompileContext *ccontext){
pcre2_compile_context_free_8(ccontext);
}
static Pcre2Type<8>::CompileContext* compile_context_copy(Pcre2Type<8>::CompileContext* ccontext){
return pcre2_compile_context_copy_8(ccontext);
}
static const unsigned char * maketables(Pcre2Type<8>::GeneralContext* gcontext){
return pcre2_maketables_8(gcontext);
}
static int set_character_tables(Pcre2Type<8>::CompileContext * ccontext, const unsigned char * table){
return pcre2_set_character_tables_8(ccontext, table);
}
static Pcre2Type<8>::Pcre2Code * compile(Pcre2Type<8>::Pcre2Sptr pattern,
PCRE2_SIZE length,
uint32_t options,
int *errorcode,
PCRE2_SIZE *erroroffset,
Pcre2Type<8>::CompileContext *ccontext){
return pcre2_compile_8(pattern, length, options, errorcode, erroroffset, ccontext);
}
static int jit_compile(Pcre2Type<8>::Pcre2Code *code, uint32_t options){
return pcre2_jit_compile_8(code, options);
}
static int substitute( const Pcre2Type<8>::Pcre2Code *code,
Pcre2Type<8>::Pcre2Sptr subject,
PCRE2_SIZE length,
PCRE2_SIZE startoffset,
uint32_t options,
Pcre2Type<8>::MatchData *match_data,
Pcre2Type<8>::MatchContext *mcontext,
Pcre2Type<8>::Pcre2Sptr replacement,
PCRE2_SIZE rlength,
Pcre2Type<8>::Pcre2Uchar *outputbuffer,
PCRE2_SIZE *outlengthptr){
return pcre2_substitute_8( code, subject, length, startoffset, options, match_data,
mcontext, replacement, rlength, outputbuffer, outlengthptr);
}
//~ static int substring_get_bynumber(Pcre2Type<8>::MatchData *match_data,
//~ uint32_t number,
//~ Pcre2Type<8>::Pcre2Uchar **bufferptr,
//~ PCRE2_SIZE *bufflen){
//~ return pcre2_substring_get_bynumber_8(match_data, number, bufferptr, bufflen);
//~ }
//~ static int substring_get_byname(Pcre2Type<8>::MatchData *match_data,
//~ Pcre2Type<8>::Pcre2Sptr name,
//~ Pcre2Type<8>::Pcre2Uchar **bufferptr,
//~ PCRE2_SIZE *bufflen){
//~ return pcre2_substring_get_byname_8(match_data, name, bufferptr, bufflen);
//~ }
//~ static void substring_free(Pcre2Type<8>::Pcre2Uchar *buffer){
//~ pcre2_substring_free_8(buffer);
//~ }
//~ static Pcre2Type<8>::Pcre2Code * code_copy(const Pcre2Type<8>::Pcre2Code *code){
//~ return pcre2_code_copy_8(code);
//~ }
static void code_free(Pcre2Type<8>::Pcre2Code *code){
pcre2_code_free_8(code);
}
static int get_error_message( int errorcode,
Pcre2Type<8>::Pcre2Uchar *buffer,
PCRE2_SIZE bufflen){
return pcre2_get_error_message_8(errorcode, buffer, bufflen);
}
static Pcre2Type<8>::MatchData * match_data_create_from_pattern(
const Pcre2Type<8>::Pcre2Code *code,
Pcre2Type<8>::GeneralContext *gcontext){
return pcre2_match_data_create_from_pattern_8(code, gcontext);
}
static int match( const Pcre2Type<8>::Pcre2Code *code,
Pcre2Type<8>::Pcre2Sptr subject,
PCRE2_SIZE length,
PCRE2_SIZE startoffset,
uint32_t options,
Pcre2Type<8>::MatchData *match_data,
Pcre2Type<8>::MatchContext *mcontext){
return pcre2_match_8(code, subject, length, startoffset, options, match_data, mcontext);
}
static void match_data_free(Pcre2Type<8>::MatchData *match_data){
pcre2_match_data_free_8(match_data);
}
static PCRE2_SIZE * get_ovector_pointer(Pcre2Type<8>::MatchData *match_data){
return pcre2_get_ovector_pointer_8(match_data);
}
static int pattern_info(const Pcre2Type<8>::Pcre2Code *code, uint32_t what, void *where){
return pcre2_pattern_info_8(code, what, where);
}
static int set_newline(Pcre2Type<8>::CompileContext *ccontext, uint32_t value){
return pcre2_set_newline_8(ccontext, value);
}
//~ static void jit_stack_assign(Pcre2Type<8>::MatchContext *mcontext,
//~ Pcre2Type<8>::JitCallback callback_function,
//~ void *callback_data){
//~ pcre2_jit_stack_assign_8(mcontext, callback_function, callback_data);
//~ }
//~ static Pcre2Type<8>::JitStack *jit_stack_create(PCRE2_SIZE startsize, PCRE2_SIZE maxsize,
//~ Pcre2Type<8>::GeneralContext *gcontext){
//~ return pcre2_jit_stack_create_8(startsize, maxsize, gcontext);
//~ }
//~ static void jit_stack_free(Pcre2Type<8>::JitStack *jit_stack){
//~ pcre2_jit_stack_free_8(jit_stack);
//~ }
//~ static void jit_free_unused_memory(Pcre2Type<8>::GeneralContext *gcontext){
//~ pcre2_jit_free_unused_memory_8(gcontext);
//~ }
//~ static Pcre2Type<8>::MatchContext *match_context_create(Pcre2Type<8>::GeneralContext *gcontext){
//~ return pcre2_match_context_create_8(gcontext);
//~ }
//~ static Pcre2Type<8>::MatchContext *match_context_copy(Pcre2Type<8>::MatchContext *mcontext){
//~ return pcre2_match_context_copy_8(mcontext);
//~ }
//~ static void match_context_free(Pcre2Type<8>::MatchContext *mcontext){
//~ pcre2_match_context_free_8(mcontext);
//~ }
static uint32_t get_ovector_count(Pcre2Type<8>::MatchData *match_data){
return pcre2_get_ovector_count_8(match_data);
}
};
//16-bit version
template<> struct Pcre2Func<16> {
static Pcre2Type<16>::CompileContext* compile_context_create(Pcre2Type<16>::GeneralContext *gcontext){
return pcre2_compile_context_create_16(gcontext);
}
static void compile_context_free(Pcre2Type<16>::CompileContext *ccontext){
pcre2_compile_context_free_16(ccontext);
}
static Pcre2Type<16>::CompileContext* compile_context_copy(Pcre2Type<16>::CompileContext* ccontext){
return pcre2_compile_context_copy_16(ccontext);
}
static const unsigned char * maketables(Pcre2Type<16>::GeneralContext* gcontext){
return pcre2_maketables_16(gcontext);
}
static int set_character_tables(Pcre2Type<16>::CompileContext * ccontext, const unsigned char * table){
return pcre2_set_character_tables_16(ccontext, table);
}
static Pcre2Type<16>::Pcre2Code * compile(Pcre2Type<16>::Pcre2Sptr pattern,
PCRE2_SIZE length,
uint32_t options,
int *errorcode,
PCRE2_SIZE *erroroffset,
Pcre2Type<16>::CompileContext *ccontext){
return pcre2_compile_16(pattern, length, options, errorcode, erroroffset, ccontext);
}
static int jit_compile(Pcre2Type<16>::Pcre2Code *code, uint32_t options){
return pcre2_jit_compile_16(code, options);
}
static int substitute( const Pcre2Type<16>::Pcre2Code *code,
Pcre2Type<16>::Pcre2Sptr subject,
PCRE2_SIZE length,
PCRE2_SIZE startoffset,
uint32_t options,
Pcre2Type<16>::MatchData *match_data,
Pcre2Type<16>::MatchContext *mcontext,
Pcre2Type<16>::Pcre2Sptr replacement,
PCRE2_SIZE rlength,
Pcre2Type<16>::Pcre2Uchar *outputbuffer,
PCRE2_SIZE *outlengthptr){
return pcre2_substitute_16( code, subject, length, startoffset, options, match_data,
mcontext, replacement, rlength, outputbuffer, outlengthptr);
}
//~ static int substring_get_bynumber(Pcre2Type<16>::MatchData *match_data,
//~ uint32_t number,
//~ Pcre2Type<16>::Pcre2Uchar **bufferptr,
//~ PCRE2_SIZE *bufflen){
//~ return pcre2_substring_get_bynumber_16(match_data, number, bufferptr, bufflen);
//~ }
//~ static int substring_get_byname(Pcre2Type<16>::MatchData *match_data,
//~ Pcre2Type<16>::Pcre2Sptr name,
//~ Pcre2Type<16>::Pcre2Uchar **bufferptr,
//~ PCRE2_SIZE *bufflen){
//~ return pcre2_substring_get_byname_16(match_data, name, bufferptr, bufflen);
//~ }
//~ static void substring_free(Pcre2Type<16>::Pcre2Uchar *buffer){
//~ pcre2_substring_free_16(buffer);
//~ }
//~ static Pcre2Type<16>::Pcre2Code * code_copy(const Pcre2Type<16>::Pcre2Code *code){
//~ return pcre2_code_copy_16(code);
//~ }
static void code_free(Pcre2Type<16>::Pcre2Code *code){
pcre2_code_free_16(code);
}
static int get_error_message( int errorcode,
Pcre2Type<16>::Pcre2Uchar *buffer,
PCRE2_SIZE bufflen){
return pcre2_get_error_message_16(errorcode, buffer, bufflen);
}
static Pcre2Type<16>::MatchData * match_data_create_from_pattern(
const Pcre2Type<16>::Pcre2Code *code,
Pcre2Type<16>::GeneralContext *gcontext){
return pcre2_match_data_create_from_pattern_16(code, gcontext);
}
static int match( const Pcre2Type<16>::Pcre2Code *code,
Pcre2Type<16>::Pcre2Sptr subject,
PCRE2_SIZE length,
PCRE2_SIZE startoffset,
uint32_t options,
Pcre2Type<16>::MatchData *match_data,
Pcre2Type<16>::MatchContext *mcontext){
return pcre2_match_16(code, subject, length, startoffset, options, match_data, mcontext);
}
static void match_data_free(Pcre2Type<16>::MatchData *match_data){
pcre2_match_data_free_16(match_data);
}
static PCRE2_SIZE * get_ovector_pointer(Pcre2Type<16>::MatchData *match_data){
return pcre2_get_ovector_pointer_16(match_data);
}
static int pattern_info(const Pcre2Type<16>::Pcre2Code *code, uint32_t what, void *where){
return pcre2_pattern_info_16(code, what, where);
}
static int set_newline(Pcre2Type<16>::CompileContext *ccontext, uint32_t value){
return pcre2_set_newline_16(ccontext, value);
}
//~ static void jit_stack_assign(Pcre2Type<16>::MatchContext *mcontext,
//~ Pcre2Type<16>::JitCallback callback_function,
//~ void *callback_data){
//~ pcre2_jit_stack_assign_16(mcontext, callback_function, callback_data);
//~ }
//~ static Pcre2Type<16>::JitStack *jit_stack_create(PCRE2_SIZE startsize, PCRE2_SIZE maxsize,
//~ Pcre2Type<16>::GeneralContext *gcontext){
//~ return pcre2_jit_stack_create_16(startsize, maxsize, gcontext);
//~ }
//~ static void jit_stack_free(Pcre2Type<16>::JitStack *jit_stack){
//~ pcre2_jit_stack_free_16(jit_stack);
//~ }
//~ static void jit_free_unused_memory(Pcre2Type<16>::GeneralContext *gcontext){
//~ pcre2_jit_free_unused_memory_16(gcontext);
//~ }
//~ static Pcre2Type<16>::MatchContext *match_context_create(Pcre2Type<16>::GeneralContext *gcontext){
//~ return pcre2_match_context_create_16(gcontext);
//~ }
//~ static Pcre2Type<16>::MatchContext *match_context_copy(Pcre2Type<16>::MatchContext *mcontext){
//~ return pcre2_match_context_copy_16(mcontext);
//~ }
//~ static void match_context_free(Pcre2Type<16>::MatchContext *mcontext){
//~ pcre2_match_context_free_16(mcontext);
//~ }
static uint32_t get_ovector_count(Pcre2Type<16>::MatchData *match_data){
return pcre2_get_ovector_count_16(match_data);
}
};
//32-bit version
template<> struct Pcre2Func<32> {
static Pcre2Type<32>::CompileContext* compile_context_create(Pcre2Type<32>::GeneralContext *gcontext){
return pcre2_compile_context_create_32(gcontext);
}
static void compile_context_free(Pcre2Type<32>::CompileContext *ccontext){
pcre2_compile_context_free_32(ccontext);
}
static Pcre2Type<32>::CompileContext* compile_context_copy(Pcre2Type<32>::CompileContext* ccontext){
return pcre2_compile_context_copy_32(ccontext);
}
static const unsigned char * maketables(Pcre2Type<32>::GeneralContext* gcontext){
return pcre2_maketables_32(gcontext);
}
static int set_character_tables(Pcre2Type<32>::CompileContext * ccontext, const unsigned char * table){
return pcre2_set_character_tables_32(ccontext, table);
}
static Pcre2Type<32>::Pcre2Code * compile(Pcre2Type<32>::Pcre2Sptr pattern,
PCRE2_SIZE length,
uint32_t options,
int *errorcode,
PCRE2_SIZE *erroroffset,
Pcre2Type<32>::CompileContext *ccontext){
return pcre2_compile_32(pattern, length, options, errorcode, erroroffset, ccontext);
}
static int jit_compile(Pcre2Type<32>::Pcre2Code *code, uint32_t options){
return pcre2_jit_compile_32(code, options);
}
static int substitute( const Pcre2Type<32>::Pcre2Code *code,
Pcre2Type<32>::Pcre2Sptr subject,
PCRE2_SIZE length,
PCRE2_SIZE startoffset,
uint32_t options,
Pcre2Type<32>::MatchData *match_data,
Pcre2Type<32>::MatchContext *mcontext,
Pcre2Type<32>::Pcre2Sptr replacement,
PCRE2_SIZE rlength,
Pcre2Type<32>::Pcre2Uchar *outputbuffer,
PCRE2_SIZE *outlengthptr){
return pcre2_substitute_32( code, subject, length, startoffset, options, match_data,
mcontext, replacement, rlength, outputbuffer, outlengthptr);
}
//~ static int substring_get_bynumber(Pcre2Type<32>::MatchData *match_data,
//~ uint32_t number,
//~ Pcre2Type<32>::Pcre2Uchar **bufferptr,
//~ PCRE2_SIZE *bufflen){
//~ return pcre2_substring_get_bynumber_32(match_data, number, bufferptr, bufflen);
//~ }
//~ static int substring_get_byname(Pcre2Type<32>::MatchData *match_data,
//~ Pcre2Type<32>::Pcre2Sptr name,
//~ Pcre2Type<32>::Pcre2Uchar **bufferptr,
//~ PCRE2_SIZE *bufflen){
//~ return pcre2_substring_get_byname_32(match_data, name, bufferptr, bufflen);
//~ }
//~ static void substring_free(Pcre2Type<32>::Pcre2Uchar *buffer){
//~ pcre2_substring_free_32(buffer);
//~ }
//~ static Pcre2Type<32>::Pcre2Code * code_copy(const Pcre2Type<32>::Pcre2Code *code){
//~ return pcre2_code_copy_32(code);
//~ }
static void code_free(Pcre2Type<32>::Pcre2Code *code){
pcre2_code_free_32(code);
}
static int get_error_message( int errorcode,
Pcre2Type<32>::Pcre2Uchar *buffer,
PCRE2_SIZE bufflen){
return pcre2_get_error_message_32(errorcode, buffer, bufflen);
}
static Pcre2Type<32>::MatchData * match_data_create_from_pattern(
const Pcre2Type<32>::Pcre2Code *code,
Pcre2Type<32>::GeneralContext *gcontext){
return pcre2_match_data_create_from_pattern_32(code, gcontext);
}
static int match( const Pcre2Type<32>::Pcre2Code *code,
Pcre2Type<32>::Pcre2Sptr subject,
PCRE2_SIZE length,
PCRE2_SIZE startoffset,
uint32_t options,
Pcre2Type<32>::MatchData *match_data,
Pcre2Type<32>::MatchContext *mcontext){
return pcre2_match_32(code, subject, length, startoffset, options, match_data, mcontext);
}
static void match_data_free(Pcre2Type<32>::MatchData *match_data){
pcre2_match_data_free_32(match_data);
}
static PCRE2_SIZE * get_ovector_pointer(Pcre2Type<32>::MatchData *match_data){
return pcre2_get_ovector_pointer_32(match_data);
}
static int pattern_info(const Pcre2Type<32>::Pcre2Code *code, uint32_t what, void *where){
return pcre2_pattern_info_32(code, what, where);
}
static int set_newline(Pcre2Type<32>::CompileContext *ccontext, uint32_t value){
return pcre2_set_newline_32(ccontext, value);
}
//~ static void jit_stack_assign(Pcre2Type<32>::MatchContext *mcontext,
//~ Pcre2Type<32>::JitCallback callback_function,
//~ void *callback_data){
//~ pcre2_jit_stack_assign_32(mcontext, callback_function, callback_data);
//~ }
//~ static Pcre2Type<32>::JitStack *jit_stack_create(PCRE2_SIZE startsize, PCRE2_SIZE maxsize,
//~ Pcre2Type<32>::GeneralContext *gcontext){
//~ return pcre2_jit_stack_create_32(startsize, maxsize, gcontext);
//~ }
//~ static void jit_stack_free(Pcre2Type<32>::JitStack *jit_stack){
//~ pcre2_jit_stack_free_32(jit_stack);
//~ }
//~ static void jit_free_unused_memory(Pcre2Type<32>::GeneralContext *gcontext){
//~ pcre2_jit_free_unused_memory_32(gcontext);
//~ }
//~ static Pcre2Type<32>::MatchContext *match_context_create(Pcre2Type<32>::GeneralContext *gcontext){
//~ return pcre2_match_context_create_32(gcontext);
//~ }
//~ static Pcre2Type<32>::MatchContext *match_context_copy(Pcre2Type<32>::MatchContext *mcontext){
//~ return pcre2_match_context_copy_32(mcontext);
//~ }
//~ static void match_context_free(Pcre2Type<32>::MatchContext *mcontext){
//~ pcre2_match_context_free_32(mcontext);
//~ }
static uint32_t get_ovector_count(Pcre2Type<32>::MatchData *match_data){
return pcre2_get_ovector_count_32(match_data);
}
};
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///Class to take a std::string modifier value with null safety.
///You don't need to make an instance of this class to pass modifier,
///just pass std::string or char const*, whatever seems feasible,
///implicit conversion will kick in and take care of things for you.
class Modifier{
std::string mod;
public:
///Default constructor.
Modifier(){}
///Constructor that takes a std::string.
///@param x std::string as a reference.
Modifier(std::string const& x):mod(x){}
///Constructor that takes char const * (null safety is provided by this one)
///@param x char const *
Modifier(char const *x):mod(x?x:""){}
///Returns the modifier string
///@return modifier string (std::string)
std::string str() const { return mod; }
///Returns the c_str() of modifier string
///@return char const *
char const * c_str() const { return mod.c_str(); }
///Returns the length of the modifier string
///@return length
SIZE_T length() const{ return mod.length(); }
///operator[] overload to access character by index.
///@param i index
///@return character at index i.
char operator[](SIZE_T i) const { return mod[i]; }
};
// Namespace for modifier constants.
// For each modifier constant there is a jpcre2::Uint option value.
// Some modifiers may have multiple values set together (ORed in bitwise operation) and
// thus they may include other modifiers. Such an example is the 'n' modifier. It is combined together with 'u'.
namespace MOD {
// Define modifiers for compile
// String of compile modifier characters for PCRE2 options
static const char C_N[] = "eijmnsuxADJU";
// Array of compile modifier values for PCRE2 options
// Uint is being used in getModifier() in for loop to get the number of element in this array,
// be sure to chnage there if you change here.
static const jpcre2::Uint C_V[12] = { PCRE2_MATCH_UNSET_BACKREF, // Modifier e
PCRE2_CASELESS, // Modifier i
PCRE2_ALT_BSUX | PCRE2_MATCH_UNSET_BACKREF, // Modifier j
PCRE2_MULTILINE, // Modifier m
PCRE2_UTF | PCRE2_UCP, // Modifier n (includes u)
PCRE2_DOTALL, // Modifier s
PCRE2_UTF, // Modifier u
PCRE2_EXTENDED, // Modifier x
PCRE2_ANCHORED, // Modifier A
PCRE2_DOLLAR_ENDONLY, // Modifier D
PCRE2_DUPNAMES, // Modifier J
PCRE2_UNGREEDY // Modifier U
};
// String of compile modifier characters for JPCRE2 options
static const char CJ_N[] = "S";
// Array of compile modifier values for JPCRE2 options
static const jpcre2::Uint CJ_V[1] = { JIT_COMPILE, // Modifier S
};
// Define modifiers for replace
// String of action (replace) modifier characters for PCRE2 options
static const char R_N[] = "eEgx";
// Array of action (replace) modifier values for PCRE2 options
static const jpcre2::Uint R_V[4] = { PCRE2_SUBSTITUTE_UNSET_EMPTY, // Modifier e
PCRE2_SUBSTITUTE_UNKNOWN_UNSET | PCRE2_SUBSTITUTE_UNSET_EMPTY, // Modifier E (includes e)
PCRE2_SUBSTITUTE_GLOBAL, // Modifier g
PCRE2_SUBSTITUTE_EXTENDED // Modifier x
};
// String of action (replace) modifier characters for JPCRE2 options
static const char RJ_N[] = "";
// Array of action (replace) modifier values for JPCRE2 options
static const jpcre2::Uint RJ_V[1] = { NONE //placeholder
};
// Define modifiers for match
// String of action (match) modifier characters for PCRE2 options
static const char M_N[] = "A";
// Array of action (match) modifier values for PCRE2 options
static const jpcre2::Uint M_V[1] = { PCRE2_ANCHORED // Modifier A
};
// String of action (match) modifier characters for JPCRE2 options
static const char MJ_N[] = "g";
// Array of action (match) modifier values for JPCRE2 options
static const jpcre2::Uint MJ_V[1] = { FIND_ALL, // Modifier g
};
static inline void toOption(Modifier const& mod, bool x,
Uint const * J_V, char const * J_N, SIZE_T SJ,
Uint const * V, char const * N, SIZE_T S,
Uint* po, Uint* jo,
int* en, SIZE_T* eo
){
//loop through mod
SIZE_T n = mod.length();
for (SIZE_T i = 0; i < n; ++i) {
//First check for JPCRE2 mods
for(SIZE_T j = 0; j < SJ; ++j){
if(J_N[j] == mod[i]) {
if(x) *jo |= J_V[j];
else *jo &= ~J_V[j];
goto endfor;
}
}
//Now check for PCRE2 mods
for(SIZE_T j = 0; j< S; ++j){
if(N[j] == mod[i]){
if(x) *po |= V[j];
else *po &= ~V[j];
goto endfor;
}
}
//Modifier didn't match, invalid modifier
*en = (int)ERROR::INVALID_MODIFIER;
*eo = (int)mod[i];
endfor:;
}
}
static inline void toMatchOption(Modifier const& mod, bool x, Uint* po, Uint* jo, int* en, SIZE_T* eo){
toOption(mod, x,
MJ_V, MJ_N, sizeof(MJ_V)/sizeof(Uint),
M_V, M_N, sizeof(M_V)/sizeof(Uint),
po, jo, en, eo);
}
static inline void toReplaceOption(Modifier const& mod, bool x, Uint* po, Uint* jo, int* en, SIZE_T* eo){
toOption(mod, x,
RJ_V, RJ_N, sizeof(RJ_V)/sizeof(Uint),
R_V, R_N, sizeof(R_V)/sizeof(Uint),
po, jo, en, eo);
}
static inline void toCompileOption(Modifier const& mod, bool x, Uint* po, Uint* jo, int* en, SIZE_T* eo){
toOption(mod, x,
CJ_V, CJ_N, sizeof(CJ_V)/sizeof(Uint),
C_V, C_N, sizeof(C_V)/sizeof(Uint),
po, jo, en, eo);
}
static inline std::string fromOption(Uint const * J_V, char const * J_N, SIZE_T SJ,
Uint const * V, char const * N, SIZE_T S,
Uint po, Uint jo
){
std::string mod;
//Calculate PCRE2 mod
for(SIZE_T i = 0; i < S; ++i){
if( (V[i] & po) != 0 &&
(V[i] & po) == V[i]) //One option can include other
mod += N[i];
}
//Calculate JPCRE2 mod
for(SIZE_T i = 0; i < SJ; ++i){
if( (J_V[i] & jo) != 0 &&
(J_V[i] & jo) == J_V[i]) //One option can include other
mod += J_N[i];
}
return mod;
}
static inline std::string fromMatchOption(Uint po, Uint jo){
return fromOption(MJ_V, MJ_N, sizeof(MJ_V)/sizeof(Uint),
M_V, M_N, sizeof(M_V)/sizeof(Uint),
po, jo);
}
static inline std::string fromReplaceOption(Uint po, Uint jo){
return fromOption(RJ_V, RJ_N, sizeof(RJ_V)/sizeof(Uint),
R_V, R_N, sizeof(R_V)/sizeof(Uint),
po, jo);
}
static inline std::string fromCompileOption(Uint po, Uint jo){
return fromOption(CJ_V, CJ_N, sizeof(CJ_V)/sizeof(Uint),
C_V, C_N, sizeof(C_V)/sizeof(Uint),
po, jo);
}
} //MOD namespace ends
///Lets you create custom modifier tables.
///An instance of this class can be passed to
///match, replace or compile related class objects.
class ModifierTable{
std::string tabjms;
std::string tabms;
std::string tabjrs;
std::string tabrs;
std::string tabjcs;
std::string tabcs;
VecOpt tabjmv;
VecOpt tabmv;
VecOpt tabjrv;
VecOpt tabrv;
VecOpt tabjcv;
VecOpt tabcv;
void toOption(Modifier const& mod, bool x,
VecOpt const& J_V, std::string const& J_N,
VecOpt const& V, std::string const& N,
Uint* po, Uint* jo, int* en, SIZE_T* eo
) const{
SIZE_T SJ = J_V.size();
SIZE_T S = V.size();
JPCRE2_ASSERT(SJ == J_N.length(), ("ValueError: Modifier character and value table must be of the same size (" + _tostdstring(SJ) + " == " + _tostdstring(J_N.length()) + ").").c_str());
JPCRE2_ASSERT(S == N.length(), ("ValueError: Modifier character and value table must be of the same size (" + _tostdstring(S) + " == " + _tostdstring(N.length()) + ").").c_str());
MOD::toOption(mod, x,
J_V.empty()?0:&J_V[0], J_N.c_str(), SJ,
V.empty()?0:&V[0], N.c_str(), S,
po, jo, en, eo
);
}
std::string fromOption(VecOpt const& J_V, std::string const& J_N,
VecOpt const& V, std::string const& N,
Uint po, Uint jo) const{
SIZE_T SJ = J_V.size();
SIZE_T S = V.size();
JPCRE2_ASSERT(SJ == J_N.length(), ("ValueError: Modifier character and value table must be of the same size (" + _tostdstring(SJ) + " == " + _tostdstring(J_N.length()) + ").").c_str());
JPCRE2_ASSERT(S == N.length(), ("ValueError: Modifier character and value table must be of the same size (" + _tostdstring(S) + " == " + _tostdstring(N.length()) + ").").c_str());
return MOD::fromOption(J_V.empty()?0:&J_V[0], J_N.c_str(), SJ,
V.empty()?0:&V[0], N.c_str(), S,
po, jo);
}
void parseModifierTable(std::string& tabjs, VecOpt& tabjv,
std::string& tab_s, VecOpt& tab_v,
std::string const& tabs, VecOpt const& tabv);
public:
///Default constructor that creates an empty modifier table.
ModifierTable(){}
///@overload
///@param deflt Initialize with default table if true, otherwise keep empty.
ModifierTable(bool deflt){
if(deflt) setAllToDefault();
}
///Reset the match modifier table to its initial (empty) state including memory.
///@return A reference to the calling ModifierTable object.
ModifierTable& resetMatchModifierTable(){
std::string().swap(tabjms);
std::string().swap(tabms);
VecOpt().swap(tabjmv);
VecOpt().swap(tabmv);
return *this;
}
///Reset the replace modifier table to its initial (empty) state including memory.
///@return A reference to the calling ModifierTable object.
ModifierTable& resetReplaceModifierTable(){
std::string().swap(tabjrs);
std::string().swap(tabrs);
VecOpt().swap(tabjrv);
VecOpt().swap(tabrv);
return *this;
}
///Reset the compile modifier table to its initial (empty) state including memory.
///@return A reference to the calling ModifierTable object.
ModifierTable& resetCompileModifierTable(){
std::string().swap(tabjcs);
std::string().swap(tabcs);
VecOpt().swap(tabjcv);
VecOpt().swap(tabcv);
return *this;
}
///Reset the modifier tables to their initial (empty) state including memory.
///@return A reference to the calling ModifierTable object.
ModifierTable& reset(){
resetMatchModifierTable();
resetReplaceModifierTable();
resetCompileModifierTable();
return *this;
}
///Clear the match modifier table to its initial (empty) state.
///Memory may retain for further use.
///@return A reference to the calling ModifierTable object.
ModifierTable& clearMatchModifierTable(){
tabjms.clear();
tabms.clear();
tabjmv.clear();
tabmv.clear();
return *this;
}
///Clear the replace modifier table to its initial (empty) state.
///Memory may retain for further use.
///@return A reference to the calling ModifierTable object.
ModifierTable& clearReplaceModifierTable(){
tabjrs.clear();
tabrs.clear();
tabjrv.clear();
tabrv.clear();
return *this;
}
///Clear the compile modifier table to its initial (empty) state.
///Memory may retain for further use.
///@return A reference to the calling ModifierTable object.
ModifierTable& clearCompileModifierTable(){
tabjcs.clear();
tabcs.clear();
tabjcv.clear();
tabcv.clear();
return *this;
}
///Clear the modifier tables to their initial (empty) state.
///Memory may retain for further use.
///@return A reference to the calling ModifierTable object.
ModifierTable& clear(){
clearMatchModifierTable();
clearReplaceModifierTable();
clearCompileModifierTable();
return *this;
}
///Modifier parser for match related options.
///@param mod modifier string
///@param x whether to add or remove the modifers.
///@param po pointer to PCRE2 match option that will be modified.
///@param jo pointer to JPCRE2 match option that will be modified.
///@param en where to put the error number.
///@param eo where to put the error offset.
void toMatchOption(Modifier const& mod, bool x, Uint* po, Uint* jo, int* en, SIZE_T* eo) const {
toOption(mod, x,tabjmv,tabjms,tabmv, tabms,po,jo,en,eo);
}
///Modifier parser for replace related options.
///@param mod modifier string
///@param x whether to add or remove the modifers.
///@param po pointer to PCRE2 replace option that will be modified.
///@param jo pointer to JPCRE2 replace option that will be modified.
///@param en where to put the error number.
///@param eo where to put the error offset.
void toReplaceOption(Modifier const& mod, bool x, Uint* po, Uint* jo, int* en, SIZE_T* eo) const {
return toOption(mod, x,tabjrv,tabjrs,tabrv,tabrs,po,jo,en,eo);
}
///Modifier parser for compile related options.
///@param mod modifier string
///@param x whether to add or remove the modifers.
///@param po pointer to PCRE2 compile option that will be modified.
///@param jo pointer to JPCRE2 compile option that will be modified.
///@param en where to put the error number.
///@param eo where to put the error offset.
void toCompileOption(Modifier const& mod, bool x, Uint* po, Uint* jo, int* en, SIZE_T* eo) const {