utf8proc

annotate pgsql/utf8proc_pgsql.c @ 5:c18366878af9

Version 1.0.2

- included a check in Integer#utf8, which raises an exception, if the given code-point is invalid because of being too high (this was missing yet)
- added support for PostgreSQL version 8.2
author jbe
date Tue Dec 26 12:00:00 2006 +0100 (2006-12-26)
parents 4ee0d5f54af1
children fcfd8c836c64
rev   line source
jbe@0 1 /*
jbe@0 2 * Copyright (c) 2006, FlexiGuided GmbH, Berlin, Germany
jbe@0 3 * Author: Jan Behrens <jan.behrens@flexiguided.de>
jbe@0 4 * All rights reserved.
jbe@0 5 *
jbe@0 6 * Redistribution and use in source and binary forms, with or without
jbe@0 7 * modification, are permitted provided that the following conditions are
jbe@0 8 * met:
jbe@0 9 *
jbe@0 10 * 1. Redistributions of source code must retain the above copyright
jbe@0 11 * notice, this list of conditions and the following disclaimer.
jbe@0 12 * 2. Redistributions in binary form must reproduce the above copyright
jbe@0 13 * notice, this list of conditions and the following disclaimer in the
jbe@0 14 * documentation and/or other materials provided with the distribution.
jbe@0 15 * 3. Neither the name of the FlexiGuided GmbH nor the names of its
jbe@0 16 * contributors may be used to endorse or promote products derived from
jbe@0 17 * this software without specific prior written permission.
jbe@0 18 *
jbe@0 19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
jbe@0 20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
jbe@0 21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
jbe@0 22 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
jbe@0 23 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
jbe@0 24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
jbe@0 25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
jbe@0 26 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
jbe@0 27 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
jbe@0 28 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
jbe@0 29 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
jbe@0 30 *
jbe@0 31 */
jbe@0 32
jbe@0 33
jbe@0 34 /*
jbe@0 35 * File name: pgsql/utf8proc_pgsql.c
jbe@3 36 * Version: 1.0
jbe@3 37 * Last changed: 2006-09-17
jbe@0 38 *
jbe@0 39 * Description:
jbe@0 40 * PostgreSQL extension to provide a function 'unifold', which can be used
jbe@0 41 * to case-fold and normalize index fields.
jbe@0 42 */
jbe@0 43
jbe@0 44
jbe@0 45 #include "../utf8proc.c"
jbe@0 46
jbe@0 47 #include <postgres.h>
jbe@0 48 #include <utils/elog.h>
jbe@0 49 #include <fmgr.h>
jbe@1 50 #include <string.h>
jbe@0 51 #include <unistd.h>
jbe@0 52 #include <utils/builtins.h>
jbe@0 53
jbe@5 54 #ifdef PG_MODULE_MAGIC
jbe@5 55 PG_MODULE_MAGIC;
jbe@5 56 #endif
jbe@5 57
jbe@2 58 #define UTF8PROC_PGSQL_OPTS ( UTF8PROC_REJECTNA | UTF8PROC_COMPAT | \
jbe@2 59 UTF8PROC_COMPOSE | UTF8PROC_STABLE | UTF8PROC_IGNORE | UTF8PROC_STRIPCC | \
jbe@3 60 UTF8PROC_NLF2LF | UTF8PROC_CASEFOLD | UTF8PROC_LUMP )
jbe@1 61
jbe@0 62 PG_FUNCTION_INFO_V1(utf8proc_pgsql_unifold);
jbe@0 63 Datum utf8proc_pgsql_unifold(PG_FUNCTION_ARGS) {
jbe@1 64 text *input_string;
jbe@1 65 text *output_string = NULL;
jbe@0 66 ssize_t result;
jbe@1 67 input_string = PG_GETARG_TEXT_P(0);
jbe@1 68 do {
jbe@1 69 result = utf8proc_decompose(
jbe@1 70 VARDATA(input_string), VARSIZE(input_string) - VARHDRSZ,
jbe@1 71 NULL, 0, UTF8PROC_PGSQL_OPTS
jbe@1 72 );
jbe@1 73 if (result < 0) break;
jbe@1 74 if (result > (SIZE_MAX-1-VARHDRSZ)/sizeof(int32_t)) {
jbe@1 75 result = UTF8PROC_ERROR_OVERFLOW;
jbe@1 76 break;
jbe@1 77 }
jbe@1 78 output_string = palloc(result * sizeof(int32_t) + 1 + VARHDRSZ);
jbe@1 79 // reserve one extra byte for termination
jbe@1 80 if (!output_string) {
jbe@1 81 result = UTF8PROC_ERROR_NOMEM;
jbe@1 82 break;
jbe@1 83 }
jbe@1 84 result = utf8proc_decompose(
jbe@1 85 VARDATA(input_string), VARSIZE(input_string) - VARHDRSZ,
jbe@1 86 (int32_t *)VARDATA(output_string), result, UTF8PROC_PGSQL_OPTS);
jbe@1 87 if (result < 0) break;
jbe@1 88 result = utf8proc_reencode((int32_t *)VARDATA(output_string), result,
jbe@1 89 UTF8PROC_PGSQL_OPTS);
jbe@1 90 } while (0);
jbe@1 91 PG_FREE_IF_COPY(input_string, 0);
jbe@0 92 if (result < 0) {
jbe@0 93 int sqlerrcode;
jbe@1 94 if (output_string) pfree(output_string);
jbe@0 95 switch(result) {
jbe@0 96 case UTF8PROC_ERROR_NOMEM:
jbe@0 97 sqlerrcode = ERRCODE_OUT_OF_MEMORY; break;
jbe@0 98 case UTF8PROC_ERROR_OVERFLOW:
jbe@0 99 sqlerrcode = ERRCODE_PROGRAM_LIMIT_EXCEEDED; break;
jbe@0 100 case UTF8PROC_ERROR_INVALIDUTF8:
jbe@0 101 case UTF8PROC_ERROR_NOTASSIGNED:
jbe@1 102 PG_RETURN_NULL();
jbe@0 103 default:
jbe@0 104 sqlerrcode = ERRCODE_INTERNAL_ERROR;
jbe@0 105 }
jbe@0 106 ereport(ERROR, (
jbe@0 107 errcode(sqlerrcode),
jbe@0 108 errmsg("%s", utf8proc_errmsg(result))
jbe@0 109 ));
jbe@1 110 } else {
jbe@1 111 VARATT_SIZEP(output_string) = result + VARHDRSZ;
jbe@1 112 PG_RETURN_TEXT_P(output_string);
jbe@0 113 }
jbe@1 114 PG_RETURN_NULL(); // prohibit compiler warning
jbe@0 115 }
jbe@0 116
jbe@0 117

Impressum / About Us