#include "mchar.h" #include "mb/pg_wchar.h" PG_FUNCTION_INFO_V1(mchar_length); Datum mchar_length(PG_FUNCTION_ARGS); Datum mchar_length(PG_FUNCTION_ARGS) { MChar *m = PG_GETARG_MCHAR(0); int32 l = UCHARLENGTH(m); while( l>0 && m_isspace( m->data[ l-1 ] ) ) l--; l = u_countChar32(m->data, l); PG_FREE_IF_COPY(m,0); PG_RETURN_INT32(l); } PG_FUNCTION_INFO_V1(mvarchar_length); Datum mvarchar_length(PG_FUNCTION_ARGS); Datum mvarchar_length(PG_FUNCTION_ARGS) { MVarChar *m = PG_GETARG_MVARCHAR(0); int32 l = UVARCHARLENGTH(m); while( l>0 && m_isspace( m->data[ l-1 ] ) ) l--; l = u_countChar32(m->data, l); PG_FREE_IF_COPY(m,0); PG_RETURN_INT32(l); } static int32 uchar_substring( UChar *str, int32 strl, int32 start, int32 length, bool length_not_specified, UChar *dst) { int32 S = start-1; /* start position */ int32 S1; /* adjusted start position */ int32 L1; /* adjusted substring length */ int32 subbegin=0, subend=0; S1 = Max(S, 0); if (length_not_specified) L1 = -1; else { /* end position */ int32 E = S + length; /* * A negative value for L is the only way for the end position to * be before the start. SQL99 says to throw an error. */ if (E < S) ereport(ERROR, (errcode(ERRCODE_SUBSTRING_ERROR), errmsg("negative substring length not allowed"))); /* * A zero or negative value for the end position can happen if the * start was negative or one. SQL99 says to return a zero-length * string. */ if (E < 0) return 0; L1 = E - S1; } U16_FWD_N( str, subbegin, strl, S1 ); if ( subbegin >= strl ) return 0; if (length_not_specified) subend = strl; else { subend = subbegin; U16_FWD_N( str, subend, strl, L1 ); } memcpy( dst, str+subbegin, sizeof(UChar)*(subend-subbegin) ); return subend-subbegin; } typedef int32 (*MCharCaseMapFunc) (UChar *dest, int32 destCapacity, const UChar *src, int32 srcLength, const char *locale, UErrorCode *pErrorCode); static int32 uchar_case_map_preflight(const UChar *src, int32 srclen, MCharCaseMapFunc mapfunc, const char *funcname) { UErrorCode err = U_ZERO_ERROR; int32 required; required = mapfunc(NULL, 0, src, srclen, NULL, &err); if (err != U_BUFFER_OVERFLOW_ERROR && U_FAILURE(err)) elog(ERROR, "ICU %s preflight fails and returns %d (%s)", funcname, err, u_errorName(err)); return required; } static int32 uchar_case_map_apply(const UChar *src, int32 srclen, MCharCaseMapFunc mapfunc, const char *funcname, UChar *dst, int32 dstcapacity) { UErrorCode err = U_ZERO_ERROR; int32 produced; produced = mapfunc(dst, dstcapacity, src, srclen, NULL, &err); if (U_FAILURE(err)) elog(ERROR, "ICU %s fails and returns %d (%s)", funcname, err, u_errorName(err)); return produced; } static MChar * mchar_case_convert(MChar *src, MCharCaseMapFunc mapfunc, const char *funcname) { int32 srcunits = UCHARLENGTH(src); int32 required; Size dstcapacity_size; int32 dstcapacity; Size allocsz; MChar *dst; int32 produced = 0; required = uchar_case_map_preflight(src->data, srcunits, mapfunc, funcname); dstcapacity_size = add_size((Size) required, 1); if (dstcapacity_size > PG_INT32_MAX) elog(ERROR, "ICU %s result is too large", funcname); dstcapacity = (int32) dstcapacity_size; allocsz = add_size(MCHARHDRSZ, mul_size(dstcapacity_size, sizeof(UChar))); dst = (MChar *) palloc(allocsz); dst->typmod = src->typmod; if (srcunits != 0) { int32 mapped_charlen; produced = uchar_case_map_apply(src->data, srcunits, mapfunc, funcname, dst->data, dstcapacity); mapped_charlen = u_countChar32(dst->data, produced); if (dst->typmod > 0 && mapped_charlen > dst->typmod) dst->typmod = mapped_charlen; } SET_VARSIZE(dst, add_size(MCHARHDRSZ, mul_size((Size) produced, sizeof(UChar)))); return dst; } static MVarChar * mvarchar_case_convert(MVarChar *src, MCharCaseMapFunc mapfunc, const char *funcname) { int32 srcunits = UVARCHARLENGTH(src); int32 required; Size dstcapacity_size; int32 dstcapacity; Size allocsz; MVarChar *dst; int32 produced = 0; required = uchar_case_map_preflight(src->data, srcunits, mapfunc, funcname); dstcapacity_size = add_size((Size) required, 1); if (dstcapacity_size > PG_INT32_MAX) elog(ERROR, "ICU %s result is too large", funcname); dstcapacity = (int32) dstcapacity_size; allocsz = add_size(MVARCHARHDRSZ, mul_size(dstcapacity_size, sizeof(UChar))); dst = (MVarChar *) palloc(allocsz); if (srcunits != 0) produced = uchar_case_map_apply(src->data, srcunits, mapfunc, funcname, dst->data, dstcapacity); SET_VARSIZE(dst, add_size(MVARCHARHDRSZ, mul_size((Size) produced, sizeof(UChar)))); return dst; } PG_FUNCTION_INFO_V1(mchar_substring); Datum mchar_substring(PG_FUNCTION_ARGS); Datum mchar_substring(PG_FUNCTION_ARGS) { MChar *src = PG_GETARG_MCHAR(0); MChar *dst; int32 length; dst = (MChar*)palloc( VARSIZE(src) ); length = uchar_substring( src->data, UCHARLENGTH(src), PG_GETARG_INT32(1), PG_GETARG_INT32(2), false, dst->data); dst->typmod = src->typmod; SET_VARSIZE(dst, MCHARHDRSZ + length *sizeof(UChar)); PG_FREE_IF_COPY(src, 0); PG_RETURN_MCHAR(dst); } PG_FUNCTION_INFO_V1(mchar_substring_no_len); Datum mchar_substring_no_len(PG_FUNCTION_ARGS); Datum mchar_substring_no_len(PG_FUNCTION_ARGS) { MChar *src = PG_GETARG_MCHAR(0); MChar *dst; int32 length; dst = (MChar*)palloc( VARSIZE(src) ); length = uchar_substring( src->data, UCHARLENGTH(src), PG_GETARG_INT32(1), -1, true, dst->data); dst->typmod = src->typmod; SET_VARSIZE(dst, MCHARHDRSZ + length *sizeof(UChar)); PG_FREE_IF_COPY(src, 0); PG_RETURN_MCHAR(dst); } PG_FUNCTION_INFO_V1(mvarchar_substring); Datum mvarchar_substring(PG_FUNCTION_ARGS); Datum mvarchar_substring(PG_FUNCTION_ARGS) { MVarChar *src = PG_GETARG_MVARCHAR(0); MVarChar *dst; int32 length; dst = (MVarChar*)palloc( VARSIZE(src) ); length = uchar_substring( src->data, UVARCHARLENGTH(src), PG_GETARG_INT32(1), PG_GETARG_INT32(2), false, dst->data); SET_VARSIZE(dst, MVARCHARHDRSZ + length *sizeof(UChar)); PG_FREE_IF_COPY(src, 0); PG_RETURN_MVARCHAR(dst); } PG_FUNCTION_INFO_V1(mvarchar_substring_no_len); Datum mvarchar_substring_no_len(PG_FUNCTION_ARGS); Datum mvarchar_substring_no_len(PG_FUNCTION_ARGS) { MVarChar *src = PG_GETARG_MVARCHAR(0); MVarChar *dst; int32 length; dst = (MVarChar*)palloc( VARSIZE(src) ); length = uchar_substring( src->data, UVARCHARLENGTH(src), PG_GETARG_INT32(1), -1, true, dst->data); SET_VARSIZE(dst, MVARCHARHDRSZ + length *sizeof(UChar)); PG_FREE_IF_COPY(src, 0); PG_RETURN_MVARCHAR(dst); } PG_FUNCTION_INFO_V1(mvarchar_hash); Datum mvarchar_hash(PG_FUNCTION_ARGS) { MVarChar *src = PG_GETARG_MVARCHAR(0); Datum res; res = hash_uchar( src->data, lengthWithoutSpaceVarChar(src) ); PG_FREE_IF_COPY(src, 0); PG_RETURN_DATUM( res ); } PG_FUNCTION_INFO_V1(mchar_hash); Datum mchar_hash(PG_FUNCTION_ARGS) { MChar *src = PG_GETARG_MCHAR(0); Datum res; res = hash_uchar( src->data, lengthWithoutSpaceChar(src) ); PG_FREE_IF_COPY(src, 0); PG_RETURN_DATUM( res ); } PG_FUNCTION_INFO_V1(mchar_upper); Datum mchar_upper(PG_FUNCTION_ARGS); Datum mchar_upper(PG_FUNCTION_ARGS) { MChar *src = PG_GETARG_MCHAR(0); MChar *dst = mchar_case_convert(src, u_strToUpper, "u_strToUpper"); PG_FREE_IF_COPY(src, 0); PG_RETURN_MCHAR( dst ); } PG_FUNCTION_INFO_V1(mchar_lower); Datum mchar_lower(PG_FUNCTION_ARGS); Datum mchar_lower(PG_FUNCTION_ARGS) { MChar *src = PG_GETARG_MCHAR(0); MChar *dst = mchar_case_convert(src, u_strToLower, "u_strToLower"); PG_FREE_IF_COPY(src, 0); PG_RETURN_MCHAR( dst ); } PG_FUNCTION_INFO_V1(mvarchar_upper); Datum mvarchar_upper(PG_FUNCTION_ARGS); Datum mvarchar_upper(PG_FUNCTION_ARGS) { MVarChar *src = PG_GETARG_MVARCHAR(0); MVarChar *dst = mvarchar_case_convert(src, u_strToUpper, "u_strToUpper"); PG_FREE_IF_COPY(src, 0); PG_RETURN_MVARCHAR( dst ); } PG_FUNCTION_INFO_V1(mvarchar_lower); Datum mvarchar_lower(PG_FUNCTION_ARGS); Datum mvarchar_lower(PG_FUNCTION_ARGS) { MVarChar *src = PG_GETARG_MVARCHAR(0); MVarChar *dst = mvarchar_case_convert(src, u_strToLower, "u_strToLower"); PG_FREE_IF_COPY(src, 0); PG_RETURN_MVARCHAR( dst ); }