PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
like_match.c File Reference
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Macros

#define GETCHAR(t)   (t)
 

Functions

static int MatchText (char *t, int tlen, char *p, int plen, pg_locale_t locale, bool locale_is_c)
 

Macro Definition Documentation

#define GETCHAR (   t)    (t)

Definition at line 75 of file like_match.c.

Referenced by MatchText().

Function Documentation

static int MatchText ( char *  t,
int  tlen,
char *  p,
int  plen,
pg_locale_t  locale,
bool  locale_is_c 
)
static

Definition at line 79 of file like_match.c.

References check_stack_depth(), ereport, errcode(), errmsg(), ERROR, GETCHAR, LIKE_ABORT, LIKE_FALSE, LIKE_TRUE, NextByte, and NextChar.

81 {
82  /* Fast path for match-everything pattern */
83  if (plen == 1 && *p == '%')
84  return LIKE_TRUE;
85 
86  /* Since this function recurses, it could be driven to stack overflow */
88 
89  /*
90  * In this loop, we advance by char when matching wildcards (and thus on
91  * recursive entry to this function we are properly char-synced). On other
92  * occasions it is safe to advance by byte, as the text and pattern will
93  * be in lockstep. This allows us to perform all comparisons between the
94  * text and pattern on a byte by byte basis, even for multi-byte
95  * encodings.
96  */
97  while (tlen > 0 && plen > 0)
98  {
99  if (*p == '\\')
100  {
101  /* Next pattern byte must match literally, whatever it is */
102  NextByte(p, plen);
103  /* ... and there had better be one, per SQL standard */
104  if (plen <= 0)
105  ereport(ERROR,
106  (errcode(ERRCODE_INVALID_ESCAPE_SEQUENCE),
107  errmsg("LIKE pattern must not end with escape character")));
108  if (GETCHAR(*p) != GETCHAR(*t))
109  return LIKE_FALSE;
110  }
111  else if (*p == '%')
112  {
113  char firstpat;
114 
115  /*
116  * % processing is essentially a search for a text position at
117  * which the remainder of the text matches the remainder of the
118  * pattern, using a recursive call to check each potential match.
119  *
120  * If there are wildcards immediately following the %, we can skip
121  * over them first, using the idea that any sequence of N _'s and
122  * one or more %'s is equivalent to N _'s and one % (ie, it will
123  * match any sequence of at least N text characters). In this way
124  * we will always run the recursive search loop using a pattern
125  * fragment that begins with a literal character-to-match, thereby
126  * not recursing more than we have to.
127  */
128  NextByte(p, plen);
129 
130  while (plen > 0)
131  {
132  if (*p == '%')
133  NextByte(p, plen);
134  else if (*p == '_')
135  {
136  /* If not enough text left to match the pattern, ABORT */
137  if (tlen <= 0)
138  return LIKE_ABORT;
139  NextChar(t, tlen);
140  NextByte(p, plen);
141  }
142  else
143  break; /* Reached a non-wildcard pattern char */
144  }
145 
146  /*
147  * If we're at end of pattern, match: we have a trailing % which
148  * matches any remaining text string.
149  */
150  if (plen <= 0)
151  return LIKE_TRUE;
152 
153  /*
154  * Otherwise, scan for a text position at which we can match the
155  * rest of the pattern. The first remaining pattern char is known
156  * to be a regular or escaped literal character, so we can compare
157  * the first pattern byte to each text byte to avoid recursing
158  * more than we have to. This fact also guarantees that we don't
159  * have to consider a match to the zero-length substring at the
160  * end of the text.
161  */
162  if (*p == '\\')
163  {
164  if (plen < 2)
165  ereport(ERROR,
166  (errcode(ERRCODE_INVALID_ESCAPE_SEQUENCE),
167  errmsg("LIKE pattern must not end with escape character")));
168  firstpat = GETCHAR(p[1]);
169  }
170  else
171  firstpat = GETCHAR(*p);
172 
173  while (tlen > 0)
174  {
175  if (GETCHAR(*t) == firstpat)
176  {
177  int matched = MatchText(t, tlen, p, plen,
178  locale, locale_is_c);
179 
180  if (matched != LIKE_FALSE)
181  return matched; /* TRUE or ABORT */
182  }
183 
184  NextChar(t, tlen);
185  }
186 
187  /*
188  * End of text with no match, so no point in trying later places
189  * to start matching this pattern.
190  */
191  return LIKE_ABORT;
192  }
193  else if (*p == '_')
194  {
195  /* _ matches any single character, and we know there is one */
196  NextChar(t, tlen);
197  NextByte(p, plen);
198  continue;
199  }
200  else if (GETCHAR(*p) != GETCHAR(*t))
201  {
202  /* non-wildcard pattern char fails to match text char */
203  return LIKE_FALSE;
204  }
205 
206  /*
207  * Pattern and text match, so advance.
208  *
209  * It is safe to use NextByte instead of NextChar here, even for
210  * multi-byte character sets, because we are not following immediately
211  * after a wildcard character. If we are in the middle of a multibyte
212  * character, we must already have matched at least one byte of the
213  * character from both text and pattern; so we cannot get out-of-sync
214  * on character boundaries. And we know that no backend-legal
215  * encoding allows ASCII characters such as '%' to appear as non-first
216  * bytes of characters, so we won't mistakenly detect a new wildcard.
217  */
218  NextByte(t, tlen);
219  NextByte(p, plen);
220  }
221 
222  if (tlen > 0)
223  return LIKE_FALSE; /* end of pattern, but not of text */
224 
225  /*
226  * End of text, but perhaps not of pattern. Match iff the remaining
227  * pattern can match a zero-length string, ie, it's zero or more %'s.
228  */
229  while (plen > 0 && *p == '%')
230  NextByte(p, plen);
231  if (plen <= 0)
232  return LIKE_TRUE;
233 
234  /*
235  * End of text with no match, so no point in trying later places to start
236  * matching this pattern.
237  */
238  return LIKE_ABORT;
239 } /* MatchText() */
#define LIKE_FALSE
Definition: like.c:30
#define GETCHAR(t)
Definition: like_match.c:75
#define LIKE_TRUE
Definition: like.c:29
int errcode(int sqlerrcode)
Definition: elog.c:575
static int MatchText(char *t, int tlen, char *p, int plen, pg_locale_t locale, bool locale_is_c)
Definition: like_match.c:79
#define NextChar(p, plen)
Definition: like.c:143
#define ERROR
Definition: elog.h:43
void check_stack_depth(void)
Definition: postgres.c:3117
#define ereport(elevel, rest)
Definition: elog.h:122
#define NextByte(p, plen)
Definition: like.c:106
#define LIKE_ABORT
Definition: like.c:31
int errmsg(const char *fmt,...)
Definition: elog.c:797