utf8proc

annotate pgsql/utf8proc_pgsql.c @ 3:4ee0d5f54af1

Version 1.0

- added the LUMP option, which lumps certain characters together (see lump.txt) (also used for the PostgreSQL "unifold" function)
- added the STRIPMARK option, which strips marking characters (or marks of composed characters)
- deprecated ruby method String#char_ary in favour of String#utf8chars
author jbe
date Sun Sep 17 12:00:00 2006 +0200 (2006-09-17)
parents aaad485d5335
children c18366878af9
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@2 54 #define UTF8PROC_PGSQL_OPTS ( UTF8PROC_REJECTNA | UTF8PROC_COMPAT | \
jbe@2 55 UTF8PROC_COMPOSE | UTF8PROC_STABLE | UTF8PROC_IGNORE | UTF8PROC_STRIPCC | \
jbe@3 56 UTF8PROC_NLF2LF | UTF8PROC_CASEFOLD | UTF8PROC_LUMP )
jbe@1 57
jbe@0 58 PG_FUNCTION_INFO_V1(utf8proc_pgsql_unifold);
jbe@0 59 Datum utf8proc_pgsql_unifold(PG_FUNCTION_ARGS) {
jbe@1 60 text *input_string;
jbe@1 61 text *output_string = NULL;
jbe@0 62 ssize_t result;
jbe@1 63 input_string = PG_GETARG_TEXT_P(0);
jbe@1 64 do {
jbe@1 65 result = utf8proc_decompose(
jbe@1 66 VARDATA(input_string), VARSIZE(input_string) - VARHDRSZ,
jbe@1 67 NULL, 0, UTF8PROC_PGSQL_OPTS
jbe@1 68 );
jbe@1 69 if (result < 0) break;
jbe@1 70 if (result > (SIZE_MAX-1-VARHDRSZ)/sizeof(int32_t)) {
jbe@1 71 result = UTF8PROC_ERROR_OVERFLOW;
jbe@1 72 break;
jbe@1 73 }
jbe@1 74 output_string = palloc(result * sizeof(int32_t) + 1 + VARHDRSZ);
jbe@1 75 // reserve one extra byte for termination
jbe@1 76 if (!output_string) {
jbe@1 77 result = UTF8PROC_ERROR_NOMEM;
jbe@1 78 break;
jbe@1 79 }
jbe@1 80 result = utf8proc_decompose(
jbe@1 81 VARDATA(input_string), VARSIZE(input_string) - VARHDRSZ,
jbe@1 82 (int32_t *)VARDATA(output_string), result, UTF8PROC_PGSQL_OPTS);
jbe@1 83 if (result < 0) break;
jbe@1 84 result = utf8proc_reencode((int32_t *)VARDATA(output_string), result,
jbe@1 85 UTF8PROC_PGSQL_OPTS);
jbe@1 86 } while (0);
jbe@1 87 PG_FREE_IF_COPY(input_string, 0);
jbe@0 88 if (result < 0) {
jbe@0 89 int sqlerrcode;
jbe@1 90 if (output_string) pfree(output_string);
jbe@0 91 switch(result) {
jbe@0 92 case UTF8PROC_ERROR_NOMEM:
jbe@0 93 sqlerrcode = ERRCODE_OUT_OF_MEMORY; break;
jbe@0 94 case UTF8PROC_ERROR_OVERFLOW:
jbe@0 95 sqlerrcode = ERRCODE_PROGRAM_LIMIT_EXCEEDED; break;
jbe@0 96 case UTF8PROC_ERROR_INVALIDUTF8:
jbe@0 97 case UTF8PROC_ERROR_NOTASSIGNED:
jbe@1 98 PG_RETURN_NULL();
jbe@0 99 default:
jbe@0 100 sqlerrcode = ERRCODE_INTERNAL_ERROR;
jbe@0 101 }
jbe@0 102 ereport(ERROR, (
jbe@0 103 errcode(sqlerrcode),
jbe@0 104 errmsg("%s", utf8proc_errmsg(result))
jbe@0 105 ));
jbe@1 106 } else {
jbe@1 107 VARATT_SIZEP(output_string) = result + VARHDRSZ;
jbe@1 108 PG_RETURN_TEXT_P(output_string);
jbe@0 109 }
jbe@1 110 PG_RETURN_NULL(); // prohibit compiler warning
jbe@0 111 }
jbe@0 112
jbe@0 113

Impressum / About Us