-
Notifications
You must be signed in to change notification settings - Fork 7
/
pg_cjk_parser.c
3009 lines (2682 loc) · 85.3 KB
/
pg_cjk_parser.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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*-------------------------------------------------------------------------
*
* wparser_def.c
* Default text search parser
*
* Portions Copyright (c) 1996-2018, PostgreSQL Global Development Group
*
*
* IDENTIFICATION
* src/backend/tsearch/wparser_def.c
*
*-------------------------------------------------------------------------
*/
#include "postgres.h"
#include <limits.h>
#include "catalog/pg_collation.h"
#include "commands/defrem.h"
#include "tsearch/ts_locale.h"
#include "tsearch/ts_public.h"
#include "tsearch/ts_type.h"
#include "tsearch/ts_utils.h"
#include "utils/builtins.h"
#include "zht2zhs.h"
Datum prsd2_start(PG_FUNCTION_ARGS);
Datum prsd2_nexttoken(PG_FUNCTION_ARGS);
Datum prsd2_end(PG_FUNCTION_ARGS);
Datum prsd2_headline(PG_FUNCTION_ARGS);
Datum prsd2_lextype(PG_FUNCTION_ARGS);
PG_MODULE_MAGIC;
PG_FUNCTION_INFO_V1(prsd2_start);
PG_FUNCTION_INFO_V1(prsd2_nexttoken);
PG_FUNCTION_INFO_V1(prsd2_end);
PG_FUNCTION_INFO_V1(prsd2_headline);
PG_FUNCTION_INFO_V1(prsd2_lextype);
/* Define me to enable tracing of parser behavior */
/* #define WPARSER_TRACE */
/* Output token categories */
#define ASCIIWORD 1
#define WORD_T 2
#define NUMWORD 3
#define EMAIL 4
#define URL_T 5
#define HOST 6
#define SCIENTIFIC 7
#define VERSIONNUMBER 8
#define NUMPARTHWORD 9
#define PARTHWORD 10
#define ASCIIPARTHWORD 11
#define SPACE 12
#define TAG_T 13
#define PROTOCOL 14
#define NUMHWORD 15
#define ASCIIHWORD 16
#define HWORD 17
#define URLPATH 18
#define FILEPATH 19
#define DECIMAL_T 20
#define SIGNEDINT 21
#define UNSIGNEDINT 22
#define XMLENTITY 23
#define CJK_CHAR 24
#define LASTNUM 24
static const char *const tok_alias[] = {
"",
"asciiword",
"word",
"numword",
"email",
"url",
"host",
"sfloat",
"version",
"hword_numpart",
"hword_part",
"hword_asciipart",
"blank",
"tag",
"protocol",
"numhword",
"asciihword",
"hword",
"url_path",
"file",
"float",
"int",
"uint",
"entity",
"cjk"
};
static const char *const lex_descr[] = {
"",
"Word, all ASCII",
"Word, all letters",
"Word, letters and digits",
"Email address",
"URL",
"Host",
"Scientific notation",
"Version number",
"Hyphenated word part, letters and digits",
"Hyphenated word part, all letters",
"Hyphenated word part, all ASCII",
"Space symbols",
"XML tag",
"Protocol head",
"Hyphenated word, letters and digits",
"Hyphenated word, all ASCII",
"Hyphenated word, all letters",
"URL path",
"File or path name",
"Decimal notation",
"Signed integer",
"Unsigned integer",
"XML entity",
"CJK Char"
};
/* Parser states */
typedef enum
{
TPS_Base = 0,
TPS_InNumWord,
TPS_InAsciiWord,
TPS_InWord,
TPS_InUnsignedInt,
TPS_InSignedIntFirst,
TPS_InSignedInt,
TPS_InSpace,
TPS_InUDecimalFirst,
TPS_InUDecimal,
TPS_InDecimalFirst,
TPS_InDecimal,
TPS_InVerVersion,
TPS_InSVerVersion,
TPS_InVersionFirst,
TPS_InVersion,
TPS_InMantissaFirst,
TPS_InMantissaSign,
TPS_InMantissa,
TPS_InXMLEntityFirst,
TPS_InXMLEntity,
TPS_InXMLEntityNumFirst,
TPS_InXMLEntityNum,
TPS_InXMLEntityHexNumFirst,
TPS_InXMLEntityHexNum,
TPS_InXMLEntityEnd,
TPS_InTagFirst,
TPS_InXMLBegin,
TPS_InTagCloseFirst,
TPS_InTagName,
TPS_InTagBeginEnd,
TPS_InTag,
TPS_InTagEscapeK,
TPS_InTagEscapeKK,
TPS_InTagBackSleshed,
TPS_InTagEnd,
TPS_InCommentFirst,
TPS_InCommentLast,
TPS_InComment,
TPS_InCloseCommentFirst,
TPS_InCloseCommentLast,
TPS_InCommentEnd,
TPS_InHostFirstDomain,
TPS_InHostDomainSecond,
TPS_InHostDomain,
TPS_InPortFirst,
TPS_InPort,
TPS_InHostFirstAN,
TPS_InHost,
TPS_InEmail,
TPS_InFileFirst,
TPS_InFileTwiddle,
TPS_InPathFirst,
TPS_InPathFirstFirst,
TPS_InPathSecond,
TPS_InFile,
TPS_InFileNext,
TPS_InURLPathFirst,
TPS_InURLPathStart,
TPS_InURLPath,
TPS_InFURL,
TPS_InProtocolFirst,
TPS_InProtocolSecond,
TPS_InProtocolEnd,
TPS_InHyphenAsciiWordFirst,
TPS_InHyphenAsciiWord,
TPS_InHyphenWordFirst,
TPS_InHyphenWord,
TPS_InHyphenNumWordFirst,
TPS_InHyphenNumWord,
TPS_InHyphenDigitLookahead,
TPS_InParseHyphen,
TPS_InParseHyphenHyphen,
TPS_InHyphenWordPart,
TPS_InHyphenAsciiWordPart,
TPS_InHyphenNumWordPart,
TPS_InHyphenUnsignedInt,
TPS_InCJK,
TPS_Null /* last state (fake value) */
} TParserState;
/* forward declaration */
struct TParser;
typedef int (*TParserCharTest) (struct TParser *); /* any p_is* functions
* except p_iseq */
typedef void (*TParserSpecial) (struct TParser *); /* special handler for
* special cases... */
typedef struct
{
TParserCharTest isclass;
char c;
uint16 flags;
TParserState tostate;
int type;
TParserSpecial special;
} TParserStateActionItem;
/* Flag bits in TParserStateActionItem.flags */
#define A_NEXT 0x0000
#define A_BINGO 0x0001
#define A_POP 0x0002
#define A_PUSH 0x0004
#define A_RERUN 0x0008
#define A_CLEAR 0x0010
#define A_MERGE 0x0020
#define A_CLRALL 0x0040
typedef struct TParserPosition
{
int posbyte; /* position of parser in bytes */
int poschar; /* position of parser in characters */
int charlen; /* length of current char */
int lenbytetoken; /* length of token-so-far in bytes */
int lenchartoken; /* and in chars */
TParserState state;
struct TParserPosition *prev;
const TParserStateActionItem *pushedAtAction;
} TParserPosition;
typedef struct TParser
{
/* string and position information */
char *str; /* multibyte string */
int lenstr; /* length of mbstring */
wchar_t *wstr; /* wide character string */
pg_wchar *pgwstr; /* wide character string for C-locale */
bool usewide;
/* State of parse */
int charmaxlen;
TParserPosition *state;
bool ignore;
bool wanthost;
/* silly char */
char c;
/* out */
char *token;
int lenbytetoken;
int lenchartoken;
int type;
} TParser;
/* forward decls here */
static bool TParserGet(TParser *prs);
static TParserPosition *
newTParserPosition(TParserPosition *prev)
{
TParserPosition *res = (TParserPosition *) palloc(sizeof(TParserPosition));
if (prev)
memcpy(res, prev, sizeof(TParserPosition));
else
memset(res, 0, sizeof(TParserPosition));
res->prev = prev;
res->pushedAtAction = NULL;
return res;
}
static TParser *
TParserInit(char *str, int len)
{
TParser *prs = (TParser *) palloc0(sizeof(TParser));
prs->charmaxlen = pg_database_encoding_max_length();
prs->str = str;
prs->lenstr = len;
/*
* Use wide char code only when max encoding length > 1.
*/
if (prs->charmaxlen > 1)
{
Oid collation = DEFAULT_COLLATION_OID; /* TODO */
pg_locale_t mylocale = 0; /* TODO */
prs->usewide = true;
if (lc_ctype_is_c(collation))
{
/*
* char2wchar doesn't work for C-locale and sizeof(pg_wchar) could
* be different from sizeof(wchar_t)
*/
prs->pgwstr = (pg_wchar *) palloc(sizeof(pg_wchar) * (prs->lenstr + 1));
pg_mb2wchar_with_len(prs->str, prs->pgwstr, prs->lenstr);
}
else
{
prs->wstr = (wchar_t *) palloc(sizeof(wchar_t) * (prs->lenstr + 1));
char2wchar(prs->wstr, prs->lenstr + 1, prs->str, prs->lenstr,
mylocale);
}
}
else
prs->usewide = false;
prs->state = newTParserPosition(NULL);
prs->state->state = TPS_Base;
#ifdef WPARSER_TRACE
/*
* Use of %.*s here is a bit risky since it can misbehave if the data is
* not in what libc thinks is the prevailing encoding. However, since
* this is just a debugging aid, we choose to live with that.
*/
fprintf(stderr, "parsing \"%.*s\"\n", len, str);
#endif
return prs;
}
/*
* As an alternative to a full TParserInit one can create a
* TParserCopy which basically is a regular TParser without a private
* copy of the string - instead it uses the one from another TParser.
* This is useful because at some places TParsers are created
* recursively and the repeated copying around of the strings can
* cause major inefficiency if the source string is long.
* The new parser starts parsing at the original's current position.
*
* Obviously one must not close the original TParser before the copy.
*/
static TParser *
TParserCopyInit(const TParser *orig)
{
TParser *prs = (TParser *) palloc0(sizeof(TParser));
prs->charmaxlen = orig->charmaxlen;
prs->str = orig->str + orig->state->posbyte;
prs->lenstr = orig->lenstr - orig->state->posbyte;
prs->usewide = orig->usewide;
if (orig->pgwstr)
prs->pgwstr = orig->pgwstr + orig->state->poschar;
if (orig->wstr)
prs->wstr = orig->wstr + orig->state->poschar;
prs->state = newTParserPosition(NULL);
prs->state->state = TPS_Base;
#ifdef WPARSER_TRACE
/* See note above about %.*s */
fprintf(stderr, "parsing copy of \"%.*s\"\n", prs->lenstr, prs->str);
#endif
return prs;
}
static void
TParserClose(TParser *prs)
{
while (prs->state)
{
TParserPosition *ptr = prs->state->prev;
pfree(prs->state);
prs->state = ptr;
}
if (prs->wstr)
pfree(prs->wstr);
if (prs->pgwstr)
pfree(prs->pgwstr);
#ifdef WPARSER_TRACE
fprintf(stderr, "closing parser\n");
#endif
pfree(prs);
}
/*
* Close a parser created with TParserCopyInit
*/
static void
TParserCopyClose(TParser *prs)
{
while (prs->state)
{
TParserPosition *ptr = prs->state->prev;
pfree(prs->state);
prs->state = ptr;
}
#ifdef WPARSER_TRACE
fprintf(stderr, "closing parser copy\n");
#endif
pfree(prs);
}
/*
* Character-type support functions, equivalent to is* macros, but
* working with any possible encodings and locales. Notes:
* - with multibyte encoding and C-locale isw* function may fail
* or give wrong result.
* - multibyte encoding and C-locale often are used for
* Asian languages.
* - if locale is C then we use pgwstr instead of wstr.
*/
#define p_iswhat(type, nonascii) \
\
static int \
p_is##type(TParser *prs) \
{ \
Assert(prs->state); \
if (prs->usewide) \
{ \
if (prs->pgwstr) \
{ \
unsigned int c = *(prs->pgwstr + prs->state->poschar); \
if (c > 0x7f) \
return nonascii; \
return is##type(c); \
} \
return isw##type(*(prs->wstr + prs->state->poschar)); \
} \
return is##type(*(unsigned char *) (prs->str + prs->state->posbyte)); \
} \
\
static int \
p_isnot##type(TParser *prs) \
{ \
return !p_is##type(prs); \
}
/*
* In C locale with a multibyte encoding, any non-ASCII symbol is considered
* an alpha character, but not a member of other char classes.
*/
p_iswhat(alnum, 1)
p_iswhat(alpha, 1)
p_iswhat(digit, 0)
p_iswhat(lower, 0)
p_iswhat(print, 0)
p_iswhat(punct, 0)
p_iswhat(space, 0)
p_iswhat(upper, 0)
p_iswhat(xdigit, 0)
/* p_iseq should be used only for ascii symbols */
static int
p_iseq(TParser *prs, char c)
{
Assert(prs->state);
return ((prs->state->charlen == 1 && *(prs->str + prs->state->posbyte) == c)) ? 1 : 0;
}
static int
p_isEOF(TParser *prs)
{
Assert(prs->state);
return (prs->state->posbyte == prs->lenstr || prs->state->charlen == 0) ? 1 : 0;
}
static int
p_iseqC(TParser *prs)
{
return p_iseq(prs, prs->c);
}
static int
p_isneC(TParser *prs)
{
return !p_iseq(prs, prs->c);
}
static int
p_isascii(TParser *prs)
{
return (prs->state->charlen == 1 && isascii((unsigned char) *(prs->str + prs->state->posbyte))) ? 1 : 0;
}
static int
p_isasclet(TParser *prs)
{
return (p_isascii(prs) && p_isalpha(prs)) ? 1 : 0;
}
static int
p_isurlchar(TParser *prs)
{
char ch;
/* no non-ASCII need apply */
if (prs->state->charlen != 1)
return 0;
ch = *(prs->str + prs->state->posbyte);
/* no spaces or control characters */
if (ch <= 0x20 || ch >= 0x7F)
return 0;
/* reject characters disallowed by RFC 3986 */
switch (ch)
{
case '"':
case '<':
case '>':
case '\\':
case '^':
case '`':
case '{':
case '|':
case '}':
return 0;
}
return 1;
}
/**
*
2E80–2EFF CJK Radicals Supplement 中日韓部首補充
2F00–2FDF Kangxi Radicals 康熙部首
2FF0–2FFF Ideographic Description Characters 漢字結構描述字符
3000–303F CJK Symbols and Punctuation 中日韓符號和標點
3040–309F Hiragana 平假名
30A0–30FF Katakana 片假名
3100–312F Bopomofo 注音符號
3130–318F Hangul Compatibility Jamo 諺文相容字母
3190–319F Kanbun 漢文訓點號
31A0–31BF Bopomofo Extended 注音符號擴充
31C0–31EF CJK Strokes 中日韓筆畫部件
31F0–31FF Katakana Phonetic Extensions 片假名音標擴充
3200–32FF Enclosed CJK Letters and Months 括圈中日韓字母及月份
3300–33FF CJK Compatibility 中日韓相容字元
3400–4DBF CJK Unified Ideographs Extension A 中日韓統一表意文字擴充A
4DC0–4DFF Yijing Hexagram Symbols 易經六十四卦象
4E00–9FFF CJK Unified Ideographs 中日韓統一表意文字
1D300–1D35F Tai Xuan Jing Symbols 太玄經符號
20000–2A6DF CJK Unified Ideographs Extension B 中日韓統一表意文字擴充B
2A700–2B73F CJK Unified Ideographs Extension C 中日韓統一表意文字擴充C
2B740–2B81F CJK Unified Ideographs Extension D 中日韓統一表意文字擴充D
2B820–2CEAF CJK Unified Ideographs Extension E 中日韓統一表意文字擴充E
2CEB0–2EBEF CJK Unified Ideographs Extension F 中日韓統一表意文字擴充F
2F800–2FA1F CJK Compatibility Ideographs Supplement 中日韓相容表意文字補充
* */
static int ext_code_plane_cjk[] = {
0x1D300, 0x1D35F,
0x20000, 0x2B73F,
0x2A700, 0x2B7F3,
0x2B740, 0x2B8EF,
0x2B820, 0x2CEAF,
0x2CEB9, 0x2EBEF,
0x2F800, 0x2FA1F,
};
static int
p_isnotCJK(TParser *prs){
/*
* pg_dsplen could return -1 which means error or control character
*/
if (pg_dsplen(prs->str + prs->state->posbyte) == 0)
return 1;
if (GetDatabaseEncoding() == PG_UTF8 && prs->usewide) {
//p_isCJKchar only works in UTF8 encoding
pg_wchar c;
if (prs->pgwstr)
c = *(prs->pgwstr + prs->state->poschar);
else
c = (pg_wchar) *(prs->wstr + prs->state->poschar);
if ((c >= 0x2E80 && c <= 0x9FFF) || (c >= 0xAC00 && c <= 0xD7A3)){
return 0;
}
for(int i=0; i<7; i++){
if (c >= ext_code_plane_cjk[i*2] && c <= ext_code_plane_cjk[i*2+1]){
#ifdef WPARSER_TRACE
fprintf(stderr, "%x is extended CJK [%x, %x]?", c, ext_code_plane_cjk[i*2], ext_code_plane_cjk[i*2+1]); fprintf(stderr, " = true\n");
#endif
return 0;
}
}
}
return 1;
}
static int
p_isCJK(TParser *prs){
/*
* pg_dsplen could return -1 which means error or control character
*/
if (pg_dsplen(prs->str + prs->state->posbyte) == 0)
return 0;
if (GetDatabaseEncoding() == PG_UTF8 && prs->usewide) {
//p_isCJKchar only works in UTF8 encoding
pg_wchar c;
if (prs->pgwstr)
c = *(prs->pgwstr + prs->state->poschar);
else
c = (pg_wchar) *(prs->wstr + prs->state->poschar);
if ((c >= 0x2E80 && c <= 0x9FFF) || (c >= 0xAC00 && c <= 0xD7A3)){
#ifdef WPARSER_TRACE
fprintf(stderr, "%x isCJK?", c); fprintf(stderr, " = true\n");
#endif
return 1;
}
for(int i=0; i<7; i++){
if (c >= ext_code_plane_cjk[i*2] && c <= ext_code_plane_cjk[i*2+1]){
#ifdef WPARSER_TRACE
fprintf(stderr, "%x is extended CJK [%x, %x]?", c, ext_code_plane_cjk[i*2], ext_code_plane_cjk[i*2+1]); fprintf(stderr, " = true\n");
#endif
return 1;
}
}
}
return 0;
}
static int
p_isCJK2gram(TParser *prs){
/*
* pg_dsplen could return -1 which means error or control character
*/
if (pg_dsplen(prs->str + prs->state->posbyte) == 0)
return 0;
if (GetDatabaseEncoding() == PG_UTF8 && prs->usewide) {
//p_isCJKchar only works in UTF8 encoding
pg_wchar c;
if (prs->pgwstr)
c = *(prs->pgwstr + prs->state->poschar);
else
c = (pg_wchar) *(prs->wstr + prs->state->poschar);
if ((c >= 0x3040 && c <= 0x9FFF) || (c >= 0xAC00 && c <= 0xD7A3)){
//CJK Unified Ideographs
//a 2-gram token
return 1;
}
}
return 0;
}
static unsigned int
utf8_cjkCodePoint(char * s){
unsigned int c = *s;
unsigned int a, b;
if(((c ^ 0xE0) & 0xF0) == 0){
a = ((s[0] & 0xF)<<4) | ((s[1]>>2) & 0xF);
b = ((s[1] & 0x3)<<6) | (s[2] & 0x3f);
c = ((a<<8) | b);
return c;
}
if(((c ^ 0xF0) & 0xF8) == 0){
a = ((s[0] & 0x7)<<6) | ((s[1]) & 0x3F);
b = ((s[2] & 0x3F)<<6) | (s[3] & 0x3F);
c = ((a<<12) | b);
}
return 0;
}
static void
utf8_setCjkCodePoint(char * s, unsigned int codePoint){
if((codePoint >= 0x2E80 && codePoint <= 0x9FFF) || (codePoint >= 0xAC00 && codePoint <= 0xD7A3)){
s[0] = 0xE0 | (codePoint>>12);
s[1] = 0x80 | ((codePoint>>6) & 0x3F);
s[2] = 0x80 | (codePoint & 0x3F);
return;
}
for(int i=0; i<7; i++){
if (codePoint >= ext_code_plane_cjk[i*2] && codePoint <= ext_code_plane_cjk[i*2+1]){
//four bytes
s[0] = 0xF0 | ((codePoint>>18) & 0x0F);
s[1] = 0x80 | ((codePoint>>12) & 0x3F);
s[2] = 0x80 | ((codePoint>>6) & 0x3F);
s[3] = 0x80 | (codePoint & 0x3F);
return;
}
}
}
static int
p_isCJK2gram_twice(TParser *prs){
pg_wchar c;
pg_wchar a, b;
/*
* pg_dsplen could return -1 which means error or control character
*/
if (pg_dsplen(prs->str + prs->state->posbyte) == 0)
return 0;
if (GetDatabaseEncoding() == PG_UTF8 && prs->usewide) {
if(prs->state->posbyte > prs->lenstr){
return 0;
}
//p_isCJKchar only works in UTF8 encoding
if(((prs->token[0] ^ 0xE0) & 0xF0) == 0 ){
//utf8 3 bytes per character, 1110
a = ((prs->token[0] & 0xF)<<4) | ((prs->token[1]>>2) & 0xF);
b = ((prs->token[1] & 0x3)<<6) | (prs->token[2] & 0x3f);
c = ((a<<8) | b);
}
else{
//non CJK or extended CJK
return 0;
}
if ((c >= 0x3040 && c <= 0x9FFF) || (c >= 0xAC00 && c <= 0xD7A3)){
//CJK Unified Ideographs
//token as if it is a 2-gram
pg_wchar nc;
if (prs->pgwstr)
nc = *(prs->pgwstr + prs->state->poschar);
else
nc = (pg_wchar) *(prs->wstr + prs->state->poschar);
if ((nc >= 0x3040 && nc <= 0x9FFF) || (nc >= 0xAC00 && nc <= 0xD7A3)){
#ifdef WPARSER_TRACE
fprintf(stderr, " %x %x is 2-gram state=", c, nc);
fprintf(stderr, "%d \n", prs->state->state);
#endif
return 1;
}
#ifdef WPARSER_TRACE
fprintf(stderr, " %x %x is not 2-gram state=", c, nc);
fprintf(stderr, "%d \n", prs->state->state);
#endif
return 0;
}
else if (c >= 0x2E80 && c < 0x3040){
//other CJK,
//one character per token
#ifdef WPARSER_TRACE
fprintf(stderr, " %x is unigram state=", c);
fprintf(stderr, "%d \n", prs->state->state);
#endif
return 0;
}
//if control reaches here, that means either non-CJK or extended 4-byte CJK
}
return 0;
}
static pg_wchar
p_prevChar(TParser *prs){
pg_wchar c = 0;
if(prs->state->poschar < 2)return c;
if (prs->pgwstr)
c = *(prs->pgwstr + prs->state->poschar - 2);
else
c = (pg_wchar) *(prs->wstr + prs->state->poschar - 2);
return c;
}
static pg_wchar
p_nextChar(TParser *prs){
pg_wchar c = 0;
if(prs->state->posbyte >= prs->lenstr){
return c;
}
if (prs->pgwstr)
c = *(prs->pgwstr + prs->state->poschar);
else
c = (pg_wchar) *(prs->wstr + prs->state->poschar);
return c;
}
static int
p_isCJKunigram(TParser *prs){
int a, b;
pg_wchar c;
#ifdef WPARSER_TRACE
fprintf(stderr, "p_isCJKunigram: enter\n");
#endif
if (GetDatabaseEncoding() == PG_UTF8 && prs->usewide) {
//p_isCJKchar only works in UTF8 encoding
if(((prs->token[0] ^ 0xF0) & 0xF8) == 0){
//could be a 4-byte CJK
a = ((prs->token[0] & 0x7)<<6) | ((prs->token[1]) & 0x3F);
b = ((prs->token[2] & 0x3F)<<6) | (prs->token[3] & 0x3F);
c = ((a<<12) | b);
#ifdef WPARSER_TRACE
fprintf(stderr, "p_isCJKunigram: current char is a 4-byte char %x\n", c);
#endif
for(int i=0; i<7; i++){
if (c >= ext_code_plane_cjk[i*2] && c <= ext_code_plane_cjk[i*2+1]){
return 1;
}
}
}
if(((prs->token[0] ^ 0xE0) & 0xF0) != 0){
#ifdef WPARSER_TRACE
fprintf(stderr, "p_isCJKunigram: current char is not 3-byte CJK\n");
#endif
return 0;//not CJK
}
a = ((prs->token[0] & 0xF)<<4) | ((prs->token[1]>>2) & 0xF);
b = ((prs->token[1] & 0x3)<<6) | (prs->token[2] & 0x3f);
c = ((a<<8) | b);
#ifdef WPARSER_TRACE
fprintf(stderr, "p_isCJKunigram: current char = %x\n", c);
#endif
if ((c >= 0x3040 && c <= 0x9FFF) || (c >= 0xAC00 && c <= 0xD7A3)){
//CJK Unified Ideographs
//if it is surrounded by non-CJK chars or CJK unigrams,
//it is also unigram
//1. check whether previous char is CJK 3000 to 9FFF
//2. check whether next char is CJ3000 to 9FFF
//next char
c = p_nextChar(prs);
#ifdef WPARSER_TRACE
fprintf(stderr, "p_isCJKunigram: next char = %x\n", c);
#endif
if( !((c >= 0x3040 && c <= 0x9FFF) || (c >= 0xAC00 && c <= 0xD7A3)) ){
c = p_prevChar(prs);
#ifdef WPARSER_TRACE
fprintf(stderr, "p_isCJKunigram: prev char = %x\n", c);
#endif
if( !((c >= 0x3040 && c <= 0x9FFF) || (c >= 0xAC00 && c <= 0xD7A3)) )return 1;
}
return 0;
}
else if (c >= 0x2E80 && c < 0x3040){
//other CJK,
//one character per token
#ifdef WPARSER_TRACE
fprintf(stderr, "p_isCJKunigram: unigram detected\n");
#endif
return 1;
}
}
#ifdef WPARSER_TRACE
fprintf(stderr, "p_isCJKunigram: exit database not PG_UTF8\n");
#endif
return 0;
}
/* deliberately suppress unused-function complaints for the above */
void _make_compiler_happy(void);
void
_make_compiler_happy(void)
{
p_isalnum(NULL);
p_isnotalnum(NULL);
p_isalpha(NULL);
p_isnotalpha(NULL);
p_isdigit(NULL);
p_isnotdigit(NULL);
p_islower(NULL);
p_isnotlower(NULL);
p_isprint(NULL);
p_isnotprint(NULL);
p_ispunct(NULL);
p_isnotpunct(NULL);
p_isspace(NULL);
p_isnotspace(NULL);
p_isupper(NULL);
p_isnotupper(NULL);
p_isxdigit(NULL);
p_isnotxdigit(NULL);
p_isEOF(NULL);
p_iseqC(NULL);
p_isneC(NULL);
p_isCJK2gram(NULL);
p_isCJK2gram_twice(NULL);
p_isCJKunigram(NULL);
p_isCJK(NULL);
p_isnotCJK(NULL);
}
static void
SpecialTags(TParser *prs)
{
switch (prs->state->lenchartoken)
{
case 8: /* </script */
if (pg_strncasecmp(prs->token, "</script", 8) == 0)
prs->ignore = false;
break;
case 7: /* <script || </style */
if (pg_strncasecmp(prs->token, "</style", 7) == 0)
prs->ignore = false;
else if (pg_strncasecmp(prs->token, "<script", 7) == 0)
prs->ignore = true;
break;
case 6: /* <style */
if (pg_strncasecmp(prs->token, "<style", 6) == 0)
prs->ignore = true;
break;
default:
break;
}
}
static void
SpecialFURL(TParser *prs)
{
prs->wanthost = true;
prs->state->posbyte -= prs->state->lenbytetoken;
prs->state->poschar -= prs->state->lenchartoken;
}
static void
SpecialHyphen(TParser *prs)
{
prs->state->posbyte -= prs->state->lenbytetoken;
prs->state->poschar -= prs->state->lenchartoken;
}
static void
SpecialVerVersion(TParser *prs)
{
prs->state->posbyte -= prs->state->lenbytetoken;
prs->state->poschar -= prs->state->lenchartoken;
prs->state->lenbytetoken = 0;
prs->state->lenchartoken = 0;
}
static int
p_isstophost(TParser *prs)
{
if (prs->wanthost)
{