~ubuntu-branches/ubuntu/lucid/postgresql-8.4/lucid-proposed

« back to all changes in this revision

Viewing changes to src/backend/utils/adt/varlena.c

  • Committer: Bazaar Package Importer
  • Author(s): Martin Pitt
  • Date: 2009-07-11 16:59:35 UTC
  • mfrom: (5.1.1 karmic)
  • Revision ID: james.westby@ubuntu.com-20090711165935-jfwin6gfrxf0gfsi
Tags: 8.4.0-2
* debian/libpq-dev.install: Ship catalog/genbki.h. (Closes: #536139)
* debian/rules: Drop --enable-cassert for final release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
8
8
 *
9
9
 *
10
10
 * IDENTIFICATION
11
 
 *        $PostgreSQL: pgsql/src/backend/utils/adt/varlena.c,v 1.170 2009/04/23 07:19:09 heikki Exp $
 
11
 *        $PostgreSQL: pgsql/src/backend/utils/adt/varlena.c,v 1.171 2009/06/11 14:49:04 momjian Exp $
12
12
 *
13
13
 *-------------------------------------------------------------------------
14
14
 */
41
41
        int                     len2;
42
42
        /* Skip table for Boyer-Moore-Horspool search algorithm: */
43
43
        int                     skiptablemask;  /* mask for ANDing with skiptable subscripts */
44
 
        int                     skiptable[256]; /* skip distance for given mismatched char */
 
44
        int                     skiptable[256]; /* skip distance for given mismatched char */
45
45
} TextPositionState;
46
46
 
47
47
#define DatumGetUnknownP(X)                     ((unknown *) PG_DETOAST_DATUM(X))
121
121
 
122
122
        if (tunpacked != t)
123
123
                pfree(tunpacked);
124
 
        
 
124
 
125
125
        return result;
126
126
}
127
127
 
150
150
                dst_len--;
151
151
                if (dst_len >= src_len)
152
152
                        dst_len = src_len;
153
 
                else                                    /* ensure truncation is encoding-safe */
 
153
                else    /* ensure truncation is encoding-safe */
154
154
                        dst_len = pg_mbcliplen(VARDATA_ANY(srcunpacked), src_len, dst_len);
155
155
                memcpy(dst, VARDATA_ANY(srcunpacked), dst_len);
156
156
                dst[dst_len] = '\0';
186
186
        char       *inputText = PG_GETARG_CSTRING(0);
187
187
        char       *tp;
188
188
        char       *rp;
189
 
        int                     byte;
 
189
        int byte;
190
190
        bytea      *result;
191
191
 
192
 
        for (byte = 0, tp = inputText; *tp != '\0'; byte++)
 
192
        for (byte = 0, tp = inputText; *tp != '\0'; byte ++)
193
193
        {
194
194
                if (tp[0] != '\\')
195
195
                        tp++;
212
212
                }
213
213
        }
214
214
 
215
 
        byte += VARHDRSZ;
 
215
        byte      +=VARHDRSZ;
 
216
 
216
217
        result = (bytea *) palloc(byte);
217
218
        SET_VARSIZE(result, byte);
218
219
 
228
229
                                 (tp[3] >= '0' && tp[3] <= '7'))
229
230
                {
230
231
                        byte = VAL(tp[1]);
231
 
                        byte <<= 3;
232
 
                        byte += VAL(tp[2]);
233
 
                        byte <<= 3;
234
 
                        *rp++ = byte + VAL(tp[3]);
 
232
                        byte     <<=3;
 
233
                        byte      +=VAL(tp[2]);
 
234
                        byte     <<=3;
 
235
                        *rp++ = byte +VAL(tp[3]);
 
236
 
235
237
                        tp += 4;
236
238
                }
