1 /*-------------------------------------------------------------------------
4 * support routines for the lex/flex scanner, used by both the normal
5 * backend as well as the bootstrap backend
7 * Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
8 * Portions Copyright (c) 1994, Regents of the University of California
12 * $PostgreSQL: pgsql/src/backend/parser/scansup.c,v 1.29 2004/12/31 22:00:27 pgsql Exp $
14 *-------------------------------------------------------------------------
20 #include "miscadmin.h"
21 #include "parser/scansup.h"
22 #include "mb/pg_wchar.h"
28 * if the string passed in has escaped codes, map the escape codes to actual
31 * the string returned is palloc'd and should eventually be pfree'd by the
37 scanstr(const char *s)
44 if (s == NULL || s[0] == '\0')
49 newStr = palloc(len + 1); /* string cannot get longer */
51 for (i = 0, j = 0; i < len; i++)
56 * Note: if scanner is working right, unescaped quotes can
57 * only appear in pairs, so there should be another character.
62 else if (s[i] == '\\')
95 s[i + k] >= '0' && s[i + k] <= '7' && k < 3;
97 octVal = (octVal << 3) + (s[i + k] - '0');
99 newStr[j] = ((char) octVal);
117 * downcase_truncate_identifier() --- do appropriate downcasing and
118 * truncation of an unquoted identifier. Optionally warn of truncation.
120 * Returns a palloc'd string containing the adjusted identifier.
122 * Note: in some usages the passed string is not null-terminated.
124 * Note: the API of this function is designed to allow for downcasing
125 * transformations that increase the string length, but we don't yet
126 * support that. If you want to implement it, you'll need to fix
127 * SplitIdentifierString() in utils/adt/varlena.c.
130 downcase_truncate_identifier(const char *ident, int len, bool warn)
135 result = palloc(len + 1);
138 * SQL99 specifies Unicode-aware case normalization, which we don't
139 * yet have the infrastructure for. Instead we use tolower() to
140 * provide a locale-aware translation. However, there are some
141 * locales where this is not right either (eg, Turkish may do strange
142 * things with 'i' and 'I'). Our current compromise is to use
143 * tolower() for characters with the high bit set, and use an
144 * ASCII-only downcasing for 7-bit characters.
146 for (i = 0; i < len; i++)
148 unsigned char ch = (unsigned char) ident[i];
150 if (ch >= 'A' && ch <= 'Z')
152 else if (ch >= 0x80 && isupper(ch))
154 result[i] = (char) ch;
158 if (i >= NAMEDATALEN)
159 truncate_identifier(result, i, warn);
165 * truncate_identifier() --- truncate an identifier to NAMEDATALEN-1 bytes.
167 * The given string is modified in-place, if necessary. A warning is
168 * issued if requested.
170 * We require the caller to pass in the string length since this saves a
171 * strlen() call in some common usages.
174 truncate_identifier(char *ident, int len, bool warn)
176 if (len >= NAMEDATALEN)
178 len = pg_mbcliplen(ident, len, NAMEDATALEN - 1);
181 (errcode(ERRCODE_NAME_TOO_LONG),
182 errmsg("identifier \"%s\" will be truncated to \"%.*s\"",
183 ident, len, ident)));