1
/* Copyright (C) 2000 MySQL AB
3
This program is free software; you can redistribute it and/or modify
4
it under the terms of the GNU General Public License as published by
5
the Free Software Foundation; version 2 of the License.
7
This program is distributed in the hope that it will be useful,
8
but WITHOUT ANY WARRANTY; without even the implied warranty of
9
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
GNU General Public License for more details.
12
You should have received a copy of the GNU General Public License
13
along with this program; if not, write to the Free Software
14
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
16
/****************************************************************
17
* SOUNDEX ALGORITHM in C *
19
* The basic Algorithm source is taken from EDN Nov. *
22
* As a test Those in Illinois will find that the *
23
* first group of numbers in their drivers license *
24
* number is the soundex number for their last name. *
26
* RHW PC-IBBS ID. #1230 *
28
* As an extension if remove_garbage is set then all non- *
29
* alpha characters are skipped *
31
* Note, that this implementation corresponds to the *
32
* original version of the algorithm, not to the more *
33
* popular "enhanced" version, described by Knuth. *
34
****************************************************************/
36
#include "mysys_priv.h"
38
#include "my_static.h"
40
static char get_scode(CHARSET_INFO * cs, char **ptr,pbool remove_garbage);
42
/* outputed string is 4 byte long */
43
/* out_pntr can be == in_pntr */
45
void soundex(CHARSET_INFO * cs,register char * out_pntr, char * in_pntr,
50
register uchar *map=cs->to_upper;
54
while (*in_pntr && !my_isalpha(cs,*in_pntr)) /* Skip pre-space */
57
*out_pntr++ = map[(uchar)*in_pntr]; /* Copy first letter */
58
last_ch = get_scode(cs,&in_pntr,0); /* code of the first letter */
59
/* for the first 'double-letter */
61
end=out_pntr+3; /* Loop on input letters until */
62
/* end of input (null) or output */
63
/* letter code count = 3 */
66
while (out_pntr < end && (ch = get_scode(cs,&in_pntr,remove_garbage)) != 0)
69
if ((ch != '0') && (ch != last_ch)) /* if not skipped or double */
71
*out_pntr++ = ch; /* letter, copy to output */
72
} /* for next double-letter check */
73
last_ch = ch; /* save code of last input letter */
75
while (out_pntr < end)
77
*out_pntr=0; /* end string */
83
If alpha, map input letter to soundex code.
84
If not alpha and remove_garbage is set then skip to next char
88
static char get_scode(CHARSET_INFO * cs,char **ptr, pbool remove_garbage)
94
while (**ptr && !my_isalpha(cs,**ptr))
97
ch=my_toupper(cs,**ptr);
98
if (ch < 'A' || ch > 'Z')
100
if (my_isalpha(cs,ch)) /* If extended alfa (country spec) */
101
return '0'; /* threat as vokal */
102
return 0; /* Can't map */
104
return(soundex_map[ch-'A']);