PostgreSQL Source Code  git master
fuzzystrmatch.c File Reference
#include "postgres.h"
#include <ctype.h>
#include "mb/pg_wchar.h"
#include "utils/builtins.h"
#include "utils/varlena.h"
#include "varatt.h"
Include dependency graph for fuzzystrmatch.c:

Go to the source code of this file.

Macros

#define SOUNDEX_LEN   4
 
#define MAX_METAPHONE_STRLEN   255
 
#define SH   'X'
 
#define TH   '0'
 
#define isvowel(c)   (getcode(c) & 1) /* AEIOU */
 
#define NOCHANGE(c)   (getcode(c) & 2) /* FJMNR */
 
#define AFFECTH(c)   (getcode(c) & 4) /* CGPST */
 
#define MAKESOFT(c)   (getcode(c) & 8) /* EIY */
 
#define NOGHTOF(c)   (getcode(c) & 16) /* BDH */
 
#define Next_Letter   (toupper((unsigned char) word[w_idx+1]))
 
#define Curr_Letter   (toupper((unsigned char) word[w_idx]))
 
#define Look_Back_Letter(n)    (w_idx >= (n) ? toupper((unsigned char) word[w_idx-(n)]) : '\0')
 
#define Prev_Letter   (Look_Back_Letter(1))
 
#define After_Next_Letter    (Next_Letter != '\0' ? toupper((unsigned char) word[w_idx+2]) : '\0')
 
#define Look_Ahead_Letter(n)   toupper((unsigned char) Lookahead(word+w_idx, n))
 
#define Phonize(c)   do {(*phoned_word)[p_idx++] = c;} while (0)
 
#define End_Phoned_Word   do {(*phoned_word)[p_idx] = '\0';} while (0)
 
#define Phone_Len   (p_idx)
 
#define Isbreak(c)   (!isalpha((unsigned char) (c)))
 

Functions

static void _soundex (const char *instr, char *outstr)
 
static char soundex_code (char letter)
 
static char Lookahead (char *word, int how_far)
 
static void _metaphone (char *word, int max_phonemes, char **phoned_word)
 
static int getcode (char c)
 
 PG_FUNCTION_INFO_V1 (levenshtein_with_costs)
 
Datum levenshtein_with_costs (PG_FUNCTION_ARGS)
 
 PG_FUNCTION_INFO_V1 (levenshtein)
 
Datum levenshtein (PG_FUNCTION_ARGS)
 
 PG_FUNCTION_INFO_V1 (levenshtein_less_equal_with_costs)
 
Datum levenshtein_less_equal_with_costs (PG_FUNCTION_ARGS)
 
 PG_FUNCTION_INFO_V1 (levenshtein_less_equal)
 
Datum levenshtein_less_equal (PG_FUNCTION_ARGS)
 
 PG_FUNCTION_INFO_V1 (metaphone)
 
Datum metaphone (PG_FUNCTION_ARGS)
 
 PG_FUNCTION_INFO_V1 (soundex)
 
Datum soundex (PG_FUNCTION_ARGS)
 
 PG_FUNCTION_INFO_V1 (difference)
 
Datum difference (PG_FUNCTION_ARGS)
 

Variables

 PG_MODULE_MAGIC
 
static const char * soundex_table = "01230120022455012623010202"
 
static const char _codes [26]
 

Macro Definition Documentation

◆ AFFECTH

#define AFFECTH (   c)    (getcode(c) & 4) /* CGPST */

Definition at line 139 of file fuzzystrmatch.c.

◆ After_Next_Letter

#define After_Next_Letter    (Next_Letter != '\0' ? toupper((unsigned char) word[w_idx+2]) : '\0')

Definition at line 311 of file fuzzystrmatch.c.

◆ Curr_Letter

#define Curr_Letter   (toupper((unsigned char) word[w_idx]))

Definition at line 304 of file fuzzystrmatch.c.

◆ End_Phoned_Word

#define End_Phoned_Word   do {(*phoned_word)[p_idx] = '\0';} while (0)

Definition at line 336 of file fuzzystrmatch.c.

◆ Isbreak

#define Isbreak (   c)    (!isalpha((unsigned char) (c)))

Definition at line 341 of file fuzzystrmatch.c.

◆ isvowel

#define isvowel (   c)    (getcode(c) & 1) /* AEIOU */

Definition at line 133 of file fuzzystrmatch.c.

