Commit 4591fb1a authored by Neil Conway's avatar Neil Conway

Code cleanup for the new regexp UDFs: we can hardcode the OID and some

properties of the "text" type, and then simplify the code accordingly.
Patch from Jeremy Drake.
parent 74b667ad
...@@ -8,7 +8,7 @@ ...@@ -8,7 +8,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $PostgreSQL: pgsql/src/backend/utils/adt/regexp.c,v 1.70 2007/03/20 05:44:59 neilc Exp $ * $PostgreSQL: pgsql/src/backend/utils/adt/regexp.c,v 1.71 2007/03/28 22:59:37 neilc Exp $
* *
* Alistair Crooks added the code for the regex caching * Alistair Crooks added the code for the regex caching
* agc - cached the regular expressions used - there's a good chance * agc - cached the regular expressions used - there's a good chance
...@@ -30,6 +30,7 @@ ...@@ -30,6 +30,7 @@
#include "postgres.h" #include "postgres.h"
#include "access/heapam.h" #include "access/heapam.h"
#include "catalog/pg_type.h"
#include "funcapi.h" #include "funcapi.h"
#include "regex/regex.h" #include "regex/regex.h"
#include "utils/builtins.h" #include "utils/builtins.h"
...@@ -95,12 +96,6 @@ typedef struct regexp_matches_ctx ...@@ -95,12 +96,6 @@ typedef struct regexp_matches_ctx
size_t offset; size_t offset;
re_comp_flags flags; re_comp_flags flags;
/* text type info */
Oid param_type;
int16 typlen;
bool typbyval;
char typalign;
} regexp_matches_ctx; } regexp_matches_ctx;
typedef struct regexp_split_ctx typedef struct regexp_split_ctx
...@@ -119,8 +114,7 @@ typedef struct regexp_split_ctx ...@@ -119,8 +114,7 @@ typedef struct regexp_split_ctx
static int num_res = 0; /* # of cached re's */ static int num_res = 0; /* # of cached re's */
static cached_re_str re_array[MAX_CACHED_RES]; /* cached re's */ static cached_re_str re_array[MAX_CACHED_RES]; /* cached re's */
static regexp_matches_ctx *setup_regexp_matches(FunctionCallInfo fcinfo, static regexp_matches_ctx *setup_regexp_matches(text *orig_str, text *pattern,
text *orig_str, text *pattern,
text *flags); text *flags);
static ArrayType *perform_regexp_matches(regexp_matches_ctx *matchctx); static ArrayType *perform_regexp_matches(regexp_matches_ctx *matchctx);
...@@ -760,8 +754,8 @@ regexp_matches(PG_FUNCTION_ARGS) ...@@ -760,8 +754,8 @@ regexp_matches(PG_FUNCTION_ARGS)
oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx); oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
/* be sure to copy the input string into the multi-call ctx */ /* be sure to copy the input string into the multi-call ctx */
matchctx = setup_regexp_matches(fcinfo, PG_GETARG_TEXT_P_COPY(0), matchctx = setup_regexp_matches(PG_GETARG_TEXT_P_COPY(0), pattern,
pattern, flags); flags);
MemoryContextSwitchTo(oldcontext); MemoryContextSwitchTo(oldcontext);
funcctx->user_fctx = (void *) matchctx; funcctx->user_fctx = (void *) matchctx;
...@@ -822,7 +816,7 @@ regexp_matches_no_flags(PG_FUNCTION_ARGS) ...@@ -822,7 +816,7 @@ regexp_matches_no_flags(PG_FUNCTION_ARGS)
} }
static regexp_matches_ctx * static regexp_matches_ctx *
setup_regexp_matches(FunctionCallInfo fcinfo, text *orig_str, text *pattern, text *flags) setup_regexp_matches(text *orig_str, text *pattern, text *flags)
{ {
regexp_matches_ctx *matchctx = palloc(sizeof(regexp_matches_ctx)); regexp_matches_ctx *matchctx = palloc(sizeof(regexp_matches_ctx));
...@@ -835,11 +829,6 @@ setup_regexp_matches(FunctionCallInfo fcinfo, text *orig_str, text *pattern, tex ...@@ -835,11 +829,6 @@ setup_regexp_matches(FunctionCallInfo fcinfo, text *orig_str, text *pattern, tex
matchctx->pmatch = palloc(sizeof(regmatch_t) * (matchctx->cpattern->re_nsub + 1)); matchctx->pmatch = palloc(sizeof(regmatch_t) * (matchctx->cpattern->re_nsub + 1));
matchctx->offset = 0; matchctx->offset = 0;
/* get text type oid, too lazy to do it some other way */
matchctx->param_type = get_fn_expr_argtype(fcinfo->flinfo, 0);
get_typlenbyvalalign(matchctx->param_type, &matchctx->typlen,
&matchctx->typbyval, &matchctx->typalign);
matchctx->wide_str = palloc(sizeof(pg_wchar) * (matchctx->orig_len + 1)); matchctx->wide_str = palloc(sizeof(pg_wchar) * (matchctx->orig_len + 1));
matchctx->wide_len = pg_mb2wchar_with_len(VARDATA(matchctx->orig_str), matchctx->wide_len = pg_mb2wchar_with_len(VARDATA(matchctx->orig_str),
matchctx->wide_str, matchctx->orig_len); matchctx->wide_str, matchctx->orig_len);
...@@ -915,9 +904,9 @@ perform_regexp_matches(regexp_matches_ctx *matchctx) ...@@ -915,9 +904,9 @@ perform_regexp_matches(regexp_matches_ctx *matchctx)
dims[0] = 1; dims[0] = 1;
} }
/* XXX: this hardcodes assumptions about the text type */
return construct_md_array(elems, nulls, ndims, dims, lbs, return construct_md_array(elems, nulls, ndims, dims, lbs,
matchctx->param_type, matchctx->typlen, TEXTOID, -1, false, 'i');
matchctx->typbyval, matchctx->typalign);
} }
Datum Datum
...@@ -976,16 +965,12 @@ Datum regexp_split_to_array(PG_FUNCTION_ARGS) ...@@ -976,16 +965,12 @@ Datum regexp_split_to_array(PG_FUNCTION_ARGS)
{ {
ArrayBuildState *astate = NULL; ArrayBuildState *astate = NULL;
regexp_split_ctx *splitctx; regexp_split_ctx *splitctx;
Oid param_type;
int nitems; int nitems;
splitctx = setup_regexp_split(PG_GETARG_TEXT_P(0), splitctx = setup_regexp_split(PG_GETARG_TEXT_P(0),
PG_GETARG_TEXT_P(1), PG_GETARG_TEXT_P(1),
PG_GETARG_TEXT_P_IF_EXISTS(2)); PG_GETARG_TEXT_P_IF_EXISTS(2));
/* get text type oid, too lazy to do it some other way */
param_type = get_fn_expr_argtype(fcinfo->flinfo, 0);
for (nitems = 0; splitctx->offset < splitctx->wide_len; nitems++) for (nitems = 0; splitctx->offset < splitctx->wide_len; nitems++)
{ {
if (nitems > splitctx->wide_len) if (nitems > splitctx->wide_len)
...@@ -995,7 +980,7 @@ Datum regexp_split_to_array(PG_FUNCTION_ARGS) ...@@ -995,7 +980,7 @@ Datum regexp_split_to_array(PG_FUNCTION_ARGS)
astate = accumArrayResult(astate, astate = accumArrayResult(astate,
get_next_split(splitctx), get_next_split(splitctx),
false, false,
param_type, TEXTOID,
CurrentMemoryContext); CurrentMemoryContext);
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment