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-2009, 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.37 2009/01/01 17:23:46 momjian Exp $
14 *-------------------------------------------------------------------------
20 #include "parser/scansup.h"
21 #include "mb/pg_wchar.h"
27 * if the string passed in has escaped codes, map the escape codes to actual
30 * the string returned is palloc'd and should eventually be pfree'd by the
36 scanstr(const char *s)
43 if (s == NULL || s[0] == '\0')
48 newStr = palloc(len + 1); /* string cannot get longer */
50 for (i = 0, j = 0; i < len; i++)
55 * Note: if scanner is working right, unescaped quotes can only
56 * appear in pairs, so there should be another character.
61 else if (s[i] == '\\')
94 s[i + k] >= '0' && s[i + k] <= '7' && k < 3;
96 octVal = (octVal << 3) + (s[i + k] - '0');
98 newStr[j] = ((char) octVal);
116 * downcase_truncate_identifier() --- do appropriate downcasing and
117 * truncation of an unquoted identifier. Optionally warn of truncation.
119 * Returns a palloc'd string containing the adjusted identifier.
121 * Note: in some usages the passed string is not null-terminated.
123 * Note: the API of this function is designed to allow for downcasing
124 * transformations that increase the string length, but we don't yet
125 * support that. If you want to implement it, you'll need to fix
126 * SplitIdentifierString() in utils/adt/varlena.c.
129 downcase_truncate_identifier(const char *ident, int len, bool warn)
134 result = palloc(len + 1);
137 * SQL99 specifies Unicode-aware case normalization, which we don't yet
138 * have the infrastructure for. Instead we use tolower() to provide a
139 * locale-aware translation. However, there are some locales where this
140 * is not right either (eg, Turkish may do strange things with 'i' and
141 * 'I'). Our current compromise is to use tolower() for characters with
142 * the high bit set, and use an ASCII-only downcasing for 7-bit
145 for (i = 0; i < len; i++)
147 unsigned char ch = (unsigned char) ident[i];
149 if (ch >= 'A' && ch <= 'Z')
151 else if (IS_HIGHBIT_SET(ch) && isupper(ch))
153 result[i] = (char) ch;
157 if (i >= NAMEDATALEN)
158 truncate_identifier(result, i, warn);
164 * truncate_identifier() --- truncate an identifier to NAMEDATALEN-1 bytes.
166 * The given string is modified in-place, if necessary. A warning is
167 * issued if requested.
169 * We require the caller to pass in the string length since this saves a
170 * strlen() call in some common usages.
173 truncate_identifier(char *ident, int len, bool warn)
175 if (len >= NAMEDATALEN)
177 len = pg_mbcliplen(ident, len, NAMEDATALEN - 1);
180 (errcode(ERRCODE_NAME_TOO_LONG),
181 errmsg("identifier \"%s\" will be truncated to \"%.*s\"",
182 ident, len, ident)));
188 * scanner_isspace() --- return TRUE if flex scanner considers char whitespace
190 * This should be used instead of the potentially locale-dependent isspace()
191 * function when it's important to match the lexer's behavior.
193 * In principle we might need similar functions for isalnum etc, but for the
194 * moment only isspace seems needed.
197 scanner_isspace(char ch)
199 /* This must match scan.l's list of {space} characters */
200 /* and plpgsql's scan.l as well */