◆ Look_Ahead_Letter

#define Look_Ahead_Letter (   n)    toupper((unsigned char) Lookahead(word+w_idx, n))

Definition at line 313 of file fuzzystrmatch.c.

◆ Look_Back_Letter

#define Look_Back_Letter (   n)     (w_idx >= (n) ? toupper((unsigned char) word[w_idx-(n)]) : '\0')

Definition at line 306 of file fuzzystrmatch.c.

◆ MAKESOFT

#define MAKESOFT (   c)    (getcode(c) & 8) /* EIY */

Definition at line 142 of file fuzzystrmatch.c.

◆ MAX_METAPHONE_STRLEN

#define MAX_METAPHONE_STRLEN   255

Definition at line 73 of file fuzzystrmatch.c.

◆ Next_Letter

#define Next_Letter   (toupper((unsigned char) word[w_idx+1]))

Definition at line 302 of file fuzzystrmatch.c.

◆ NOCHANGE

#define NOCHANGE (   c)    (getcode(c) & 2) /* FJMNR */

Definition at line 136 of file fuzzystrmatch.c.

◆ NOGHTOF

#define NOGHTOF (   c)    (getcode(c) & 16) /* BDH */

Definition at line 145 of file fuzzystrmatch.c.

◆ Phone_Len

#define Phone_Len   (p_idx)

Definition at line 338 of file fuzzystrmatch.c.

◆ Phonize

#define Phonize (   c)    do {(*phoned_word)[p_idx++] = c;} while (0)

Definition at line 334 of file fuzzystrmatch.c.

◆ Prev_Letter

#define Prev_Letter   (Look_Back_Letter(1))

Definition at line 309 of file fuzzystrmatch.c.

◆ SH

#define SH   'X'

Definition at line 104 of file fuzzystrmatch.c.

◆ SOUNDEX_LEN

#define SOUNDEX_LEN   4

Definition at line 55 of file fuzzystrmatch.c.

◆ TH

#define TH   '0'

Definition at line 105 of file fuzzystrmatch.c.

Function Documentation

◆ _metaphone()

static void _metaphone ( char *  word,
int  max_phonemes,
char **  phoned_word 
)
static

Definition at line 345 of file fuzzystrmatch.c.

