Header And Logo

PostgreSQL
| The world's most advanced open source database.

Defines | Functions

like.c File Reference

#include "postgres.h"
#include <ctype.h>
#include "catalog/pg_collation.h"
#include "mb/pg_wchar.h"
#include "utils/builtins.h"
#include "utils/pg_locale.h"
#include "like_match.c"
Include dependency graph for like.c:

Go to the source code of this file.

Defines

#define LIKE_TRUE   1
#define LIKE_FALSE   0
#define LIKE_ABORT   (-1)
#define NextByte(p, plen)   ((p)++, (plen)--)
#define CHAREQ(p1, p2)   wchareq((p1), (p2))
#define NextChar(p, plen)   do { int __l = pg_mblen(p); (p) +=__l; (plen) -=__l; } while (0)
#define CopyAdvChar(dst, src, srclen)
#define MatchText   MB_MatchText
#define do_like_escape   MB_do_like_escape
#define CHAREQ(p1, p2)   (*(p1) == *(p2))
#define NextChar(p, plen)   NextByte((p), (plen))
#define CopyAdvChar(dst, src, srclen)   (*(dst)++ = *(src)++, (srclen)--)
#define MatchText   SB_MatchText
#define do_like_escape   SB_do_like_escape
#define MATCH_LOWER(t)   SB_lower_char((unsigned char) (t), locale, locale_is_c)
#define NextChar(p, plen)   NextByte((p), (plen))
#define MatchText   SB_IMatchText
#define NextChar(p, plen)   do { (p)++; (plen)--; } while ((plen) > 0 && (*(p) & 0xC0) == 0x80 )
#define MatchText   UTF8_MatchText

Functions

static int SB_MatchText (char *t, int tlen, char *p, int plen, pg_locale_t locale, bool locale_is_c)
static textSB_do_like_escape (text *, text *)
static int MB_MatchText (char *t, int tlen, char *p, int plen, pg_locale_t locale, bool locale_is_c)
static textMB_do_like_escape (text *, text *)
static int UTF8_MatchText (char *t, int tlen, char *p, int plen, pg_locale_t locale, bool locale_is_c)
static int SB_IMatchText (char *t, int tlen, char *p, int plen, pg_locale_t locale, bool locale_is_c)
static int GenericMatchText (char *s, int slen, char *p, int plen)
static int Generic_Text_IC_like (text *str, text *pat, Oid collation)
static int wchareq (char *p1, char *p2)
static char SB_lower_char (unsigned char c, pg_locale_t locale, bool locale_is_c)
Datum namelike (PG_FUNCTION_ARGS)
Datum namenlike (PG_FUNCTION_ARGS)
Datum textlike (PG_FUNCTION_ARGS)
Datum textnlike (PG_FUNCTION_ARGS)
Datum bytealike (PG_FUNCTION_ARGS)
Datum byteanlike (PG_FUNCTION_ARGS)
Datum nameiclike (PG_FUNCTION_ARGS)
Datum nameicnlike (PG_FUNCTION_ARGS)
Datum texticlike (PG_FUNCTION_ARGS)
Datum texticnlike (PG_FUNCTION_ARGS)
Datum like_escape (PG_FUNCTION_ARGS)
Datum like_escape_bytea (PG_FUNCTION_ARGS)

Define Documentation

#define CHAREQ (   p1,
  p2 
)    wchareq((p1), (p2))

Definition at line 124 of file like.c.

#define CHAREQ (   p1,
  p2 
)    (*(p1) == *(p2))

Definition at line 124 of file like.c.

#define CopyAdvChar (   dst,
  src,
  srclen 
)
Value:
do { int __l = pg_mblen(src); \
         (srclen) -= __l; \
         while (__l-- > 0) \
             *(dst)++ = *(src)++; \
       } while (0)

Definition at line 126 of file like.c.

#define CopyAdvChar (   dst,
  src,
  srclen 
)    (*(dst)++ = *(src)++, (srclen)--)

Definition at line 126 of file like.c.

#define do_like_escape   MB_do_like_escape

Definition at line 129 of file like.c.

#define do_like_escape   SB_do_like_escape

Definition at line 129 of file like.c.

#define LIKE_ABORT   (-1)

Definition at line 30 of file like.c.

#define LIKE_FALSE   0

Definition at line 29 of file like.c.

Referenced by MatchText().

#define LIKE_TRUE   1

Definition at line 28 of file like.c.

#define MATCH_LOWER (   t  )     SB_lower_char((unsigned char) (t), locale, locale_is_c)

Definition at line 134 of file like.c.

#define MatchText   SB_IMatchText

Definition at line 144 of file like.c.

#define MatchText   MB_MatchText

Definition at line 144 of file like.c.

#define MatchText   UTF8_MatchText

Definition at line 144 of file like.c.

#define MatchText   SB_MatchText

Definition at line 144 of file like.c.

#define NextByte (   p,
  plen 
)    ((p)++, (plen)--)

Definition at line 105 of file like.c.

Referenced by MatchText().

#define NextChar (   p,
  plen 
)    do { int __l = pg_mblen(p); (p) +=__l; (plen) -=__l; } while (0)

Definition at line 142 of file like.c.

Referenced by MatchText().

#define NextChar (   p,
  plen 
)    NextByte((p), (plen))

Definition at line 142 of file like.c.

#define NextChar (   p,
  plen 
)    do { (p)++; (plen)--; } while ((plen) > 0 && (*(p) & 0xC0) == 0x80 )

Definition at line 142 of file like.c.

#define NextChar (   p,
  plen 
)    NextByte((p), (plen))

Definition at line 142 of file like.c.


Function Documentation

Datum bytealike ( PG_FUNCTION_ARGS   ) 

Definition at line 314 of file like.c.

References PG_GETARG_BYTEA_PP, PG_RETURN_BOOL, SB_MatchText(), VARDATA_ANY, and VARSIZE_ANY_EXHDR.

{
    bytea      *str = PG_GETARG_BYTEA_PP(0);
    bytea      *pat = PG_GETARG_BYTEA_PP(1);
    bool        result;
    char       *s,
               *p;
    int         slen,
                plen;

    s = VARDATA_ANY(str);
    slen = VARSIZE_ANY_EXHDR(str);
    p = VARDATA_ANY(pat);
    plen = VARSIZE_ANY_EXHDR(pat);

    result = (SB_MatchText(s, slen, p, plen, 0, true) == LIKE_TRUE);

    PG_RETURN_BOOL(result);
}

Datum byteanlike ( PG_FUNCTION_ARGS   ) 

Definition at line 335 of file like.c.

References PG_GETARG_BYTEA_PP, PG_RETURN_BOOL, SB_MatchText(), VARDATA_ANY, and VARSIZE_ANY_EXHDR.

{
    bytea      *str = PG_GETARG_BYTEA_PP(0);
    bytea      *pat = PG_GETARG_BYTEA_PP(1);
    bool        result;
    char       *s,
               *p;
    int         slen,
                plen;

    s = VARDATA_ANY(str);
    slen = VARSIZE_ANY_EXHDR(str);
    p = VARDATA_ANY(pat);
    plen = VARSIZE_ANY_EXHDR(pat);

    result = (SB_MatchText(s, slen, p, plen, 0, true) != LIKE_TRUE);

    PG_RETURN_BOOL(result);
}

static int Generic_Text_IC_like ( text str,
text pat,
Oid  collation 
) [inline, static]

Definition at line 161 of file like.c.

References DatumGetTextP, DEFAULT_COLLATION_OID, DirectFunctionCall1Coll(), ereport, errcode(), errhint(), errmsg(), ERROR, GetDatabaseEncoding(), lc_ctype_is_c(), locale, lower(), MB_MatchText(), OidIsValid, pg_database_encoding_max_length(), pg_newlocale_from_collation(), PointerGetDatum, SB_IMatchText(), UTF8_MatchText(), VARDATA, VARDATA_ANY, VARSIZE, and VARSIZE_ANY_EXHDR.

Referenced by nameiclike(), nameicnlike(), texticlike(), and texticnlike().

{
    char       *s,
               *p;
    int         slen,
                plen;

    /*
     * For efficiency reasons, in the single byte case we don't call lower()
     * on the pattern and text, but instead call SB_lower_char on each
     * character.  In the multi-byte case we don't have much choice :-(
     */

    if (pg_database_encoding_max_length() > 1)
    {
        /* lower's result is never packed, so OK to use old macros here */
        pat = DatumGetTextP(DirectFunctionCall1Coll(lower, collation,
                                                    PointerGetDatum(pat)));
        p = VARDATA(pat);
        plen = (VARSIZE(pat) - VARHDRSZ);
        str = DatumGetTextP(DirectFunctionCall1Coll(lower, collation,
                                                    PointerGetDatum(str)));
        s = VARDATA(str);
        slen = (VARSIZE(str) - VARHDRSZ);
        if (GetDatabaseEncoding() == PG_UTF8)
            return UTF8_MatchText(s, slen, p, plen, 0, true);
        else
            return MB_MatchText(s, slen, p, plen, 0, true);
    }
    else
    {
        /*
         * Here we need to prepare locale information for SB_lower_char. This
         * should match the methods used in str_tolower().
         */
        pg_locale_t locale = 0;
        bool        locale_is_c = false;

        if (lc_ctype_is_c(collation))
            locale_is_c = true;
        else if (collation != DEFAULT_COLLATION_OID)
        {
            if (!OidIsValid(collation))
            {
                /*
                 * This typically means that the parser could not resolve a
                 * conflict of implicit collations, so report it that way.
                 */
                ereport(ERROR,
                        (errcode(ERRCODE_INDETERMINATE_COLLATION),
                         errmsg("could not determine which collation to use for ILIKE"),
                         errhint("Use the COLLATE clause to set the collation explicitly.")));
            }
            locale = pg_newlocale_from_collation(collation);
        }

        p = VARDATA_ANY(pat);
        plen = VARSIZE_ANY_EXHDR(pat);
        s = VARDATA_ANY(str);
        slen = VARSIZE_ANY_EXHDR(str);
        return SB_IMatchText(s, slen, p, plen, locale, locale_is_c);
    }
}

