PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
fuzzystrmatch.c File Reference
#include "postgres.h"
#include <ctype.h>
#include "mb/pg_wchar.h"
#include "utils/builtins.h"
#include "utils/varlena.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

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

Definition at line 138 of file fuzzystrmatch.c.

Referenced by _metaphone().

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

Definition at line 310 of file fuzzystrmatch.c.

Referenced by _metaphone().

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

Definition at line 303 of file fuzzystrmatch.c.

Referenced by _metaphone().

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

Definition at line 335 of file fuzzystrmatch.c.

Referenced by _metaphone().

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

Definition at line 340 of file fuzzystrmatch.c.

Referenced by _metaphone().

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

Definition at line 132 of file fuzzystrmatch.c.

Referenced by _metaphone().

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

Definition at line 312 of file fuzzystrmatch.c.

Referenced by _metaphone().

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

Definition at line 305 of file fuzzystrmatch.c.

Referenced by _metaphone().

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

Definition at line 141 of file fuzzystrmatch.c.

Referenced by _metaphone().

#define MAX_METAPHONE_STRLEN   255

Definition at line 72 of file fuzzystrmatch.c.

Referenced by metaphone().

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

Definition at line 301 of file fuzzystrmatch.c.

Referenced by _metaphone().

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

Definition at line 135 of file fuzzystrmatch.c.

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

Definition at line 144 of file fuzzystrmatch.c.

Referenced by _metaphone().

#define Phone_Len   (p_idx)

Definition at line 337 of file fuzzystrmatch.c.

Referenced by _metaphone().

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

Definition at line 333 of file fuzzystrmatch.c.

Referenced by _metaphone().

#define Prev_Letter   (Look_Back_Letter(1))

Definition at line 308 of file fuzzystrmatch.c.

Referenced by _metaphone().

#define SH   'X'

Definition at line 103 of file fuzzystrmatch.c.

Referenced by _metaphone().

#define SOUNDEX_LEN   4

Definition at line 54 of file fuzzystrmatch.c.

Referenced by _soundex(), difference(), and soundex().

#define TH   '0'

Definition at line 104 of file fuzzystrmatch.c.

Referenced by _metaphone().

Function Documentation

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

Definition at line 344 of file fuzzystrmatch.c.

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, and TH.

Referenced by metaphone().

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

Definition at line 725 of file fuzzystrmatch.c.

References AssertArg, soundex_code(), and SOUNDEX_LEN.

Referenced by difference(), and soundex().

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

Definition at line 776 of file fuzzystrmatch.c.

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

Referenced by checkcondition_str(), ExtendMultiXactMember(), find_wordentry(), findJsonbValueFromContainer(), findObjectByCatalogId(), findObjectByOid(), findOwningExtension(), hstore_concat(), hstore_delete_array(), hstore_delete_hstore(), hstoreFindKey(), ScanCKeywordLookup(), and ScanKeywordLookup().

777 {
778  char sndx1[SOUNDEX_LEN + 1],
779  sndx2[SOUNDEX_LEN + 1];
780  int i,
781  result;
782 
785 
786  result = 0;
787  for (i = 0; i < SOUNDEX_LEN; i++)
788  {
789  if (sndx1[i] == sndx2[i])
790  result++;
791  }
792 
793  PG_RETURN_INT32(result);
794 }
#define PG_RETURN_INT32(x)
Definition: fmgr.h:314
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:273
static void _soundex(const char *instr, char *outstr)
#define SOUNDEX_LEN
Definition: fuzzystrmatch.c:54
char * text_to_cstring(const text *t)
Definition: varlena.c:182
int i
static int getcode ( char  c)
static

Definition at line 120 of file fuzzystrmatch.c.

References _codes.

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

Definition at line 174 of file fuzzystrmatch.c.

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

175 {
176  text *src = PG_GETARG_TEXT_PP(0);
177  text *dst = PG_GETARG_TEXT_PP(1);
178  const char *s_data;
179  const char *t_data;
180  int s_bytes,
181  t_bytes;
182 
183  /* Extract a pointer to the actual character data */
184  s_data = VARDATA_ANY(src);
185  t_data = VARDATA_ANY(dst);
186  /* Determine length of each string in bytes */
187  s_bytes = VARSIZE_ANY_EXHDR(src);
188  t_bytes = VARSIZE_ANY_EXHDR(dst);
189 
190  PG_RETURN_INT32(varstr_levenshtein(s_data, s_bytes, t_data, t_bytes,
191  1, 1, 1, false));
192 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:347
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
#define PG_RETURN_INT32(x)
Definition: fmgr.h:314
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:273
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:340
Definition: c.h:433
Datum levenshtein_less_equal ( PG_FUNCTION_ARGS  )

Definition at line 226 of file fuzzystrmatch.c.

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

227 {
228  text *src = PG_GETARG_TEXT_PP(0);
229  text *dst = PG_GETARG_TEXT_PP(1);
230  int max_d = PG_GETARG_INT32(2);
231  const char *s_data;
232  const char *t_data;
233  int s_bytes,
234  t_bytes;
235 
236  /* Extract a pointer to the actual character data */
237  s_data = VARDATA_ANY(src);
238  t_data = VARDATA_ANY(dst);
239  /* Determine length of each string in bytes */
240  s_bytes = VARSIZE_ANY_EXHDR(src);
241  t_bytes = VARSIZE_ANY_EXHDR(dst);
242 
244  t_data, t_bytes,
245  1, 1, 1,
246  max_d, false));
247 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:234
#define VARDATA_ANY(PTR)
Definition: postgres.h:347
#define PG_RETURN_INT32(x)
Definition: fmgr.h:314
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:273
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)
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:340
Definition: c.h:433
Datum levenshtein_less_equal_with_costs ( PG_FUNCTION_ARGS  )

