1 /*-------------------------------------------------------------------------
4 * Functions for the variable-length built-in types.
6 * Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
11 * $Header: /cvsroot/pgsql/src/backend/utils/adt/varlena.c,v 1.83 2002/04/15 07:54:37 ishii Exp $
13 *-------------------------------------------------------------------------
19 #include "mb/pg_wchar.h"
20 #include "miscadmin.h"
21 #include "utils/builtins.h"
22 #include "utils/pg_locale.h"
24 static int text_cmp(text *arg1, text *arg2);
27 /*****************************************************************************
29 *****************************************************************************/
32 #define VAL(CH) ((CH) - '0')
33 #define DIG(VAL) ((VAL) + '0')
36 * byteain - converts from printable representation of byte array
38 * Non-printable characters must be passed as '\nnn' (octal) and are
39 * converted to internal form. '\' must be passed as '\\'.
40 * elog(ERROR, ...) if bad form.
43 * The input is scaned twice.
44 * The error checking of input is minimal.
47 byteain(PG_FUNCTION_ARGS)
49 char *inputText = PG_GETARG_CSTRING(0);
55 for (byte = 0, tp = inputText; *tp != '\0'; byte++)
59 else if ((tp[0] == '\\') &&
60 (tp[1] >= '0' && tp[1] <= '3') &&
61 (tp[2] >= '0' && tp[2] <= '7') &&
62 (tp[3] >= '0' && tp[3] <= '7'))
64 else if ((tp[0] == '\\') &&
70 * one backslash, not followed by 0 or ### valid octal
72 elog(ERROR, "Bad input string for type bytea");
77 result = (bytea *) palloc(byte);
78 result->vl_len = byte; /* set varlena length */
86 else if ((tp[0] == '\\') &&
87 (tp[1] >= '0' && tp[1] <= '3') &&
88 (tp[2] >= '0' && tp[2] <= '7') &&
89 (tp[3] >= '0' && tp[3] <= '7'))
95 *rp++ = byte + VAL(tp[3]);
98 else if ((tp[0] == '\\') &&
107 * We should never get here. The first pass should not allow
110 elog(ERROR, "Bad input string for type bytea");
114 PG_RETURN_BYTEA_P(result);
118 * byteaout - converts to printable representation of byte array
120 * Non-printable characters are inserted as '\nnn' (octal) and '\' as
123 * NULL vlena should be an error--returning string with NULL for now.
126 byteaout(PG_FUNCTION_ARGS)
128 bytea *vlena = PG_GETARG_BYTEA_P(0);
132 int val; /* holds unprintable chars */
136 len = 1; /* empty string has 1 char */
138 for (i = vlena->vl_len - VARHDRSZ; i != 0; i--, vp++)
142 else if (isprint((unsigned char) *vp))
147 rp = result = (char *) palloc(len);
149 for (i = vlena->vl_len - VARHDRSZ; i != 0; i--, vp++)
156 else if (isprint((unsigned char) *vp))
162 rp[3] = DIG(val & 07);
164 rp[2] = DIG(val & 07);
166 rp[1] = DIG(val & 03);
171 PG_RETURN_CSTRING(result);
176 * textin - converts "..." to internal representation
179 textin(PG_FUNCTION_ARGS)
181 char *inputText = PG_GETARG_CSTRING(0);
189 len = strlen(inputText) + VARHDRSZ;
192 if ((ermsg = pg_verifymbstr(inputText, len - VARHDRSZ)))
193 elog(ERROR, "%s", ermsg);
196 result = (text *) palloc(len);
197 VARATT_SIZEP(result) = len;
199 memcpy(VARDATA(result), inputText, len - VARHDRSZ);
202 convertstr(VARDATA(result), len - VARHDRSZ, 0);
205 PG_RETURN_TEXT_P(result);
209 * textout - converts internal representation to "..."
212 textout(PG_FUNCTION_ARGS)
214 text *t = PG_GETARG_TEXT_P(0);
218 len = VARSIZE(t) - VARHDRSZ;
219 result = (char *) palloc(len + 1);
220 memcpy(result, VARDATA(t), len);
224 convertstr(result, len, 1);
227 PG_RETURN_CSTRING(result);
231 /* ========== PUBLIC ROUTINES ========== */
235 * returns the logical length of a text*
236 * (which is less than the VARSIZE of the text*)
239 textlen(PG_FUNCTION_ARGS)
241 text *t = PG_GETARG_TEXT_P(0);
244 /* optimization for single byte encoding */
245 if (pg_database_encoding_max_length() <= 1)
246 PG_RETURN_INT32(VARSIZE(t) - VARHDRSZ);
249 pg_mbstrlen_with_len(VARDATA(t), VARSIZE(t) - VARHDRSZ)
252 PG_RETURN_INT32(VARSIZE(t) - VARHDRSZ);
258 * returns the physical length of a text*
259 * (which is less than the VARSIZE of the text*)
262 textoctetlen(PG_FUNCTION_ARGS)
264 text *arg = PG_GETARG_TEXT_P(0);
266 PG_RETURN_INT32(VARSIZE(arg) - VARHDRSZ);
271 * takes two text* and returns a text* that is the concatenation of
274 * Rewritten by Sapa, sapa@hq.icb.chel.su. 8-Jul-96.
275 * Updated by Thomas, Thomas.Lockhart@jpl.nasa.gov 1997-07-10.
276 * Allocate space for output in all cases.
277 * XXX - thomas 1997-07-10
280 textcat(PG_FUNCTION_ARGS)
282 text *t1 = PG_GETARG_TEXT_P(0);
283 text *t2 = PG_GETARG_TEXT_P(1);
290 len1 = (VARSIZE(t1) - VARHDRSZ);
294 len2 = (VARSIZE(t2) - VARHDRSZ);
298 len = len1 + len2 + VARHDRSZ;
299 result = (text *) palloc(len);
301 /* Set size of result string... */
302 VARATT_SIZEP(result) = len;
304 /* Fill data field of result string... */
305 ptr = VARDATA(result);
307 memcpy(ptr, VARDATA(t1), len1);
309 memcpy(ptr + len1, VARDATA(t2), len2);
311 PG_RETURN_TEXT_P(result);
316 * Return a substring starting at the specified position.
317 * - thomas 1997-12-31
321 * - starting position (is one-based)
324 * If the starting position is zero or less, then return from the start of the string
325 * adjusting the length to be consistent with the "negative start" per SQL92.
326 * If the length is less than zero, return the remaining string.
328 * Note that the arguments operate on octet length,
329 * so not aware of multi-byte character sets.
331 * Added multi-byte support.
332 * - Tatsuo Ishii 1998-4-21
333 * Changed behavior if starting position is less than one to conform to SQL92 behavior.
334 * Formerly returned the entire string; now returns a portion.
335 * - Thomas Lockhart 1998-12-10
336 * Now uses faster TOAST-slicing interface
337 * - John Gray 2002-02-22
340 text_substr(PG_FUNCTION_ARGS)
343 int32 m = PG_GETARG_INT32(1);
344 int32 n = PG_GETARG_INT32(2);
356 * starting position before the start of the string? then offset into
357 * the string per SQL92 spec...
364 /* Check for m > octet length is made in TOAST access routine */
366 /* m will now become a zero-based starting position */
371 eml = pg_database_encoding_max_length ();
377 sn = (m + n) * eml + 3; /* +3 to avoid mb characters overhanging slice end */
379 sn = n; /* n < 0 is special-cased by heap_tuple_untoast_attr_slice */
383 string = PG_GETARG_TEXT_P_SLICE (0, sm, sn);
387 PG_RETURN_TEXT_P (string);
390 PG_RETURN_NULL(); /* notreached: suppress compiler warning */
394 len = pg_mbstrlen_with_len (VARDATA (string), sn - 3);
395 else /* n < 0 is special-cased; need full string length */
396 len = pg_mbstrlen_with_len (VARDATA (string), VARSIZE(string)-VARHDRSZ);
404 if (((m + n) > len) || (n < 0))
408 for (i = 0; i < m; i++)
410 m = p - VARDATA(string);
411 for (i = 0; i < n; i++)
413 n = p - (VARDATA(string) + m);
415 ret = (text *) palloc(VARHDRSZ + n);
416 VARATT_SIZEP(ret) = VARHDRSZ + n;
418 memcpy(VARDATA(ret), VARDATA(string) + m, n);
420 PG_RETURN_TEXT_P(ret);
426 * Return the position of the specified substring.
427 * Implements the SQL92 POSITION() function.
428 * Ref: A Guide To The SQL Standard, Date & Darwen, 1997
429 * - thomas 1997-07-27
431 * Added multi-byte support.
432 * - Tatsuo Ishii 1998-4-21
435 textpos(PG_FUNCTION_ARGS)
437 text *t1 = PG_GETARG_TEXT_P(0);
438 text *t2 = PG_GETARG_TEXT_P(1);
452 if (VARSIZE(t2) <= VARHDRSZ)
453 PG_RETURN_INT32(1); /* result for empty pattern */
455 len1 = (VARSIZE(t1) - VARHDRSZ);
456 len2 = (VARSIZE(t2) - VARHDRSZ);
458 ps1 = p1 = (pg_wchar *) palloc((len1 + 1) * sizeof(pg_wchar));
459 (void) pg_mb2wchar_with_len((unsigned char *) VARDATA(t1), p1, len1);
460 len1 = pg_wchar_strlen(p1);
461 ps2 = p2 = (pg_wchar *) palloc((len2 + 1) * sizeof(pg_wchar));
462 (void) pg_mb2wchar_with_len((unsigned char *) VARDATA(t2), p2, len2);
463 len2 = pg_wchar_strlen(p2);
470 for (p = 0; p <= px; p++)
473 if ((*p2 == *p1) && (pg_wchar_strncmp(p1, p2, len2) == 0))
475 if ((*p2 == *p1) && (strncmp(p1, p2, len2) == 0))
487 PG_RETURN_INT32(pos);
491 * Comparison function for text strings with given lengths.
492 * Includes locale support, but must copy strings to temporary memory
493 * to allow null-termination for inputs to strcoll().
497 varstr_cmp(char *arg1, int len1, char *arg2, int len2)
504 * Unfortunately, there is no strncoll(), so in the non-C locale
505 * case we have to do some memory copying. This turns out to be
506 * significantly slower, so we optimize the case were LC_COLLATE
509 if (!lc_collate_is_c())
511 a1p = (char *) palloc(len1 + 1);
512 a2p = (char *) palloc(len2 + 1);
514 memcpy(a1p, arg1, len1);
515 *(a1p + len1) = '\0';
516 memcpy(a2p, arg2, len2);
517 *(a2p + len2) = '\0';
519 result = strcoll(a1p, a2p);
529 result = strncmp(a1p, a2p, Min(len1, len2));
530 if ((result == 0) && (len1 != len2))
531 result = (len1 < len2) ? -1 : 1;
539 * Internal comparison function for text strings.
543 text_cmp(text *arg1, text *arg2)
553 len1 = VARSIZE(arg1) - VARHDRSZ;
554 len2 = VARSIZE(arg2) - VARHDRSZ;
556 return varstr_cmp(a1p, len1, a2p, len2);
560 * Comparison functions for text strings.
562 * Note: btree indexes need these routines not to leak memory; therefore,
563 * be careful to free working copies of toasted datums. Most places don't
564 * need to be so careful.
568 texteq(PG_FUNCTION_ARGS)
570 text *arg1 = PG_GETARG_TEXT_P(0);
571 text *arg2 = PG_GETARG_TEXT_P(1);
574 /* fast path for different-length inputs */
575 if (VARSIZE(arg1) != VARSIZE(arg2))
578 result = (text_cmp(arg1, arg2) == 0);
580 PG_FREE_IF_COPY(arg1, 0);
581 PG_FREE_IF_COPY(arg2, 1);
583 PG_RETURN_BOOL(result);
587 textne(PG_FUNCTION_ARGS)
589 text *arg1 = PG_GETARG_TEXT_P(0);
590 text *arg2 = PG_GETARG_TEXT_P(1);
593 /* fast path for different-length inputs */
594 if (VARSIZE(arg1) != VARSIZE(arg2))
597 result = (text_cmp(arg1, arg2) != 0);
599 PG_FREE_IF_COPY(arg1, 0);
600 PG_FREE_IF_COPY(arg2, 1);
602 PG_RETURN_BOOL(result);
606 text_lt(PG_FUNCTION_ARGS)
608 text *arg1 = PG_GETARG_TEXT_P(0);
609 text *arg2 = PG_GETARG_TEXT_P(1);
612 result = (text_cmp(arg1, arg2) < 0);
614 PG_FREE_IF_COPY(arg1, 0);
615 PG_FREE_IF_COPY(arg2, 1);
617 PG_RETURN_BOOL(result);
621 text_le(PG_FUNCTION_ARGS)
623 text *arg1 = PG_GETARG_TEXT_P(0);
624 text *arg2 = PG_GETARG_TEXT_P(1);
627 result = (text_cmp(arg1, arg2) <= 0);
629 PG_FREE_IF_COPY(arg1, 0);
630 PG_FREE_IF_COPY(arg2, 1);
632 PG_RETURN_BOOL(result);
636 text_gt(PG_FUNCTION_ARGS)
638 text *arg1 = PG_GETARG_TEXT_P(0);
639 text *arg2 = PG_GETARG_TEXT_P(1);
642 result = (text_cmp(arg1, arg2) > 0);
644 PG_FREE_IF_COPY(arg1, 0);
645 PG_FREE_IF_COPY(arg2, 1);
647 PG_RETURN_BOOL(result);
651 text_ge(PG_FUNCTION_ARGS)
653 text *arg1 = PG_GETARG_TEXT_P(0);
654 text *arg2 = PG_GETARG_TEXT_P(1);
657 result = (text_cmp(arg1, arg2) >= 0);
659 PG_FREE_IF_COPY(arg1, 0);
660 PG_FREE_IF_COPY(arg2, 1);
662 PG_RETURN_BOOL(result);
666 bttextcmp(PG_FUNCTION_ARGS)
668 text *arg1 = PG_GETARG_TEXT_P(0);
669 text *arg2 = PG_GETARG_TEXT_P(1);
672 result = text_cmp(arg1, arg2);
674 PG_FREE_IF_COPY(arg1, 0);
675 PG_FREE_IF_COPY(arg2, 1);
677 PG_RETURN_INT32(result);
682 text_larger(PG_FUNCTION_ARGS)
684 text *arg1 = PG_GETARG_TEXT_P(0);
685 text *arg2 = PG_GETARG_TEXT_P(1);
688 result = ((text_cmp(arg1, arg2) > 0) ? arg1 : arg2);
690 PG_RETURN_TEXT_P(result);
694 text_smaller(PG_FUNCTION_ARGS)
696 text *arg1 = PG_GETARG_TEXT_P(0);
697 text *arg2 = PG_GETARG_TEXT_P(1);
700 result = ((text_cmp(arg1, arg2) < 0) ? arg1 : arg2);
702 PG_RETURN_TEXT_P(result);
705 /*-------------------------------------------------------------
708 * get the number of bytes contained in an instance of type 'bytea'
709 *-------------------------------------------------------------
712 byteaoctetlen(PG_FUNCTION_ARGS)
714 bytea *v = PG_GETARG_BYTEA_P(0);
716 PG_RETURN_INT32(VARSIZE(v) - VARHDRSZ);
721 * takes two bytea* and returns a bytea* that is the concatenation of
724 * Cloned from textcat and modified as required.
727 byteacat(PG_FUNCTION_ARGS)
729 bytea *t1 = PG_GETARG_BYTEA_P(0);
730 bytea *t2 = PG_GETARG_BYTEA_P(1);
737 len1 = (VARSIZE(t1) - VARHDRSZ);
741 len2 = (VARSIZE(t2) - VARHDRSZ);
745 len = len1 + len2 + VARHDRSZ;
746 result = (bytea *) palloc(len);
748 /* Set size of result string... */
749 VARATT_SIZEP(result) = len;
751 /* Fill data field of result string... */
752 ptr = VARDATA(result);
754 memcpy(ptr, VARDATA(t1), len1);
756 memcpy(ptr + len1, VARDATA(t2), len2);
758 PG_RETURN_BYTEA_P(result);
763 * Return a substring starting at the specified position.
764 * Cloned from text_substr and modified as required.
768 * - starting position (is one-based)
771 * If the starting position is zero or less, then return from the start of the string
772 * adjusting the length to be consistent with the "negative start" per SQL92.
773 * If the length is less than zero, return the remaining string.
777 bytea_substr(PG_FUNCTION_ARGS)
779 int32 m = PG_GETARG_INT32(1);
780 int32 n = PG_GETARG_INT32(2);
783 * starting position before the start of the string? then offset into
784 * the string per SQL92 spec...
792 /* m will now become a zero-based starting position */
795 PG_RETURN_BYTEA_P(PG_GETARG_BYTEA_P_SLICE (0, m, n));
800 * Return the position of the specified substring.
801 * Implements the SQL92 POSITION() function.
802 * Cloned from textpos and modified as required.
805 byteapos(PG_FUNCTION_ARGS)
807 bytea *t1 = PG_GETARG_BYTEA_P(0);
808 bytea *t2 = PG_GETARG_BYTEA_P(1);
817 if (VARSIZE(t2) <= VARHDRSZ)
818 PG_RETURN_INT32(1); /* result for empty pattern */
820 len1 = (VARSIZE(t1) - VARHDRSZ);
821 len2 = (VARSIZE(t2) - VARHDRSZ);
828 for (p = 0; p <= px; p++)
830 if ((*p2 == *p1) && (memcmp(p1, p2, len2) == 0))
838 PG_RETURN_INT32(pos);
841 /*-------------------------------------------------------------
844 * this routine treats "bytea" as an array of bytes.
845 * It returns the Nth byte (a number between 0 and 255).
846 *-------------------------------------------------------------
849 byteaGetByte(PG_FUNCTION_ARGS)
851 bytea *v = PG_GETARG_BYTEA_P(0);
852 int32 n = PG_GETARG_INT32(1);
856 len = VARSIZE(v) - VARHDRSZ;
858 if (n < 0 || n >= len)
859 elog(ERROR, "byteaGetByte: index %d out of range [0..%d]",
862 byte = ((unsigned char *) VARDATA(v))[n];
864 PG_RETURN_INT32(byte);
867 /*-------------------------------------------------------------
870 * This routine treats a "bytea" type like an array of bits.
871 * It returns the value of the Nth bit (0 or 1).
873 *-------------------------------------------------------------
876 byteaGetBit(PG_FUNCTION_ARGS)
878 bytea *v = PG_GETARG_BYTEA_P(0);
879 int32 n = PG_GETARG_INT32(1);
885 len = VARSIZE(v) - VARHDRSZ;
887 if (n < 0 || n >= len * 8)
888 elog(ERROR, "byteaGetBit: index %d out of range [0..%d]",
894 byte = ((unsigned char *) VARDATA(v))[byteNo];
896 if (byte & (1 << bitNo))
902 /*-------------------------------------------------------------
905 * Given an instance of type 'bytea' creates a new one with
906 * the Nth byte set to the given value.
908 *-------------------------------------------------------------
911 byteaSetByte(PG_FUNCTION_ARGS)
913 bytea *v = PG_GETARG_BYTEA_P(0);
914 int32 n = PG_GETARG_INT32(1);
915 int32 newByte = PG_GETARG_INT32(2);
919 len = VARSIZE(v) - VARHDRSZ;
921 if (n < 0 || n >= len)
922 elog(ERROR, "byteaSetByte: index %d out of range [0..%d]",
926 * Make a copy of the original varlena.
928 res = (bytea *) palloc(VARSIZE(v));
929 memcpy((char *) res, (char *) v, VARSIZE(v));
934 ((unsigned char *) VARDATA(res))[n] = newByte;
936 PG_RETURN_BYTEA_P(res);
939 /*-------------------------------------------------------------
942 * Given an instance of type 'bytea' creates a new one with
943 * the Nth bit set to the given value.
945 *-------------------------------------------------------------
948 byteaSetBit(PG_FUNCTION_ARGS)
950 bytea *v = PG_GETARG_BYTEA_P(0);
951 int32 n = PG_GETARG_INT32(1);
952 int32 newBit = PG_GETARG_INT32(2);
960 len = VARSIZE(v) - VARHDRSZ;
962 if (n < 0 || n >= len * 8)
963 elog(ERROR, "byteaSetBit: index %d out of range [0..%d]",
972 if (newBit != 0 && newBit != 1)
973 elog(ERROR, "byteaSetBit: new bit must be 0 or 1");
976 * Make a copy of the original varlena.
978 res = (bytea *) palloc(VARSIZE(v));
979 memcpy((char *) res, (char *) v, VARSIZE(v));
984 oldByte = ((unsigned char *) VARDATA(res))[byteNo];
987 newByte = oldByte & (~(1 << bitNo));
989 newByte = oldByte | (1 << bitNo);
991 ((unsigned char *) VARDATA(res))[byteNo] = newByte;
993 PG_RETURN_BYTEA_P(res);
998 * Converts a text type to a Name type.
1001 text_name(PG_FUNCTION_ARGS)
1003 text *s = PG_GETARG_TEXT_P(0);
1007 len = VARSIZE(s) - VARHDRSZ;
1009 /* Truncate oversize input */
1010 if (len >= NAMEDATALEN)
1011 len = NAMEDATALEN - 1;
1014 printf("text- convert string length %d (%d) ->%d\n",
1015 VARSIZE(s) - VARHDRSZ, VARSIZE(s), len);
1018 result = (Name) palloc(NAMEDATALEN);
1019 memcpy(NameStr(*result), VARDATA(s), len);
1021 /* now null pad to full length... */
1022 while (len < NAMEDATALEN)
1024 *(NameStr(*result) + len) = '\0';
1028 PG_RETURN_NAME(result);
1032 * Converts a Name type to a text type.
1035 name_text(PG_FUNCTION_ARGS)
1037 Name s = PG_GETARG_NAME(0);
1041 len = strlen(NameStr(*s));
1044 printf("text- convert string length %d (%d) ->%d\n",
1045 VARSIZE(s) - VARHDRSZ, VARSIZE(s), len);
1048 result = palloc(VARHDRSZ + len);
1049 VARATT_SIZEP(result) = VARHDRSZ + len;
1050 memcpy(VARDATA(result), NameStr(*s), len);
1052 PG_RETURN_TEXT_P(result);
1057 * textToQualifiedNameList - convert a text object to list of names
1059 * This implements the input parsing needed by nextval() and other
1060 * functions that take a text parameter representing a qualified name.
1061 * We split the name at dots, downcase if not double-quoted, and
1062 * truncate names if they're too long.
1065 textToQualifiedNameList(text *textval, const char *caller)
1072 /* Convert to C string (handles possible detoasting). */
1073 /* Note we rely on being able to modify rawname below. */
1074 rawname = DatumGetCString(DirectFunctionCall1(textout,
1075 PointerGetDatum(textval)));
1077 if (!SplitIdentifierString(rawname, '.', &namelist))
1078 elog(ERROR, "%s: invalid name syntax", caller);
1080 if (namelist == NIL)
1081 elog(ERROR, "%s: invalid name syntax", caller);
1083 foreach(l, namelist)
1085 char *curname = (char *) lfirst(l);
1087 result = lappend(result, makeString(pstrdup(curname)));
1097 * SplitIdentifierString --- parse a string containing identifiers
1099 * This is the guts of textToQualifiedNameList, and is exported for use in
1100 * other situations such as parsing GUC variables. In the GUC case, it's
1101 * important to avoid memory leaks, so the API is designed to minimize the
1102 * amount of stuff that needs to be allocated and freed.
1105 * rawstring: the input string; must be overwritable! On return, it's
1106 * been modified to contain the separated identifiers.
1107 * separator: the separator punctuation expected between identifiers
1108 * (typically '.' or ','). Whitespace may also appear around
1111 * namelist: filled with a palloc'd list of pointers to identifiers within
1112 * rawstring. Caller should freeList() this even on error return.
1114 * Returns TRUE if okay, FALSE if there is a syntax error in the string.
1116 * Note that an empty string is considered okay here, though not in
1117 * textToQualifiedNameList.
1120 SplitIdentifierString(char *rawstring, char separator,
1123 char *nextp = rawstring;
1128 while (isspace((unsigned char) *nextp))
1129 nextp++; /* skip leading whitespace */
1132 return true; /* allow empty string */
1134 /* At the top of the loop, we are at start of a new identifier. */
1143 /* Quoted name --- collapse quote-quote pairs, no downcasing */
1144 curname = nextp + 1;
1147 endp = strchr(nextp + 1, '\"');
1149 return false; /* mismatched quotes */
1150 if (endp[1] != '\"')
1151 break; /* found end of quoted name */
1152 /* Collapse adjacent quotes into one quote, and look again */
1153 memmove(endp, endp+1, strlen(endp));
1156 /* endp now points at the terminating quote */
1161 /* Unquoted name --- extends to separator or whitespace */
1163 while (*nextp && *nextp != separator &&
1164 !isspace((unsigned char) *nextp))
1167 * It's important that this match the identifier downcasing
1168 * code used by backend/parser/scan.l.
1170 if (isupper((unsigned char) *nextp))
1171 *nextp = tolower((unsigned char) *nextp);
1175 if (curname == nextp)
1176 return false; /* empty unquoted name not allowed */
1179 while (isspace((unsigned char) *nextp))
1180 nextp++; /* skip trailing whitespace */
1182 if (*nextp == separator)
1185 while (isspace((unsigned char) *nextp))
1186 nextp++; /* skip leading whitespace for next */
1187 /* we expect another name, so done remains false */
1189 else if (*nextp == '\0')
1192 return false; /* invalid syntax */
1194 /* Now safe to overwrite separator with a null */
1197 /* Truncate name if it's overlength; again, should match scan.l */
1198 curlen = strlen(curname);
1199 if (curlen >= NAMEDATALEN)
1202 curlen = pg_mbcliplen(curname, curlen, NAMEDATALEN - 1);
1203 curname[curlen] = '\0';
1205 curname[NAMEDATALEN - 1] = '\0';
1210 * Finished isolating current name --- add it to list
1212 *namelist = lappend(*namelist, curname);
1214 /* Loop back if we didn't reach end of string */
1221 /*****************************************************************************
1222 * Comparison Functions used for bytea
1224 * Note: btree indexes need these routines not to leak memory; therefore,
1225 * be careful to free working copies of toasted datums. Most places don't
1226 * need to be so careful.
1227 *****************************************************************************/
1230 byteaeq(PG_FUNCTION_ARGS)
1232 bytea *arg1 = PG_GETARG_BYTEA_P(0);
1233 bytea *arg2 = PG_GETARG_BYTEA_P(1);
1238 len1 = VARSIZE(arg1) - VARHDRSZ;
1239 len2 = VARSIZE(arg2) - VARHDRSZ;
1241 /* fast path for different-length inputs */
1245 result = (memcmp(VARDATA(arg1), VARDATA(arg2), len1) == 0);
1247 PG_FREE_IF_COPY(arg1, 0);
1248 PG_FREE_IF_COPY(arg2, 1);
1250 PG_RETURN_BOOL(result);
1254 byteane(PG_FUNCTION_ARGS)
1256 bytea *arg1 = PG_GETARG_BYTEA_P(0);
1257 bytea *arg2 = PG_GETARG_BYTEA_P(1);
1262 len1 = VARSIZE(arg1) - VARHDRSZ;
1263 len2 = VARSIZE(arg2) - VARHDRSZ;
1265 /* fast path for different-length inputs */
1269 result = (memcmp(VARDATA(arg1), VARDATA(arg2), len1) != 0);
1271 PG_FREE_IF_COPY(arg1, 0);
1272 PG_FREE_IF_COPY(arg2, 1);
1274 PG_RETURN_BOOL(result);
1278 bytealt(PG_FUNCTION_ARGS)
1280 bytea *arg1 = PG_GETARG_BYTEA_P(0);
1281 bytea *arg2 = PG_GETARG_BYTEA_P(1);
1286 len1 = VARSIZE(arg1) - VARHDRSZ;
1287 len2 = VARSIZE(arg2) - VARHDRSZ;
1289 cmp = memcmp(VARDATA(arg1), VARDATA(arg2), Min(len1, len2));
1291 PG_FREE_IF_COPY(arg1, 0);
1292 PG_FREE_IF_COPY(arg2, 1);
1294 PG_RETURN_BOOL((cmp < 0) || ((cmp == 0) && (len1 < len2)));
1298 byteale(PG_FUNCTION_ARGS)
1300 bytea *arg1 = PG_GETARG_BYTEA_P(0);
1301 bytea *arg2 = PG_GETARG_BYTEA_P(1);
1306 len1 = VARSIZE(arg1) - VARHDRSZ;
1307 len2 = VARSIZE(arg2) - VARHDRSZ;
1309 cmp = memcmp(VARDATA(arg1), VARDATA(arg2), Min(len1, len2));
1311 PG_FREE_IF_COPY(arg1, 0);
1312 PG_FREE_IF_COPY(arg2, 1);
1314 PG_RETURN_BOOL((cmp < 0) || ((cmp == 0) && (len1 <= len2)));
1318 byteagt(PG_FUNCTION_ARGS)
1320 bytea *arg1 = PG_GETARG_BYTEA_P(0);
1321 bytea *arg2 = PG_GETARG_BYTEA_P(1);
1326 len1 = VARSIZE(arg1) - VARHDRSZ;
1327 len2 = VARSIZE(arg2) - VARHDRSZ;
1329 cmp = memcmp(VARDATA(arg1), VARDATA(arg2), Min(len1, len2));
1331 PG_FREE_IF_COPY(arg1, 0);
1332 PG_FREE_IF_COPY(arg2, 1);
1334 PG_RETURN_BOOL((cmp > 0) || ((cmp == 0) && (len1 > len2)));
1338 byteage(PG_FUNCTION_ARGS)
1340 bytea *arg1 = PG_GETARG_BYTEA_P(0);
1341 bytea *arg2 = PG_GETARG_BYTEA_P(1);
1346 len1 = VARSIZE(arg1) - VARHDRSZ;
1347 len2 = VARSIZE(arg2) - VARHDRSZ;
1349 cmp = memcmp(VARDATA(arg1), VARDATA(arg2), Min(len1, len2));
1351 PG_FREE_IF_COPY(arg1, 0);
1352 PG_FREE_IF_COPY(arg2, 1);
1354 PG_RETURN_BOOL((cmp > 0) || ((cmp == 0) && (len1 >= len2)));
1358 byteacmp(PG_FUNCTION_ARGS)
1360 bytea *arg1 = PG_GETARG_BYTEA_P(0);
1361 bytea *arg2 = PG_GETARG_BYTEA_P(1);
1366 len1 = VARSIZE(arg1) - VARHDRSZ;
1367 len2 = VARSIZE(arg2) - VARHDRSZ;
1369 cmp = memcmp(VARDATA(arg1), VARDATA(arg2), Min(len1, len2));
1370 if ((cmp == 0) && (len1 != len2))
1371 cmp = (len1 < len2) ? -1 : 1;
1373 PG_FREE_IF_COPY(arg1, 0);
1374 PG_FREE_IF_COPY(arg2, 1);
1376 PG_RETURN_INT32(cmp);