static int GenericMatchText ( char *  s,
int  slen,
char *  p,
int  plen 
) [inline, static]

Definition at line 150 of file like.c.

References GetDatabaseEncoding(), MB_MatchText(), pg_database_encoding_max_length(), PG_UTF8, SB_MatchText(), and UTF8_MatchText().

Referenced by namelike(), namenlike(), textlike(), and textnlike().

{
    if (pg_database_encoding_max_length() == 1)
        return SB_MatchText(s, slen, p, plen, 0, true);
    else if (GetDatabaseEncoding() == PG_UTF8)
        return UTF8_MatchText(s, slen, p, plen, 0, true);
    else
        return MB_MatchText(s, slen, p, plen, 0, true);
}

Datum like_escape ( PG_FUNCTION_ARGS   ) 

Definition at line 418 of file like.c.

References MB_do_like_escape(), pg_database_encoding_max_length(), PG_GETARG_TEXT_PP, PG_RETURN_TEXT_P, and SB_do_like_escape().

{
    text       *pat = PG_GETARG_TEXT_PP(0);
    text       *esc = PG_GETARG_TEXT_PP(1);
    text       *result;

    if (pg_database_encoding_max_length() == 1)
        result = SB_do_like_escape(pat, esc);
    else
        result = MB_do_like_escape(pat, esc);

    PG_RETURN_TEXT_P(result);
}

Datum like_escape_bytea ( PG_FUNCTION_ARGS   ) 

Definition at line 437 of file like.c.

References PG_GETARG_BYTEA_PP, PG_RETURN_BYTEA_P, and SB_do_like_escape().

{
    bytea      *pat = PG_GETARG_BYTEA_PP(0);
    bytea      *esc = PG_GETARG_BYTEA_PP(1);
    bytea      *result = SB_do_like_escape((text *) pat, (text *) esc);

    PG_RETURN_BYTEA_P((bytea *) result);
}

static text* MB_do_like_escape ( text ,
text  
) [static]

Referenced by like_escape().

static int MB_MatchText ( char *  t,
int  tlen,
char *  p,
int  plen,
pg_locale_t  locale,
bool  locale_is_c 
) [static]
Datum nameiclike ( PG_FUNCTION_ARGS   ) 
Datum nameicnlike ( PG_FUNCTION_ARGS   ) 
Datum namelike ( PG_FUNCTION_ARGS   ) 

Definition at line 230 of file like.c.

References GenericMatchText(), NameStr, PG_GETARG_NAME, PG_GETARG_TEXT_PP, PG_RETURN_BOOL, VARDATA_ANY, and VARSIZE_ANY_EXHDR.

{
    Name        str = PG_GETARG_NAME(0);
    text       *pat = PG_GETARG_TEXT_PP(1);
    bool        result;
    char       *s,
               *p;
    int         slen,
                plen;

    s = NameStr(*str);
    slen = strlen(s);
    p = VARDATA_ANY(pat);
    plen = VARSIZE_ANY_EXHDR(pat);

    result = (GenericMatchText(s, slen, p, plen) == LIKE_TRUE);

    PG_RETURN_BOOL(result);
}

Datum namenlike ( PG_FUNCTION_ARGS   ) 

Definition at line 251 of file like.c.

References GenericMatchText(), NameStr, PG_GETARG_NAME, PG_GETARG_TEXT_PP, PG_RETURN_BOOL, VARDATA_ANY, and VARSIZE_ANY_EXHDR.

