| 1 |
|
| #ifdef __GNUC__ |
| 2 |
|
| #if __GNUC__ >= 5 |
| 3 |
|
| #define IF_HAVE_GCC_5(X) X |
| 4 |
|
| #endif |
| 5 |
|
| |
| 6 |
|
| #if (__GNUC__ == 4 && __GNUC_MINOR__ >= 6) || __GNUC__ >= 5 |
| 7 |
|
| #define PRAGMA_GCC_(X) _Pragma(#X) |
| 8 |
|
| #define PRAGMA_GCC(X) PRAGMA_GCC_(GCC X) |
| 9 |
|
| #endif |
| 10 |
|
| #endif |
| 11 |
|
| |
| 12 |
|
| #ifndef IF_HAVE_GCC_5 |
| 13 |
|
| #define IF_HAVE_GCC_5(X) |
| 14 |
|
| #endif |
| 15 |
|
| |
| 16 |
|
| #ifndef PRAGMA_GCC |
| 17 |
|
| #define PRAGMA_GCC(X) |
| 18 |
|
| #endif |
| 19 |
|
| |
| 20 |
|
| #ifdef DEVEL |
| 21 |
|
| #define WARNINGS_RESET PRAGMA_GCC(diagnostic pop) |
| 22 |
|
| #define WARNINGS_ENABLEW(X) PRAGMA_GCC(diagnostic error #X) |
| 23 |
|
| #define WARNINGS_ENABLE \ |
| 24 |
|
| WARNINGS_ENABLEW(-Wall) \ |
| 25 |
|
| WARNINGS_ENABLEW(-Wextra) \ |
| 26 |
|
| WARNINGS_ENABLEW(-Wundef) \ |
| 27 |
|
| WARNINGS_ENABLEW(-Wshadow) \ |
| 28 |
|
| WARNINGS_ENABLEW(-Wbad-function-cast) \ |
| 29 |
|
| WARNINGS_ENABLEW(-Wcast-align) \ |
| 30 |
|
| WARNINGS_ENABLEW(-Wwrite-strings) \ |
| 31 |
|
| WARNINGS_ENABLEW(-Wstrict-prototypes) \ |
| 32 |
|
| WARNINGS_ENABLEW(-Wmissing-prototypes) \ |
| 33 |
|
| WARNINGS_ENABLEW(-Winline) \ |
| 34 |
|
| WARNINGS_ENABLEW(-Wdisabled-optimization) \ |
| 35 |
|
| IF_HAVE_GCC_5(WARNINGS_ENABLEW(-Wnested-externs)) |
| 36 |
|
| |
| 37 |
|
| #else |
| 38 |
|
| #define WARNINGS_RESET |
| 39 |
|
| #define WARNINGS_ENABLE |
| 40 |
|
| #endif |
| 41 |
|
| |
| 42 |
|
| #define PERL_NO_GET_CONTEXT |
| 43 |
|
| #include "EXTERN.h" |
| 44 |
|
| #include "perl.h" |
| 45 |
|
| #include "XSUB.h" |
| 46 |
|
| |
| 47 |
|
| #include <errno.h> |
| 48 |
|
| #include <inttypes.h> |
| 49 |
|
| #include <stdlib.h> |
| 50 |
|
| #include <string.h> |
| 51 |
|
| |
| 52 |
|
| #ifdef DEVEL |
| 53 |
|
| #undef NDEBUG |
| 54 |
|
| #include <assert.h> |
| 55 |
|
| #endif |
| 56 |
|
| |
| 57 |
|
| WARNINGS_ENABLE |
| 58 |
|
| |
| 59 |
|
| #define X_FN_NAME distance_bytes |
| 60 |
|
| #define X_IS_CONTINUATION(X) 0 |
| 61 |
|
| #define X_LENGTH(P, P_END) ((P_END) - (P)) |
| 62 |
|
| #define X_CHARBYTES(X, Y) 1 |
| 63 |
|
| #define X_CHAR_EQ(PX, NX, PY, NY) (*(PX) == *(PY)) |
| 64 |
|
| #include "xedistance.h" |
| 65 |
|
| |
| 66 |
|
| // work around perl core bug https://github.com/Perl/perl5/issues/25120 |
| 67 |
|
| #undef UTF8_SAFE_SKIP |
| 68 |
|
| |
| 69 |
|
| #ifndef UTF8_SAFE_SKIP |
| 70 |
18 |
| static size_t utf8_skip(pTHX_ const char *p, const char *p_end) { |
| 71 |
|
| assert(p <= p_end); |
| 72 |
18 |
▶ | if (p >= p_end) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (p >= p_end) { |
0 |
18 |
18 |
|
| 73 |
0 |
| return 0; |
| 74 |
|
| } |
| 75 |
18 |
| size_t r = p_end - p, s = UTF8SKIP(p); |
| 76 |
18 |
| return r < s ? r : s; |
| 77 |
|
| } |
| 78 |
|
| #define UTF8_SAFE_SKIP(P, P_END) utf8_skip(aTHX_ P, P_END) |
| 79 |
|
| #endif |
| 80 |
|
| |
| 81 |
|
| #define X_FN_NAME distance_utf8 |
| 82 |
|
| #define X_IS_CONTINUATION(C) UTF8_IS_CONTINUATION(C) |
| 83 |
|
| #define X_LENGTH(P, P_END) utf8_length((const unsigned char *)(P), (const unsigned char *)(P_END)) |
| 84 |
|
| #define X_CHARBYTES(P, P_END) UTF8_SAFE_SKIP(P, P_END) |
| 85 |
|
| #define X_CHAR_EQ(PX, NX, PY, NY) ((NX) == (NY) && memEQ(PX, PY, NX)) |
| 86 |
|
| #include "xedistance.h" |
| 87 |
|
| |
| 88 |
|
| WARNINGS_RESET |
| 89 |
|
| |
| 90 |
|
| MODULE = Text::EditDistance::DamerauLevenshtein PACKAGE = Text::EditDistance::DamerauLevenshtein |
| 91 |
|
| PROTOTYPES: DISABLE |
| 92 |
|
| |
| 93 |
|
| size_t |
| 94 |
|
| dl_distance(SV *from, SV *to) |
| 95 |
|
| CODE: |
| 96 |
|
| size_t from_len; |
| 97 |
22 |
| const char *pv_from = SvPV(from, from_len); |
| 98 |
22 |
▶ | bool from_is_utf8 = !!SvUTF8(from); |
Branch: 1 branch
| Branch | True | False | Total |
| bool from_is_utf8 = !!SvUTF8(from); |
21 |
1 |
22 |
|
| 99 |
|
| size_t to_len; |
| 100 |
22 |
| const char *pv_to = SvPV(to, to_len); |
| 101 |
22 |
| switch (from_is_utf8 << 1 | (from_is_utf8 == !!SvUTF8(to))) { |
| 102 |
14 |
| case 0 << 1 | 1: |
| 103 |
14 |
| RETVAL = distance_bytes(aTHX_ pv_from, pv_from + from_len, pv_to, pv_to + to_len); |
| 104 |
14 |
| break; |
| 105 |
1 |
| case 0 << 1 | 0: |
| 106 |
1 |
| from = newSVpvn_flags(pv_from, from_len, SVs_TEMP); |
| 107 |
1 |
| from_len = sv_utf8_upgrade_nomg(from); |
| 108 |
1 |
| pv_from = SvPVX_const(from); |
| 109 |
|
| if (0) |
| 110 |
|
| case 1 << 1 | 0: { |
| 111 |
0 |
| to = newSVpvn_flags(pv_to, to_len, SVs_TEMP); |
| 112 |
0 |
| to_len = sv_utf8_upgrade_nomg(to); |
| 113 |
0 |
| pv_to = SvPVX_const(to); |
| 114 |
|
| } |
| 115 |
|
| case 1 << 1 | 1: |
| 116 |
8 |
| RETVAL = distance_utf8(aTHX_ pv_from, pv_from + from_len, pv_to, pv_to + to_len); |
| 117 |
8 |
| break; |
| 118 |
|
| } |
| 119 |
22 |
▶ | if (RETVAL == SIZE_MAX) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (RETVAL == SIZE_MAX) { |
0 |
22 |
22 |
|
| 120 |
0 |
| XSRETURN_UNDEF; |
| 121 |
|
| } |
| 122 |
|
| OUTPUT: |
| 123 |
|
| RETVAL |