| 1 |
|
| #include "EXTERN.h" |
| 2 |
|
| #include "perl.h" |
| 3 |
|
| #include "XSUB.h" |
| 4 |
|
| #include <stdio.h> |
| 5 |
|
| #include <stdlib.h> |
| 6 |
|
| #include <fcntl.h> |
| 7 |
|
| #include "tables.h" |
| 8 |
|
| |
| 9 |
|
| int Rcmp(const void *, const void *); |
| 10 |
|
| |
| 11 |
|
| typedef struct { |
| 12 |
|
| double p; |
| 13 |
|
| uchar *str; |
| 14 |
|
| int prev; |
| 15 |
|
| int skip; |
| 16 |
|
| int pos; |
| 17 |
|
| } Rslot; |
| 18 |
|
| |
| 19 |
0 |
| SV* decode(SV *string) |
| 20 |
|
| { |
| 21 |
|
| uchar *from; |
| 22 |
|
| int i, j, nrus, rslots, flen; |
| 23 |
|
| uchar Rstrstr[NSLOTS*1024]; |
| 24 |
0 |
| Rslot R[NSLOTS], E, *result=&E; |
| 25 |
|
| SV *to; |
| 26 |
|
| |
| 27 |
0 |
▶ | if (! SvOK(string)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (! SvOK(string)) |
0 |
0 |
0 |
|
| 28 |
0 |
| return(&PL_sv_undef); |
| 29 |
|
| |
| 30 |
0 |
| from=SvPV(string, flen); |
| 31 |
|
| /* !!! WARNING !!! |
| 32 |
|
| arbitrary limit here. I'm too lazy to remove it right now, |
| 33 |
|
| may be later... |
| 34 |
|
| */ |
| 35 |
0 |
▶ | if (flen>1023) flen=1023; |
Branch: 1 branch
| Branch | True | False | Total |
| if (flen>1023) flen=1023; |
0 |
0 |
0 |
|
| 36 |
|
| |
| 37 |
0 |
▶ | for (i=0; i<NSLOTS; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (i=0; i<NSLOTS; i++) |
0 |
0 |
0 |
|
| 38 |
0 |
| R[i].str=Rstrstr+i*1024; |
| 39 |
|
| |
| 40 |
0 |
| nrus=rslots=1; |
| 41 |
0 |
| R[0].p=E.p=1; |
| 42 |
0 |
| R[0].prev=E.prev=0; |
| 43 |
0 |
| R[0].skip=1; |
| 44 |
0 |
| R[0].pos=0; |
| 45 |
0 |
| E.str=from; |
| 46 |
0 |
| E.pos=flen; |
| 47 |
|
| |
| 48 |
0 |
▶ | for (i=0; i<flen; i++, from++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (i=0; i<flen; i++, from++) |
0 |
0 |
0 |
|
| 49 |
|
| { |
| 50 |
|
| int cur; |
| 51 |
|
| |
| 52 |
0 |
▶ | if (*from >127) goto done; |
Branch: 1 branch
| Branch | True | False | Total |
| if (*from >127) goto done; |
0 |
0 |
0 |
|
| 53 |
0 |
| cur=ord[*from]; |
| 54 |
0 |
| E.p*=Etrans[E.prev][cur]; |
| 55 |
0 |
| E.prev=cur; |
| 56 |
0 |
▶ | for (j=0; j<nrus; j++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (j=0; j<nrus; j++) |
0 |
0 |
0 |
|
| 57 |
|
| { |
| 58 |
|
| uchar **trs, *s; |
| 59 |
|
| |
| 60 |
0 |
▶ | if (--R[j].skip) |
Branch: 1 branch
| Branch | True | False | Total |
| if (--R[j].skip) |
0 |
0 |
0 |
|
| 61 |
0 |
| continue; |
| 62 |
|
| |
| 63 |
0 |
▶ | for (trs=E2R[E.prev]; s=*trs; trs++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (trs=E2R[E.prev]; s=*trs; trs++) |
0 |
0 |
0 |
|
| 64 |
|
| { |
| 65 |
|
| int x, slot; |
| 66 |
0 |
▶ | for (x=1;s[x] && from[x] && ord[from[x]]==ord[s[x]];x++); |
Branch: 3 branches
| Branch | True | False | Total |
| for (x=1;s[x] && from[x] && ord[from[x]]==ord[s[x]];x++); |
0 |
0 |
0 |
| for (x=1;s[x] && from[x] && ord[from[x]]==ord[s[x]];x++); |
0 |
0 |
0 |
| for (x=1;s[x] && from[x] && ord[from[x]]==ord[s[x]];x++); |
0 |
0 |
0 |
|
| 67 |
0 |
▶ | if (s[x]) |
Branch: 1 branch
| Branch | True | False | Total |
| if (s[x]) |
0 |
0 |
0 |
|
| 68 |
0 |
| continue; |
| 69 |
|
| |
| 70 |
0 |
▶ | if (trs[1]) |
Branch: 1 branch
| Branch | True | False | Total |
| if (trs[1]) |
0 |
0 |
0 |
|
| 71 |
0 |
| memcpy(R[slot=rslots++].str, R[j].str, i); |
| 72 |
|
| else |
| 73 |
0 |
| slot=j; |
| 74 |
|
| |
| 75 |
0 |
| cur=ord[*s]; |
| 76 |
0 |
▶ | R[slot].str[R[j].pos]= (cur ? (*from>'Z' ? *s-32 : *s) : *from); |
Branch: 2 branches
| Branch | True | False | Total |
| R[slot].str[R[j].pos]= (cur ? (*from>'Z' ? *s-32 : *s) : *from); |
0 |
0 |
0 |
| R[slot].str[R[j].pos]= (cur ? (*from>'Z' ? *s-32 : *s) : *from); |
0 |
0 |
0 |
|
| 77 |
0 |
| R[slot].pos=R[j].pos+1; |
| 78 |
0 |
| R[slot].p=R[j].p*Rtrans[R[j].prev][cur]; |
| 79 |
0 |
| R[slot].prev=cur; |
| 80 |
0 |
| R[slot].skip=x; |
| 81 |
|
| } |
| 82 |
|
| } |
| 83 |
0 |
▶ | if (rslots > CLEANUP_THRESHOLD) |
Branch: 1 branch
| Branch | True | False | Total |
| if (rslots > CLEANUP_THRESHOLD) |
0 |
0 |
0 |
|
| 84 |
|
| { |
| 85 |
0 |
| qsort(R, rslots, sizeof(Rslot), Rcmp); |
| 86 |
0 |
| rslots=CLEANUP_THRESHOLD; |
| 87 |
|
| } |
| 88 |
0 |
| nrus=rslots; |
| 89 |
|
| } |
| 90 |
0 |
| qsort(R, rslots, sizeof(Rslot), Rcmp); |
| 91 |
|
| |
| 92 |
0 |
▶ | if (R[0].p > 0 && R[0].p > E.p) |
Branch: 2 branches
| Branch | True | False | Total |
| if (R[0].p > 0 && R[0].p > E.p) |
0 |
0 |
0 |
| if (R[0].p > 0 && R[0].p > E.p) |
0 |
0 |
0 |
|
| 93 |
0 |
| result=R; |
| 94 |
0 |
| done: |
| 95 |
0 |
| to = newSVpv(result->str, result->pos); |
| 96 |
0 |
| } |
| 97 |
|
| |
| 98 |
|
| // sort with decreasing probability |
| 99 |
0 |
| int Rcmp(const void *a, const void *b) |
| 100 |
|
| { |
| 101 |
0 |
▶ | return ((Rslot*)a)->p < ((Rslot*)b)->p ? 1 : |
Branch: 1 branch
| Branch | True | False | Total |
| return ((Rslot*)a)->p < ((Rslot*)b)->p ? 1 : |
0 |
0 |
0 |
|
| 102 |
0 |
▶ | ((Rslot*)a)->p > ((Rslot*)b)->p ? -1 : 0; |
Branch: 1 branch
| Branch | True | False | Total |
| ((Rslot*)a)->p > ((Rslot*)b)->p ? -1 : 0; |
0 |
0 |
0 |
|
| 103 |
|
| } |
| 104 |
|
| |
| 105 |
|
| MODULE = Lingua::RU::Translit PACKAGE = Lingua::RU::Translit |
| 106 |
|
| |
| 107 |
|
| SV* |
| 108 |
|
| translit2koi(input) |
| 109 |
|
| SV *input |
| 110 |
|
| CODE: |
| 111 |
0 |
| RETVAL=decode(input); |
| 112 |
|
| OUTPUT: |
| 113 |
|
| RETVAL |
| 114 |
|
| |