@@ -646,7 +646,11 @@ static inline void add_offset_pair(zval *result, char *str, int len, int offset,
646
646
array_init_size (& match_pair , 2 );
647
647
648
648
/* Add (match, offset) to the return value */
649
- ZVAL_STRINGL (& tmp , str , len );
649
+ if (offset < 0 ) { /* unset substring */
650
+ ZVAL_NULL (& tmp );
651
+ } else {
652
+ ZVAL_STRINGL (& tmp , str , len );
653
+ }
650
654
zend_hash_next_index_insert_new (Z_ARRVAL (match_pair ), & tmp );
651
655
ZVAL_LONG (& tmp , offset );
652
656
zend_hash_next_index_insert_new (Z_ARRVAL (match_pair ), & tmp );
@@ -847,8 +851,12 @@ PHPAPI void php_pcre_match_impl(pcre_cache_entry *pce, char *subject, int subjec
847
851
}
848
852
} else {
849
853
for (i = 0 ; i < count ; i ++ ) {
850
- add_next_index_stringl (& match_sets [i ], (char * )stringlist [i ],
851
- offsets [(i <<1 )+ 1 ] - offsets [i <<1 ]);
854
+ if (offsets [i <<1 ] < 0 ) { /* unset substring */
855
+ add_next_index_null (& match_sets [i ]);
856
+ } else {
857
+ add_next_index_stringl (& match_sets [i ], (char * )stringlist [i ],
858
+ offsets [(i <<1 )+ 1 ] - offsets [i <<1 ]);
859
+ }
852
860
}
853
861
}
854
862
/* Add MARK, if available */
@@ -861,11 +869,11 @@ PHPAPI void php_pcre_match_impl(pcre_cache_entry *pce, char *subject, int subjec
861
869
/*
862
870
* If the number of captured subpatterns on this run is
863
871
* less than the total possible number, pad the result
864
- * arrays with empty strings .
872
+ * arrays with NULLs .
865
873
*/
866
874
if (count < num_subpats ) {
867
875
for (; i < num_subpats ; i ++ ) {
868
- add_next_index_string (& match_sets [i ], "" );
876
+ add_next_index_null (& match_sets [i ]);
869
877
}
870
878
}
871
879
} else {
@@ -882,11 +890,19 @@ PHPAPI void php_pcre_match_impl(pcre_cache_entry *pce, char *subject, int subjec
882
890
} else {
883
891
for (i = 0 ; i < count ; i ++ ) {
884
892
if (subpat_names [i ]) {
885
- add_assoc_stringl (& result_set , subpat_names [i ], (char * )stringlist [i ],
893
+ if (offsets [i <<1 ] < 0 ) { /* unset substring */
894
+ add_assoc_null (& result_set , subpat_names [i ]);
895
+ } else {
896
+ add_assoc_stringl (& result_set , subpat_names [i ], (char * )stringlist [i ],
897
+ offsets [(i <<1 )+ 1 ] - offsets [i <<1 ]);
898
+ }
899
+ }
900
+ if (offsets [i <<1 ] < 0 ) { /* unset substring */
901
+ add_next_index_null (& result_set );
902
+ } else {
903
+ add_next_index_stringl (& result_set , (char * )stringlist [i ],
886
904
offsets [(i <<1 )+ 1 ] - offsets [i <<1 ]);
887
905
}
888
- add_next_index_stringl (& result_set , (char * )stringlist [i ],
889
- offsets [(i <<1 )+ 1 ] - offsets [i <<1 ]);
890
906
}
891
907
}
892
908
} else {
@@ -897,8 +913,12 @@ PHPAPI void php_pcre_match_impl(pcre_cache_entry *pce, char *subject, int subjec
897
913
}
898
914
} else {
899
915
for (i = 0 ; i < count ; i ++ ) {
900
- add_next_index_stringl (& result_set , (char * )stringlist [i ],
901
- offsets [(i <<1 )+ 1 ] - offsets [i <<1 ]);
916
+ if (offsets [i <<1 ] < 0 ) { /* unset substring */
917
+ add_next_index_null (& result_set );
918
+ } else {
919
+ add_next_index_stringl (& result_set , (char * )stringlist [i ],
920
+ offsets [(i <<1 )+ 1 ] - offsets [i <<1 ]);
921
+ }
902
922
}
903
923
}
904
924
}
@@ -921,11 +941,19 @@ PHPAPI void php_pcre_match_impl(pcre_cache_entry *pce, char *subject, int subjec
921
941
} else {
922
942
for (i = 0 ; i < count ; i ++ ) {
923
943
if (subpat_names [i ]) {
924
- add_assoc_stringl (subpats , subpat_names [i ], (char * )stringlist [i ],
925
- offsets [(i <<1 )+ 1 ] - offsets [i <<1 ]);
944
+ if (offsets [i <<1 ] < 0 ) { /* unset substring */
945
+ add_assoc_null (subpats , subpat_names [i ]);
946
+ } else {
947
+ add_assoc_stringl (subpats , subpat_names [i ], (char * )stringlist [i ],
948
+ offsets [(i <<1 )+ 1 ] - offsets [i <<1 ]);
949
+ }
950
+ }
951
+ if (offsets [i <<1 ] < 0 ) { /* unset substring */
952
+ add_next_index_null (subpats );
953
+ } else {
954
+ add_next_index_stringl (subpats , (char * )stringlist [i ],
955
+ offsets [(i <<1 )+ 1 ] - offsets [i <<1 ]);
926
956
}
927
- add_next_index_stringl (subpats , (char * )stringlist [i ],
928
- offsets [(i <<1 )+ 1 ] - offsets [i <<1 ]);
929
957
}
930
958
}
931
959
} else {
@@ -937,8 +965,12 @@ PHPAPI void php_pcre_match_impl(pcre_cache_entry *pce, char *subject, int subjec
937
965
}
938
966
} else {
939
967
for (i = 0 ; i < count ; i ++ ) {
940
- add_next_index_stringl (subpats , (char * )stringlist [i ],
941
- offsets [(i <<1 )+ 1 ] - offsets [i <<1 ]);
968
+ if (offsets [i <<1 ] < 0 ) { /* unset substring */
969
+ add_next_index_null (subpats );
970
+ } else {
971
+ add_next_index_stringl (subpats , (char * )stringlist [i ],
972
+ offsets [(i <<1 )+ 1 ] - offsets [i <<1 ]);
973
+ }
942
974
}
943
975
}
944
976
}
0 commit comments