{
    Name        str = PG_GETARG_NAME(0);
    text       *pat = PG_GETARG_TEXT_PP(1);
    bool        result;
    char       *s,
               *p;
    int         slen,
                plen;

    s = NameStr(*str);
    slen = strlen(s);
    p = VARDATA_ANY(pat);
    plen = VARSIZE_ANY_EXHDR(pat);

    result = (GenericMatchText(s, slen, p, plen) != LIKE_TRUE);

    PG_RETURN_BOOL(result);
}

static text* SB_do_like_escape ( text ,
text  
) [static]

Referenced by like_escape(), and like_escape_bytea().

static int SB_IMatchText ( char *  t,
int  tlen,
char *  p,
int  plen,
pg_locale_t  locale,
bool  locale_is_c 
) [static]

Referenced by Generic_Text_IC_like().

static char SB_lower_char ( unsigned char  c,
pg_locale_t  locale,
bool  locale_is_c 
) [static]

Definition at line 92 of file like.c.

References pg_ascii_tolower(), pg_tolower(), and tolower_l.

{
    if (locale_is_c)
        return pg_ascii_tolower(c);
#ifdef HAVE_LOCALE_T
    else if (locale)
        return tolower_l(c, locale);
#endif
    else
        return pg_tolower(c);
}

static int SB_MatchText ( char *  t,
int  tlen,
char *  p,
int  plen,
pg_locale_t  locale,
bool  locale_is_c 
) [static]
Datum texticlike ( PG_FUNCTION_ARGS   ) 

Definition at line 390 of file like.c.

References Generic_Text_IC_like(), PG_GET_COLLATION, PG_GETARG_TEXT_PP, and PG_RETURN_BOOL.

{
    text       *str = PG_GETARG_TEXT_PP(0);
    text       *pat = PG_GETARG_TEXT_PP(1);
    bool        result;

    result = (Generic_Text_IC_like(str, pat, PG_GET_COLLATION()) == LIKE_TRUE);

    PG_RETURN_BOOL(result);
}

Datum texticnlike ( PG_FUNCTION_ARGS   ) 

Definition at line 402 of file like.c.

References Generic_Text_IC_like(), PG_GET_COLLATION, PG_GETARG_TEXT_PP, and PG_RETURN_BOOL.

{
    text       *str = PG_GETARG_TEXT_PP(0);
    text       *pat = PG_GETARG_TEXT_PP(1);
    bool        result;

    result = (Generic_Text_IC_like(str, pat, PG_GET_COLLATION()) != LIKE_TRUE);

    PG_RETURN_BOOL(result);
}

Datum textlike ( PG_FUNCTION_ARGS   ) 

Definition at line 272 of file like.c.

References GenericMatchText(), PG_GETARG_TEXT_PP, PG_RETURN_BOOL, VARDATA_ANY, and VARSIZE_ANY_EXHDR.

{
    text       *str = PG_GETARG_TEXT_PP(0);
    text       *pat = PG_GETARG_TEXT_PP(1);
    bool        result;
    char       *s,
               *p;
    int         slen,
                plen;

    s = VARDATA_ANY(str);
    slen = VARSIZE_ANY_EXHDR(str);
    p = VARDATA_ANY(pat);
    plen = VARSIZE_ANY_EXHDR(pat);

    result = (GenericMatchText(s, slen, p, plen) == LIKE_TRUE);

    PG_RETURN_BOOL(result);
}

Datum textnlike ( PG_FUNCTION_ARGS   ) 

Definition at line 293 of file like.c.

References GenericMatchText(), PG_GETARG_TEXT_PP, PG_RETURN_BOOL, VARDATA_ANY, and VARSIZE_ANY_EXHDR.

{
    text       *str = PG_GETARG_TEXT_PP(0);
    text       *pat = PG_GETARG_TEXT_PP(1);
    bool        result;
    char       *s,
               *p;
    int         slen,
                plen;

    s = VARDATA_ANY(str);
    slen = VARSIZE_ANY_EXHDR(str);
    p = VARDATA_ANY(pat);
    plen = VARSIZE_ANY_EXHDR(pat);

    result = (GenericMatchText(s, slen, p, plen) != LIKE_TRUE);

    PG_RETURN_BOOL(result);
}

static int UTF8_MatchText ( char *  t,
int  tlen,
char *  p,
int  plen,
pg_locale_t  locale,
bool  locale_is_c 
) [static]
static int wchareq ( char *  p1,
char *  p2 
) [inline, static]

Definition at line 56 of file like.c.

References pg_mblen().

{
    int         p1_len;

    /* Optimization:  quickly compare the first byte. */
    if (*p1 != *p2)
        return 0;

    p1_len = pg_mblen(p1);
    if (pg_mblen(p2) != p1_len)
        return 0;

    /* They are the same length */
    while (p1_len--)
    {
        if (*p1++ != *p2++)
            return 0;
    }
    return 1;
}