Definition at line 197 of file fuzzystrmatch.c.

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

198 {
199  text *src = PG_GETARG_TEXT_PP(0);
200  text *dst = PG_GETARG_TEXT_PP(1);
201  int ins_c = PG_GETARG_INT32(2);
202  int del_c = PG_GETARG_INT32(3);
203  int sub_c = PG_GETARG_INT32(4);
204  int max_d = PG_GETARG_INT32(5);
205  const char *s_data;
206  const char *t_data;
207  int s_bytes,
208  t_bytes;
209 
210  /* Extract a pointer to the actual character data */
211  s_data = VARDATA_ANY(src);
212  t_data = VARDATA_ANY(dst);
213  /* Determine length of each string in bytes */
214  s_bytes = VARSIZE_ANY_EXHDR(src);
215  t_bytes = VARSIZE_ANY_EXHDR(dst);
216 
218  t_data, t_bytes,
219  ins_c, del_c, sub_c,
220  max_d, false));
221 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:234
#define VARDATA_ANY(PTR)
Definition: postgres.h:347
#define PG_RETURN_INT32(x)
Definition: fmgr.h:314
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:273
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)
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:340
Definition: c.h:433
Datum levenshtein_with_costs ( PG_FUNCTION_ARGS  )

Definition at line 148 of file fuzzystrmatch.c.

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

149 {
150  text *src = PG_GETARG_TEXT_PP(0);
151  text *dst = PG_GETARG_TEXT_PP(1);
152  int ins_c = PG_GETARG_INT32(2);
153  int del_c = PG_GETARG_INT32(3);
154  int sub_c = PG_GETARG_INT32(4);
155  const char *s_data;
156  const char *t_data;
157  int s_bytes,
158  t_bytes;
159 
160  /* Extract a pointer to the actual character data */
161  s_data = VARDATA_ANY(src);
162  t_data = VARDATA_ANY(dst);
163  /* Determine length of each string in bytes */
164  s_bytes = VARSIZE_ANY_EXHDR(src);
165  t_bytes = VARSIZE_ANY_EXHDR(dst);
166 
167  PG_RETURN_INT32(varstr_levenshtein(s_data, s_bytes, t_data, t_bytes,
168  ins_c, del_c, sub_c, false));
169 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:234
#define VARDATA_ANY(PTR)
Definition: postgres.h:347
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
#define PG_RETURN_INT32(x)
Definition: fmgr.h:314
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:273
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:340
Definition: c.h:433
static char Lookahead ( char *  word,
int  how_far 
)
static

Definition at line 318 of file fuzzystrmatch.c.

References idx().

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

Definition at line 257 of file fuzzystrmatch.c.

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

258 {
259  char *str_i = TextDatumGetCString(PG_GETARG_DATUM(0));
260  size_t str_i_len = strlen(str_i);
261  int reqlen;
262  char *metaph;
263 
264  /* return an empty string if we receive one */
265  if (!(str_i_len > 0))
267 
268  if (str_i_len > MAX_METAPHONE_STRLEN)
269  ereport(ERROR,
270  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
271  errmsg("argument exceeds the maximum length of %d bytes",
273 
274  reqlen = PG_GETARG_INT32(1);
275  if (reqlen > MAX_METAPHONE_STRLEN)
276  ereport(ERROR,
277  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
278  errmsg("output exceeds the maximum length of %d bytes",
280 
281  if (!(reqlen > 0))
282  ereport(ERROR,
283  (errcode(ERRCODE_ZERO_LENGTH_CHARACTER_STRING),
284  errmsg("output cannot be empty string")));
285 
286  _metaphone(str_i, reqlen, &metaph);
288 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:234
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:233
int errcode(int sqlerrcode)
Definition: elog.c:575
static void _metaphone(char *word, int max_phonemes, char **phoned_word)
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:122
#define TextDatumGetCString(d)
Definition: builtins.h:92
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:331
text * cstring_to_text(const char *s)
Definition: varlena.c:149
#define MAX_METAPHONE_STRLEN
Definition: fuzzystrmatch.c:72
int errmsg(const char *fmt,...)
Definition: elog.c:797
PG_FUNCTION_INFO_V1 ( levenshtein_with_costs  )
PG_FUNCTION_INFO_V1 ( levenshtein  )
PG_FUNCTION_INFO_V1 ( levenshtein_less_equal_with_costs  )
PG_FUNCTION_INFO_V1 ( levenshtein_less_equal  )
PG_FUNCTION_INFO_V1 ( metaphone  )
PG_FUNCTION_INFO_V1 ( soundex  )
PG_FUNCTION_INFO_V1 ( difference  )
Datum soundex ( PG_FUNCTION_ARGS  )

Definition at line 712 of file fuzzystrmatch.c.

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

713 {
714  char outstr[SOUNDEX_LEN + 1];
715  char *arg;
716 
718 
719  _soundex(arg, outstr);
720 
722 }
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:273
static void _soundex(const char *instr, char *outstr)
#define SOUNDEX_LEN
Definition: fuzzystrmatch.c:54
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:331
text * cstring_to_text(const char *s)
Definition: varlena.c:149
char * text_to_cstring(const text *t)
Definition: varlena.c:182
void * arg
static char soundex_code ( char  letter)
static

Definition at line 60 of file fuzzystrmatch.c.

References soundex_table.

Referenced by _soundex().

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

Variable Documentation

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 114 of file fuzzystrmatch.c.

Referenced by getcode().

PG_MODULE_MAGIC

Definition at line 47 of file fuzzystrmatch.c.

const char* soundex_table = "01230120022455012623010202"
static

Definition at line 57 of file fuzzystrmatch.c.

Referenced by soundex_code().