Hi, so I'm new to this site.
I just posted a post it and already has more than 4 thousand views! 
I have a question.
I am not well versed with PCRE2.
In my other thread, the attached file had a gsub() function that takes three arguments.
Code:
#define PCRE2_CODE_UNIT_WIDTH 8
#include <pcre2.h>
char* gsub (const char* str, const char* reg, const char* rep) {
size_t outlen;
static const char* error_str = \"DONT KNOW HOW TO ALLOCATE MEM FOR REGEX\";
char* out = malloc(outlen =strlen(str) * strlen(rep)) ;
if (out == NULL) {
error:
fputs(error_str, stderr);
return NULL;
}
int error;
PCRE2_SIZE erroffset;
// printf(\" %l \",
const PCRE2_SPTR pattern = (PCRE2_SPTR)reg;
const PCRE2_SPTR subject = (PCRE2_SPTR)str;
const PCRE2_SPTR replacement = (PCRE2_SPTR)rep;
pcre2_code *re = pcre2_compile(pattern, PCRE2_ZERO_TERMINATED, 0, &error, &erroffset, 0);
if (re == 0) return NULL;
// return 1;
pcre2_jit_compile(re, PCRE2_JIT_COMPLETE);
int rc = pcre2_substitute(re, subject, PCRE2_ZERO_TERMINATED, 0, PCRE2_SUBSTITUTE_GLOBAL | PCRE2_SUBSTITUTE_EXTENDED, 0, 0, replacement, PCRE2_ZERO_TERMINATED, out, &outlen);
if (rc >= 0);
// printf(%s\\n, output);
pcre2_code_free(re);
size_t new_len = strlen(out);
char* new_out = realloc(out,new_len + 1);
if (new_out == NULL) goto error;
return new_out;
}
The PCRE2 API is really complicated.
Questions
1. Is there any way to have PCRE automatically account for memory allocation sizes? With a callback, I could do this manually.
2. So is the best solution to always have a callback function?
3. I want to implement something like Ruby's
str.gsub /foo/ do |i|
....
end
Could someone post a code snippet that does this?
4. Would anyone want to contribute to my other project? (https://cboard.cprogramming.com/c-programming/181160-hi-i-have-created-some-work-i-think-will-really-valuable-community.html)
I can re-write it in C.
It's just the current compiler is using gsub() functions in Crystal.
I'm not an expert in PCRE2.
Thank you.