There is a regex related question on SO - https://stackoverflow.com/questions/26072947/postgresql-regex-replace-first-level-square-brackets-with-curly-braces, and further down the source code for a C extension for the same task.
There is a small github with the full details - https://github.com/ringerc/scrapcode/tree/master/postgresql/string_transform
Replicated here:
What would be the equivalent of this in Nim?
#include "postgres.h"
#include "fmgr.h"
#include "utils/builtins.h"
PG_MODULE_MAGIC;
PG_FUNCTION_INFO_V1(replc);
Datum replc(PG_FUNCTION_ARGS);
PGDLLEXPORT Datum
replc(PG_FUNCTION_ARGS)
{
/* Set `buf` to a palloc'd copy of the input string, deTOASTed if needed */
char * const buf = text_to_cstring(PG_GETARG_TEXT_PP(0));
char * ch = buf;
int depth = 0;
while (*ch != '\0')
{
switch (*ch)
{
case '[':
depth++;
if (depth <= 2)
*ch = '{';
break;
case ']':
if (depth <= 2)
*ch = '}';
depth--;
break;
}
ch++;
}
if (depth != 0)
ereport(WARNING,
(errmsg("Opening and closing []s did not match, got %d extra [s", depth)));
PG_RETURN_DATUM(CStringGetTextDatum(buf));
}
There aren't any guides as such though there has been some effort to get Nim running on PostgreSQL, both as an extension and as a procedural language.
Both of those repos provide a very brief example.
Hope this helps!