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

 PG_MODULE_MAGIC_EXT (.name="fuzzystrmatch",.version=PG_VERSION)
 
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

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

◆ Curr_Letter

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

Definition at line 306 of file fuzzystrmatch.c.

◆ End_Phoned_Word

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

Definition at line 338 of file fuzzystrmatch.c.

◆ Isbreak

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

Definition at line 343 of file fuzzystrmatch.c.

◆ isvowel

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

Definition at line 135 of file fuzzystrmatch.c.

◆ Look_Ahead_Letter

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

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

◆ MAKESOFT

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

Definition at line 144 of file fuzzystrmatch.c.

◆ MAX_METAPHONE_STRLEN

#define MAX_METAPHONE_STRLEN   255

Definition at line 75 of file fuzzystrmatch.c.

◆ Next_Letter

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

Definition at line 304 of file fuzzystrmatch.c.

◆ NOCHANGE

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

Definition at line 138 of file fuzzystrmatch.c.

◆ NOGHTOF

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

Definition at line 147 of file fuzzystrmatch.c.

◆ Phone_Len

#define Phone_Len   (p_idx)

Definition at line 340 of file fuzzystrmatch.c.

◆ Phonize

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

Definition at line 336 of file fuzzystrmatch.c.

◆ Prev_Letter

#define Prev_Letter   (Look_Back_Letter(1))

Definition at line 311 of file fuzzystrmatch.c.

◆ SH

#define SH   'X'

Definition at line 106 of file fuzzystrmatch.c.

◆ SOUNDEX_LEN

#define SOUNDEX_LEN   4

Definition at line 57 of file fuzzystrmatch.c.

◆ TH

#define TH   '0'

Definition at line 107 of file fuzzystrmatch.c.

Function Documentation

◆ _metaphone()

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

Definition at line 347 of file fuzzystrmatch.c.

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

727{
728 int count;
729
730 Assert(instr);
731 Assert(outstr);
732
733 /* Skip leading non-alphabetic characters */
734 while (*instr && !isalpha((unsigned char) *instr))
735 ++instr;
736
737 /* If no string left, return all-zeroes buffer */
738 if (!*instr)
739 {
740 memset(outstr, '\0', SOUNDEX_LEN + 1);
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);
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
771 /* And null-terminate */
772 *outstr = '\0';
773}
#define SOUNDEX_LEN
Definition: fuzzystrmatch.c:57
static char soundex_code(char letter)
Definition: fuzzystrmatch.c:63
Assert(PointerIsAligned(start, uint64))

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

Referenced by difference(), and soundex().

◆ difference()

Datum difference ( PG_FUNCTION_ARGS  )

Definition at line 778 of file fuzzystrmatch.c.

779{
780 char sndx1[SOUNDEX_LEN + 1],
781 sndx2[SOUNDEX_LEN + 1];
782 int i,
783 result;
784
787
788 result = 0;
789 for (i = 0; i < SOUNDEX_LEN; i++)
790 {
791 if (sndx1[i] == sndx2[i])
792 result++;
793 }
794
795 PG_RETURN_INT32(result);
796}
#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:77
char * text_to_cstring(const text *t)
Definition: varlena.c:225

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

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

References _codes.

◆ levenshtein()

Datum levenshtein ( PG_FUNCTION_ARGS  )

Definition at line 177 of file fuzzystrmatch.c.

178{
179 text *src = PG_GETARG_TEXT_PP(0);
180 text *dst = PG_GETARG_TEXT_PP(1);
181 const char *s_data;
182 const char *t_data;
183 int s_bytes,
184 t_bytes;
185
186 /* Extract a pointer to the actual character data */
187 s_data = VARDATA_ANY(src);
188 t_data = VARDATA_ANY(dst);
189 /* Determine length of each string in bytes */
190 s_bytes = VARSIZE_ANY_EXHDR(src);
191 t_bytes = VARSIZE_ANY_EXHDR(dst);
192
193 PG_RETURN_INT32(varstr_levenshtein(s_data, s_bytes, t_data, t_bytes,
194 1, 1, 1, false));
195}
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:658
#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 229 of file fuzzystrmatch.c.

230{
231 text *src = PG_GETARG_TEXT_PP(0);
232 text *dst = PG_GETARG_TEXT_PP(1);
233 int max_d = PG_GETARG_INT32(2);
234 const char *s_data;
235 const char *t_data;
236 int s_bytes,
237 t_bytes;
238
239 /* Extract a pointer to the actual character data */
240 s_data = VARDATA_ANY(src);
241 t_data = VARDATA_ANY(dst);
242 /* Determine length of each string in bytes */
243 s_bytes = VARSIZE_ANY_EXHDR(src);
244 t_bytes = VARSIZE_ANY_EXHDR(dst);
245
247 t_data, t_bytes,
248 1, 1, 1,
249 max_d, false));
250}
#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 200 of file fuzzystrmatch.c.

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

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

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

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

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

References idx(), and word().

◆ metaphone()

Datum metaphone ( PG_FUNCTION_ARGS  )

Definition at line 260 of file fuzzystrmatch.c.

261{
262 char *str_i = TextDatumGetCString(PG_GETARG_DATUM(0));
263 size_t str_i_len = strlen(str_i);
264 int reqlen;
265 char *metaph;
266
267 /* return an empty string if we receive one */
268 if (!(str_i_len > 0))
270
271 if (str_i_len > MAX_METAPHONE_STRLEN)
273 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
274 errmsg("argument exceeds the maximum length of %d bytes",
276
277 reqlen = PG_GETARG_INT32(1);
278 if (reqlen > MAX_METAPHONE_STRLEN)
280 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
281 errmsg("output exceeds the maximum length of %d bytes",
283
284 if (!(reqlen > 0))
286 (errcode(ERRCODE_ZERO_LENGTH_CHARACTER_STRING),
287 errmsg("output cannot be empty string")));
288
289 _metaphone(str_i, reqlen, &metaph);
291}
#define TextDatumGetCString(d)
Definition: builtins.h:98
int errcode(int sqlerrcode)
Definition: elog.c:854
int errmsg(const char *fmt,...)
Definition: elog.c:1071
#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:75
text * cstring_to_text(const char *s)
Definition: varlena.c:192

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  )

◆ PG_MODULE_MAGIC_EXT()

PG_MODULE_MAGIC_EXT ( name = "fuzzystrmatch",
version = PG_VERSION 
)

◆ soundex()

Datum soundex ( PG_FUNCTION_ARGS  )

Definition at line 713 of file fuzzystrmatch.c.

714{
715 char outstr[SOUNDEX_LEN + 1];
716 char *arg;
717
719
720 _soundex(arg, outstr);
721
723}
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 63 of file fuzzystrmatch.c.

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

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

Referenced by getcode().

◆ soundex_table

const char* const soundex_table = "01230120022455012623010202"
static

Definition at line 60 of file fuzzystrmatch.c.

Referenced by soundex_code().