utf8proc
annotate pgsql/utf8proc_pgsql.c @ 1:61a89ecc2fb9
Version 0.2
- changed behaviour of PostgreSQL function to return NULL in case of invalid input, rather than raising an exceptional condition
- improved efficiency of PostgreSQL function (no transformation to C string is done)
- added -fpic compiler flag in Makefile
- fixed bug in the C code for the ruby library (usage of non-existent function)
- changed behaviour of PostgreSQL function to return NULL in case of invalid input, rather than raising an exceptional condition
- improved efficiency of PostgreSQL function (no transformation to C string is done)
- added -fpic compiler flag in Makefile
- fixed bug in the C code for the ruby library (usage of non-existent function)
| author | jbe | 
|---|---|
| date | Tue Jun 20 12:00:00 2006 +0200 (2006-06-20) | 
| parents | a0368662434c | 
| children | aaad485d5335 | 
| 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@1 | 36 * Version: 0.2 | 
| jbe@1 | 37 * Last changed: 2006-06-05 | 
| 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@1 | 54 #define UTF8PROC_PGSQL_OPTS ( UTF8PROC_REJECTNA | \ | 
| jbe@1 | 55 UTF8PROC_COMPOSE | UTF8PROC_IGNORE | UTF8PROC_STRIPCC | UTF8PROC_CASEFOLD) | 
| jbe@1 | 56 | 
| jbe@0 | 57 PG_FUNCTION_INFO_V1(utf8proc_pgsql_unifold); | 
| jbe@0 | 58 Datum utf8proc_pgsql_unifold(PG_FUNCTION_ARGS) { | 
| jbe@1 | 59 text *input_string; | 
| jbe@1 | 60 text *output_string = NULL; | 
| jbe@0 | 61 ssize_t result; | 
| jbe@1 | 62 input_string = PG_GETARG_TEXT_P(0); | 
| jbe@1 | 63 do { | 
| jbe@1 | 64 result = utf8proc_decompose( | 
| jbe@1 | 65 VARDATA(input_string), VARSIZE(input_string) - VARHDRSZ, | 
| jbe@1 | 66 NULL, 0, UTF8PROC_PGSQL_OPTS | 
| jbe@1 | 67 ); | 
| jbe@1 | 68 if (result < 0) break; | 
| jbe@1 | 69 if (result > (SIZE_MAX-1-VARHDRSZ)/sizeof(int32_t)) { | 
| jbe@1 | 70 result = UTF8PROC_ERROR_OVERFLOW; | 
| jbe@1 | 71 break; | 
| jbe@1 | 72 } | 
| jbe@1 | 73 output_string = palloc(result * sizeof(int32_t) + 1 + VARHDRSZ); | 
| jbe@1 | 74 // reserve one extra byte for termination | 
| jbe@1 | 75 if (!output_string) { | 
| jbe@1 | 76 result = UTF8PROC_ERROR_NOMEM; | 
| jbe@1 | 77 break; | 
| jbe@1 | 78 } | 
| jbe@1 | 79 result = utf8proc_decompose( | 
| jbe@1 | 80 VARDATA(input_string), VARSIZE(input_string) - VARHDRSZ, | 
| jbe@1 | 81 (int32_t *)VARDATA(output_string), result, UTF8PROC_PGSQL_OPTS); | 
| jbe@1 | 82 if (result < 0) break; | 
| jbe@1 | 83 result = utf8proc_reencode((int32_t *)VARDATA(output_string), result, | 
| jbe@1 | 84 UTF8PROC_PGSQL_OPTS); | 
| jbe@1 | 85 } while (0); | 
| jbe@1 | 86 PG_FREE_IF_COPY(input_string, 0); | 
| jbe@0 | 87 if (result < 0) { | 
| jbe@0 | 88 int sqlerrcode; | 
| jbe@1 | 89 if (output_string) pfree(output_string); | 
| jbe@0 | 90 switch(result) { | 
| jbe@0 | 91 case UTF8PROC_ERROR_NOMEM: | 
| jbe@0 | 92 sqlerrcode = ERRCODE_OUT_OF_MEMORY; break; | 
| jbe@0 | 93 case UTF8PROC_ERROR_OVERFLOW: | 
| jbe@0 | 94 sqlerrcode = ERRCODE_PROGRAM_LIMIT_EXCEEDED; break; | 
| jbe@0 | 95 case UTF8PROC_ERROR_INVALIDUTF8: | 
| jbe@0 | 96 case UTF8PROC_ERROR_NOTASSIGNED: | 
| jbe@1 | 97 PG_RETURN_NULL(); | 
| jbe@0 | 98 default: | 
| jbe@0 | 99 sqlerrcode = ERRCODE_INTERNAL_ERROR; | 
| jbe@0 | 100 } | 
| jbe@0 | 101 ereport(ERROR, ( | 
| jbe@0 | 102 errcode(sqlerrcode), | 
| jbe@0 | 103 errmsg("%s", utf8proc_errmsg(result)) | 
| jbe@0 | 104 )); | 
| jbe@1 | 105 } else { | 
| jbe@1 | 106 VARATT_SIZEP(output_string) = result + VARHDRSZ; | 
| jbe@1 | 107 PG_RETURN_TEXT_P(output_string); | 
| jbe@0 | 108 } | 
| jbe@1 | 109 PG_RETURN_NULL(); // prohibit compiler warning | 
| jbe@0 | 110 } | 
| jbe@0 | 111 | 
| jbe@0 | 112 |