utf8proc
diff pgsql/utf8proc_pgsql.c @ 0:a0368662434c
Version 0.1
author | jbe |
---|---|
date | Fri Jun 02 12:00:00 2006 +0200 (2006-06-02) |
parents | |
children | 61a89ecc2fb9 |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/pgsql/utf8proc_pgsql.c Fri Jun 02 12:00:00 2006 +0200 1.3 @@ -0,0 +1,92 @@ 1.4 +/* 1.5 + * Copyright (c) 2006, FlexiGuided GmbH, Berlin, Germany 1.6 + * Author: Jan Behrens <jan.behrens@flexiguided.de> 1.7 + * All rights reserved. 1.8 + * 1.9 + * Redistribution and use in source and binary forms, with or without 1.10 + * modification, are permitted provided that the following conditions are 1.11 + * met: 1.12 + * 1.13 + * 1. Redistributions of source code must retain the above copyright 1.14 + * notice, this list of conditions and the following disclaimer. 1.15 + * 2. Redistributions in binary form must reproduce the above copyright 1.16 + * notice, this list of conditions and the following disclaimer in the 1.17 + * documentation and/or other materials provided with the distribution. 1.18 + * 3. Neither the name of the FlexiGuided GmbH nor the names of its 1.19 + * contributors may be used to endorse or promote products derived from 1.20 + * this software without specific prior written permission. 1.21 + * 1.22 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 1.23 + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 1.24 + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 1.25 + * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER 1.26 + * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 1.27 + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 1.28 + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 1.29 + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 1.30 + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 1.31 + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 1.32 + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 1.33 + * 1.34 + */ 1.35 + 1.36 + 1.37 +/* 1.38 + * File name: pgsql/utf8proc_pgsql.c 1.39 + * Version: 0.1 1.40 + * Last changed: 2006-05-31 1.41 + * 1.42 + * Description: 1.43 + * PostgreSQL extension to provide a function 'unifold', which can be used 1.44 + * to case-fold and normalize index fields. 1.45 + */ 1.46 + 1.47 + 1.48 +#include "../utf8proc.c" 1.49 + 1.50 +#include <postgres.h> 1.51 +#include <utils/elog.h> 1.52 +#include <fmgr.h> 1.53 +#include <unistd.h> 1.54 +#include <string.h> 1.55 +#include <utils/builtins.h> 1.56 + 1.57 +PG_FUNCTION_INFO_V1(utf8proc_pgsql_unifold); 1.58 + 1.59 +Datum utf8proc_pgsql_unifold(PG_FUNCTION_ARGS) { 1.60 + char *input_string; 1.61 + uint8_t *output_string; 1.62 + ssize_t result; 1.63 + input_string = DatumGetCString( 1.64 + DirectFunctionCall1(textout, PG_GETARG_DATUM(0)) 1.65 + ); 1.66 + result = utf8proc_map(input_string, 0, &output_string, UTF8PROC_NULLTERM | 1.67 + UTF8PROC_COMPOSE | UTF8PROC_IGNORE | UTF8PROC_STRIPCC | UTF8PROC_CASEFOLD); 1.68 + pfree(input_string); 1.69 + if (result < 0) { 1.70 + int sqlerrcode; 1.71 + switch(result) { 1.72 + case UTF8PROC_ERROR_NOMEM: 1.73 + sqlerrcode = ERRCODE_OUT_OF_MEMORY; break; 1.74 + case UTF8PROC_ERROR_OVERFLOW: 1.75 + sqlerrcode = ERRCODE_PROGRAM_LIMIT_EXCEEDED; break; 1.76 + case UTF8PROC_ERROR_INVALIDUTF8: 1.77 + case UTF8PROC_ERROR_NOTASSIGNED: 1.78 + sqlerrcode = ERRCODE_DATA_EXCEPTION; break; 1.79 + default: 1.80 + sqlerrcode = ERRCODE_INTERNAL_ERROR; 1.81 + } 1.82 + ereport(ERROR, ( 1.83 + errcode(sqlerrcode), 1.84 + errmsg("%s", utf8proc_errmsg(result)) 1.85 + )); 1.86 + } 1.87 + { 1.88 + Datum retval; 1.89 + retval = DirectFunctionCall1(textin, CStringGetDatum(output_string)); 1.90 + free(output_string); 1.91 + PG_RETURN_TEXT_P(DatumGetTextP(retval)); 1.92 + } 1.93 +} 1.94 + 1.95 +