utf8proc

view pgsql/utf8proc_pgsql.c @ 2:aaad485d5335

Version 0.3

- changed normalization from NFC to NFKC for postgresql unifold function
- added support to mark the beginning of a grapheme cluster with 0xFF (option: CHARBOUND)
- added the ruby method String#chars, which is returning an array of UTF-8 encoded grapheme clusters
- added NLF2LF transformation in postgresql unifold function
- added the DECOMPOSE option, if you neither use COMPOSE or DECOMPOSE, no normalization will be performed (different from previous versions)
- using integer constants rather than C-strings for character properties
- fixed (hopefully) a problem with the ruby library on Mac OS X, which occured when compiler optimization was switched on
author jbe
date Fri Aug 04 12:00:00 2006 +0200 (2006-08-04)
parents 61a89ecc2fb9
children 4ee0d5f54af1
line source
1 /*
2 * Copyright (c) 2006, FlexiGuided GmbH, Berlin, Germany
3 * Author: Jan Behrens <jan.behrens@flexiguided.de>
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met:
9 *
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the FlexiGuided GmbH nor the names of its
16 * contributors may be used to endorse or promote products derived from
17 * this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
22 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
23 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
26 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
27 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 *
31 */
34 /*
35 * File name: pgsql/utf8proc_pgsql.c
36 * Version: 0.3
37 * Last changed: 2006-08-04
38 *
39 * Description:
40 * PostgreSQL extension to provide a function 'unifold', which can be used
41 * to case-fold and normalize index fields.
42 */
45 #include "../utf8proc.c"
47 #include <postgres.h>
48 #include <utils/elog.h>
49 #include <fmgr.h>
50 #include <string.h>
51 #include <unistd.h>
52 #include <utils/builtins.h>
54 #define UTF8PROC_PGSQL_OPTS ( UTF8PROC_REJECTNA | UTF8PROC_COMPAT | \
55 UTF8PROC_COMPOSE | UTF8PROC_STABLE | UTF8PROC_IGNORE | UTF8PROC_STRIPCC | \
56 UTF8PROC_NLF2LF | UTF8PROC_CASEFOLD )
58 PG_FUNCTION_INFO_V1(utf8proc_pgsql_unifold);
59 Datum utf8proc_pgsql_unifold(PG_FUNCTION_ARGS) {
60 text *input_string;
61 text *output_string = NULL;
62 ssize_t result;
63 input_string = PG_GETARG_TEXT_P(0);
64 do {
65 result = utf8proc_decompose(
66 VARDATA(input_string), VARSIZE(input_string) - VARHDRSZ,
67 NULL, 0, UTF8PROC_PGSQL_OPTS
68 );
69 if (result < 0) break;
70 if (result > (SIZE_MAX-1-VARHDRSZ)/sizeof(int32_t)) {
71 result = UTF8PROC_ERROR_OVERFLOW;
72 break;
73 }
74 output_string = palloc(result * sizeof(int32_t) + 1 + VARHDRSZ);
75 // reserve one extra byte for termination
76 if (!output_string) {
77 result = UTF8PROC_ERROR_NOMEM;
78 break;
79 }
80 result = utf8proc_decompose(
81 VARDATA(input_string), VARSIZE(input_string) - VARHDRSZ,
82 (int32_t *)VARDATA(output_string), result, UTF8PROC_PGSQL_OPTS);
83 if (result < 0) break;
84 result = utf8proc_reencode((int32_t *)VARDATA(output_string), result,
85 UTF8PROC_PGSQL_OPTS);
86 } while (0);
87 PG_FREE_IF_COPY(input_string, 0);
88 if (result < 0) {
89 int sqlerrcode;
90 if (output_string) pfree(output_string);
91 switch(result) {
92 case UTF8PROC_ERROR_NOMEM:
93 sqlerrcode = ERRCODE_OUT_OF_MEMORY; break;
94 case UTF8PROC_ERROR_OVERFLOW:
95 sqlerrcode = ERRCODE_PROGRAM_LIMIT_EXCEEDED; break;
96 case UTF8PROC_ERROR_INVALIDUTF8:
97 case UTF8PROC_ERROR_NOTASSIGNED:
98 PG_RETURN_NULL();
99 default:
100 sqlerrcode = ERRCODE_INTERNAL_ERROR;
101 }
102 ereport(ERROR, (
103 errcode(sqlerrcode),
104 errmsg("%s", utf8proc_errmsg(result))
105 ));
106 } else {
107 VARATT_SIZEP(output_string) = result + VARHDRSZ;
108 PG_RETURN_TEXT_P(output_string);
109 }
110 PG_RETURN_NULL(); // prohibit compiler warning
111 }

Impressum / About Us