array_init_size(&match_pair, 2);
/* Add (match, offset) to the return value */
- ZVAL_STRINGL(&tmp, str, len);
+ if (offset < 0) { /* unset substring */
+ ZVAL_NULL(&tmp);
+ } else {
+ ZVAL_STRINGL(&tmp, str, len);
+ }
zend_hash_next_index_insert_new(Z_ARRVAL(match_pair), &tmp);
ZVAL_LONG(&tmp, offset);
zend_hash_next_index_insert_new(Z_ARRVAL(match_pair), &tmp);
}
} else {
for (i = 0; i < count; i++) {
- add_next_index_stringl(&match_sets[i], (char *)stringlist[i],
- offsets[(i<<1)+1] - offsets[i<<1]);
+ if (offsets[i<<1] < 0) { /* unset substring */
+ add_next_index_null(&match_sets[i]);
+ } else {
+ add_next_index_stringl(&match_sets[i], (char *)stringlist[i],
+ offsets[(i<<1)+1] - offsets[i<<1]);
+ }
}
}
/* Add MARK, if available */
/*
* If the number of captured subpatterns on this run is
* less than the total possible number, pad the result
- * arrays with empty strings.
+ * arrays with NULLs.
*/
if (count < num_subpats) {
for (; i < num_subpats; i++) {
- add_next_index_string(&match_sets[i], "");
+ add_next_index_null(&match_sets[i]);
}
}
} else {
} else {
for (i = 0; i < count; i++) {
if (subpat_names[i]) {
- add_assoc_stringl(&result_set, subpat_names[i], (char *)stringlist[i],
+ if (offsets[i<<1] < 0) { /* unset substring */
+ add_assoc_null(&result_set, subpat_names[i]);
+ } else {
+ add_assoc_stringl(&result_set, subpat_names[i], (char *)stringlist[i],
+ offsets[(i<<1)+1] - offsets[i<<1]);
+ }
+ }
+ if (offsets[i<<1] < 0) { /* unset substring */
+ add_next_index_null(&result_set);
+ } else {
+ add_next_index_stringl(&result_set, (char *)stringlist[i],
offsets[(i<<1)+1] - offsets[i<<1]);
}
- add_next_index_stringl(&result_set, (char *)stringlist[i],
- offsets[(i<<1)+1] - offsets[i<<1]);
}
}
} else {
}
} else {
for (i = 0; i < count; i++) {
- add_next_index_stringl(&result_set, (char *)stringlist[i],
- offsets[(i<<1)+1] - offsets[i<<1]);
+ if (offsets[i<<1] < 0) { /* unset substring */
+ add_next_index_null(&result_set);
+ } else {
+ add_next_index_stringl(&result_set, (char *)stringlist[i],
+ offsets[(i<<1)+1] - offsets[i<<1]);
+ }
}
}
}
} else {
for (i = 0; i < count; i++) {
if (subpat_names[i]) {
- add_assoc_stringl(subpats, subpat_names[i], (char *)stringlist[i],
- offsets[(i<<1)+1] - offsets[i<<1]);
+ if (offsets[i<<1] < 0) { /* unset substring */
+ add_assoc_null(subpats, subpat_names[i]);
+ } else {
+ add_assoc_stringl(subpats, subpat_names[i], (char *)stringlist[i],
+ offsets[(i<<1)+1] - offsets[i<<1]);
+ }
+ }
+ if (offsets[i<<1] < 0) { /* unset substring */
+ add_next_index_null(subpats);
+ } else {
+ add_next_index_stringl(subpats, (char *)stringlist[i],
+ offsets[(i<<1)+1] - offsets[i<<1]);
}
- add_next_index_stringl(subpats, (char *)stringlist[i],
- offsets[(i<<1)+1] - offsets[i<<1]);
}
}
} else {
}
} else {
for (i = 0; i < count; i++) {
- add_next_index_stringl(subpats, (char *)stringlist[i],
- offsets[(i<<1)+1] - offsets[i<<1]);
+ if (offsets[i<<1] < 0) { /* unset substring */
+ add_next_index_null(subpats);
+ } else {
+ add_next_index_stringl(subpats, (char *)stringlist[i],
+ offsets[(i<<1)+1] - offsets[i<<1]);
+ }
}
}
}