File: soundex.c

package info (click to toggle)
serpento 0.3.6
  • links: PTS
  • area: main
  • in suites: woody
  • size: 292 kB
  • ctags: 381
  • sloc: python: 1,644; ansic: 666; perl: 157; sh: 116; makefile: 72
file content (92 lines) | stat: -rw-r--r-- 1,765 bytes parent folder | download | duplicates (3)
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
#include <ctype.h>

/* soundex routine is sto^H^H^Hborrowed from python soundexmodule.c */
void soundex_hash(char *str, char *result, int len)
{
    char *sptr = str;		/* pointer into str */
    char *rptr = result;	/* pointer into result */

    if (*str == '\0') {
        memset(result, '0', len);
        result[len+1] = '\0';
	return;
    }
    /*  Preserve the first character of the input string.
     */
    *(rptr++) = toupper(*(sptr++));

    /* Translate the rest of the input string into result.  The following
       transformations are used:

       1) All vowels, W, and H, are skipped.

       2) BFPV = 1
       CGJKQSXZ = 2
       DT = 3
       L = 4
       MN = 5
       R = 6

       3) Only translate the first of adjacent equal translations.  I.E.
       remove duplicate digits.
     */

    for (; (rptr - result) < len && *sptr != '\0'; sptr++) {
	switch (toupper(*sptr)) {
	case 'W':
	case 'H':
	case 'A':
	case 'I':
	case 'O':
	case 'U':
	case 'Y':
	    break;
	case 'B':
	case 'F':
	case 'P':
	case 'V':
	    if (*(rptr - 1) != '1')
		*(rptr++) = '1';
	    break;
	case 'C':
	case 'G':
	case 'J':
	case 'K':
	case 'Q':
	case 'S':
	case 'X':
	case 'Z':
	    if (*(rptr - 1) != '2')
		*(rptr++) = '2';
	    break;
	case 'D':
	case 'T':
	    if (*(rptr - 1) != '3')
		*(rptr++) = '3';
	    break;
	case 'L':
	    if (*(rptr - 1) != '4')
		*(rptr++) = '4';
	    break;
	case 'M':
	case 'N':
	    if (*(rptr - 1) != '5')
		*(rptr++) = '5';
	    break;
	case 'R':
	    if (*(rptr - 1) != '6')
		*(rptr++) = '6';
	default:
	    break;
	}
    }

    /* Pad 0's on right side of string out to len characters.
     */
    for (; rptr < result + len; rptr++)
	*rptr = '0';

    /* Terminate the result string.
     */
    *(result + len) = '\0';
}