Skip to content

Commit 12e1c9e

Browse files
author
Thomas G. Lockhart
committed
Clean up code to remove the explicit backslash cruft.
If the backslash default is still wanted, just pass a backslash to MatchText() for the two-parameter callable routines.
1 parent 371a485 commit 12e1c9e

File tree

1 file changed

+83
-102
lines changed

1 file changed

+83
-102
lines changed

src/backend/utils/adt/like.c

Lines changed: 83 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@
1111
* Portions Copyright (c) 1994, Regents of the University of California
1212
*
1313
* IDENTIFICATION
14-
* $Header: /cvsroot/pgsql/src/backend/utils/adt/like.c,v 1.38 2000/08/06 18:05:41 thomas Exp $
14+
* $Header: /cvsroot/pgsql/src/backend/utils/adt/like.c,v 1.39 2000/08/07 01:45:00 thomas Exp $
1515
*
1616
*-------------------------------------------------------------------------
1717
*/
1818
#include "postgres.h"
19-
19+
#include <ctype.h>
2020
#include "mb/pg_wchar.h"
2121
#include "utils/builtins.h"
2222

@@ -307,59 +307,51 @@ MatchText(pg_wchar * t, int tlen, pg_wchar * p, int plen, char *e)
307307
if ((plen <= 0) || (*t != *p))
308308
return LIKE_FALSE;
309309
}
310-
else
310+
else if (*p == '%')
311311
{
312-
switch (*p)
312+
/* %% is the same as % according to the SQL standard */
313+
/* Advance past all %'s */
314+
while ((plen > 0) && (*p == '%'))
315+
NextChar(p, plen);
316+
/* Trailing percent matches everything. */
317+
if (plen <= 0)
318+
return LIKE_TRUE;
319+
320+
/*
321+
* Otherwise, scan for a text position at which we can
322+
* match the rest of the pattern.
323+
*/
324+
while (tlen > 0)
313325
{
314-
case '\\':
315-
/* Literal match with following character. */
316-
NextChar(p, plen);
317-
/* FALLTHROUGH */
318-
default:
319-
if (*t != *p)
320-
return LIKE_FALSE;
321-
break;
322-
case '_':
323-
/* Match any single character. */
324-
break;
325-
case '%':
326-
/* %% is the same as % according to the SQL standard */
327-
/* Advance past all %'s */
328-
while ((plen > 0) && (*p == '%'))
329-
NextChar(p, plen);
330-
/* Trailing percent matches everything. */
331-
if (plen <= 0)
332-
return LIKE_TRUE;
333-
334-
/*
335-
* Otherwise, scan for a text position at which we can
336-
* match the rest of the pattern.
337-
*/
338-
while (tlen > 0)
339-
{
340-
/*
341-
* Optimization to prevent most recursion: don't
342-
* recurse unless first pattern char might match this
343-
* text char.
344-
*/
345-
if ((*t == *p) || (*p == '\\') || (*p == '_')
346-
|| ((e != NULL) && (*p == *e)))
347-
{
348-
int matched = MatchText(t, tlen, p, plen, e);
349-
350-
if (matched != LIKE_FALSE)
351-
return matched; /* TRUE or ABORT */
352-
}
353-
354-
NextChar(t, tlen);
355-
}
356-
357-
/*
358-
* End of text with no match, so no point in trying later
359-
* places to start matching this pattern.
360-
*/
361-
return LIKE_ABORT;
326+
/*
327+
* Optimization to prevent most recursion: don't
328+
* recurse unless first pattern char might match this
329+
* text char.
330+
*/
331+
if ((*t == *p) || (*p == '_')
332+
|| ((e != NULL) && (*p == *e)))
333+
{
334+
int matched = MatchText(t, tlen, p, plen, e);
335+
336+
if (matched != LIKE_FALSE)
337+
return matched; /* TRUE or ABORT */
338+
}
339+
340+
NextChar(t, tlen);
362341
}
342+
343+
/*
344+
* End of text with no match, so no point in trying later
345+
* places to start matching this pattern.
346+
*/
347+
return LIKE_ABORT;
348+
}
349+
else if ((*p != '_') && (*t != *p))
350+
{
351+
/* Not the single-character wildcard and no explicit match?
352+
* Then time to quit...
353+
*/
354+
return LIKE_FALSE;
363355
}
364356

365357
NextChar(t, tlen);
@@ -404,59 +396,48 @@ MatchTextLower(pg_wchar * t, int tlen, pg_wchar * p, int plen, char *e)
404396
if ((plen <= 0) || (tolower(*t) != tolower(*p)))
405397
return LIKE_FALSE;
406398
}
407-
else
399+
else if (*p == '%')
408400
{
409-
switch (*p)
401+
/* %% is the same as % according to the SQL standard */
402+
/* Advance past all %'s */
403+
while ((plen > 0) && (*p == '%'))
404+
NextChar(p, plen);
405+
/* Trailing percent matches everything. */
406+
if (plen <= 0)
407+
return LIKE_TRUE;
408+
409+
/*
410+
* Otherwise, scan for a text position at which we can
411+
* match the rest of the pattern.
412+
*/
413+
while (tlen > 0)
410414
{
411-
case '\\':
412-
/* Literal match with following character. */
413-
NextChar(p, plen);
414-
/* FALLTHROUGH */
415-
default:
416-
if (tolower(*t) != tolower(*p))
417-
return LIKE_FALSE;
418-
break;
419-
case '_':
420-
/* Match any single character. */
421-
break;
422-
case '%':
423-
/* %% is the same as % according to the SQL standard */
424-
/* Advance past all %'s */
425-
while ((plen > 0) && (*p == '%'))
426-
NextChar(p, plen);
427-
/* Trailing percent matches everything. */
428-
if (plen <= 0)
429-
return LIKE_TRUE;
430-
431-
/*
432-
* Otherwise, scan for a text position at which we can
433-
* match the rest of the pattern.
434-
*/
435-
while (tlen > 0)
436-
{
437-
/*
438-
* Optimization to prevent most recursion: don't
439-
* recurse unless first pattern char might match this
440-
* text char.
441-
*/
442-
if ((tolower(*t) == tolower(*p)) || (*p == '\\') || (*p == '_')
443-
|| ((e != NULL) && (tolower(*p) == tolower(*e))))
444-
{
445-
int matched = MatchText(t, tlen, p, plen, e);
446-
447-
if (matched != LIKE_FALSE)
448-
return matched; /* TRUE or ABORT */
449-
}
450-
451-
NextChar(t, tlen);
452-
}
453-
454-
/*
455-
* End of text with no match, so no point in trying later
456-
* places to start matching this pattern.
457-
*/
458-
return LIKE_ABORT;
415+
/*
416+
* Optimization to prevent most recursion: don't
417+
* recurse unless first pattern char might match this
418+
* text char.
419+
*/
420+
if ((tolower(*t) == tolower(*p)) || (*p == '_')
421+
|| ((e != NULL) && (tolower(*p) == tolower(*e))))
422+
{
423+
int matched = MatchText(t, tlen, p, plen, e);
424+
425+
if (matched != LIKE_FALSE)
426+
return matched; /* TRUE or ABORT */
427+
}
428+
429+
NextChar(t, tlen);
459430
}
431+
432+
/*
433+
* End of text with no match, so no point in trying later
434+
* places to start matching this pattern.
435+
*/
436+
return LIKE_ABORT;
437+
}
438+
else if ((*p != '_') && (tolower(*t) != tolower(*p)))
439+
{
440+
return LIKE_FALSE;
460441
}
461442

462443
NextChar(t, tlen);

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy