| 1 |
|
| #define PERL_NO_GET_CONTEXT |
| 2 |
|
| #define NO_XSLOCKS |
| 3 |
|
| #include "EXTERN.h" |
| 4 |
|
| #include "perl.h" |
| 5 |
|
| #include "XSUB.h" |
| 6 |
|
| |
| 7 |
|
| #include "damerau-int.c" |
| 8 |
|
| |
| 9 |
|
| #define MAX(a,b) (((a)>(b))?(a):(b)) |
| 10 |
|
| |
| 11 |
|
| /* use the system malloc and free */ |
| 12 |
|
| #undef malloc |
| 13 |
|
| #undef free |
| 14 |
|
| |
| 15 |
|
| MODULE = Text::Levenshtein::Damerau::XS PACKAGE = Text::Levenshtein::Damerau::XS |
| 16 |
|
| |
| 17 |
|
| PROTOTYPES: ENABLE |
| 18 |
|
| |
| 19 |
|
| int |
| 20 |
|
| cxs_edistance (arraySource, arrayTarget, maxDistance) |
| 21 |
|
| AV * arraySource |
| 22 |
|
| AV * arrayTarget |
| 23 |
|
| SV * maxDistance |
| 24 |
|
| PPCODE: |
| 25 |
|
| { |
| 26 |
24 |
▶ | dXSTARG; |
Branch: 1 branch
| Branch | True | False | Total |
| dXSTARG; |
24 |
0 |
24 |
|
| 27 |
24 |
| PUSHs(TARG); |
| 28 |
24 |
| PUTBACK; |
| 29 |
|
| { |
| 30 |
|
| unsigned int i; |
| 31 |
24 |
| unsigned int lenSource = av_len(arraySource)+1; |
| 32 |
24 |
| unsigned int lenTarget = av_len(arrayTarget)+1; |
| 33 |
|
| int retval; |
| 34 |
|
| |
| 35 |
24 |
▶ | if(lenSource > 0 && lenTarget > 0) { |
Branch: 2 branches
| Branch | True | False | Total |
| if(lenSource > 0 && lenTarget > 0) { |
22 |
2 |
24 |
| if(lenSource > 0 && lenTarget > 0) { |
21 |
1 |
22 |
|
| 36 |
|
| int matchBool; |
| 37 |
21 |
| unsigned int srctgt_max = MAX(lenSource,lenTarget); |
| 38 |
21 |
▶ | if(lenSource != lenTarget) |
Branch: 1 branch
| Branch | True | False | Total |
| if(lenSource != lenTarget) |
13 |
8 |
21 |
|
| 39 |
13 |
| matchBool = 0; |
| 40 |
8 |
| else matchBool = 1; |
| 41 |
|
| |
| 42 |
|
| { |
| 43 |
|
| /* Convert Perl array to C array */ |
| 44 |
21 |
| unsigned int * arrTarget = malloc(sizeof(int) * lenTarget ); |
| 45 |
21 |
| unsigned int * arrSource = malloc(sizeof(int) * lenSource ); |
| 46 |
|
| |
| 47 |
106 |
▶ | for (i=0; i < srctgt_max; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i=0; i < srctgt_max; i++) { |
85 |
21 |
106 |
|
| 48 |
85 |
▶ | if(i < lenSource) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(i < lenSource) { |
78 |
7 |
85 |
|
| 49 |
78 |
| SV* elem = sv_2mortal(av_shift(arraySource)); |
| 50 |
78 |
| arrSource[ i ] = (int)SvIV((SV *)elem); |
| 51 |
|
| } |
| 52 |
85 |
▶ | if(i < lenTarget) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(i < lenTarget) { |
76 |
9 |
85 |
|
| 53 |
76 |
| SV* elem2 = sv_2mortal(av_shift(arrayTarget)); |
| 54 |
76 |
| arrTarget[ i ] = (int)SvIV((SV *)elem2); |
| 55 |
|
| |
| 56 |
|
| /* checks for match */ |
| 57 |
76 |
▶ | if(matchBool && i < lenSource) |
Branch: 2 branches
| Branch | True | False | Total |
| if(matchBool && i < lenSource) |
19 |
57 |
76 |
| if(matchBool && i < lenSource) |
19 |
0 |
19 |
|
| 58 |
19 |
▶ | if(arrSource[i] != arrTarget[i]) |
Branch: 1 branch
| Branch | True | False | Total |
| if(arrSource[i] != arrTarget[i]) |
6 |
13 |
19 |
|
| 59 |
6 |
| matchBool = 0; |
| 60 |
|
| } |
| 61 |
|
| } |
| 62 |
|
| |
| 63 |
21 |
▶ | if(matchBool == 1) |
Branch: 1 branch
| Branch | True | False | Total |
| if(matchBool == 1) |
2 |
19 |
21 |
|
| 64 |
2 |
| retval = 0; |
| 65 |
|
| else |
| 66 |
19 |
| retval = distance(arrSource,arrTarget,lenSource,lenTarget,SvIV(maxDistance)); |
| 67 |
|
| |
| 68 |
21 |
| free(arrSource); |
| 69 |
21 |
| free(arrTarget); |
| 70 |
|
| } |
| 71 |
|
| } |
| 72 |
|
| else { |
| 73 |
|
| /* handle a blank string */ |
| 74 |
3 |
| retval = (lenSource>lenTarget)?lenSource:lenTarget; |
| 75 |
|
| } |
| 76 |
|
| |
| 77 |
24 |
| sv_setiv_mg(TARG, retval); |
| 78 |
24 |
| return; /*we did a PUTBACK earlier, do not let xsubpp's PUTBACK run */ |
| 79 |
|
| } |
| 80 |
|
| } |