- /* Reassigning pt to the beginning of the string */
- pt = new_str;
-
-
- /* Getting the sub patterns */
- do
- {
- if((*pt == OR) || (*pt == '\0'))
- {
- if(*pt == '\0')
- {
- end_of_string = 1;
- }
-
- *pt = '\0';
-
- /* If string starts with ^, set the BEGIN SET flag */
- if(*new_str == BEGINREGEX)
- {
- new_str++;
- reg->flags[i]|=BEGIN_SET;
- }
-
- /* If string ends with $, set the END_SET flag */
- if(*(pt-1) == ENDREGEX)
- {
- *(pt-1) = '\0';
- reg->flags[i]|=END_SET;
- }
-
- reg->patterns[i] = strdup(new_str);
-
- if(!reg->patterns[i])
- {
- reg->error = OS_REGEX_OUTOFMEMORY;
- goto compile_error;
-
- }
-
-
- /* Setting the parenthesis closures */
- /* The parenthesis closure if set */
- if(reg->prts_closure)
- {
- int tmp_int = 0;
- char *tmp_str;
-
-
- /* search the whole pattern for parenthesis */
- prts_size = 0;
-
- /* First loop we get the number of parenthesis.
- * We allocate the memory and loop again setting
- * the parenthesis closures.
- */
- tmp_str = reg->patterns[i];
- while(*tmp_str != '\0')
- {
- if(prts(*tmp_str))
- {
- prts_size++;
- }
- tmp_str++;
- }
-
- /* Getting the maximum number of parenthesis for
- * all sub strings. We need that to set up the maximum
- * number of substrings to be returned.
- */
- if(max_prts_size < prts_size)
- {
- max_prts_size = prts_size;
- }
-
- /* Allocating the memory */
- reg->prts_closure[i] = calloc(prts_size + 1, sizeof(char *));
- reg->prts_str[i] = calloc(prts_size + 1, sizeof(char *));
- if((reg->prts_closure[i] == NULL)||(reg->prts_str[i] == NULL))
- {
- reg->error = OS_REGEX_OUTOFMEMORY;
- goto compile_error;
- }
-
- /* Next loop to set the closures */
- tmp_str = reg->patterns[i];
- while(*tmp_str != '\0')
- {
- if(prts(*tmp_str))
- {
- if(tmp_int >= prts_size)
- {
- reg->error = OS_REGEX_BADPARENTHESIS;
- goto compile_error;
- }
-
- /* Setting to the pointer to the string */
- reg->prts_closure[i][tmp_int] = tmp_str;
- reg->prts_str[i][tmp_int] = NULL;
-
- tmp_int++;
- }
-
- tmp_str++;
- }
- }
-
-
- if(end_of_string)
- {
- break;
- }
-
- new_str = ++pt;
- i++;
- continue;
+ if (flags & OS_RETURN_SUBSTRING) {
+ pcre2_pattern_info(reg->regex, PCRE2_INFO_CAPTURECOUNT, (void *)&count);
+ count++; // to store NULL pointer at the end
+ reg->sub_strings = calloc(count, sizeof(char *));
+ if (reg->sub_strings == NULL) {
+ reg->error = OS_REGEX_OUTOFMEMORY;
+ goto compile_error;
+ }
+ for (i = 0; i < count; i++) {
+ reg->sub_strings[i] = NULL;