348 {
349  int w_idx = 0; /* point in the phonization we're at. */
350  int p_idx = 0; /* end of the phoned phrase */
351 
352  /*-- Parameter checks --*/
353 
354  /*
355  * Shouldn't be necessary, but left these here anyway jec Aug 3, 2001
356  */
357 
358  /* Negative phoneme length is meaningless */
359  if (!(max_phonemes > 0))
360  /* internal error */
361  elog(ERROR, "metaphone: Requested output length must be > 0");
362 
363  /* Empty/null string is meaningless */
364  if ((word == NULL) || !(strlen(word) > 0))
365  /* internal error */
366  elog(ERROR, "metaphone: Input string length must be > 0");
367 
368  /*-- Allocate memory for our phoned_phrase --*/
369  if (max_phonemes == 0)
370  { /* Assume largest possible */
371  *phoned_word = palloc(sizeof(char) * strlen(word) + 1);
372  }
373  else
374  {
375  *phoned_word = palloc(sizeof(char) * max_phonemes + 1);
376  }
377 
378  /*-- The first phoneme has to be processed specially. --*/
379  /* Find our first letter */
380  for (; !isalpha((unsigned char) (Curr_Letter)); w_idx++)
381  {
382  /* On the off chance we were given nothing but crap... */
383  if (Curr_Letter == '\0')
384  {
386  return;
387  }
388  }
389 
390  switch (Curr_Letter)
391  {
392  /* AE becomes E */
393  case 'A':
394  if (Next_Letter == 'E')
395  {
396  Phonize('E');
397  w_idx += 2;
398  }
399  /* Remember, preserve vowels at the beginning */
400  else
401  {
402  Phonize('A');
403  w_idx++;
404  }
405  break;
406  /* [GKP]N becomes N */
407  case 'G':
408  case 'K':
409  case 'P':
410  if (Next_Letter == 'N')
411  {
412  Phonize('N');
413  w_idx += 2;
414  }
415  break;
416 
417  /*
418  * WH becomes H, WR becomes R W if followed by a vowel
419  */
420  case 'W':
421  if (Next_Letter == 'H' ||
422  Next_Letter == 'R')
423  {
425  w_idx += 2;
426  }
427  else if (isvowel(Next_Letter))
428  {
429  Phonize('W');
430  w_idx += 2;
431  }
432  /* else ignore */
433  break;
434  /* X becomes S */
435  case 'X':
436  Phonize('S');
437  w_idx++;
438  break;
439  /* Vowels are kept */
440 
441  /*
442  * We did A already case 'A': case 'a':
443  */
444  case 'E':
445  case 'I':
446  case 'O':
447  case 'U':
449  w_idx++;
450  break;
451  default:
452  /* do nothing */
453  break;
454  }
455 
456 
457 
458  /* On to the metaphoning */
459  for (; Curr_Letter != '\0' &&
460  (max_phonemes == 0 || Phone_Len < max_phonemes);
461  w_idx++)
462  {
463  /*
464  * How many letters to skip because an earlier encoding handled
465  * multiple letters
466  */
467  unsigned short int skip_letter = 0;
468 
469 
470  /*
471  * THOUGHT: It would be nice if, rather than having things like...
472  * well, SCI. For SCI you encode the S, then have to remember to skip
473  * the C. So the phonome SCI invades both S and C. It would be
474  * better, IMHO, to skip the C from the S part of the encoding. Hell,
475  * I'm trying it.
476  */
477 
478  /* Ignore non-alphas */
479  if (!isalpha((unsigned char) (Curr_Letter)))
480  continue;
481 
482  /* Drop duplicates, except CC */
483  if (Curr_Letter == Prev_Letter &&
484  Curr_Letter != 'C')
485  continue;
486 
487  switch (Curr_Letter)
488  {
489  /* B -> B unless in MB */
490  case 'B':
491  if (Prev_Letter != 'M')
492  Phonize('B');
493  break;
494 
495  /*
496  * 'sh' if -CIA- or -CH, but not SCH, except SCHW. (SCHW is
497  * handled in S) S if -CI-, -CE- or -CY- dropped if -SCI-,
498  * SCE-, -SCY- (handed in S) else K
499  */
500  case 'C':
501  if (MAKESOFT(Next_Letter))
502  { /* C[IEY] */
503  if (After_Next_Letter == 'A' &&
504  Next_Letter == 'I')
505  { /* CIA */
506  Phonize(SH);
507  }
508  /* SC[IEY] */
509  else if (Prev_Letter == 'S')
510  {
511  /* Dropped */
512  }
513  else
514  Phonize('S');
515  }
516  else if (Next_Letter == 'H')
517  {
518 #ifndef USE_TRADITIONAL_METAPHONE
519  if (After_Next_Letter == 'R' ||
520  Prev_Letter == 'S')
521  { /* Christ, School */
522  Phonize('K');
523  }
524  else
525  Phonize(SH);
526 #else
527  Phonize(SH);
528 #endif
529  skip_letter++;
530  }
531  else
532  Phonize('K');
533  break;
534 
535  /*
536  * J if in -DGE-, -DGI- or -DGY- else T
537  */
538  case 'D':
539  if (Next_Letter == 'G' &&
541  {
542  Phonize('J');
543  skip_letter++;
544  }
545  else
546  Phonize('T');
547  break;
548 
549  /*
550  * F if in -GH and not B--GH, D--GH, -H--GH, -H---GH else
551  * dropped if -GNED, -GN, else dropped if -DGE-, -DGI- or
552  * -DGY- (handled in D) else J if in -GE-, -GI, -GY and not GG
553  * else K
554  */
555  case 'G':
556  if (Next_Letter == 'H')
557  {
558  if (!(NOGHTOF(Look_Back_Letter(3)) ||
559  Look_Back_Letter(4) == 'H'))
560  {
561  Phonize('F');
562  skip_letter++;
563  }
564  else
565  {
566  /* silent */
567  }
568  }
569  else if (Next_Letter == 'N')
570  {
571  if (Isbreak(After_Next_Letter) ||
572  (After_Next_Letter == 'E' &&
573  Look_Ahead_Letter(3) == 'D'))
574  {
575  /* dropped */
576  }
577  else
578  Phonize('K');
579  }
580  else if (MAKESOFT(Next_Letter) &&
581  Prev_Letter != 'G')
582  Phonize('J');
583  else
584  Phonize('K');
585  break;
586  /* H if before a vowel and not after C,G,P,S,T */
587  case 'H':
588  if (isvowel(Next_Letter) &&
590  Phonize('H');
591  break;
592 
593  /*
594  * dropped if after C else K
595  */
596  case 'K':
597  if (Prev_Letter != 'C')
598  Phonize('K');
599  break;
600 
601  /*
602  * F if before H else P
603  */
604  case 'P':
605  if (Next_Letter == 'H')
606  Phonize('F');
607  else
608  Phonize('P');
609  break;
610 
611  /*
612  * K
613  */
614  case 'Q':
615  Phonize('K');
616  break;
617 
618  /*
619  * 'sh' in -SH-, -SIO- or -SIA- or -SCHW- else S
620  */
621  case 'S':
622  if (Next_Letter == 'I' &&
623  (After_Next_Letter == 'O' ||
624  After_Next_Letter == 'A'))
625  Phonize(SH);
626  else if (Next_Letter == 'H')
627  {
628  Phonize(SH);
629  skip_letter++;
630  }
631 #ifndef USE_TRADITIONAL_METAPHONE
632  else if (Next_Letter == 'C' &&
633  Look_Ahead_Letter(2) == 'H' &&
634  Look_Ahead_Letter(3) == 'W')
635  {
636  Phonize(SH);
637  skip_letter += 2;
638  }
639 #endif
640  else
641  Phonize('S');
642  break;
643 
644  /*
645  * 'sh' in -TIA- or -TIO- else 'th' before H else T
646  */
647  case 'T':
648  if (Next_Letter == 'I' &&
649  (After_Next_Letter == 'O' ||
650  After_Next_Letter == 'A'))
651  Phonize(SH);
652  else if (Next_Letter == 'H')
653  {
654  Phonize(TH);
655  skip_letter++;
656  }
657  else
658  Phonize('T');
659  break;
660  /* F */
661  case 'V':
662  Phonize('F');
663  break;
664  /* W before a vowel, else dropped */
665  case 'W':
666  if (isvowel(Next_Letter))
667  Phonize('W');
668  break;
669  /* KS */
670  case 'X':
671  Phonize('K');
672  if (max_phonemes == 0 || Phone_Len < max_phonemes)
673  Phonize('S');
674  break;
675  /* Y if followed by a vowel */
676  case 'Y':
677  if (isvowel(Next_Letter))
678  Phonize('Y');
679  break;
680  /* S */
681  case 'Z':
682  Phonize('S');
683  break;
684  /* No transformation */
685  case 'F':
686  case 'J':
687  case 'L':
688  case 'M':
689  case 'N':
690  case 'R':
692  break;
693  default:
694  /* nothing */
695  break;
696  } /* END SWITCH */
697 
698  w_idx += skip_letter;
699  } /* END FOR */
700 
702 } /* END metaphone */
#define ERROR
Definition: elog.h:39
#define After_Next_Letter
#define Look_Back_Letter(n)
#define Curr_Letter
#define SH
#define MAKESOFT(c)
#define Isbreak(c)
#define End_Phoned_Word
#define isvowel(c)
#define Prev_Letter
#define TH
#define NOGHTOF(c)
#define Next_Letter
#define Phonize(c)
#define AFFECTH(c)
#define Phone_Len
#define Look_Ahead_Letter(n)
void * palloc(Size size)
Definition: mcxt.c:1210
static void word(struct vars *v, int dir, struct state *lp, struct state *rp)
Definition: regcomp.c:1476

References AFFECTH, After_Next_Letter, Curr_Letter, elog(), End_Phoned_Word, ERROR, Isbreak, isvowel, Look_Ahead_Letter, Look_Back_Letter, MAKESOFT, Next_Letter, NOGHTOF, palloc(), Phone_Len, Phonize, Prev_Letter, SH, TH, and word().

Referenced by metaphone().

◆ _soundex()

static void _soundex ( const char *  instr,
char *  outstr 
)
static

Definition at line 724 of file fuzzystrmatch.c.

725 {
726  int count;
727 
728  Assert(instr);
729  Assert(outstr);
730 
731  outstr[SOUNDEX_LEN] = '\0';
732 
733  /* Skip leading non-alphabetic characters */
734  while (!isalpha((unsigned char) instr[0]) && instr[0])
735  ++instr;
736 
737  /* No string left */
738  if (!instr[0])
739  {
740  outstr[0] = (char) 0;
741  return;
742  }
743 
744  /* Take the first letter as is */
745  *outstr++ = (char) toupper((unsigned char) *instr++);
746 
747  count = 1;
748  while (*instr && count < SOUNDEX_LEN)
749  {
750  if (isalpha((unsigned char) *instr) &&
751  soundex_code(*instr) != soundex_code(*(instr - 1)))
752  {
753  *outstr = soundex_code(instr[0]);
754  if (*outstr != '0')
755  {
756  ++outstr;
757  ++count;
758  }
759  }
760  ++instr;
761  }
762 
763  /* Fill with 0's */
764  while (count < SOUNDEX_LEN)
765  {
766  *outstr = '0';
767  ++outstr;
768  ++count;
769  }
770 }
#define SOUNDEX_LEN
Definition: fuzzystrmatch.c:55
static char soundex_code(char letter)
Definition: fuzzystrmatch.c:61
Assert(fmt[strlen(fmt) - 1] !='\n')

References Assert(), soundex_code(), and SOUNDEX_LEN.

Referenced by difference(), and soundex().

◆ difference()

Datum difference ( PG_FUNCTION_ARGS  )

Definition at line 775 of file fuzzystrmatch.c.

776 {
777  char sndx1[SOUNDEX_LEN + 1],
778  sndx2[SOUNDEX_LEN + 1];
779  int i,
780  result;
781 
784 
785  result = 0;
786  for (i = 0; i < SOUNDEX_LEN; i++)
787  {
788  if (sndx1[i] == sndx2[i])
789  result++;
790  }
791 
792  PG_RETURN_INT32(result);
793 }
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:309
#define PG_RETURN_INT32(x)
Definition: fmgr.h:354
static void _soundex(const char *instr, char *outstr)
int i
Definition: isn.c:73
char * text_to_cstring(const text *t)
Definition: varlena.c:215

References _soundex(), i, PG_GETARG_TEXT_PP, PG_RETURN_INT32, SOUNDEX_LEN, and text_to_cstring().

Referenced by checkcondition_str(), ExtendMultiXactMember(), find_wordentry(), getKeyJsonValueFromContainer(), hstore_concat(), hstore_delete_array(), hstore_delete_hstore(), hstore_subscript_assign(), and hstoreFindKey().

◆ getcode()

static int getcode ( char  c)
static

Definition at line 121 of file fuzzystrmatch.c.

122 {
123  if (isalpha((unsigned char) c))
124  {
125  c = toupper((unsigned char) c);
126  /* Defend against non-ASCII letters */
127  if (c >= 'A' && c <= 'Z')
128  return _codes[c - 'A'];
129  }
130  return 0;
131 }
static const char _codes[26]
char * c

References _codes.

◆ levenshtein()

Datum levenshtein ( PG_FUNCTION_ARGS  )

Definition at line 175 of file fuzzystrmatch.c.

176 {
177  text *src = PG_GETARG_TEXT_PP(0);
178  text *dst = PG_GETARG_TEXT_PP(1);
179  const char *s_data;
180  const char *t_data;
181  int s_bytes,
182  t_bytes;
183 
184  /* Extract a pointer to the actual character data */
185  s_data = VARDATA_ANY(src);
186  t_data = VARDATA_ANY(dst);
187  /* Determine length of each string in bytes */
188  s_bytes = VARSIZE_ANY_EXHDR(src);
189  t_bytes = VARSIZE_ANY_EXHDR(dst);
190 
191  PG_RETURN_INT32(varstr_levenshtein(s_data, s_bytes, t_data, t_bytes,
192  1, 1, 1, false));
193 }
int varstr_levenshtein(const char *source, int slen, const char *target, int tlen, int ins_c, int del_c, int sub_c, bool trusted)
Definition: levenshtein.c:73
Definition: c.h:671
#define VARDATA_ANY(PTR)
Definition: varatt.h:324
#define VARSIZE_ANY_EXHDR(PTR)
Definition: varatt.h:317

References PG_GETARG_TEXT_PP, PG_RETURN_INT32, VARDATA_ANY, VARSIZE_ANY_EXHDR, and varstr_levenshtein().

◆ levenshtein_less_equal()

Datum levenshtein_less_equal ( PG_FUNCTION_ARGS  )

Definition at line 227 of file fuzzystrmatch.c.

228 {
229  text *src = PG_GETARG_TEXT_PP(0);
230  text *dst = PG_GETARG_TEXT_PP(1);
231  int max_d = PG_GETARG_INT32(2);
232  const char *s_data;
233  const char *t_data;
234  int s_bytes,
235  t_bytes;
236 
237  /* Extract a pointer to the actual character data */
238  s_data = VARDATA_ANY(src);
239  t_data = VARDATA_ANY(dst);
240  /* Determine length of each string in bytes */
241  s_bytes = VARSIZE_ANY_EXHDR(src);
242  t_bytes = VARSIZE_ANY_EXHDR(dst);
243 
245  t_data, t_bytes,
246  1, 1, 1,
247  max_d, false));
248 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:269
int varstr_levenshtein_less_equal(const char *source, int slen, const char *target, int tlen, int ins_c, int del_c, int sub_c, int max_d, bool trusted)

References PG_GETARG_INT32, PG_GETARG_TEXT_PP, PG_RETURN_INT32, VARDATA_ANY, VARSIZE_ANY_EXHDR, and varstr_levenshtein_less_equal().

◆ levenshtein_less_equal_with_costs()

Datum levenshtein_less_equal_with_costs ( PG_FUNCTION_ARGS  )

Definition at line 198 of file fuzzystrmatch.c.

199 {
200  text *src = PG_GETARG_TEXT_PP(0);
201  text *dst = PG_GETARG_TEXT_PP(1);
202  int ins_c = PG_GETARG_INT32(2);
203  int del_c = PG_GETARG_INT32(3);
204  int sub_c = PG_GETARG_INT32(4);
205  int max_d = PG_GETARG_INT32(5);
206  const char *s_data;
207  const char *t_data;
208  int s_bytes,
209  t_bytes;
210 
211  /* Extract a pointer to the actual character data */
212  s_data = VARDATA_ANY(src);
213  t_data = VARDATA_ANY(dst);
214  /* Determine length of each string in bytes */
215  s_bytes = VARSIZE_ANY_EXHDR(src);
216  t_bytes = VARSIZE_ANY_EXHDR(dst);
217 
219  t_data, t_bytes,
220  ins_c, del_c, sub_c,
221  max_d, false));
222 }

