-
Notifications
You must be signed in to change notification settings - Fork 7
/
icu_break.c
267 lines (224 loc) · 6.21 KB
/
icu_break.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
/*
* icu_break.c
*
* Part of icu_ext: a PostgreSQL extension to expose functionality from ICU
* (see http://icu-project.org)
*
* By Daniel Vérité, 2018-2023. See LICENSE.md
*/
#include "icu_ext.h"
#include "access/htup_details.h"
#include "funcapi.h"
#include "miscadmin.h"
#include "utils/builtins.h"
#include "utils/pg_locale.h"
#include "mb/pg_wchar.h"
#include "unicode/ubrk.h"
#include "unicode/ucnv.h"
#include "unicode/ucol.h"
#include "unicode/uloc.h"
#include "unicode/ustring.h"
#include "unicode/utext.h"
/*
* PG set-returning functions exposing ICU's BreakIterator API for
* characters, words, line-wrapping, sentences
*/
PG_FUNCTION_INFO_V1(icu_character_boundaries);
PG_FUNCTION_INFO_V1(icu_word_boundaries);
PG_FUNCTION_INFO_V1(icu_sentence_boundaries);
PG_FUNCTION_INFO_V1(icu_line_boundaries);
struct ubreak_ctxt {
UBreakIterator *iter;
UText* ut;
char* source_text;
UChar* cnv_text; /* unused and NULL if the database encoding is UTF-8 */
int32_t len;
TupleDesc tupdesc;
};
/*
* Initialize the context to iterate on the input.
* arg1=input string, arg2=locale
* The main difference between break iterators is:
* - UBRK_CHARACTER: return SETOF text
* - others: return SETOF (int,text)
*/
static void
init_srf_first_call(UBreakIteratorType break_type, PG_FUNCTION_ARGS)
{
MemoryContext oldcontext;
const char *brk_locale;
UErrorCode status = U_ZERO_ERROR;
FuncCallContext *funcctx;
struct ubreak_ctxt *ctxt;
funcctx = SRF_FIRSTCALL_INIT();
/*
* Switch to memory context appropriate for multiple function calls
*/
oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
ctxt = palloc(sizeof(struct ubreak_ctxt));
if (break_type != UBRK_CHARACTER)
{
TupleDesc tupdesc;
/* Construct tuple descriptor */
if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
errmsg("function returning record called in context that cannot accept type record")));
ctxt->tupdesc = BlessTupleDesc(tupdesc);
}
else
ctxt->tupdesc = NULL;
/* Use the UTF-8 ICU functions if our string is in UTF-8 */
if (GetDatabaseEncoding() == PG_UTF8)
{
text *txt = PG_GETARG_TEXT_PP(0);
ctxt->len = VARSIZE_ANY_EXHDR(txt);
ctxt->source_text = (char*)palloc(ctxt->len);
ctxt->cnv_text = NULL;
memcpy(ctxt->source_text, VARDATA_ANY(txt), ctxt->len);
ctxt->ut = utext_openUTF8(NULL,
ctxt->source_text,
ctxt->len,
&status);
if (U_FAILURE(status))
elog(ERROR, "utext_openUTF8() failed: %s", u_errorName(status));
}
else
{
text *input = PG_GETARG_TEXT_PP(0);
/* database encoding to UChar buffer */
ctxt->len = icu_to_uchar(&ctxt->cnv_text,
text_to_cstring(input),
VARSIZE_ANY_EXHDR(input));
ctxt->ut = utext_openUChars(NULL,
ctxt->cnv_text,
ctxt->len,
&status);
if (U_FAILURE(status))
elog(ERROR, "utext_openUChars() failed: %s", u_errorName(status));
}
funcctx->user_fctx = (void *) ctxt;
brk_locale = text_to_cstring(PG_GETARG_TEXT_PP(1));
MemoryContextSwitchTo(oldcontext);
ctxt->iter = ubrk_open(break_type, brk_locale, NULL, 0, &status);
if (U_FAILURE(status))
{
utext_close(ctxt->ut);
elog(ERROR, "ubrk_open failed: %s", u_errorName(status));
}
ubrk_setUText(ctxt->iter, ctxt->ut, &status);
if (U_FAILURE(status))
{
ubrk_close(ctxt->iter);
utext_close(ctxt->ut);
elog(ERROR, "ubrk_setText() failed: %s", u_errorName(status));
}
}
/*
* Return substrings (SETOF text). In general, they're are
* one-character only but CRLF are returned in one piece,
* and combining+base characters are also pieced together.
* In this respect it differs from regexp_split_to_table(text, '')
*/
Datum
icu_character_boundaries(PG_FUNCTION_ARGS)
{
FuncCallContext *funcctx;
int32_t pos0, pos;
struct ubreak_ctxt *ctxt;
if (SRF_IS_FIRSTCALL())
{
init_srf_first_call(UBRK_CHARACTER, fcinfo);
}
funcctx = SRF_PERCALL_SETUP();
ctxt = (struct ubreak_ctxt*) funcctx->user_fctx;
if (ctxt->len == 0)
SRF_RETURN_DONE(funcctx); /* no result */
pos0 = ubrk_current(ctxt->iter);
pos = ubrk_next(ctxt->iter);
if (pos != UBRK_DONE)
{
text *item;
if (ctxt->source_text != NULL)
item = cstring_to_text_with_len(ctxt->source_text+pos0, pos-pos0);
else
{
char *buf;
/* convert UChar to a buffer in the database encoding */
int32_t len = icu_from_uchar(&buf, ctxt->cnv_text+pos0, pos-pos0);
item = cstring_to_text_with_len(buf, len);
}
SRF_RETURN_NEXT(funcctx, PointerGetDatum(item));
}
else /* end of SRF iteration */
{
ubrk_close(ctxt->iter);
utext_close(ctxt->ut);
SRF_RETURN_DONE(funcctx);
}
}
/*
* Return (tag,content) tuples
*/
static Datum
icu_boundaries_internal(UBreakIteratorType break_type, PG_FUNCTION_ARGS)
{
FuncCallContext *funcctx;
int32_t pos0, pos1;
struct ubreak_ctxt *ctxt;
if (SRF_IS_FIRSTCALL())
{
init_srf_first_call(break_type, fcinfo);
}
funcctx = SRF_PERCALL_SETUP();
ctxt = (struct ubreak_ctxt*) funcctx->user_fctx;
if (ctxt->len == 0)
SRF_RETURN_DONE(funcctx); /* no result */
pos0 = ubrk_current(ctxt->iter);
do {
pos1 = ubrk_next(ctxt->iter);
if (pos1 != UBRK_DONE)
{
Datum values[2];
bool nulls[2];
HeapTuple tuple;
text *item;
if (ctxt->source_text != NULL)
{
item = cstring_to_text_with_len(ctxt->source_text + pos0, pos1-pos0);
}
else
{
char *buf;
/* convert back UChar to a buffer in the database encoding */
int32_t len = icu_from_uchar(&buf, ctxt->cnv_text + pos0, pos1-pos0);
item = cstring_to_text_with_len(buf, len);
}
values[0] = Int32GetDatum(ubrk_getRuleStatus(ctxt->iter));
nulls[0] = false;
values[1] = PointerGetDatum(item);
nulls[1] = false;
tuple = heap_form_tuple(ctxt->tupdesc, values, nulls);
SRF_RETURN_NEXT(funcctx, HeapTupleGetDatum(tuple));
}
} while (pos1 != UBRK_DONE);
/* end of SRF iteration */
ubrk_close(ctxt->iter);
utext_close(ctxt->ut);
SRF_RETURN_DONE(funcctx);
}
Datum
icu_word_boundaries(PG_FUNCTION_ARGS)
{
return icu_boundaries_internal(UBRK_WORD, fcinfo);
}
Datum
icu_line_boundaries(PG_FUNCTION_ARGS)
{
return icu_boundaries_internal(UBRK_LINE, fcinfo);
}
Datum
icu_sentence_boundaries(PG_FUNCTION_ARGS)
{
return icu_boundaries_internal(UBRK_SENTENCE, fcinfo);
}