PostgreSQL Source Code  git master
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
fuzzystrmatch.c File Reference
#include "postgres.h"
#include <ctype.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 *const soundex_table = "01230120022455012623010202"
 
static const char _codes [26]
 

Macro Definition Documentation

◆ AFFECTH

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

Definition at line 138 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 310 of file fuzzystrmatch.c.

◆ Curr_Letter

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

Definition at line 303 of file fuzzystrmatch.c.

◆ End_Phoned_Word

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

Definition at line 335 of file fuzzystrmatch.c.

◆ Isbreak

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

Definition at line 340 of file fuzzystrmatch.c.

◆ isvowel

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

Definition at line 132 of file fuzzystrmatch.c.

◆ Look_Ahead_Letter

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

Definition at line 312 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 305 of file fuzzystrmatch.c.

◆ MAKESOFT

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

Definition at line 141 of file fuzzystrmatch.c.

◆ MAX_METAPHONE_STRLEN

#define MAX_METAPHONE_STRLEN   255

Definition at line 72 of file fuzzystrmatch.c.

◆ Next_Letter

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

Definition at line 301 of file fuzzystrmatch.c.

◆ NOCHANGE

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

Definition at line 135 of file fuzzystrmatch.c.

◆ NOGHTOF

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

Definition at line 144 of file fuzzystrmatch.c.

◆ Phone_Len

#define Phone_Len   (p_idx)

Definition at line 337 of file fuzzystrmatch.c.

◆ Phonize

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

Definition at line 333 of file fuzzystrmatch.c.

◆ Prev_Letter

#define Prev_Letter   (Look_Back_Letter(1))

Definition at line 308 of file fuzzystrmatch.c.

◆ SH

#define SH   'X'

Definition at line 103 of file fuzzystrmatch.c.

◆ SOUNDEX_LEN

#define SOUNDEX_LEN   4

Definition at line 54 of file fuzzystrmatch.c.

◆ TH

#define TH   '0'

Definition at line 104 of file fuzzystrmatch.c.

Function Documentation

◆ _metaphone()

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

Definition at line 344 of file fuzzystrmatch.c.

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 } /* END metaphone */
#define ERROR
Definition: elog.h:39
#define elog(elevel,...)
Definition: elog.h:225
#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:1317
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 723 of file fuzzystrmatch.c.

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

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:72
char * text_to_cstring(const text *t)
Definition: varlena.c:217

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

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

References _codes.

◆ levenshtein()

Datum levenshtein ( PG_FUNCTION_ARGS  )

Definition at line 174 of file fuzzystrmatch.c.

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 }
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:692
#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 226 of file fuzzystrmatch.c.

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

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 }

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

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 }

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

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:259

References idx(), and word().

◆ metaphone()

Datum metaphone ( PG_FUNCTION_ARGS  )

Definition at line 257 of file fuzzystrmatch.c.

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 TextDatumGetCString(d)
Definition: builtins.h:98
int errcode(int sqlerrcode)
Definition: elog.c:853
int errmsg(const char *fmt,...)
Definition: elog.c:1070
#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:72
text * cstring_to_text(const char *s)
Definition: varlena.c:184

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

711 {
712  char outstr[SOUNDEX_LEN + 1];
713  char *arg;
714 
716 
717  _soundex(arg, outstr);
718 
720 }
void * arg

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

Referenced by daitch_mokotoff(), daitch_mokotoff_coding(), find_or_create_child_node(), update_leaves(), and update_node().

◆ soundex_code()

static char soundex_code ( char  letter)
static

Definition at line 60 of file fuzzystrmatch.c.

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 *const soundex_table
Definition: fuzzystrmatch.c:57

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

Referenced by getcode().

◆ PG_MODULE_MAGIC

PG_MODULE_MAGIC

Definition at line 47 of file fuzzystrmatch.c.

◆ soundex_table

const char* const soundex_table = "01230120022455012623010202"
static

Definition at line 57 of file fuzzystrmatch.c.

Referenced by soundex_code().