References PG_GETARG_INT32, PG_GETARG_TEXT_PP, PG_RETURN_INT32, VARDATA_ANY, VARSIZE_ANY_EXHDR, and varstr_levenshtein_less_equal().

◆ levenshtein_with_costs()

Datum levenshtein_with_costs ( PG_FUNCTION_ARGS  )

Definition at line 149 of file fuzzystrmatch.c.

150 {
151  text *src = PG_GETARG_TEXT_PP(0);
152  text *dst = PG_GETARG_TEXT_PP(1);
153  int ins_c = PG_GETARG_INT32(2);
154  int del_c = PG_GETARG_INT32(3);
155  int sub_c = PG_GETARG_INT32(4);
156  const char *s_data;
157  const char *t_data;
158  int s_bytes,
159  t_bytes;
160 
161  /* Extract a pointer to the actual character data */
162  s_data = VARDATA_ANY(src);
163  t_data = VARDATA_ANY(dst);
164  /* Determine length of each string in bytes */
165  s_bytes = VARSIZE_ANY_EXHDR(src);
166  t_bytes = VARSIZE_ANY_EXHDR(dst);
167 
168  PG_RETURN_INT32(varstr_levenshtein(s_data, s_bytes, t_data, t_bytes,
169  ins_c, del_c, sub_c, false));
170 }

