static text * do_like_escape(text *pat, text *esc) { text *result; char *p, *e, *r; int plen, elen; bool afterescape; p = VARDATA_ANY(pat); plen = VARSIZE_ANY_EXHDR(pat); e = VARDATA_ANY(esc); elen = VARSIZE_ANY_EXHDR(esc); /* * Worst-case pattern growth is 2x --- unlikely, but it's hardly worth * trying to calculate the size more accurately than that. */ result = (text *) palloc(plen * 2 + VARHDRSZ); r = VARDATA(result); if (elen == 0) { /* * No escape character is wanted. Double any backslashes in the * pattern to make them act like ordinary characters. */ while (plen > 0) { if (*p == '\\') *r++ = '\\'; CopyAdvChar(r, p, plen); } } else { /* * The specified escape must be only a single character. */ NextChar(e, elen); if (elen != 0) ereport(ERROR, (errcode(ERRCODE_INVALID_ESCAPE_SEQUENCE), errmsg("invalid escape string"), errhint("Escape string must be empty or one character."))); e = VARDATA_ANY(esc); /* * If specified escape is '\', just copy the pattern as-is. */ if (*e == '\\') { memcpy(result, pat, VARSIZE_ANY(pat)); return result; } /* * Otherwise, convert occurrences of the specified escape character to * '\', and double occurrences of '\' --- unless they immediately * follow an escape character! */ afterescape = false; while (plen > 0) { if (CHAREQ(p, e) && !afterescape) { *r++ = '\\'; NextChar(p, plen); afterescape = true; } else if (*p == '\\') { *r++ = '\\'; if (!afterescape) *r++ = '\\'; NextChar(p, plen); afterescape = false; } else { CopyAdvChar(r, p, plen); afterescape = false; } } } SET_VARSIZE(result, r - ((char *) result)); return result; }
static int MatchText(unsigned char *t, int tlen, unsigned char *p, int plen) { /* Fast path for match-everything pattern */ if ((plen == 1) && (*p == '%')) return LIKE_TRUE; while ((tlen > 0) && (plen > 0)) { if (*p == '\\') { /* Next pattern char must match literally, whatever it is */ NextChar(p, plen); if ((plen <= 0) || !CHAREQ(t, p)) return LIKE_FALSE; } else if (*p == '%') { /* %% is the same as % according to the SQL standard */ /* Advance past all %'s */ while ((plen > 0) && (*p == '%')) NextChar(p, plen); /* Trailing percent matches everything. */ if (plen <= 0) return LIKE_TRUE; /* * Otherwise, scan for a text position at which we can match * the rest of the pattern. */ while (tlen > 0) { /* * Optimization to prevent most recursion: don't recurse * unless first pattern char might match this text char. */ if (CHAREQ(t, p) || (*p == '\\') || (*p == '_')) { int matched = MatchText(t, tlen, p, plen); if (matched != LIKE_FALSE) return matched; /* TRUE or ABORT */ } NextChar(t, tlen); } /* * End of text with no match, so no point in trying later * places to start matching this pattern. */ return LIKE_ABORT; } else if ((*p != '_') && !CHAREQ(t, p)) { /* * Not the single-character wildcard and no explicit match? * Then time to quit... */ return LIKE_FALSE; } NextChar(t, tlen); NextChar(p, plen); } if (tlen > 0) return LIKE_FALSE; /* end of pattern, but not of text */ /* End of input string. Do we have matching pattern remaining? */ while ((plen > 0) && (*p == '%')) /* allow multiple %'s at end of * pattern */ NextChar(p, plen); if (plen <= 0) return LIKE_TRUE; /* * End of text with no match, so no point in trying later places to * start matching this pattern. */ return LIKE_ABORT; } /* MatchText() */