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
|
#include "EXTERN.h"
#include "perl.h"
#include "XSUB.h"
/* These 5 files are prepared by mkheader */
#include "unfcmb.h"
#include "unfcan.h"
#include "unfcpt.h"
#include "unfcmp.h"
#include "unfexc.h"
/* Perl 5.6.1 ? */
#ifndef uvuni_to_utf8
#define uvuni_to_utf8 uv_to_utf8
#endif /* uvuni_to_utf8 */
/* Perl 5.6.1 ? */
#ifndef utf8n_to_uvuni
#define utf8n_to_uvuni utf8_to_uv
#endif /* utf8n_to_uvuni */
/* UTF8_ALLOW_BOM is used before Perl 5.8.0 */
#ifdef UTF8_ALLOW_BOM
#define AllowAnyUTF (UTF8_ALLOW_SURROGATE|UTF8_ALLOW_BOM|UTF8_ALLOW_FFFF)
#else
#define AllowAnyUTF (UTF8_ALLOW_SURROGATE|UTF8_ALLOW_FFFF)
#endif
/* if utf8n_to_uvuni() sets retlen to 0 (?) */
#define ErrRetlenIsZero "panic (Unicode::Normalize): zero-length character"
/* utf8_hop() hops back before start. Maybe broken UTF-8 */
#define ErrHopBeforeStart "panic (Unicode::Normalize): hopping before start"
/* At present, char > 0x10ffff are unaffected without complaint, right? */
#define VALID_UTF_MAX (0x10ffff)
#define OVER_UTF_MAX(uv) (VALID_UTF_MAX < (uv))
/* HANGUL_H */
#define Hangul_SBase 0xAC00
#define Hangul_SFinal 0xD7A3
#define Hangul_SCount 11172
#define Hangul_NCount 588
#define Hangul_LBase 0x1100
#define Hangul_LFinal 0x1112
#define Hangul_LCount 19
#define Hangul_VBase 0x1161
#define Hangul_VFinal 0x1175
#define Hangul_VCount 21
#define Hangul_TBase 0x11A7
#define Hangul_TFinal 0x11C2
#define Hangul_TCount 28
#define Hangul_IsS(u) ((Hangul_SBase <= (u)) && ((u) <= Hangul_SFinal))
#define Hangul_IsN(u) (((u) - Hangul_SBase) % Hangul_TCount == 0)
#define Hangul_IsLV(u) (Hangul_IsS(u) && Hangul_IsN(u))
#define Hangul_IsL(u) ((Hangul_LBase <= (u)) && ((u) <= Hangul_LFinal))
#define Hangul_IsV(u) ((Hangul_VBase <= (u)) && ((u) <= Hangul_VFinal))
#define Hangul_IsT(u) ((Hangul_TBase < (u)) && ((u) <= Hangul_TFinal))
/* HANGUL_H */
/* this is used for canonical ordering of combining characters (c.c.). */
typedef struct {
U8 cc; /* combining class */
UV uv; /* codepoint */
STRLEN pos; /* position */
} UNF_cc;
static int compare_cc (const void *a, const void *b)
{
int ret_cc;
ret_cc = ((UNF_cc*) a)->cc - ((UNF_cc*) b)->cc;
if (ret_cc)
return ret_cc;
return ( ((UNF_cc*) a)->pos > ((UNF_cc*) b)->pos )
- ( ((UNF_cc*) a)->pos < ((UNF_cc*) b)->pos );
}
static U8* dec_canonical (UV uv)
{
U8 ***plane, **row;
if (OVER_UTF_MAX(uv))
return NULL;
plane = (U8***)UNF_canon[uv >> 16];
if (! plane)
return NULL;
row = plane[(uv >> 8) & 0xff];
return row ? row[uv & 0xff] : NULL;
}
static U8* dec_compat (UV uv)
{
U8 ***plane, **row;
if (OVER_UTF_MAX(uv))
return NULL;
plane = (U8***)UNF_compat[uv >> 16];
if (! plane)
return NULL;
row = plane[(uv >> 8) & 0xff];
return row ? row[uv & 0xff] : NULL;
}
static UV composite_uv (UV uv, UV uv2)
{
UNF_complist ***plane, **row, *cell, *i;
if (! uv2 || OVER_UTF_MAX(uv) || OVER_UTF_MAX(uv2))
return 0;
if (Hangul_IsL(uv) && Hangul_IsV(uv2)) {
uv -= Hangul_LBase; /* lindex */
uv2 -= Hangul_VBase; /* vindex */
return(Hangul_SBase + (uv * Hangul_VCount + uv2) * Hangul_TCount);
}
if (Hangul_IsLV(uv) && Hangul_IsT(uv2)) {
uv2 -= Hangul_TBase; /* tindex */
return(uv + uv2);
}
plane = UNF_compos[uv >> 16];
if (! plane)
return 0;
row = plane[(uv >> 8) & 0xff];
if (! row)
return 0;
cell = row[uv & 0xff];
if (! cell)
return 0;
for (i = cell; i->nextchar; i++) {
if (uv2 == i->nextchar)
return i->composite;
}
return 0;
}
static U8 getCombinClass (UV uv)
{
U8 **plane, *row;
if (OVER_UTF_MAX(uv))
return 0;
plane = (U8**)UNF_combin[uv >> 16];
if (! plane)
return 0;
row = plane[(uv >> 8) & 0xff];
return row ? row[uv & 0xff] : 0;
}
static void sv_cat_decompHangul (SV* sv, UV uv)
{
UV sindex, lindex, vindex, tindex;
U8 *t, tmp[3 * UTF8_MAXLEN + 1];
if (! Hangul_IsS(uv))
return;
sindex = uv - Hangul_SBase;
lindex = sindex / Hangul_NCount;
vindex = (sindex % Hangul_NCount) / Hangul_TCount;
tindex = sindex % Hangul_TCount;
t = tmp;
t = uvuni_to_utf8(t, (lindex + Hangul_LBase));
t = uvuni_to_utf8(t, (vindex + Hangul_VBase));
if (tindex)
t = uvuni_to_utf8(t, (tindex + Hangul_TBase));
*t = '\0';
sv_catpvn(sv, (char *)tmp, t - tmp);
}
static void sv_cat_uvuni (SV* sv, UV uv)
{
U8 *t, tmp[UTF8_MAXLEN + 1];
t = tmp;
t = uvuni_to_utf8(t, uv);
*t = '\0';
sv_catpvn(sv, (char *)tmp, t - tmp);
}
static char * sv_2pvunicode(SV *sv, STRLEN *lp)
{
char *s;
STRLEN len;
s = (char*)SvPV(sv,len);
if (!SvUTF8(sv)) {
SV* tmpsv = sv_mortalcopy(sv);
if (!SvPOK(tmpsv))
(void)sv_pvn_force(tmpsv,&len);
sv_utf8_upgrade(tmpsv);
s = (char*)SvPV(tmpsv,len);
}
*lp = len;
return s;
}
MODULE = Unicode::Normalize PACKAGE = Unicode::Normalize
SV*
decompose(src, compat = &PL_sv_no)
SV * src
SV * compat
PROTOTYPE: $;$
PREINIT:
SV *dst;
STRLEN srclen, retlen;
U8 *s, *e, *p, *r;
UV uv;
bool iscompat;
CODE:
iscompat = SvTRUE(compat);
s = (U8*)sv_2pvunicode(src,&srclen);
e = s + srclen;
dst = newSV(1);
(void)SvPOK_only(dst);
SvUTF8_on(dst);
for (p = s; p < e; p += retlen) {
uv = utf8n_to_uvuni(p, e - p, &retlen, AllowAnyUTF);
if (!retlen)
croak(ErrRetlenIsZero);
if (Hangul_IsS(uv))
sv_cat_decompHangul(dst, uv);
else {
r = iscompat ? dec_compat(uv) : dec_canonical(uv);
if (r)
sv_catpv(dst, (char *)r);
else
sv_cat_uvuni(dst, uv);
}
}
RETVAL = dst;
OUTPUT:
RETVAL
SV*
reorder(src)
SV * src
PROTOTYPE: $
PREINIT:
SV *dst;
STRLEN srclen, dstlen, retlen, stk_cc_max;
U8 *s, *e, *p, *d, curCC;
UV uv, uvlast;
UNF_cc * stk_cc;
STRLEN i, cc_pos;
bool valid_uvlast;
CODE:
s = (U8*)sv_2pvunicode(src,&srclen);
e = s + srclen;
dstlen = srclen + 1;
dst = newSV(dstlen);
(void)SvPOK_only(dst);
SvUTF8_on(dst);
d = (U8*)SvPVX(dst);
stk_cc_max = 10; /* enough as an initial value? */
New(0, stk_cc, stk_cc_max, UNF_cc);
for (p = s; p < e;) {
uv = utf8n_to_uvuni(p, e - p, &retlen, AllowAnyUTF);
if (!retlen)
croak(ErrRetlenIsZero);
p += retlen;
curCC = getCombinClass(uv);
if (curCC == 0) {
d = uvuni_to_utf8(d, uv);
continue;
}
cc_pos = 0;
stk_cc[cc_pos].cc = curCC;
stk_cc[cc_pos].uv = uv;
stk_cc[cc_pos].pos = cc_pos;
valid_uvlast = FALSE;
while (p < e) {
uv = utf8n_to_uvuni(p, e - p, &retlen, AllowAnyUTF);
if (!retlen)
croak(ErrRetlenIsZero);
p += retlen;
curCC = getCombinClass(uv);
if (curCC == 0) {
uvlast = uv;
valid_uvlast = TRUE;
break;
}
cc_pos++;
if (stk_cc_max <= cc_pos) { /* extend if need */
stk_cc_max = cc_pos + 1;
Renew(stk_cc, stk_cc_max, UNF_cc);
}
stk_cc[cc_pos].cc = curCC;
stk_cc[cc_pos].uv = uv;
stk_cc[cc_pos].pos = cc_pos;
}
/* reordered if there are two c.c.'s */
if (cc_pos) {
qsort((void*)stk_cc, cc_pos + 1, sizeof(UNF_cc), compare_cc);
}
for (i = 0; i <= cc_pos; i++) {
d = uvuni_to_utf8(d, stk_cc[i].uv);
}
if (valid_uvlast)
{
d = uvuni_to_utf8(d, uvlast);
}
}
*d = '\0';
SvCUR_set(dst, d - (U8*)SvPVX(dst));
Safefree(stk_cc);
RETVAL = dst;
OUTPUT:
RETVAL
SV*
compose(src)
SV * src
PROTOTYPE: $
ALIAS:
composeContiguous = 1
PREINIT:
SV *dst, *tmp;
U8 *s, *p, *e, *d, *t, *tmp_start, curCC, preCC;
UV uv, uvS, uvComp;
STRLEN srclen, dstlen, tmplen, retlen;
bool beginning = TRUE;
CODE:
s = (U8*)sv_2pvunicode(src,&srclen);
e = s + srclen;
dstlen = srclen + 1;
dst = newSV(dstlen);
(void)SvPOK_only(dst);
SvUTF8_on(dst);
d = (U8*)SvPVX(dst);
/* for uncomposed combining char */
tmp = sv_2mortal(newSV(dstlen));
(void)SvPOK_only(tmp);
SvUTF8_on(tmp);
for (p = s; p < e;) {
if (beginning) {
uvS = utf8n_to_uvuni(p, e - p, &retlen, AllowAnyUTF);
if (!retlen)
croak(ErrRetlenIsZero);
p += retlen;
if (getCombinClass(uvS)) { /* no Starter found yet */
d = uvuni_to_utf8(d, uvS);
continue;
}
beginning = FALSE;
}
/* Starter */
t = tmp_start = (U8*)SvPVX(tmp);
preCC = 0;
/* to the next Starter */
while (p < e) {
uv = utf8n_to_uvuni(p, e - p, &retlen, AllowAnyUTF);
if (!retlen)
croak(ErrRetlenIsZero);
p += retlen;
curCC = getCombinClass(uv);
if (preCC && preCC == curCC) {
preCC = curCC;
t = uvuni_to_utf8(t, uv);
} else {
uvComp = composite_uv(uvS, uv);
if (uvComp && ! isExclusion(uvComp) &&
(ix ? (t == tmp_start) : (preCC <= curCC))) {
STRLEN leftcur, rightcur, dstcur;
leftcur = UNISKIP(uvComp);
rightcur = UNISKIP(uvS) + UNISKIP(uv);
if (leftcur > rightcur) {
dstcur = d - (U8*)SvPVX(dst);
dstlen += leftcur - rightcur;
d = (U8*)SvGROW(dst,dstlen) + dstcur;
}
/* preCC not changed to curCC */
uvS = uvComp;
} else if (! curCC && p < e) { /* blocked */
break;
} else {
preCC = curCC;
t = uvuni_to_utf8(t, uv);
}
}
}
d = uvuni_to_utf8(d, uvS); /* starter (composed or not) */
tmplen = t - tmp_start;
if (tmplen) { /* uncomposed combining char */
t = (U8*)SvPVX(tmp);
while (tmplen--)
*d++ = *t++;
}
uvS = uv;
} /* for */
*d = '\0';
SvCUR_set(dst, d - (U8*)SvPVX(dst));
RETVAL = dst;
OUTPUT:
RETVAL
void
checkNFD(src)
SV * src
PROTOTYPE: $
ALIAS:
checkNFKD = 1
PREINIT:
STRLEN srclen, retlen;
U8 *s, *e, *p, curCC, preCC;
UV uv;
CODE:
s = (U8*)sv_2pvunicode(src,&srclen);
e = s + srclen;
preCC = 0;
for (p = s; p < e; p += retlen) {
uv = utf8n_to_uvuni(p, e - p, &retlen, AllowAnyUTF);
if (!retlen)
croak(ErrRetlenIsZero);
curCC = getCombinClass(uv);
if (preCC > curCC && curCC != 0) /* canonical ordering violated */
XSRETURN_NO;
if (Hangul_IsS(uv) || (ix ? dec_compat(uv) : dec_canonical(uv)))
XSRETURN_NO;
preCC = curCC;
}
XSRETURN_YES;
void
checkNFC(src)
SV * src
PROTOTYPE: $
ALIAS:
checkNFKC = 1
PREINIT:
STRLEN srclen, retlen;
U8 *s, *e, *p, curCC, preCC;
UV uv;
bool isMAYBE;
CODE:
s = (U8*)sv_2pvunicode(src,&srclen);
e = s + srclen;
preCC = 0;
isMAYBE = FALSE;
for (p = s; p < e; p += retlen) {
uv = utf8n_to_uvuni(p, e - p, &retlen, AllowAnyUTF);
if (!retlen)
croak(ErrRetlenIsZero);
curCC = getCombinClass(uv);
if (preCC > curCC && curCC != 0) /* canonical ordering violated */
XSRETURN_NO;
/* get NFC/NFKC property */
if (Hangul_IsS(uv)) /* Hangul syllables are canonical composites */
; /* YES */
else if (isExclusion(uv) || isSingleton(uv) || isNonStDecomp(uv))
XSRETURN_NO;
else if (isComp2nd(uv))
isMAYBE = TRUE;
else if (ix) {
char *canon, *compat;
/* NFKC_NO when having compatibility mapping. */
canon = (char *) dec_canonical(uv);
compat = (char *) dec_compat(uv);
if (compat && !(canon && strEQ(canon, compat)))
XSRETURN_NO;
} /* end of get NFC/NFKC property */
preCC = curCC;
}
if (isMAYBE)
XSRETURN_UNDEF;
else
XSRETURN_YES;
void
checkFCD(src)
SV * src
PROTOTYPE: $
ALIAS:
checkFCC = 1
PREINIT:
STRLEN srclen, retlen, canlen, canret;
U8 *s, *e, *p, curCC, preCC;
UV uv, uvLead, uvTrail;
U8 *sCan, *pCan, *eCan;
bool isMAYBE;
CODE:
s = (U8*)sv_2pvunicode(src,&srclen);
e = s + srclen;
preCC = 0;
isMAYBE = FALSE;
for (p = s; p < e; p += retlen) {
uv = utf8n_to_uvuni(p, e - p, &retlen, AllowAnyUTF);
if (!retlen)
croak(ErrRetlenIsZero);
sCan = (U8*) dec_canonical(uv);
if (sCan) {
canlen = (STRLEN)strlen((char *) sCan);
uvLead = utf8n_to_uvuni(sCan, canlen, &canret, AllowAnyUTF);
}
else {
uvLead = uv;
}
curCC = getCombinClass(uvLead);
if (curCC != 0 && curCC < preCC) /* canonical ordering violated */
XSRETURN_NO;
if (ix) {
if (isExclusion(uv) || isSingleton(uv) || isNonStDecomp(uv))
XSRETURN_NO;
else if (isComp2nd(uv))
isMAYBE = TRUE;
}
if (sCan) {
eCan = sCan + canlen;
pCan = utf8_hop(eCan, -1);
if (pCan < sCan)
croak(ErrHopBeforeStart);
uvTrail = utf8n_to_uvuni(pCan, eCan - pCan, &canret, AllowAnyUTF);
preCC = getCombinClass(uvTrail);
}
else {
preCC = curCC;
}
}
if (isMAYBE)
XSRETURN_UNDEF;
else
XSRETURN_YES;
U8
getCombinClass(uv)
UV uv
PROTOTYPE: $
bool
isExclusion(uv)
UV uv
PROTOTYPE: $
bool
isSingleton(uv)
UV uv
PROTOTYPE: $
bool
isNonStDecomp(uv)
UV uv
PROTOTYPE: $
bool
isComp2nd(uv)
UV uv
PROTOTYPE: $
ALIAS:
isNFC_MAYBE = 1
isNFKC_MAYBE = 2
void
isNFD_NO(uv)
UV uv
PROTOTYPE: $
ALIAS:
isNFKD_NO = 1
CODE:
if (Hangul_IsS(uv) || (ix ? dec_compat(uv) : dec_canonical(uv)))
XSRETURN_YES; /* NFD_NO or NFKD_NO */
else
XSRETURN_NO;
void
isComp_Ex(uv)
UV uv
PROTOTYPE: $
ALIAS:
isNFC_NO = 0
isNFKC_NO = 1
CODE:
if (isExclusion(uv) || isSingleton(uv) || isNonStDecomp(uv))
XSRETURN_YES; /* NFC_NO or NFKC_NO */
else if (ix) {
char *canon, *compat;
canon = (char *) dec_canonical(uv);
compat = (char *) dec_compat(uv);
if (compat && (!canon || strNE(canon, compat)))
XSRETURN_YES; /* NFC_NO or NFKC_NO */
else
XSRETURN_NO;
}
else
XSRETURN_NO;
SV*
getComposite(uv, uv2)
UV uv
UV uv2
PROTOTYPE: $$
PREINIT:
UV composite;
CODE:
composite = composite_uv(uv, uv2);
RETVAL = composite ? newSVuv(composite) : &PL_sv_undef;
OUTPUT:
RETVAL
SV*
getCanon(uv)
UV uv
PROTOTYPE: $
ALIAS:
getCompat = 1
PREINIT:
U8 * rstr;
CODE:
if (Hangul_IsS(uv)) {
SV * dst;
dst = newSV(1);
(void)SvPOK_only(dst);
sv_cat_decompHangul(dst, uv);
RETVAL = dst;
} else {
rstr = ix ? dec_compat(uv) : dec_canonical(uv);
if (!rstr)
XSRETURN_UNDEF;
RETVAL = newSVpvn((char *)rstr, strlen((char *)rstr));
}
SvUTF8_on(RETVAL);
OUTPUT:
RETVAL
void
splitOnLastStarter(src)
SV * src
PREINIT:
SV *svp;
STRLEN srclen, retlen;
U8 *s, *e, *p;
UV uv;
PPCODE:
s = (U8*)sv_2pvunicode(src,&srclen);
e = s + srclen;
for (p = e; s < p; ) {
p = utf8_hop(p, -1);
if (p < s)
croak(ErrHopBeforeStart);
uv = utf8n_to_uvuni(p, e - p, &retlen, AllowAnyUTF);
if (getCombinClass(uv) == 0) /* Last Starter found */
break;
}
svp = sv_2mortal(newSVpvn((char*)s, p - s));
SvUTF8_on(svp);
XPUSHs(svp);
svp = sv_2mortal(newSVpvn((char*)p, e - p));
SvUTF8_on(svp);
XPUSHs(svp);
|