~mysql/mysql-server/mysql-6.0

« back to all changes in this revision

Viewing changes to mysys/mf_soundex.c

  • Committer: bk at mysql
  • Date: 2000-07-31 19:29:14 UTC
  • Revision ID: sp1r-bk@work.mysql.com-20000731192914-08846
Import changeset

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult AB
 
2
   
 
3
   This library is free software; you can redistribute it and/or
 
4
   modify it under the terms of the GNU Library General Public
 
5
   License as published by the Free Software Foundation; either
 
6
   version 2 of the License, or (at your option) any later version.
 
7
   
 
8
   This library is distributed in the hope that it will be useful,
 
9
   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
11
   Library General Public License for more details.
 
12
   
 
13
   You should have received a copy of the GNU Library General Public
 
14
   License along with this library; if not, write to the Free
 
15
   Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
 
16
   MA 02111-1307, USA */
 
17
 
 
18
/****************************************************************
 
19
*       SOUNDEX ALGORITHM in C                                  *
 
20
*                                                               *
 
21
*       The basic Algorithm source is taken from EDN Nov.       *
 
22
*       14, 1985 pg. 36.                                        *
 
23
*                                                               *
 
24
*       As a test Those in Illinois will find that the          *
 
25
*       first group of numbers in their drivers license         *
 
26
*       number is the soundex number for their last name.       *
 
27
*                                                               *
 
28
*       RHW  PC-IBBS ID. #1230                                  *
 
29
*                                                               *
 
30
*       As an extension if remove_garbage is set then all non-  *
 
31
*       alpha characters are skipped                            *
 
32
****************************************************************/
 
33
 
 
34
#include "mysys_priv.h"
 
35
#include <m_ctype.h>
 
36
#include "my_static.h"
 
37
 
 
38
static char get_scode(char **ptr,pbool remove_garbage);
 
39
 
 
40
                /* outputed string is 4 byte long */
 
41
                /* out_pntr can be == in_pntr */
 
42
 
 
43
void soundex(register my_string out_pntr, my_string in_pntr,
 
44
             pbool remove_garbage)
 
45
{
 
46
  char ch,last_ch;
 
47
  reg3 my_string end;
 
48
 
 
49
  if (remove_garbage)
 
50
  {
 
51
    while (*in_pntr && isspace(*in_pntr))       /* Skipp pre-space */
 
52
      in_pntr++;
 
53
  }
 
54
  *out_pntr++ = toupper(*in_pntr);      /* Copy first letter             */
 
55
  last_ch = get_scode(&in_pntr,0);      /* code of the first letter      */
 
56
                                        /* for the first 'double-letter  */
 
57
                                        /* check.                        */
 
58
  end=out_pntr+3;                       /* Loop on input letters until   */
 
59
                                        /* end of input (null) or output */
 
60
                                        /* letter code count = 3         */
 
61
 
 
62
  in_pntr++;
 
63
  while (out_pntr < end && (ch = get_scode(&in_pntr,remove_garbage)) != 0)
 
64
  {
 
65
    in_pntr++;
 
66
    if ((ch != '0') && (ch != last_ch)) /* if not skipped or double */
 
67
    {
 
68
      *out_pntr++ = ch;                 /* letter, copy to output */
 
69
    }                                   /* for next double-letter check */
 
70
    last_ch = ch;                       /* save code of last input letter */
 
71
  }
 
72
  while (out_pntr < end)
 
73
    *out_pntr++ = '0';
 
74
  *out_pntr=0;                          /* end string */
 
75
  return;
 
76
} /* soundex */
 
77
 
 
78
 
 
79
  /*
 
80
    If alpha, map input letter to soundex code.
 
81
    If not alpha and remove_garbage is set then skipp to next char
 
82
    else return 0
 
83
    */
 
84
 
 
85
static char get_scode(char **ptr, pbool remove_garbage)
 
86
{
 
87
  uchar ch;
 
88
 
 
89
  if (remove_garbage)
 
90
  {
 
91
    while (**ptr && !isalpha(**ptr))
 
92
      (*ptr)++;
 
93
  }
 
94
  ch=toupper(**ptr);
 
95
  if (ch < 'A' || ch > 'Z')
 
96
  {
 
97
    if (isalpha(ch))                    /* If exetended alfa (country spec) */
 
98
      return '0';                       /* threat as vokal */
 
99
    return 0;                           /* Can't map */
 
100
  }
 
101
  return(soundex_map[ch-'A']);
 
102
} /* get_scode */