| 1 |
|
| /* -*- c -*- */ |
| 2 |
|
| |
| 3 |
|
| /* (c) Copyright 1998-2003 by Mark Mielke |
| 4 |
|
| * |
| 5 |
|
| * Freedom to use these sources for whatever you want, as long as credit |
| 6 |
|
| * is given where credit is due, is hereby granted. You may make modifications |
| 7 |
|
| * where you see fit but leave this copyright somewhere visible. As well try |
| 8 |
|
| * to initial any changes you make so that if i like the changes i can |
| 9 |
|
| * incorporate them into any later versions of mine. |
| 10 |
|
| * |
| 11 |
|
| * - Mark Mielke <mark@mielke.cc> |
| 12 |
|
| */ |
| 13 |
|
| |
| 14 |
|
| #include "EXTERN.h" |
| 15 |
|
| #include "perl.h" |
| 16 |
|
| #include "XSUB.h" |
| 17 |
|
| |
| 18 |
|
| #define SOUNDEX_ACCURACY (4) /* The maximum code length... (should be>=2) */ |
| 19 |
|
| |
| 20 |
|
| #if !(PERL_REVISION >= 5 && PERL_VERSION >= 8) |
| 21 |
|
| # define utf8n_to_uvchr utf8_to_uv |
| 22 |
|
| #endif |
| 23 |
|
| |
| 24 |
|
| static char sv_soundex_table[0x100]; |
| 25 |
14 |
| static void sv_soundex_initialize (void) |
| 26 |
|
| { |
| 27 |
14 |
| memset(&sv_soundex_table[0], '\0', sizeof(sv_soundex_table)); |
| 28 |
14 |
| sv_soundex_table['A'] = '0'; |
| 29 |
14 |
| sv_soundex_table['a'] = '0'; |
| 30 |
14 |
| sv_soundex_table['E'] = '0'; |
| 31 |
14 |
| sv_soundex_table['e'] = '0'; |
| 32 |
14 |
| sv_soundex_table['H'] = '0'; |
| 33 |
14 |
| sv_soundex_table['h'] = '0'; |
| 34 |
14 |
| sv_soundex_table['I'] = '0'; |
| 35 |
14 |
| sv_soundex_table['i'] = '0'; |
| 36 |
14 |
| sv_soundex_table['O'] = '0'; |
| 37 |
14 |
| sv_soundex_table['o'] = '0'; |
| 38 |
14 |
| sv_soundex_table['U'] = '0'; |
| 39 |
14 |
| sv_soundex_table['u'] = '0'; |
| 40 |
14 |
| sv_soundex_table['W'] = '0'; |
| 41 |
14 |
| sv_soundex_table['w'] = '0'; |
| 42 |
14 |
| sv_soundex_table['Y'] = '0'; |
| 43 |
14 |
| sv_soundex_table['y'] = '0'; |
| 44 |
14 |
| sv_soundex_table['B'] = '1'; |
| 45 |
14 |
| sv_soundex_table['b'] = '1'; |
| 46 |
14 |
| sv_soundex_table['F'] = '1'; |
| 47 |
14 |
| sv_soundex_table['f'] = '1'; |
| 48 |
14 |
| sv_soundex_table['P'] = '1'; |
| 49 |
14 |
| sv_soundex_table['p'] = '1'; |
| 50 |
14 |
| sv_soundex_table['V'] = '1'; |
| 51 |
14 |
| sv_soundex_table['v'] = '1'; |
| 52 |
14 |
| sv_soundex_table['C'] = '2'; |
| 53 |
14 |
| sv_soundex_table['c'] = '2'; |
| 54 |
14 |
| sv_soundex_table['G'] = '2'; |
| 55 |
14 |
| sv_soundex_table['g'] = '2'; |
| 56 |
14 |
| sv_soundex_table['J'] = '2'; |
| 57 |
14 |
| sv_soundex_table['j'] = '2'; |
| 58 |
14 |
| sv_soundex_table['K'] = '2'; |
| 59 |
14 |
| sv_soundex_table['k'] = '2'; |
| 60 |
14 |
| sv_soundex_table['Q'] = '2'; |
| 61 |
14 |
| sv_soundex_table['q'] = '2'; |
| 62 |
14 |
| sv_soundex_table['S'] = '2'; |
| 63 |
14 |
| sv_soundex_table['s'] = '2'; |
| 64 |
14 |
| sv_soundex_table['X'] = '2'; |
| 65 |
14 |
| sv_soundex_table['x'] = '2'; |
| 66 |
14 |
| sv_soundex_table['Z'] = '2'; |
| 67 |
14 |
| sv_soundex_table['z'] = '2'; |
| 68 |
14 |
| sv_soundex_table['D'] = '3'; |
| 69 |
14 |
| sv_soundex_table['d'] = '3'; |
| 70 |
14 |
| sv_soundex_table['T'] = '3'; |
| 71 |
14 |
| sv_soundex_table['t'] = '3'; |
| 72 |
14 |
| sv_soundex_table['L'] = '4'; |
| 73 |
14 |
| sv_soundex_table['l'] = '4'; |
| 74 |
14 |
| sv_soundex_table['M'] = '5'; |
| 75 |
14 |
| sv_soundex_table['m'] = '5'; |
| 76 |
14 |
| sv_soundex_table['N'] = '5'; |
| 77 |
14 |
| sv_soundex_table['n'] = '5'; |
| 78 |
14 |
| sv_soundex_table['R'] = '6'; |
| 79 |
14 |
| sv_soundex_table['r'] = '6'; |
| 80 |
14 |
| } |
| 81 |
|
| |
| 82 |
21 |
| static SV *sv_soundex (source) |
| 83 |
|
| SV *source; |
| 84 |
|
| { |
| 85 |
|
| char *source_p; |
| 86 |
|
| char *source_end; |
| 87 |
|
| |
| 88 |
|
| { |
| 89 |
|
| STRLEN source_len; |
| 90 |
21 |
| source_p = SvPV(source, source_len); |
| 91 |
21 |
| source_end = &source_p[source_len]; |
| 92 |
|
| } |
| 93 |
|
| |
| 94 |
30 |
▶ | while (source_p != source_end) |
Branch: 1 branch
| Branch | True | False | Total |
| while (source_p != source_end) |
26 |
4 |
30 |
|
| 95 |
|
| { |
| 96 |
26 |
| char codepart_last = sv_soundex_table[(unsigned char) *source_p]; |
| 97 |
|
| |
| 98 |
26 |
▶ | if (codepart_last != '\0') |
Branch: 1 branch
| Branch | True | False | Total |
| if (codepart_last != '\0') |
17 |
9 |
26 |
|
| 99 |
|
| { |
| 100 |
17 |
| SV *code = newSV(SOUNDEX_ACCURACY); |
| 101 |
17 |
| char *code_p = SvPVX(code); |
| 102 |
17 |
| char *code_end = &code_p[SOUNDEX_ACCURACY]; |
| 103 |
|
| |
| 104 |
17 |
| SvCUR_set(code, SOUNDEX_ACCURACY); |
| 105 |
17 |
| SvPOK_only(code); |
| 106 |
|
| |
| 107 |
17 |
| *code_p++ = toupper(*source_p++); |
| 108 |
|
| |
| 109 |
97 |
▶ | while (source_p != source_end && code_p != code_end) |
Branch: 2 branches
| Branch | True | False | Total |
| while (source_p != source_end && code_p != code_end) |
84 |
13 |
97 |
| while (source_p != source_end && code_p != code_end) |
80 |
4 |
84 |
|
| 110 |
|
| { |
| 111 |
80 |
| char c = *source_p++; |
| 112 |
80 |
| char codepart = sv_soundex_table[(unsigned char) c]; |
| 113 |
|
| |
| 114 |
80 |
▶ | if (codepart != '\0') |
Branch: 1 branch
| Branch | True | False | Total |
| if (codepart != '\0') |
80 |
0 |
80 |
|
| 115 |
80 |
▶ | if (codepart != codepart_last && (codepart_last = codepart) != '0') |
Branch: 2 branches
| Branch | True | False | Total |
| if (codepart != codepart_last && (codepart_last = codepart) != '0') |
61 |
19 |
80 |
| if (codepart != codepart_last && (codepart_last = codepart) != '0') |
34 |
27 |
61 |
|
| 116 |
34 |
| *code_p++ = codepart; |
| 117 |
|
| } |
| 118 |
|
| |
| 119 |
34 |
▶ | while (code_p != code_end) |
Branch: 1 branch
| Branch | True | False | Total |
| while (code_p != code_end) |
17 |
17 |
34 |
|
| 120 |
17 |
| *code_p++ = '0'; |
| 121 |
|
| |
| 122 |
17 |
| *code_end = '\0'; |
| 123 |
|
| |
| 124 |
17 |
| return code; |
| 125 |
|
| } |
| 126 |
|
| |
| 127 |
9 |
| source_p++; |
| 128 |
|
| } |
| 129 |
|
| |
| 130 |
4 |
| return SvREFCNT_inc(perl_get_sv("Text::Soundex::nocode", FALSE)); |
| 131 |
|
| } |
| 132 |
|
| |
| 133 |
0 |
| static SV *sv_soundex_utf8 (source) |
| 134 |
|
| SV *source; |
| 135 |
|
| { |
| 136 |
|
| U8 *source_p; |
| 137 |
|
| U8 *source_end; |
| 138 |
|
| |
| 139 |
|
| { |
| 140 |
|
| STRLEN source_len; |
| 141 |
0 |
| source_p = (U8 *) SvPV(source, source_len); |
| 142 |
0 |
| source_end = &source_p[source_len]; |
| 143 |
|
| } |
| 144 |
|
| |
| 145 |
0 |
▶ | while (source_p < source_end) |
Branch: 1 branch
| Branch | True | False | Total |
| while (source_p < source_end) |
0 |
0 |
0 |
|
| 146 |
|
| { |
| 147 |
|
| STRLEN offset; |
| 148 |
0 |
| UV c = utf8n_to_uvchr(source_p, source_end-source_p, &offset, 0); |
| 149 |
0 |
▶ | char codepart_last = (c <= 0xFF) ? sv_soundex_table[c] : '\0'; |
Branch: 1 branch
| Branch | True | False | Total |
| char codepart_last = (c <= 0xFF) ? sv_soundex_table[c] : '\0'; |
0 |
0 |
0 |
|
| 150 |
0 |
▶ | source_p = (offset >= 1) ? &source_p[offset] : source_end; |
Branch: 1 branch
| Branch | True | False | Total |
| source_p = (offset >= 1) ? &source_p[offset] : source_end; |
0 |
0 |
0 |
|
| 151 |
|
| |
| 152 |
0 |
▶ | if (codepart_last != '\0') |
Branch: 1 branch
| Branch | True | False | Total |
| if (codepart_last != '\0') |
0 |
0 |
0 |
|
| 153 |
|
| { |
| 154 |
0 |
| SV *code = newSV(SOUNDEX_ACCURACY); |
| 155 |
0 |
| char *code_p = SvPVX(code); |
| 156 |
0 |
| char *code_end = &code_p[SOUNDEX_ACCURACY]; |
| 157 |
|
| |
| 158 |
0 |
| SvCUR_set(code, SOUNDEX_ACCURACY); |
| 159 |
0 |
| SvPOK_only(code); |
| 160 |
|
| |
| 161 |
0 |
| *code_p++ = toupper(c); |
| 162 |
|
| |
| 163 |
0 |
▶ | while (source_p != source_end && code_p != code_end) |
Branch: 2 branches
| Branch | True | False | Total |
| while (source_p != source_end && code_p != code_end) |
0 |
0 |
0 |
| while (source_p != source_end && code_p != code_end) |
0 |
0 |
0 |
|
| 164 |
|
| { |
| 165 |
|
| char codepart; |
| 166 |
0 |
| c = utf8n_to_uvchr(source_p, source_end-source_p, &offset, 0); |
| 167 |
0 |
▶ | codepart = (c <= 0xFF) ? sv_soundex_table[c] : '\0'; |
Branch: 1 branch
| Branch | True | False | Total |
| codepart = (c <= 0xFF) ? sv_soundex_table[c] : '\0'; |
0 |
0 |
0 |
|
| 168 |
0 |
▶ | source_p = (offset >= 1) ? &source_p[offset] : source_end; |
Branch: 1 branch
| Branch | True | False | Total |
| source_p = (offset >= 1) ? &source_p[offset] : source_end; |
0 |
0 |
0 |
|
| 169 |
|
| |
| 170 |
0 |
▶ | if (codepart != '\0') |
Branch: 1 branch
| Branch | True | False | Total |
| if (codepart != '\0') |
0 |
0 |
0 |
|
| 171 |
0 |
▶ | if (codepart != codepart_last && (codepart_last = codepart) != '0') |
Branch: 2 branches
| Branch | True | False | Total |
| if (codepart != codepart_last && (codepart_last = codepart) != '0') |
0 |
0 |
0 |
| if (codepart != codepart_last && (codepart_last = codepart) != '0') |
0 |
0 |
0 |
|
| 172 |
0 |
| *code_p++ = codepart; |
| 173 |
|
| } |
| 174 |
|
| |
| 175 |
0 |
▶ | while (code_p != code_end) |
Branch: 1 branch
| Branch | True | False | Total |
| while (code_p != code_end) |
0 |
0 |
0 |
|
| 176 |
0 |
| *code_p++ = '0'; |
| 177 |
|
| |
| 178 |
0 |
| *code_end = '\0'; |
| 179 |
|
| |
| 180 |
0 |
| return code; |
| 181 |
|
| } |
| 182 |
|
| |
| 183 |
0 |
| source_p++; |
| 184 |
|
| } |
| 185 |
|
| |
| 186 |
0 |
| return SvREFCNT_inc(perl_get_sv("Text::Soundex::nocode", FALSE)); |
| 187 |
|
| } |
| 188 |
|
| |
| 189 |
|
| MODULE = Text::Soundex PACKAGE = Text::Soundex |
| 190 |
|
| |
| 191 |
|
| PROTOTYPES: DISABLE |
| 192 |
|
| |
| 193 |
|
| void |
| 194 |
|
| soundex_xs (...) |
| 195 |
|
| INIT: |
| 196 |
|
| { |
| 197 |
14 |
| sv_soundex_initialize(); |
| 198 |
|
| } |
| 199 |
|
| PPCODE: |
| 200 |
|
| { |
| 201 |
|
| int i; |
| 202 |
35 |
▶ | for (i = 0; i < items; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < items; i++) |
21 |
14 |
35 |
|
| 203 |
|
| { |
| 204 |
21 |
| SV *sv = ST(i); |
| 205 |
|
| |
| 206 |
21 |
▶ | if (DO_UTF8(sv)) |
Branch: 2 branches
| Branch | True | False | Total |
| if (DO_UTF8(sv)) |
0 |
21 |
21 |
| if (DO_UTF8(sv)) |
0 |
0 |
0 |
|
| 207 |
0 |
| sv = sv_soundex_utf8(sv); |
| 208 |
|
| else |
| 209 |
21 |
| sv = sv_soundex(sv); |
| 210 |
|
| |
| 211 |
21 |
| PUSHs(sv_2mortal(sv)); |
| 212 |
|
| } |
| 213 |
|
| } |