This repository has been archived by the owner on Mar 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
re2.cpp
1736 lines (1457 loc) · 52.9 KB
/
re2.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
/*
+----------------------------------------------------------------------+
| PHP Version 5 |
+----------------------------------------------------------------------+
| Copyright (c) 1997-2011 The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.php.net/license/3_01.txt |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| [email protected] so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Author: Arpad Ray <[email protected]> |
| Author: Leif Jackson <[email protected]> |
+----------------------------------------------------------------------+
*/
extern "C" {
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "php.h"
#include "php_ini.h"
#include "ext/standard/info.h"
#include "Zend/zend_exceptions.h"
}
#include "php_re2.h"
#include <re2/re2.h>
#include <re2/set.h>
#include <string>
#if ZEND_MODULE_API_NO < 20090626
# define array_init_size(arg, size) array_init(arg)
# define Z_ADDREF_P(arg) ZVAL_ADDREF(arg)
# define Z_ADDREF_PP(arg) ZVAL_ADDREF(*(arg))
# define Z_DELREF_P(arg) ZVAL_DELREF(arg)
# define Z_DELREF_PP(arg) ZVAL_DELREF(*(arg))
#endif
ZEND_DECLARE_MODULE_GLOBALS(re2);
static PHP_GINIT_FUNCTION(re2)
{
memset(re2_globals, 0, sizeof(zend_re2_globals));
}
/* {{{ arg info */
ZEND_BEGIN_ARG_INFO_EX(arginfo_re2_match, 0, 0, 2)
ZEND_ARG_INFO(0, pattern)
ZEND_ARG_INFO(0, subject)
ZEND_ARG_INFO(1, matches)
ZEND_ARG_INFO(0, flags)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_re2_match_all, 0, 0, 3)
ZEND_ARG_INFO(0, pattern)
ZEND_ARG_INFO(0, subject)
ZEND_ARG_INFO(1, matches)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_re2_replace, 0, 0, 3)
ZEND_ARG_INFO(0, pattern)
ZEND_ARG_INFO(0, subject)
ZEND_ARG_INFO(0, replace)
ZEND_ARG_INFO(0, flags)
ZEND_ARG_INFO(1, count)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_re2_grep, 0, 0, 2)
ZEND_ARG_INFO(0, pattern)
ZEND_ARG_INFO(0, input)
ZEND_ARG_INFO(0, flags)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_re2_split, 0, 0, 2)
ZEND_ARG_INFO(0, pattern)
ZEND_ARG_INFO(0, subject)
ZEND_ARG_INFO(0, limit)
ZEND_ARG_INFO(0, flags)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_re2_construct, 0, 0, 1)
ZEND_ARG_INFO(0, pattern)
ZEND_ARG_INFO(0, options)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_re2_one_arg, 0, 0, 1)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_re2_set_construct, 0, 0, 1)
ZEND_ARG_INFO(0, options)
ZEND_ARG_INFO(0, anchor)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_re2_set_add, 0, 0, 2)
ZEND_ARG_INFO(0, pattern)
ZEND_ARG_INFO(0, error)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_re2_set_match, 0, 0, 2)
ZEND_ARG_INFO(0, subject)
ZEND_ARG_INFO(0, match_indexes)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_re2_no_args, 0, 0, 0)
ZEND_END_ARG_INFO()
/* }}} */
/* {{{ re2_functions[]
*/
static zend_function_entry re2_functions[] = {
PHP_FE(re2_match, arginfo_re2_match)
PHP_FE(re2_match_all, arginfo_re2_match_all)
PHP_FE(re2_replace, arginfo_re2_replace)
PHP_FE(re2_replace_callback, arginfo_re2_replace)
PHP_FE(re2_filter, arginfo_re2_replace)
PHP_FE(re2_grep, arginfo_re2_grep)
PHP_FE(re2_split, arginfo_re2_split)
PHP_FE(re2_quote, arginfo_re2_one_arg)
{NULL, NULL, NULL}
};
/* }}} */
/* {{{ RE2 classes */
zend_class_entry *php_re2_illegal_state_exception_class_entry;
#define PHP_RE2_ILLEGAL_STATE_EXCEPTION_CLASS_NAME "Re2IllegalStateException"
zend_class_entry *php_re2_invalid_pattern_exception_class_entry;
#define PHP_RE2_INVALID_PATTERN_EXCEPTION_CLASS_NAME "Re2InvalidPatternException"
zend_class_entry *php_re2_internal_error_exception_class_entry;
#define PHP_RE2_INTERNAL_ERROR_EXCEPTION_CLASS_NAME "Re2InternalErrorException"
zend_class_entry *php_re2_class_entry;
#define PHP_RE2_CLASS_NAME "RE2"
static zend_function_entry re2_class_functions[] = {
PHP_ME(RE2, __construct, arginfo_re2_construct, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR)
PHP_ME(RE2, getOptions, arginfo_re2_no_args, ZEND_ACC_PUBLIC)
PHP_ME(RE2, getPattern, arginfo_re2_no_args, ZEND_ACC_PUBLIC)
PHP_MALIAS(RE2, __toString, getPattern, arginfo_re2_no_args, ZEND_ACC_PUBLIC)
{NULL, NULL, NULL}
};
zend_class_entry *php_re2_options_class_entry;
#define PHP_RE2_OPTIONS_CLASS_NAME "Re2Options"
static zend_function_entry re2_options_class_functions[] = {
PHP_ME(Re2Options, __construct, arginfo_re2_no_args, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR)
PHP_ME(Re2Options, getEncoding, arginfo_re2_no_args, ZEND_ACC_PUBLIC)
PHP_ME(Re2Options, getMaxMem, arginfo_re2_no_args, ZEND_ACC_PUBLIC)
PHP_ME(Re2Options, getPosixSyntax, arginfo_re2_no_args, ZEND_ACC_PUBLIC)
PHP_ME(Re2Options, getLongestMatch, arginfo_re2_no_args, ZEND_ACC_PUBLIC)
PHP_ME(Re2Options, getLogErrors, arginfo_re2_no_args, ZEND_ACC_PUBLIC)
PHP_ME(Re2Options, getLiteral, arginfo_re2_no_args, ZEND_ACC_PUBLIC)
PHP_ME(Re2Options, getNeverNl, arginfo_re2_no_args, ZEND_ACC_PUBLIC)
PHP_ME(Re2Options, getCaseSensitive, arginfo_re2_no_args, ZEND_ACC_PUBLIC)
PHP_ME(Re2Options, getPerlClasses, arginfo_re2_no_args, ZEND_ACC_PUBLIC)
PHP_ME(Re2Options, getWordBoundary, arginfo_re2_no_args, ZEND_ACC_PUBLIC)
PHP_ME(Re2Options, getOneLine, arginfo_re2_no_args, ZEND_ACC_PUBLIC)
PHP_ME(Re2Options, setEncoding, arginfo_re2_one_arg, ZEND_ACC_PUBLIC)
PHP_ME(Re2Options, setMaxMem, arginfo_re2_one_arg, ZEND_ACC_PUBLIC)
PHP_ME(Re2Options, setPosixSyntax, arginfo_re2_one_arg, ZEND_ACC_PUBLIC)
PHP_ME(Re2Options, setLongestMatch, arginfo_re2_one_arg, ZEND_ACC_PUBLIC)
PHP_ME(Re2Options, setLogErrors, arginfo_re2_one_arg, ZEND_ACC_PUBLIC)
PHP_ME(Re2Options, setLiteral, arginfo_re2_one_arg, ZEND_ACC_PUBLIC)
PHP_ME(Re2Options, setNeverNl, arginfo_re2_one_arg, ZEND_ACC_PUBLIC)
PHP_ME(Re2Options, setCaseSensitive, arginfo_re2_one_arg, ZEND_ACC_PUBLIC)
PHP_ME(Re2Options, setPerlClasses, arginfo_re2_one_arg, ZEND_ACC_PUBLIC)
PHP_ME(Re2Options, setWordBoundary, arginfo_re2_one_arg, ZEND_ACC_PUBLIC)
PHP_ME(Re2Options, setOneLine, arginfo_re2_one_arg, ZEND_ACC_PUBLIC)
{NULL, NULL, NULL}
};
zend_class_entry *php_re2_set_class_entry;
#define PHP_RE2_SET_CLASS_NAME "Re2Set"
static zend_function_entry re2_set_class_functions[] = {
PHP_ME(Re2Set, __construct, arginfo_re2_set_construct, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR)
PHP_ME(Re2Set, add, arginfo_re2_set_add, ZEND_ACC_PUBLIC)
PHP_ME(Re2Set, compile, arginfo_re2_no_args, ZEND_ACC_PUBLIC)
PHP_ME(Re2Set, match, arginfo_re2_set_match, ZEND_ACC_PUBLIC)
{NULL, NULL, NULL}
};
/* }}} */
/* {{{ RE2 object handlers */
zend_object_handlers re2_object_handlers;
zend_object_handlers re2_options_object_handlers;
zend_object_handlers re2_set_object_handlers;
struct re2_object {
zend_object std;
RE2 *re;
bool cached;
};
struct re2_options_object {
zend_object std;
RE2::Options *options;
};
struct re2_set_object {
zend_object std;
RE2::Set *re2_set;
};
void re2_free_storage(void *object TSRMLS_DC)
{
re2_object *obj = (re2_object *)object;
if (!obj->cached) {
delete obj->re;
}
zend_objects_free_object_storage((zend_object *)object TSRMLS_CC);
}
zend_object_value re2_object_new_ex(zend_class_entry *type, re2_object **ptr TSRMLS_DC)
{
zval *tmp;
zend_object_value retval;
re2_object *obj;
obj = (re2_object *)emalloc(sizeof(re2_object));
memset(obj, 0, sizeof(re2_object));
if (ptr) {
*ptr = obj;
}
zend_object_std_init(&obj->std, type TSRMLS_CC);
#if ZEND_MODULE_API_NO >= 20100409
object_properties_init(&obj->std, type);
#else
zend_hash_copy(obj->std.properties, &type->default_properties, (copy_ctor_func_t)zval_add_ref, (void *)&tmp, sizeof(zval *));
#endif
retval.handle = zend_objects_store_put(obj, NULL, re2_free_storage, NULL TSRMLS_CC);
retval.handlers = &re2_object_handlers;
return retval;
}
zend_object_value re2_object_new(zend_class_entry *type TSRMLS_DC)
{
return re2_object_new_ex(type, NULL TSRMLS_CC);
}
zend_object_value re2_object_clone(zval *this_ptr TSRMLS_DC)
{
re2_options_object *options_obj = NULL;
zval *options = NULL;
re2_object *new_obj = NULL;
re2_object *old_obj = (re2_object *)zend_object_store_get_object(this_ptr TSRMLS_CC);
zend_object_value retval = re2_object_new_ex(old_obj->std.ce, &new_obj TSRMLS_CC);
zend_objects_clone_members(&new_obj->std, retval, &old_obj->std, Z_OBJ_HANDLE_P(this_ptr) TSRMLS_CC);
new_obj->re = new RE2(old_obj->re->pattern());
new_obj->cached = old_obj->cached;
/*
options = zend_read_property(old_obj->std.ce, this_ptr, "options", strlen("options"), 1 TSRMLS_CC);
options_obj = (re2_options_object *)zend_object_store_get_object(options TSRMLS_CC);
new_obj->re = new RE2(old_obj->re->pattern(), *options_obj->options);
Z_DELREF_P(options);
*/
return retval;
}
void re2_options_free_storage(void *object TSRMLS_DC)
{
re2_options_object *obj = (re2_options_object *)object;
delete obj->options;
zend_objects_free_object_storage((zend_object *)object TSRMLS_CC);
}
zend_object_value re2_options_object_new_ex(zend_class_entry *type, re2_options_object **ptr TSRMLS_DC)
{
zval *tmp;
zend_object_value retval;
re2_options_object *obj = (re2_options_object *)emalloc(sizeof(re2_options_object));
memset(obj, 0, sizeof(re2_options_object));
obj->std.ce = type;
if (ptr) {
*ptr = obj;
}
zend_object_std_init(&obj->std, type TSRMLS_CC);
#if ZEND_MODULE_API_NO >= 20100409
object_properties_init(&obj->std, type);
#else
zend_hash_copy(obj->std.properties, &type->default_properties, (copy_ctor_func_t)zval_add_ref, (void *)&tmp, sizeof(zval *));
#endif
retval.handle = zend_objects_store_put(obj, NULL, re2_options_free_storage, NULL TSRMLS_CC);
retval.handlers = &re2_options_object_handlers;
return retval;
}
zend_object_value re2_options_object_new(zend_class_entry *type TSRMLS_DC)
{
return re2_options_object_new_ex(type, NULL TSRMLS_CC);
}
zend_object_value re2_options_object_clone(zval *this_ptr TSRMLS_DC)
{
RE2::Options *options = NULL;
re2_options_object *new_obj = NULL;
re2_options_object *old_obj = (re2_options_object *)zend_object_store_get_object(this_ptr TSRMLS_CC);
zend_object_value retval = re2_options_object_new_ex(old_obj->std.ce, &new_obj TSRMLS_CC);
zend_objects_clone_members(&new_obj->std, retval, &old_obj->std, Z_OBJ_HANDLE_P(this_ptr) TSRMLS_CC);
new_obj->options = new RE2::Options();
/* grrr.. why is Copy() private? */
new_obj->options->set_encoding(old_obj->options->encoding());
new_obj->options->set_posix_syntax(old_obj->options->posix_syntax());
new_obj->options->set_longest_match(old_obj->options->longest_match());
new_obj->options->set_log_errors(old_obj->options->log_errors());
new_obj->options->set_max_mem(old_obj->options->max_mem());
new_obj->options->set_literal(old_obj->options->literal());
new_obj->options->set_never_nl(old_obj->options->never_nl());
new_obj->options->set_case_sensitive(old_obj->options->case_sensitive());
new_obj->options->set_perl_classes(old_obj->options->perl_classes());
new_obj->options->set_word_boundary(old_obj->options->word_boundary());
new_obj->options->set_one_line(old_obj->options->one_line());
return retval;
}
void re2_set_free_storage(void *object TSRMLS_DC)
{
re2_set_object *obj = (re2_set_object *)object;
delete obj->re2_set;
zend_objects_free_object_storage((zend_object *)object TSRMLS_CC);
}
zend_object_value re2_set_object_new_ex(zend_class_entry *type, re2_set_object **ptr TSRMLS_DC)
{
zval *tmp, *hasPattern, *isCompiled;
zend_object_value retval;
re2_set_object *obj = (re2_set_object *)emalloc(sizeof(re2_set_object));
memset(obj, 0, sizeof(re2_set_object));
obj->std.ce = type;
if (ptr) {
*ptr = obj;
}
zend_object_std_init(&obj->std, type TSRMLS_CC);
#if ZEND_MODULE_API_NO >= 20100409
object_properties_init(&obj->std, type);
#else
zend_hash_copy(obj->std.properties, &type->default_properties, (copy_ctor_func_t)zval_add_ref, (void *)&tmp, sizeof(zval *));
#endif
retval.handle = zend_objects_store_put(obj, NULL, re2_set_free_storage, NULL TSRMLS_CC);
retval.handlers = &re2_set_object_handlers;
return retval;
}
zend_object_value re2_set_object_new(zend_class_entry *type TSRMLS_DC)
{
return re2_set_object_new_ex(type, NULL TSRMLS_CC);
}
/* }}} */
/* {{{ constants */
#define RE2_ANCHOR_NONE 0
#define RE2_ANCHOR_START 1
#define RE2_ANCHOR_BOTH 2
#define RE2_GREP_INVERT 4
#define RE2_OFFSET_CAPTURE 8
#define RE2_PATTERN_ORDER 16
#define RE2_SET_ORDER 32
#define RE2_SPLIT_DELIM_CAPTURE 64
#define RE2_SPLIT_NO_EMPTY 128
/* }}} */
/* {{{ match helpers */
/* _php2_re2_get_anchor_from_flags() {{{ */
static RE2::Anchor _php_re2_get_anchor_from_flags(int flags)
{
if (flags & RE2_ANCHOR_BOTH) {
return RE2::ANCHOR_BOTH;
}
if (flags & RE2_ANCHOR_START) {
return RE2::ANCHOR_START;
}
return RE2::UNANCHORED;
}
/* }}} */
#define RE2_MATCH_TO_ZVAL_PIECE(ptr, len, offset) \
{ \
char *match = ptr ? estrndup(ptr, len) : NULL; \
piece = NULL; \
MAKE_STD_ZVAL(piece); \
if (flags & RE2_OFFSET_CAPTURE) { \
array_init_size(piece, 2); \
add_next_index_stringl(piece, match, len, 1); \
add_next_index_long(piece, ptr == NULL ? -1 : offset); \
} else { \
ZVAL_STRINGL(piece, (char *)match, len, 1); \
} \
if (match) { \
efree(match); \
} \
}
/* _php2_re2_populate_matches() {{{ */
static void _php_re2_populate_matches(RE2 *re, zval **matches, re2::StringPiece subject_piece, re2::StringPiece *pieces, int argc, long flags)
{
zval *piece = NULL;
std::string *str;
const std::map<int, std::string> named_groups = re->CapturingGroupNames();
for (int i = 0, j = 0; i < argc; i++) {
RE2_MATCH_TO_ZVAL_PIECE(pieces[i].data(), pieces[i].size(), pieces[i].data() - subject_piece.data());
std::map<int, std::string>::const_iterator iter = named_groups.find(i);
if (iter != named_groups.end()) {
/* this index also has a named group */
std::string name = iter->second;
Z_ADDREF_P(piece);
add_assoc_zval_ex(flags & RE2_PATTERN_ORDER ? matches[j++] : *matches, (char *)name.c_str(), name.length() + 1, piece);
}
add_next_index_zval(flags & RE2_PATTERN_ORDER ? matches[j++] : *matches, piece);
piece = NULL;
}
}
/* }}} */
/* _php2_re2_get_backref() {{{
Copied completely from ext/pcre/pcre.c, preg_get_backref */
static int _php_re2_get_backref(const char **str, int *backref)
{
register char in_brace = 0;
register const char *walk = *str;
if (walk[1] == 0)
return 0;
if (*walk == '$' && walk[1] == '{') {
in_brace = 1;
walk++;
}
walk++;
if (*walk >= '0' && *walk <= '9') {
*backref = *walk - '0';
walk++;
} else
return 0;
if (*walk && *walk >= '0' && *walk <= '9') {
*backref = *backref * 10 + *walk - '0';
walk++;
}
if (in_brace) {
if (*walk == 0 || *walk != '}')
return 0;
else
walk++;
}
*str = walk;
return 1;
}
/* }}} */
/* _php2_re2_match_common() {{{ */
static long _php_re2_match_common(RE2 *re, zval **matches, zval *matches_out,
const char *subject, long subject_len, std::string *out, zval *out_array,
const char *replace, long replace_len, zend_fcall_info *replace_fci, zend_fcall_info_cache *replace_fci_cache,
int limit, long offset, int argc, long flags TSRMLS_DC)
{
const char *start_ptr, *ptr, *end_ptr, *last_ptr = NULL;
re2::StringPiece subject_piece = re2::StringPiece(subject, subject_len);
re2::StringPiece pieces[++argc];
const std::map<int, std::string> named_groups = re->CapturingGroupNames();
int num_groups = argc + named_groups.size();
long end_pos, num_matches = 0;
bool was_empty = false;
zval *match_array, *piece;
if (limit < 0) {
limit = 0;
}
start_ptr = subject_piece.data();
ptr = start_ptr + offset;
end_ptr = subject_piece.end();
end_pos = subject_piece.size();
while (ptr < end_ptr && re->Match(subject_piece, ptr - start_ptr, end_pos, RE2::UNANCHORED, pieces, argc)) {
if (ptr < pieces[0].begin()) {
if (out) {
out->append(ptr, pieces[0].begin() - ptr);
} else if (out_array) {
RE2_MATCH_TO_ZVAL_PIECE(ptr, pieces[0].begin() - ptr, ptr - start_ptr);
add_next_index_zval(out_array, piece);
}
} else if (out_array && !(flags & RE2_SPLIT_NO_EMPTY)) {
/* fudge for re2_split with empty pattern */
if (was_empty && !(flags & RE2_SPLIT_DELIM_CAPTURE) && (ptr != last_ptr || ptr == start_ptr)) {
RE2_MATCH_TO_ZVAL_PIECE(pieces[0].data(), pieces[0].size(), ptr - start_ptr);
add_next_index_zval(out_array, piece);
} else if (pieces[0].size()) {
RE2_MATCH_TO_ZVAL_PIECE(ptr, 0, ptr - start_ptr);
add_next_index_zval(out_array, piece);
}
}
if (pieces[0].begin() == last_ptr && pieces[0].size() == 0) {
/* allow only one empty match at end of last match, to mirror pcre */
if (was_empty) {
if (ptr < end_ptr) {
if (out) {
out->append(ptr, 1);
} else if (out_array) {
RE2_MATCH_TO_ZVAL_PIECE(ptr, 1, ptr - start_ptr);
add_next_index_zval(out_array, piece);
}
}
++ptr;
if (out_array && ptr == end_ptr && !(flags & RE2_SPLIT_NO_EMPTY)) {
/* fudge for re2_split with empty pattern */
RE2_MATCH_TO_ZVAL_PIECE(ptr, 0, ptr - start_ptr);
add_next_index_zval(out_array, piece);
if (flags & RE2_SPLIT_DELIM_CAPTURE) {
RE2_MATCH_TO_ZVAL_PIECE(ptr, 0, ptr - start_ptr);
add_next_index_zval(out_array, piece);
}
}
continue;
}
was_empty = true;
} else {
was_empty = false;
}
if (flags & RE2_SET_ORDER) {
/* initialise match arrays per result */
MAKE_STD_ZVAL(match_array);
array_init_size(match_array, num_groups);
matches = &match_array;
if (matches_out) {
add_next_index_zval(matches_out, match_array);
}
}
if (matches) {
_php_re2_populate_matches(re, matches, subject_piece, pieces, argc, flags);
}
if (out) {
if (replace_fci) {
zval *retval_ptr;
replace_fci->param_count = 1;
replace_fci->params = &matches;
replace_fci->retval_ptr_ptr = &retval_ptr;
if (zend_call_function(replace_fci, replace_fci_cache TSRMLS_CC) == SUCCESS && replace_fci->retval_ptr_ptr && *replace_fci->retval_ptr_ptr) {
zval *ret = *replace_fci->retval_ptr_ptr;
convert_to_string(ret);
out->append(Z_STRVAL_P(ret), Z_STRLEN_P(ret));
}
zval_dtor(retval_ptr);
efree(retval_ptr);
zval_ptr_dtor(matches);
} else {
const char *walk = replace, *end = replace + replace_len;
char walk_last = 0;
int n;
while (walk < end) {
if (*walk == '\\' || *walk == '$') {
if (walk_last == '\\') {
++walk;
walk_last = 0;
continue;
}
if (_php_re2_get_backref(&walk, &n)) {
if (n < argc && pieces[n].size() > 0) {
out->append(pieces[n].data(), pieces[n].size());
}
continue;
}
}
out->push_back(*walk);
walk_last = *walk;
++walk;
}
}
} else if (out_array && flags & RE2_SPLIT_DELIM_CAPTURE) {
for (int i = 1; i < argc; i++) {
RE2_MATCH_TO_ZVAL_PIECE(pieces[i].begin(), pieces[i].size(), ptr - start_ptr);
add_next_index_zval(out_array, piece);
}
}
ptr = pieces[0].end();
last_ptr = ptr;
++num_matches;
if (limit && num_matches == limit) {
break;
}
}
if (ptr < end_ptr) {
if (out) {
out->append(ptr, end_ptr - ptr);
} else if (out_array) {
RE2_MATCH_TO_ZVAL_PIECE(ptr, end_ptr - ptr, ptr - start_ptr);
add_next_index_zval(out_array, piece);
}
}
return num_matches;
}
/* }}} */
#define RE2_ENSURE_ARRAY(name) \
if (Z_TYPE_PP(name##s) == IS_ARRAY) { \
name##_array = *name##s; \
name##_count = zend_hash_num_elements(Z_ARRVAL_P(name##_array)); \
zend_hash_internal_pointer_reset(Z_ARRVAL_P(name##_array)); \
} else { \
if (Z_TYPE_PP(name##s) != IS_STRING) { \
if (Z_ISREF_PP(name##s)) { \
SEPARATE_ZVAL(name##s); \
} \
convert_to_string_ex(name##s); \
} \
MAKE_STD_ZVAL(name##_array); \
array_init_size(name##_array, 1); \
Z_ADDREF_PP(name##s); \
add_next_index_zval(name##_array, *name##s); \
created_##name##_array = true; \
}
#define RE2_FREE_ARRAY(name) \
if (created_##name##_array) { \
zval_ptr_dtor(&name##_array); \
}
#define RE2_FREE_PATTERN \
if (was_new) { \
delete re; \
}
static int _php_re2_get_pattern(zval *pattern, RE2 **re, int *argc, bool *was_new TSRMLS_DC)
{
bool cache_hit = false, cache_save = false;
*was_new = false;
if (Z_TYPE_P(pattern) == IS_STRING) {
if (RE2_G(cache_enabled)) {
RE2 **cached_re;
if (zend_hash_find(RE2_G(cache_store), Z_STRVAL_P(pattern), Z_STRLEN_P(pattern) + 1, (void **)&cached_re) == SUCCESS) {
*re = *cached_re;
cache_hit = true;
} else {
cache_save = true;
}
}
if (!cache_hit) {
std::string pattern_str = std::string(Z_STRVAL_P(pattern), Z_STRLEN_P(pattern));
*re = new RE2(pattern_str);
*was_new = !cache_save;
}
} else if (Z_TYPE_P(pattern) == IS_OBJECT && instanceof_function(Z_OBJCE_P(pattern), php_re2_class_entry TSRMLS_CC)) {
re2_object *obj = (re2_object *)zend_object_store_get_object(pattern TSRMLS_CC);
*re = obj->re;
} else {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Pattern must be a string or an RE2 object");
return FAILURE;
}
if (!(*re)->ok()) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid pattern");
return FAILURE;
}
*argc = (*re)->NumberOfCapturingGroups();
if (*argc == -1) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid pattern");
return FAILURE;
}
if (cache_save) {
zend_hash_update(RE2_G(cache_store), Z_STRVAL_P(pattern), Z_STRLEN_P(pattern) + 1, (void *)re, sizeof(RE2 *), NULL);
}
return SUCCESS;
}
/* _php2_re2_replace_subject() {{{ */
static int _php_re2_replace_subject(zval **patterns, zval *subject, zval *return_value,
int *count, long limit, long flags, bool is_filter,
zval **replaces, zend_fcall_info *replace_fci, zend_fcall_info_cache *replace_fci_cache TSRMLS_DC)
{
int argc, pattern_i = 0, replace_i = 0, pattern_count = 1, replace_count = 1, replace_len;
RE2 *re;
std::string out_str;
zval *pattern_array = NULL, *replace_array = NULL, **pattern_ptr, **replace_ptr = NULL;
bool created_pattern_array = false, created_replace_array = false, was_new;
char *replace_str;
long num_matches;
RE2_ENSURE_ARRAY(pattern);
if (!replace_fci) {
RE2_ENSURE_ARRAY(replace);
}
Z_ADDREF_P(subject);
SEPARATE_ZVAL(&subject);
while (zend_hash_get_current_data(Z_ARRVAL_P(pattern_array), (void **)&pattern_ptr) == SUCCESS) {
if (_php_re2_get_pattern(*pattern_ptr, &re, &argc, &was_new TSRMLS_CC) == FAILURE) {
RE2_FREE_PATTERN;
RE2_FREE_ARRAY(pattern);
RE2_FREE_ARRAY(replace);
zval_ptr_dtor(&subject);
ZVAL_FALSE(return_value);
return FAILURE;
}
if (replace_fci || zend_hash_get_current_data(Z_ARRVAL_P(replace_array), (void **)&replace_ptr) == FAILURE) {
replace_str = "";
replace_len = 0;
} else {
replace_str = Z_STRVAL_PP(replace_ptr);
replace_len = Z_STRLEN_PP(replace_ptr);
}
out_str = "";
num_matches = _php_re2_match_common(re, NULL, NULL, Z_STRVAL_P(subject), Z_STRLEN_P(subject),
&out_str, NULL, replace_str, replace_len, replace_fci, replace_fci_cache, limit, 0, argc, flags TSRMLS_CC);
if (num_matches) {
*count += num_matches;
zval_ptr_dtor(&subject);
MAKE_STD_ZVAL(subject);
ZVAL_STRINGL(subject, (char *)out_str.c_str(), out_str.length(), 1);
}
RE2_FREE_PATTERN;
if (++pattern_i <= pattern_count) {
zend_hash_move_forward(Z_ARRVAL_P(pattern_array));
}
if (replace_array && !created_replace_array && ++replace_i <= replace_count) {
zend_hash_move_forward(Z_ARRVAL_P(replace_array));
}
}
RE2_FREE_ARRAY(pattern);
RE2_FREE_ARRAY(replace);
Z_TYPE_P(return_value) = IS_STRING;
ZVAL_STRINGL(return_value, (char *)Z_STRVAL_P(subject), Z_STRLEN_P(subject), 1);
zval_ptr_dtor(&subject);
return SUCCESS;
}
/* }}} */
/* _php2_re2_replace_subjects() {{{ */
static void _php_re2_replace_subjects(zval **patterns, zval *subjects, zval *return_value,
zval *count_zv, long limit, long flags, bool is_filter,
zval **replaces, zend_fcall_info *replace_fci, zend_fcall_info_cache *replace_fci_cache TSRMLS_DC)
{
zval **subject_ptr, *subject_return = NULL;
int count = 0, total_count = 0;
if (Z_TYPE_P(subjects) == IS_ARRAY) {
array_init(return_value);
zend_hash_internal_pointer_reset(Z_ARRVAL_P(subjects));
while (zend_hash_get_current_data(Z_ARRVAL_P(subjects), (void **)&subject_ptr) == SUCCESS) {
MAKE_STD_ZVAL(subject_return);
count = 0;
if (_php_re2_replace_subject(patterns, *subject_ptr, subject_return, &count, limit, flags, is_filter, replaces, replace_fci, replace_fci_cache TSRMLS_CC) == FAILURE) {
zval_ptr_dtor(&subject_return);
zval_ptr_dtor(&return_value);
MAKE_STD_ZVAL(return_value);
ZVAL_FALSE(return_value);
RETVAL_FALSE;
return;
}
if (!is_filter || count) {
char *string_key = NULL;
ulong num_key;
total_count += count;
Z_ADDREF_P(subject_return);
switch (zend_hash_get_current_key(Z_ARRVAL_P(subjects), &string_key, &num_key, 0)) {
case HASH_KEY_IS_STRING:
add_assoc_zval_ex(return_value, (char *)string_key, strlen(string_key) + 1, subject_return);
break;
case HASH_KEY_IS_LONG:
zend_hash_index_update(Z_ARRVAL_P(return_value), num_key, &subject_return, sizeof(zval *), NULL);
break;
}
}
zval_ptr_dtor(&subject_return);
subject_return = NULL;
zend_hash_move_forward(Z_ARRVAL_P(subjects));
}
} else {
if (_php_re2_replace_subject(patterns, subjects, return_value, &total_count, limit, flags, is_filter, replaces, replace_fci, replace_fci_cache TSRMLS_CC) == FAILURE) {
RETVAL_FALSE;
return;
}
if (is_filter && !total_count) {
efree(Z_STRVAL_P(return_value));
RETVAL_NULL();
}
}
if (count_zv) {
ZVAL_LONG(count_zv, total_count);
}
}
/* }}} */
/* }}} */
/* {{{ options object helpers */
/* static inline int _create_re2_options_object(zval *options) {{{ */
static inline int _create_re2_options_object(zval *options TSRMLS_DC)
{
zval *ctor, unused;
MAKE_STD_ZVAL(ctor);
array_init_size(ctor, 2);
Z_ADDREF_P(options);
add_next_index_zval(ctor, options);
add_next_index_string(ctor, "__construct", 1);
if (call_user_function(&php_re2_options_class_entry->function_table, &options, ctor, &unused, 0, NULL TSRMLS_CC) == FAILURE) {
php_error_docref(NULL TSRMLS_CC, E_ERROR, "Unable to construct Re2Options");
return FAILURE;
}
zval_ptr_dtor(&ctor);
Z_DELREF_P(options);
return SUCCESS;
}
/* }}} */
/* {{{ re2_options_hash
*/
static char *re2_options_hash(zval *options TSRMLS_DC)
{
char *hash;
re2_options_object *obj = (re2_options_object *)zend_object_store_get_object(options TSRMLS_CC);
spprintf(&hash, RE2_OPTIONS_HASH_LEN, "%c%c%x",
1 |
obj->options->posix_syntax() << 1 |
obj->options->longest_match() << 2 |
obj->options->log_errors() << 3 |
obj->options->literal() << 4 |
obj->options->never_nl() << 5 |
obj->options->case_sensitive() << 6 |
obj->options->perl_classes() << 7,
1 |
obj->options->word_boundary() << 1 |
obj->options->one_line() << 2 |
(obj->options->encoding() == RE2::Options::EncodingUTF8) << 3,
obj->options->max_mem()
);
return hash;
}
/* }}} */
#define PHP_RE2_CREATE_OPTIONS_OBJECT \
MAKE_STD_ZVAL(options); \
Z_TYPE_P(options) = IS_OBJECT; \
object_init_ex(options, php_re2_options_class_entry); \
if(_create_re2_options_object(options TSRMLS_CC) == FAILURE) { \
zval_add_ref(&options); \
zval_ptr_dtor(&options); \
RETURN_NULL(); \
}
/* }}} */
/* {{{ proto bool re2_match(mixed $pattern, string $subject [, array &$matches [, int $flags = RE2_ANCHOR_NONE [, int $offset = 0]]])
Returns whether the pattern matches the subject.
*/
PHP_FUNCTION(re2_match)
{
char *subject;
re2::StringPiece subject_piece;
int subject_len, argc;
long flags = RE2_ANCHOR_NONE, offset = 0;
zval *pattern = NULL, *matches = NULL;
RE2 *re;
RE2::Anchor anchor;
bool was_new;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "zs|zll", &pattern, &subject, &subject_len, &matches, &flags, &offset) == FAILURE) {
RETURN_FALSE;
}
RETVAL_FALSE;
if (_php_re2_get_pattern(pattern, &re, &argc, &was_new TSRMLS_CC) == FAILURE) {
RE2_FREE_PATTERN;
return;
}
subject_piece = re2::StringPiece(subject, subject_len);
anchor = _php_re2_get_anchor_from_flags(flags);
if (ZEND_NUM_ARGS() > 2) {
re2::StringPiece pieces[++argc];
if (re->Match(subject_piece, offset, subject_piece.size(), anchor, pieces, argc)) {
zval_dtor(matches);
array_init_size(matches, argc);
flags |= RE2_SET_ORDER;
flags &= ~RE2_PATTERN_ORDER;
_php_re2_populate_matches(re, &matches, subject_piece, pieces, argc, flags);
RETVAL_LONG(1);
} else {
RETVAL_LONG(0);
}
} else {
RETVAL_LONG(re->Match(subject_piece, offset, subject_piece.size(), anchor, NULL, 0));
}
RE2_FREE_PATTERN;
}
/* }}} */
/* {{{ proto int re2_match_all(mixed $pattern, string $subject, array &$matches [, int $flags = RE2_PATTERN_ORDER [, int $offset = 0]])
Returns how many times the pattern matched the subject. */
PHP_FUNCTION(re2_match_all)
{
const char *subject;
int subject_len, argc, num_matches = 0;
long flags = 0, offset = 0;
zval *pattern = NULL, *matches_out = NULL, *match_array = NULL, **matches = NULL;
RE2 *re;
bool was_new;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "zsz|ll", &pattern, &subject, &subject_len, &matches_out, &flags, &offset) == FAILURE) {
RETURN_FALSE;
}
RETVAL_FALSE;
if (_php_re2_get_pattern(pattern, &re, &argc, &was_new TSRMLS_CC) == FAILURE) {
RE2_FREE_PATTERN;
return;
}
if (ZEND_NUM_ARGS() < 4 || !(flags & RE2_SET_ORDER)) {
/* default to pattern order */
flags |= RE2_PATTERN_ORDER;
}
/* initialise output array */
if (matches_out != NULL) {
zval_dtor(matches_out);
}
array_init(matches_out);
if (flags & RE2_PATTERN_ORDER) {
/* initialise match arrays per group */
int i, j;
const std::map<int, std::string> named_groups = re->CapturingGroupNames();
int num_groups = 1 + argc + named_groups.size();
zval **match_arrays = (zval **)emalloc(num_groups * sizeof(zval *));
matches = match_arrays;
for (i = 0, j = 0; i < 1 + argc; i++) {
MAKE_STD_ZVAL(match_array);