References PG_GETARG_INT32, PG_GETARG_TEXT_PP, PG_RETURN_INT32, VARDATA_ANY, VARSIZE_ANY_EXHDR, and varstr_levenshtein().

◆ Lookahead()

static char Lookahead ( char *  word,
int  how_far 
)
static

Definition at line 319 of file fuzzystrmatch.c.

320 {
321  char letter_ahead = '\0'; /* null by default */
322  int idx;
323 
324  for (idx = 0; word[idx] != '\0' && idx < how_far; idx++);
325  /* Edge forward in the string... */
326 
327  letter_ahead = word[idx]; /* idx will be either == to how_far or at the
328  * end of the string */
329  return letter_ahead;
330 }
Datum idx(PG_FUNCTION_ARGS)
Definition: _int_op.c:259

References idx(), and word().

◆ metaphone()

Datum metaphone ( PG_FUNCTION_ARGS  )

Definition at line 258 of file fuzzystrmatch.c.

259 {
260  char *str_i = TextDatumGetCString(PG_GETARG_DATUM(0));
261  size_t str_i_len = strlen(str_i);
262  int reqlen;
263  char *metaph;
264 
265  /* return an empty string if we receive one */
266  if (!(str_i_len > 0))
268 
269  if (str_i_len > MAX_METAPHONE_STRLEN)
270  ereport(ERROR,
271  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
272  errmsg("argument exceeds the maximum length of %d bytes",
274 
275  reqlen = PG_GETARG_INT32(1);
276  if (reqlen > MAX_METAPHONE_STRLEN)
277  ereport(ERROR,
278  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
279  errmsg("output exceeds the maximum length of %d bytes",
281 
282  if (!(reqlen > 0))
283  ereport(ERROR,
284  (errcode(ERRCODE_ZERO_LENGTH_CHARACTER_STRING),
285  errmsg("output cannot be empty string")));
286 
287  _metaphone(str_i, reqlen, &metaph);
289 }
#define TextDatumGetCString(d)
Definition: builtins.h:95
int errcode(int sqlerrcode)
Definition: elog.c:858
int errmsg(const char *fmt,...)
Definition: elog.c:1069
#define ereport(elevel,...)
Definition: elog.h:149
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:268
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:372
static void _metaphone(char *word, int max_phonemes, char **phoned_word)
#define MAX_METAPHONE_STRLEN
Definition: fuzzystrmatch.c:73
text * cstring_to_text(const char *s)
Definition: varlena.c:182

References _metaphone(), cstring_to_text(), ereport, errcode(), errmsg(), ERROR, MAX_METAPHONE_STRLEN, PG_GETARG_DATUM, PG_GETARG_INT32, PG_RETURN_TEXT_P, and TextDatumGetCString.

◆ PG_FUNCTION_INFO_V1() [1/7]

PG_FUNCTION_INFO_V1 ( difference  )

◆ PG_FUNCTION_INFO_V1() [2/7]

PG_FUNCTION_INFO_V1 ( levenshtein  )

◆ PG_FUNCTION_INFO_V1() [3/7]

PG_FUNCTION_INFO_V1 ( levenshtein_less_equal  )

◆ PG_FUNCTION_INFO_V1() [4/7]

PG_FUNCTION_INFO_V1 ( levenshtein_less_equal_with_costs  )

◆ PG_FUNCTION_INFO_V1() [5/7]

PG_FUNCTION_INFO_V1 ( levenshtein_with_costs  )

◆ PG_FUNCTION_INFO_V1() [6/7]

PG_FUNCTION_INFO_V1 ( metaphone  )

◆ PG_FUNCTION_INFO_V1() [7/7]

PG_FUNCTION_INFO_V1 ( soundex  )

◆ soundex()

Datum soundex ( PG_FUNCTION_ARGS  )

Definition at line 711 of file fuzzystrmatch.c.

712 {
713  char outstr[SOUNDEX_LEN + 1];
714  char *arg;
715 
717 
718  _soundex(arg, outstr);
719 
721 }
void * arg

References _soundex(), arg, cstring_to_text(), PG_GETARG_TEXT_PP, PG_RETURN_TEXT_P, SOUNDEX_LEN, and text_to_cstring().

◆ soundex_code()

static char soundex_code ( char  letter)
static

Definition at line 61 of file fuzzystrmatch.c.

62 {
63  letter = toupper((unsigned char) letter);
64  /* Defend against non-ASCII letters */
65  if (letter >= 'A' && letter <= 'Z')
66  return soundex_table[letter - 'A'];
67  return letter;
68 }
static const char * soundex_table
Definition: fuzzystrmatch.c:58

References soundex_table.

Referenced by _soundex().

Variable Documentation

◆ _codes

const char _codes[26]
static
Initial value:
= {
1, 16, 4, 16, 9, 2, 4, 16, 9, 2, 0, 2, 2, 2, 1, 4, 0, 2, 4, 4, 1, 0, 0, 0, 8, 0
}

Definition at line 115 of file fuzzystrmatch.c.

Referenced by getcode().

◆ PG_MODULE_MAGIC

PG_MODULE_MAGIC

Definition at line 48 of file fuzzystrmatch.c.

◆ soundex_table

const char* soundex_table = "01230120022455012623010202"
static

Definition at line 58 of file fuzzystrmatch.c.

Referenced by soundex_code().