237
239
                else if ((tp[0] == '\\') &&
920
922
         * searched (t1) and the "needle" is the pattern being sought (t2).
921
923
         *
922
924
         * If the needle is empty or bigger than the haystack then there is no
923
 
         * point in wasting cycles initializing the table.  We also choose not
924
 
         * to use B-M-H for needles of length 1, since the skip table can't
925
 
         * possibly save anything in that case.
 
925
         * point in wasting cycles initializing the table.      We also choose not to
 
926
         * use B-M-H for needles of length 1, since the skip table can't possibly
 
927
         * save anything in that case.
926
928
         */
927
929
        if (len1 >= len2 && len2 > 1)
928
930
        {
929
 
                int             searchlength = len1 - len2;
930
 
                int     skiptablemask;
931
 
                int     last;
932
 
                int     i;
 
931
                int                     searchlength = len1 - len2;
 
932
                int                     skiptablemask;
 
933
                int                     last;
 
934
                int                     i;
933
935
 
934
936
                /*
935
937
                 * First we must determine how much of the skip table to use.  The
936
938
                 * declaration of TextPositionState allows up to 256 elements, but for
937
939
                 * short search problems we don't really want to have to initialize so
938
940
                 * many elements --- it would take too long in comparison to the
939
 
                 * actual search time.  So we choose a useful skip table size based on
 
941
                 * actual search time.  So we choose a useful skip table size based on
940
942
                 * the haystack length minus the needle length.  The closer the needle
941
943
                 * length is to the haystack length the less useful skipping becomes.
942
944
                 *
968
970
                        state->skiptable[i] = len2;
969
971
 
970
972
                /*
971
 
                 * Now examine the needle.  For each character except the last one,
 
973
                 * Now examine the needle.      For each character except the last one,
972
974
                 * set the corresponding table element to the appropriate skip
973
975
                 * distance.  Note that when two characters share the same skip table
974
 
                 * entry, the one later in the needle must determine the skip distance.
 
976
                 * entry, the one later in the needle must determine the skip
 
977
                 * distance.
975
978
                 */
976
979
                last = len2 - 1;
977
980
 
1021
1024
                if (needle_len == 1)
1022
1025
                {
1023
1026
                        /* No point in using B-M-H for a one-character needle */
1024
 
                        char    nchar = *needle;
 
1027
                        char            nchar = *needle;
1025
1028
 
1026
1029
                        hptr = &haystack[start_pos];
1027
1030
                        while (hptr < haystack_end)
1047
1050
                                p = hptr;
1048
1051
                                while (*nptr == *p)
1049
1052
                                {
1050
 
                                        /* Matched it all?  If so, return 1-based position */
 
1053
                                        /* Matched it all?      If so, return 1-based position */
1051
1054
                                        if (nptr == needle)
1052
1055
                                                return p - haystack + 1;
1053
1056
                                        nptr--, p--;
1054
1057
                                }
 
1058
 
1055
1059
                                /*
1056
1060
                                 * No match, so use the haystack char at hptr to decide how
1057
 
                                 * far to advance.  If the needle had any occurrence of that
 
1061
                                 * far to advance.      If the needle had any occurrence of that
1058
1062
                                 * character (or more precisely, one sharing the same
1059
1063
                                 * skiptable entry) before its last character, then we advance
1060
1064
                                 * far enough to align the last such needle character with
1061
 
                                 * that haystack position.  Otherwise we can advance by the
 
1065
                                 * that haystack position.      Otherwise we can advance by the
1062
1066
                                 * whole needle length.
1063
1067
                                 */
1064
1068
                                hptr += state->skiptable[(unsigned char) *hptr & skiptablemask];
1102
1106
                                p = hptr;
1103
1107
                                while (*nptr == *p)
1104
1108
                                {
1105
 
                                        /* Matched it all?  If so, return 1-based position */
 
1109
                                        /* Matched it all?      If so, return 1-based position */
1106
1110
                                        if (nptr == needle)
1107
1111
                                                return p - haystack + 1;
1108
1112
                                        nptr--, p--;
1109
1113
                                }
 
1114
 
1110
1115
                                /*
1111
1116
                                 * No match, so use the haystack char at hptr to decide how
1112
 
                                 * far to advance.  If the needle had any occurrence of that
 
1117
                                 * far to advance.      If the needle had any occurrence of that
1113
1118
                                 * character (or more precisely, one sharing the same
1114
1119
                                 * skiptable entry) before its last character, then we advance
1115
1120
                                 * far enough to align the last such needle character with
1116
 
                                 * that haystack position.  Otherwise we can advance by the
 
1121
                                 * that haystack position.      Otherwise we can advance by the
1117
1122
                                 * whole needle length.
1118
1123
                                 */
1119
1124
                                hptr += state->skiptable[*hptr & skiptablemask];
1764
1769
        bytea      *v = PG_GETARG_BYTEA_PP(0);
1765
1770
        int32           n = PG_GETARG_INT32(1);
1766
1771
        int                     len;
1767
 
        int                     byte;
 
1772
        int byte;
1768
1773
 
1769
1774
        len = VARSIZE_ANY_EXHDR(v);
1770
1775
 
1795
1800
        int                     byteNo,
1796
1801
                                bitNo;
1797
1802
        int                     len;
1798
 
        int                     byte;
 
1803
        int byte;
1799
1804
 
1800
1805
        len = VARSIZE_ANY_EXHDR(v);
1801
1806
 
1810
1815
 
1811
1816
        byte = ((unsigned char *) VARDATA_ANY(v))[byteNo];
1812
1817
 
1813
 
        if (byte & (1 << bitNo))
 
1818
        if (byte &(1 << bitNo))
1814
1819
                PG_RETURN_INT32(1);
1815
1820
        else
1816
1821
                PG_RETURN_INT32(0);