PostgreSQL Source Code  git master
kwlookup.h File Reference
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  ScanKeywordList
 

Typedefs

typedef int(* ScanKeywordHashFunc) (const void *key, size_t keylen)
 
typedef struct ScanKeywordList ScanKeywordList
 

Functions

int ScanKeywordLookup (const char *str, const ScanKeywordList *keywords)
 
static const char * GetScanKeyword (int n, const ScanKeywordList *keywords)
 

Typedef Documentation

◆ ScanKeywordHashFunc

typedef int(* ScanKeywordHashFunc) (const void *key, size_t keylen)

Definition at line 18 of file kwlookup.h.

◆ ScanKeywordList

Function Documentation

◆ GetScanKeyword()

static const char* GetScanKeyword ( int  n,
const ScanKeywordList keywords 
)
inlinestatic

Definition at line 39 of file kwlookup.h.

40 {
41  return keywords->kw_string + keywords->kw_offsets[n];
42 }
const uint16 * kw_offsets
Definition: kwlookup.h:28
const char * kw_string
Definition: kwlookup.h:27

References ScanKeywordList::kw_offsets, and ScanKeywordList::kw_string.

Referenced by pg_get_keywords(), plpgsql_yylex(), ScanCKeywordLookup(), and ScanKeywordLookup().

◆ ScanKeywordLookup()

int ScanKeywordLookup ( const char *  str,
const ScanKeywordList keywords 
)

Definition at line 38 of file kwlookup.c.

40 {
41  size_t len;
42  int h;
43  const char *kw;
44 
45  /*
46  * Reject immediately if too long to be any keyword. This saves useless
47  * hashing and downcasing work on long strings.
48  */
49  len = strlen(str);
50  if (len > keywords->max_kw_len)
51  return -1;
52 
53  /*
54  * Compute the hash function. We assume it was generated to produce
55  * case-insensitive results. Since it's a perfect hash, we need only
56  * match to the specific keyword it identifies.
57  */
58  h = keywords->hash(str, len);
59 
60  /* An out-of-range result implies no match */
61  if (h < 0 || h >= keywords->num_keywords)
62  return -1;
63 
64  /*
65  * Compare character-by-character to see if we have a match, applying an
66  * ASCII-only downcasing to the input characters. We must not use
67  * tolower() since it may produce the wrong translation in some locales
68  * (eg, Turkish).
69  */
70  kw = GetScanKeyword(h, keywords);
71  while (*str != '\0')
72  {
73  char ch = *str++;
74 
75  if (ch >= 'A' && ch <= 'Z')
76  ch += 'a' - 'A';
77  if (ch != *kw++)
78  return -1;
79  }
80  if (*kw != '\0')
81  return -1;
82 
83  /* Success! */
84  return h;
85 }
const char * str
static const char * GetScanKeyword(int n, const ScanKeywordList *keywords)
Definition: kwlookup.h:39
const void size_t len
int num_keywords
Definition: kwlookup.h:30
ScanKeywordHashFunc hash
Definition: kwlookup.h:29

References GetScanKeyword(), ScanKeywordList::hash, len, ScanKeywordList::max_kw_len, ScanKeywordList::num_keywords, and str.

Referenced by fmtId(), plpgsql_yylex(), quote_identifier(), and ScanECPGKeywordLookup().