PostgreSQL Source Code  git master
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 (const char *t, int tlen, const char *p, int plen, pg_locale_t locale, bool locale_is_c)
 

Macro Definition Documentation

◆ GETCHAR

#define GETCHAR (   t)    (t)

Definition at line 76 of file like_match.c.

Function Documentation

◆ MatchText()

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

Definition at line 80 of file like_match.c.

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

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