| 1 |
/* ----------------------------------------------------------------------- |
| 2 |
* formatting.c |
| 3 |
* |
| 4 |
* src/backend/utils/adt/formatting.c |
| 5 |
* |
| 6 |
* |
| 7 |
* Portions Copyright (c) 1999-2025, PostgreSQL Global Development Group |
| 8 |
* |
| 9 |
* |
| 10 |
* TO_CHAR(); TO_TIMESTAMP(); TO_DATE(); TO_NUMBER(); |
| 11 |
* |
| 12 |
* The PostgreSQL routines for a timestamp/int/float/numeric formatting, |
| 13 |
* inspired by the Oracle TO_CHAR() / TO_DATE() / TO_NUMBER() routines. |
| 14 |
* |
| 15 |
* |
| 16 |
* Cache & Memory: |
| 17 |
* Routines use (itself) internal cache for format pictures. |
| 18 |
* |
| 19 |
* The cache uses a static buffer and is persistent across transactions. If |
| 20 |
* the format-picture is bigger than the cache buffer, the parser is called |
| 21 |
* always. |
| 22 |
* |
| 23 |
* NOTE for Number version: |
| 24 |
* All in this version is implemented as keywords ( => not used |
| 25 |
* suffixes), because a format picture is for *one* item (number) |
| 26 |
* only. It not is as a timestamp version, where each keyword (can) |
| 27 |
* has suffix. |
| 28 |
* |
| 29 |
* NOTE for Timestamp routines: |
| 30 |
* In this module the POSIX 'struct tm' type is *not* used, but rather |
| 31 |
* PgSQL type, which has tm_mon based on one (*non* zero) and |
| 32 |
* year *not* based on 1900, but is used full year number. |
| 33 |
* Module supports AD / BC / AM / PM. |
| 34 |
* |
| 35 |
* Supported types for to_char(): |
| 36 |
* |
| 37 |
* Timestamp, Numeric, int4, int8, float4, float8 |
| 38 |
* |
| 39 |
* Supported types for reverse conversion: |
| 40 |
* |
| 41 |
* Timestamp - to_timestamp() |
| 42 |
* Date - to_date() |
| 43 |
* Numeric - to_number() |
| 44 |
* |
| 45 |
* |
| 46 |
* Karel Zak |
| 47 |
* |
| 48 |
* TODO |
| 49 |
* - better number building (formatting) / parsing, now it isn't |
| 50 |
* ideal code |
| 51 |
* - use Assert() |
| 52 |
* - add support for number spelling |
| 53 |
* - add support for string to string formatting (we must be better |
| 54 |
* than Oracle :-), |
| 55 |
* to_char('Hello', 'X X X X X') -> 'H e l l o' |
| 56 |
* |
| 57 |
* ----------------------------------------------------------------------- |
| 58 |
*/ |
| 59 |
|
| 60 |
#ifdef DEBUG_TO_FROM_CHAR |
| 61 |
#define DEBUG_elog_output DEBUG3 |
| 62 |
#endif |
| 63 |
|
| 64 |
#include "postgres.h" |
| 65 |
|
| 66 |
#include <ctype.h> |
| 67 |
#include <unistd.h> |
| 68 |
#include <math.h> |
| 69 |
#include <float.h> |
| 70 |
#include <limits.h> |
| 71 |
#include <wctype.h> |
| 72 |
|
| 73 |
#ifdef USE_ICU |
| 74 |
#include <unicode/ustring.h> |
| 75 |
#endif |
| 76 |
|
| 77 |
#include "catalog/pg_collation.h" |
| 78 |
#include "catalog/pg_type.h" |
| 79 |
#include "common/int.h" |
| 80 |
#include "common/unicode_case.h" |
| 81 |
#include "common/unicode_category.h" |
| 82 |
#include "mb/pg_wchar.h" |
| 83 |
#include "nodes/miscnodes.h" |
| 84 |
#include "parser/scansup.h" |
| 85 |
#include "utils/builtins.h" |
| 86 |
#include "utils/date.h" |
| 87 |
#include "utils/datetime.h" |
| 88 |
#include "utils/formatting.h" |
| 89 |
#include "utils/memutils.h" |
| 90 |
#include "utils/numeric.h" |
| 91 |
#include "utils/pg_locale.h" |
| 92 |
#include "varatt.h" |
| 93 |
|
| 94 |
|
| 95 |
/* ---------- |
| 96 |
* Routines flags |
| 97 |
* ---------- |
| 98 |
*/ |
| 99 |
#define DCH_FLAG 0x1 /* DATE-TIME flag */ |
| 100 |
#define NUM_FLAG 0x2 /* NUMBER flag */ |
| 101 |
#define STD_FLAG 0x4 /* STANDARD flag */ |
| 102 |
|
| 103 |
/* ---------- |
| 104 |
* KeyWord Index (ascii from position 32 (' ') to 126 (~)) |
| 105 |
* ---------- |
| 106 |
*/ |
| 107 |
#define KeyWord_INDEX_SIZE ('~' - ' ') |
| 108 |
#define KeyWord_INDEX_FILTER(_c) ((_c) <= ' ' || (_c) >= '~' ? 0 : 1) |
| 109 |
|
| 110 |
/* ---------- |
| 111 |
* Maximal length of one node |
| 112 |
* ---------- |
| 113 |
*/ |
| 114 |
#define DCH_MAX_ITEM_SIZ 12 /* max localized day name */ |
| 115 |
#define NUM_MAX_ITEM_SIZ 8 /* roman number (RN has 15 chars) */ |
| 116 |
|
| 117 |
#define MAX_L10N_DATA 80 /* max localized day or month name */ |
| 118 |
|
| 119 |
/* ---------- |
| 120 |
* Format parser structs |
| 121 |
* ---------- |
| 122 |
*/ |
| 123 |
typedef struct |
| 124 |
{ |
| 125 |
const char *name; /* suffix string */ |
| 126 |
int len, /* suffix length */ |
| 127 |
id, /* used in node->suffix */ |
| 128 |
type; /* prefix / postfix */ |
| 129 |
} KeySuffix; |
| 130 |
|
| 131 |
/* ---------- |
| 132 |
* FromCharDateMode |
| 133 |
* ---------- |
| 134 |
* |
| 135 |
* This value is used to nominate one of several distinct (and mutually |
| 136 |
* exclusive) date conventions that a keyword can belong to. |
| 137 |
*/ |
| 138 |
typedef enum |
| 139 |
{ |
| 140 |
FROM_CHAR_DATE_NONE = 0, /* Value does not affect date mode. */ |
| 141 |
FROM_CHAR_DATE_GREGORIAN, /* Gregorian (day, month, year) style date */ |
| 142 |
FROM_CHAR_DATE_ISOWEEK, /* ISO 8601 week date */ |
| 143 |
} FromCharDateMode; |
| 144 |
|
| 145 |
typedef struct |
| 146 |
{ |
| 147 |
const char *name; |
| 148 |
int len; |
| 149 |
int id; |
| 150 |
bool is_digit; |
| 151 |
FromCharDateMode date_mode; |
| 152 |
} KeyWord; |
| 153 |
|
| 154 |
typedef struct |
| 155 |
{ |
| 156 |
uint8 type; /* NODE_TYPE_XXX, see below */ |
| 157 |
char character[MAX_MULTIBYTE_CHAR_LEN + 1]; /* if type is CHAR */ |
| 158 |
uint8 suffix; /* keyword prefix/suffix code, if any */ |
| 159 |
const KeyWord *key; /* if type is ACTION */ |
| 160 |
} FormatNode; |
| 161 |
|
| 162 |
#define NODE_TYPE_END 1 |
| 163 |
#define NODE_TYPE_ACTION 2 |
| 164 |
#define NODE_TYPE_CHAR 3 |
| 165 |
#define NODE_TYPE_SEPARATOR 4 |
| 166 |
#define NODE_TYPE_SPACE 5 |
| 167 |
|
| 168 |
#define SUFFTYPE_PREFIX 1 |
| 169 |
#define SUFFTYPE_POSTFIX 2 |
| 170 |
|
| 171 |
#define CLOCK_24_HOUR 0 |
| 172 |
#define CLOCK_12_HOUR 1 |
| 173 |
|
| 174 |
|
| 175 |
/* ---------- |
| 176 |
* Full months |
| 177 |
* ---------- |
| 178 |
*/ |
| 179 |
static const char *const months_full[] = { |
| 180 |
"January", "February", "March", "April", "May", "June", "July", |
| 181 |
"August", "September", "October", "November", "December", NULL |
| 182 |
}; |
| 183 |
|
| 184 |
static const char *const days_short[] = { |
| 185 |
"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", NULL |
| 186 |
}; |
| 187 |
|
| 188 |
/* ---------- |
| 189 |
* AD / BC |
| 190 |
* ---------- |
| 191 |
* There is no 0 AD. Years go from 1 BC to 1 AD, so we make it |
| 192 |
* positive and map year == -1 to year zero, and shift all negative |
| 193 |
* years up one. For interval years, we just return the year. |
| 194 |
*/ |
| 195 |
#define ADJUST_YEAR(year, is_interval) ((is_interval) ? (year) : ((year) <= 0 ? -((year) - 1) : (year))) |
| 196 |
|
| 197 |
#define A_D_STR "A.D." |
| 198 |
#define a_d_STR "a.d." |
| 199 |
#define AD_STR "AD" |
| 200 |
#define ad_STR "ad" |
| 201 |
|
| 202 |
#define B_C_STR "B.C." |
| 203 |
#define b_c_STR "b.c." |
| 204 |
#define BC_STR "BC" |
| 205 |
#define bc_STR "bc" |
| 206 |
|
| 207 |
/* |
| 208 |
* AD / BC strings for seq_search. |
| 209 |
* |
| 210 |
* These are given in two variants, a long form with periods and a standard |
| 211 |
* form without. |
| 212 |
* |
| 213 |
* The array is laid out such that matches for AD have an even index, and |
| 214 |
* matches for BC have an odd index. So the boolean value for BC is given by |
| 215 |
* taking the array index of the match, modulo 2. |
| 216 |
*/ |
| 217 |
static const char *const adbc_strings[] = {ad_STR, bc_STR, AD_STR, BC_STR, NULL}; |
| 218 |
static const char *const adbc_strings_long[] = {a_d_STR, b_c_STR, A_D_STR, B_C_STR, NULL}; |
| 219 |
|
| 220 |
/* ---------- |
| 221 |
* AM / PM |
| 222 |
* ---------- |
| 223 |
*/ |
| 224 |
#define A_M_STR "A.M." |
| 225 |
#define a_m_STR "a.m." |
| 226 |
#define AM_STR "AM" |
| 227 |
#define am_STR "am" |
| 228 |
|
| 229 |
#define P_M_STR "P.M." |
| 230 |
#define p_m_STR "p.m." |
| 231 |
#define PM_STR "PM" |
| 232 |
#define pm_STR "pm" |
| 233 |
|
| 234 |
/* |
| 235 |
* AM / PM strings for seq_search. |
| 236 |
* |
| 237 |
* These are given in two variants, a long form with periods and a standard |
| 238 |
* form without. |
| 239 |
* |
| 240 |
* The array is laid out such that matches for AM have an even index, and |
| 241 |
* matches for PM have an odd index. So the boolean value for PM is given by |
| 242 |
* taking the array index of the match, modulo 2. |
| 243 |
*/ |
| 244 |
static const char *const ampm_strings[] = {am_STR, pm_STR, AM_STR, PM_STR, NULL}; |
| 245 |
static const char *const ampm_strings_long[] = {a_m_STR, p_m_STR, A_M_STR, P_M_STR, NULL}; |
| 246 |
|
| 247 |
/* ---------- |
| 248 |
* Months in roman-numeral |
| 249 |
* (Must be in reverse order for seq_search (in FROM_CHAR), because |
| 250 |
* 'VIII' must have higher precedence than 'V') |
| 251 |
* ---------- |
| 252 |
*/ |
| 253 |
static const char *const rm_months_upper[] = |
| 254 |
{"XII", "XI", "X", "IX", "VIII", "VII", "VI", "V", "IV", "III", "II", "I", NULL}; |
| 255 |
|
| 256 |
static const char *const rm_months_lower[] = |
| 257 |
{"xii", "xi", "x", "ix", "viii", "vii", "vi", "v", "iv", "iii", "ii", "i", NULL}; |
| 258 |
|
| 259 |
/* ---------- |
| 260 |
* Roman numerals |
| 261 |
* ---------- |
| 262 |
*/ |
| 263 |
static const char *const rm1[] = {"I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX", NULL}; |
| 264 |
static const char *const rm10[] = {"X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC", NULL}; |
| 265 |
static const char *const rm100[] = {"C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM", NULL}; |
| 266 |
|
| 267 |
/* |
| 268 |
* MACRO: Check if the current and next characters form a valid subtraction |
| 269 |
* combination for roman numerals. |
| 270 |
*/ |
| 271 |
#define IS_VALID_SUB_COMB(curr, next) \ |
| 272 |
(((curr) == 'I' && ((next) == 'V' || (next) == 'X')) || \ |
| 273 |
((curr) == 'X' && ((next) == 'L' || (next) == 'C')) || \ |
| 274 |
((curr) == 'C' && ((next) == 'D' || (next) == 'M'))) |
| 275 |
|
| 276 |
/* |
| 277 |
* MACRO: Roman numeral value, or 0 if character isn't a roman numeral. |
| 278 |
*/ |
| 279 |
#define ROMAN_VAL(r) \ |
| 280 |
((r) == 'I' ? 1 : \ |
| 281 |
(r) == 'V' ? 5 : \ |
| 282 |
(r) == 'X' ? 10 : \ |
| 283 |
(r) == 'L' ? 50 : \ |
| 284 |
(r) == 'C' ? 100 : \ |
| 285 |
(r) == 'D' ? 500 : \ |
| 286 |
(r) == 'M' ? 1000 : 0) |
| 287 |
|
| 288 |
/* |
| 289 |
* 'MMMDCCCLXXXVIII' (3888) is the longest valid roman numeral (15 characters). |
| 290 |
*/ |
| 291 |
#define MAX_ROMAN_LEN 15 |
| 292 |
|
| 293 |
/* ---------- |
| 294 |
* Ordinal postfixes |
| 295 |
* ---------- |
| 296 |
*/ |
| 297 |
static const char *const numTH[] = {"ST", "ND", "RD", "TH", NULL}; |
| 298 |
static const char *const numth[] = {"st", "nd", "rd", "th", NULL}; |
| 299 |
|
| 300 |
/* ---------- |
| 301 |
* Flags & Options: |
| 302 |
* ---------- |
| 303 |
*/ |
| 304 |
#define TH_UPPER 1 |
| 305 |
#define TH_LOWER 2 |
| 306 |
|
| 307 |
/* ---------- |
| 308 |
* Number description struct |
| 309 |
* ---------- |
| 310 |
*/ |
| 311 |
typedef struct |
| 312 |
{ |
| 313 |
int pre, /* (count) numbers before decimal */ |
| 314 |
post, /* (count) numbers after decimal */ |
| 315 |
lsign, /* want locales sign */ |
| 316 |
flag, /* number parameters */ |
| 317 |
pre_lsign_num, /* tmp value for lsign */ |
| 318 |
multi, /* multiplier for 'V' */ |
| 319 |
zero_start, /* position of first zero */ |
| 320 |
zero_end, /* position of last zero */ |
| 321 |
need_locale; /* needs it locale */ |
| 322 |
} NUMDesc; |
| 323 |
|
| 324 |
/* ---------- |
| 325 |
* Flags for NUMBER version |
| 326 |
* ---------- |
| 327 |
*/ |
| 328 |
#define NUM_F_DECIMAL (1 << 1) |
| 329 |
#define NUM_F_LDECIMAL (1 << 2) |
| 330 |
#define NUM_F_ZERO (1 << 3) |
| 331 |
#define NUM_F_BLANK (1 << 4) |
| 332 |
#define NUM_F_FILLMODE (1 << 5) |
| 333 |
#define NUM_F_LSIGN (1 << 6) |
| 334 |
#define NUM_F_BRACKET (1 << 7) |
| 335 |
#define NUM_F_MINUS (1 << 8) |
| 336 |
#define NUM_F_PLUS (1 << 9) |
| 337 |
#define NUM_F_ROMAN (1 << 10) |
| 338 |
#define NUM_F_MULTI (1 << 11) |
| 339 |
#define NUM_F_PLUS_POST (1 << 12) |
| 340 |
#define NUM_F_MINUS_POST (1 << 13) |
| 341 |
#define NUM_F_EEEE (1 << 14) |
| 342 |
|
| 343 |
#define NUM_LSIGN_PRE (-1) |
| 344 |
#define NUM_LSIGN_POST 1 |
| 345 |
#define NUM_LSIGN_NONE 0 |
| 346 |
|
| 347 |
/* ---------- |
| 348 |
* Tests |
| 349 |
* ---------- |
| 350 |
*/ |
| 351 |
#define IS_DECIMAL(_f) ((_f)->flag & NUM_F_DECIMAL) |
| 352 |
#define IS_LDECIMAL(_f) ((_f)->flag & NUM_F_LDECIMAL) |
| 353 |
#define IS_ZERO(_f) ((_f)->flag & NUM_F_ZERO) |
| 354 |
#define IS_BLANK(_f) ((_f)->flag & NUM_F_BLANK) |
| 355 |
#define IS_FILLMODE(_f) ((_f)->flag & NUM_F_FILLMODE) |
| 356 |
#define IS_BRACKET(_f) ((_f)->flag & NUM_F_BRACKET) |
| 357 |
#define IS_MINUS(_f) ((_f)->flag & NUM_F_MINUS) |
| 358 |
#define IS_LSIGN(_f) ((_f)->flag & NUM_F_LSIGN) |
| 359 |
#define IS_PLUS(_f) ((_f)->flag & NUM_F_PLUS) |
| 360 |
#define IS_ROMAN(_f) ((_f)->flag & NUM_F_ROMAN) |
| 361 |
#define IS_MULTI(_f) ((_f)->flag & NUM_F_MULTI) |
| 362 |
#define IS_EEEE(_f) ((_f)->flag & NUM_F_EEEE) |
| 363 |
|
| 364 |
/* ---------- |
| 365 |
* Format picture cache |
| 366 |
* |
| 367 |
* We will cache datetime format pictures up to DCH_CACHE_SIZE bytes long; |
| 368 |
* likewise number format pictures up to NUM_CACHE_SIZE bytes long. |
| 369 |
* |
| 370 |
* For simplicity, the cache entries are fixed-size, so they allow for the |
| 371 |
* worst case of a FormatNode for each byte in the picture string. |
| 372 |
* |
| 373 |
* The CACHE_SIZE constants are computed to make sizeof(DCHCacheEntry) and |
| 374 |
* sizeof(NUMCacheEntry) be powers of 2, or just less than that, so that |
| 375 |
* we don't waste too much space by palloc'ing them individually. Be sure |
| 376 |
* to adjust those macros if you add fields to those structs. |
| 377 |
* |
| 378 |
* The max number of entries in each cache is DCH_CACHE_ENTRIES |
| 379 |
* resp. NUM_CACHE_ENTRIES. |
| 380 |
* ---------- |
| 381 |
*/ |
| 382 |
#define DCH_CACHE_OVERHEAD \ |
| 383 |
MAXALIGN(sizeof(bool) + sizeof(int)) |
| 384 |
#define NUM_CACHE_OVERHEAD \ |
| 385 |
MAXALIGN(sizeof(bool) + sizeof(int) + sizeof(NUMDesc)) |
| 386 |
|
| 387 |
#define DCH_CACHE_SIZE \ |
| 388 |
((2048 - DCH_CACHE_OVERHEAD) / (sizeof(FormatNode) + sizeof(char)) - 1) |
| 389 |
#define NUM_CACHE_SIZE \ |
| 390 |
((1024 - NUM_CACHE_OVERHEAD) / (sizeof(FormatNode) + sizeof(char)) - 1) |
| 391 |
|
| 392 |
#define DCH_CACHE_ENTRIES 20 |
| 393 |
#define NUM_CACHE_ENTRIES 20 |
| 394 |
|
| 395 |
typedef struct |
| 396 |
{ |
| 397 |
FormatNode format[DCH_CACHE_SIZE + 1]; |
| 398 |
char str[DCH_CACHE_SIZE + 1]; |
| 399 |
bool std; |
| 400 |
bool valid; |
| 401 |
int age; |
| 402 |
} DCHCacheEntry; |
| 403 |
|
| 404 |
typedef struct |
| 405 |
{ |
| 406 |
FormatNode format[NUM_CACHE_SIZE + 1]; |
| 407 |
char str[NUM_CACHE_SIZE + 1]; |
| 408 |
bool valid; |
| 409 |
int age; |
| 410 |
NUMDesc Num; |
| 411 |
} NUMCacheEntry; |
| 412 |
|
| 413 |
/* global cache for date/time format pictures */ |
| 414 |
static DCHCacheEntry *DCHCache[DCH_CACHE_ENTRIES]; |
| 415 |
static int n_DCHCache = 0; /* current number of entries */ |
| 416 |
static int DCHCounter = 0; /* aging-event counter */ |
| 417 |
|
| 418 |
/* global cache for number format pictures */ |
| 419 |
static NUMCacheEntry *NUMCache[NUM_CACHE_ENTRIES]; |
| 420 |
static int n_NUMCache = 0; /* current number of entries */ |
| 421 |
static int NUMCounter = 0; /* aging-event counter */ |
| 422 |
|
| 423 |
/* ---------- |
| 424 |
* For char->date/time conversion |
| 425 |
* ---------- |
| 426 |
*/ |
| 427 |
typedef struct |
| 428 |
{ |
| 429 |
FromCharDateMode mode; |
| 430 |
int hh, |
| 431 |
pm, |
| 432 |
mi, |
| 433 |
ss, |
| 434 |
ssss, |
| 435 |
d, /* stored as 1-7, Sunday = 1, 0 means missing */ |
| 436 |
dd, |
| 437 |
ddd, |
| 438 |
mm, |
| 439 |
ms, |
| 440 |
year, |
| 441 |
bc, |
| 442 |
ww, |
| 443 |
w, |
| 444 |
cc, |
| 445 |
j, |
| 446 |
us, |
| 447 |
yysz, /* is it YY or YYYY ? */ |
| 448 |
clock, /* 12 or 24 hour clock? */ |
| 449 |
tzsign, /* +1, -1, or 0 if no TZH/TZM fields */ |
| 450 |
tzh, |
| 451 |
tzm, |
| 452 |
ff; /* fractional precision */ |
| 453 |
bool has_tz; /* was there a TZ field? */ |
| 454 |
int gmtoffset; /* GMT offset of fixed-offset zone abbrev */ |
| 455 |
pg_tz *tzp; /* pg_tz for dynamic abbrev */ |
| 456 |
char *abbrev; /* dynamic abbrev */ |
| 457 |
} TmFromChar; |
| 458 |
|
| 459 |
#define ZERO_tmfc(_X) memset(_X, 0, sizeof(TmFromChar)) |
| 460 |
|
| 461 |
struct fmt_tz /* do_to_timestamp's timezone info output */ |
| 462 |
{ |
| 463 |
bool has_tz; /* was there any TZ/TZH/TZM field? */ |
| 464 |
int gmtoffset; /* GMT offset in seconds */ |
| 465 |
}; |
| 466 |
|
| 467 |
/* ---------- |
| 468 |
* Debug |
| 469 |
* ---------- |
| 470 |
*/ |
| 471 |
#ifdef DEBUG_TO_FROM_CHAR |
| 472 |
#define DEBUG_TMFC(_X) \ |
| 473 |
elog(DEBUG_elog_output, "TMFC:\nmode %d\nhh %d\npm %d\nmi %d\nss %d\nssss %d\nd %d\ndd %d\nddd %d\nmm %d\nms: %d\nyear %d\nbc %d\nww %d\nw %d\ncc %d\nj %d\nus: %d\nyysz: %d\nclock: %d", \ |
| 474 |
(_X)->mode, (_X)->hh, (_X)->pm, (_X)->mi, (_X)->ss, (_X)->ssss, \ |
| 475 |
(_X)->d, (_X)->dd, (_X)->ddd, (_X)->mm, (_X)->ms, (_X)->year, \ |
| 476 |
(_X)->bc, (_X)->ww, (_X)->w, (_X)->cc, (_X)->j, (_X)->us, \ |
| 477 |
(_X)->yysz, (_X)->clock) |
| 478 |
#define DEBUG_TM(_X) \ |
| 479 |
elog(DEBUG_elog_output, "TM:\nsec %d\nyear %d\nmin %d\nwday %d\nhour %d\nyday %d\nmday %d\nnisdst %d\nmon %d\n",\ |
| 480 |
(_X)->tm_sec, (_X)->tm_year,\ |
| 481 |
(_X)->tm_min, (_X)->tm_wday, (_X)->tm_hour, (_X)->tm_yday,\ |
| 482 |
(_X)->tm_mday, (_X)->tm_isdst, (_X)->tm_mon) |
| 483 |
#else |
| 484 |
#define DEBUG_TMFC(_X) |
| 485 |
#define DEBUG_TM(_X) |
| 486 |
#endif |
| 487 |
|
| 488 |
/* ---------- |
| 489 |
* Datetime to char conversion |
| 490 |
* |
| 491 |
* To support intervals as well as timestamps, we use a custom "tm" struct |
| 492 |
* that is almost like struct pg_tm, but has a 64-bit tm_hour field. |
| 493 |
* We omit the tm_isdst and tm_zone fields, which are not used here. |
| 494 |
* ---------- |
| 495 |
*/ |
| 496 |
struct fmt_tm |
| 497 |
{ |
| 498 |
int tm_sec; |
| 499 |
int tm_min; |
| 500 |
int64 tm_hour; |
| 501 |
int tm_mday; |
| 502 |
int tm_mon; |
| 503 |
int tm_year; |
| 504 |
int tm_wday; |
| 505 |
int tm_yday; |
| 506 |
long int tm_gmtoff; |
| 507 |
}; |
| 508 |
|
| 509 |
typedef struct TmToChar |
| 510 |
{ |
| 511 |
struct fmt_tm tm; /* almost the classic 'tm' struct */ |
| 512 |
fsec_t fsec; /* fractional seconds */ |
| 513 |
const char *tzn; /* timezone */ |
| 514 |
} TmToChar; |
| 515 |
|
| 516 |
#define tmtcTm(_X) (&(_X)->tm) |
| 517 |
#define tmtcTzn(_X) ((_X)->tzn) |
| 518 |
#define tmtcFsec(_X) ((_X)->fsec) |
| 519 |
|
| 520 |
/* Note: this is used to copy pg_tm to fmt_tm, so not quite a bitwise copy */ |
| 521 |
#define COPY_tm(_DST, _SRC) \ |
| 522 |
do { \ |
| 523 |
(_DST)->tm_sec = (_SRC)->tm_sec; \ |
| 524 |
(_DST)->tm_min = (_SRC)->tm_min; \ |
| 525 |
(_DST)->tm_hour = (_SRC)->tm_hour; \ |
| 526 |
(_DST)->tm_mday = (_SRC)->tm_mday; \ |
| 527 |
(_DST)->tm_mon = (_SRC)->tm_mon; \ |
| 528 |
(_DST)->tm_year = (_SRC)->tm_year; \ |
| 529 |
(_DST)->tm_wday = (_SRC)->tm_wday; \ |
| 530 |
(_DST)->tm_yday = (_SRC)->tm_yday; \ |
| 531 |
(_DST)->tm_gmtoff = (_SRC)->tm_gmtoff; \ |
| 532 |
} while(0) |
| 533 |
|
| 534 |
/* Caution: this is used to zero both pg_tm and fmt_tm structs */ |
| 535 |
#define ZERO_tm(_X) \ |
| 536 |
do { \ |
| 537 |
memset(_X, 0, sizeof(*(_X))); \ |
| 538 |
(_X)->tm_mday = (_X)->tm_mon = 1; \ |
| 539 |
} while(0) |
| 540 |
|
| 541 |
#define ZERO_tmtc(_X) \ |
| 542 |
do { \ |
| 543 |
ZERO_tm( tmtcTm(_X) ); \ |
| 544 |
tmtcFsec(_X) = 0; \ |
| 545 |
tmtcTzn(_X) = NULL; \ |
| 546 |
} while(0) |
| 547 |
|
| 548 |
/* |
| 549 |
* to_char(time) appears to to_char() as an interval, so this check |
| 550 |
* is really for interval and time data types. |
| 551 |
*/ |
| 552 |
#define INVALID_FOR_INTERVAL \ |
| 553 |
do { \ |
| 554 |
if (is_interval) \ |
| 555 |
ereport(ERROR, \ |
| 556 |
(errcode(ERRCODE_INVALID_DATETIME_FORMAT), \ |
| 557 |
errmsg("invalid format specification for an interval value"), \ |
| 558 |
errhint("Intervals are not tied to specific calendar dates."))); \ |
| 559 |
} while(0) |
| 560 |
|
| 561 |
/***************************************************************************** |
| 562 |
* KeyWord definitions |
| 563 |
*****************************************************************************/ |
| 564 |
|
| 565 |
/* ---------- |
| 566 |
* Suffixes (FormatNode.suffix is an OR of these codes) |
| 567 |
* ---------- |
| 568 |
*/ |
| 569 |
#define DCH_S_FM 0x01 |
| 570 |
#define DCH_S_TH 0x02 |
| 571 |
#define DCH_S_th 0x04 |
| 572 |
#define DCH_S_SP 0x08 |
| 573 |
#define DCH_S_TM 0x10 |
| 574 |
|
| 575 |
/* ---------- |
| 576 |
* Suffix tests |
| 577 |
* ---------- |
| 578 |
*/ |
| 579 |
#define S_THth(_s) ((((_s) & DCH_S_TH) || ((_s) & DCH_S_th)) ? 1 : 0) |
| 580 |
#define S_TH(_s) (((_s) & DCH_S_TH) ? 1 : 0) |
| 581 |
#define S_th(_s) (((_s) & DCH_S_th) ? 1 : 0) |
| 582 |
#define S_TH_TYPE(_s) (((_s) & DCH_S_TH) ? TH_UPPER : TH_LOWER) |
| 583 |
|
| 584 |
/* Oracle toggles FM behavior, we don't; see docs. */ |
| 585 |
#define S_FM(_s) (((_s) & DCH_S_FM) ? 1 : 0) |
| 586 |
#define S_SP(_s) (((_s) & DCH_S_SP) ? 1 : 0) |
| 587 |
#define S_TM(_s) (((_s) & DCH_S_TM) ? 1 : 0) |
| 588 |
|
| 589 |
/* ---------- |
| 590 |
* Suffixes definition for DATE-TIME TO/FROM CHAR |
| 591 |
* ---------- |
| 592 |
*/ |
| 593 |
#define TM_SUFFIX_LEN 2 |
| 594 |
|
| 595 |
static const KeySuffix DCH_suff[] = { |
| 596 |
{"FM", 2, DCH_S_FM, SUFFTYPE_PREFIX}, |
| 597 |
{"fm", 2, DCH_S_FM, SUFFTYPE_PREFIX}, |
| 598 |
{"TM", TM_SUFFIX_LEN, DCH_S_TM, SUFFTYPE_PREFIX}, |
| 599 |
{"tm", 2, DCH_S_TM, SUFFTYPE_PREFIX}, |
| 600 |
{"TH", 2, DCH_S_TH, SUFFTYPE_POSTFIX}, |
| 601 |
{"th", 2, DCH_S_th, SUFFTYPE_POSTFIX}, |
| 602 |
{"SP", 2, DCH_S_SP, SUFFTYPE_POSTFIX}, |
| 603 |
/* last */ |
| 604 |
{NULL, 0, 0, 0} |
| 605 |
}; |
| 606 |
|
| 607 |
|
| 608 |
/* ---------- |
| 609 |
* Format-pictures (KeyWord). |
| 610 |
* |
| 611 |
* The KeyWord field; alphabetic sorted, *BUT* strings alike is sorted |
| 612 |
* complicated -to-> easy: |
| 613 |
* |
| 614 |
* (example: "DDD","DD","Day","D" ) |
| 615 |
* |
| 616 |
* (this specific sort needs the algorithm for sequential search for strings, |
| 617 |
* which not has exact end; -> How keyword is in "HH12blabla" ? - "HH" |
| 618 |
* or "HH12"? You must first try "HH12", because "HH" is in string, but |
| 619 |
* it is not good. |
| 620 |
* |
| 621 |
* (!) |
| 622 |
* - Position for the keyword is similar as position in the enum DCH/NUM_poz. |
| 623 |
* (!) |
| 624 |
* |
| 625 |
* For fast search is used the 'int index[]', index is ascii table from position |
| 626 |
* 32 (' ') to 126 (~), in this index is DCH_ / NUM_ enums for each ASCII |
| 627 |
* position or -1 if char is not used in the KeyWord. Search example for |
| 628 |
* string "MM": |
| 629 |
* 1) see in index to index['M' - 32], |
| 630 |
* 2) take keywords position (enum DCH_MI) from index |
| 631 |
* 3) run sequential search in keywords[] from this position |
| 632 |
* |
| 633 |
* ---------- |
| 634 |
*/ |
| 635 |
|
| 636 |
typedef enum |
| 637 |
{ |
| 638 |
DCH_A_D, |
| 639 |
DCH_A_M, |
| 640 |
DCH_AD, |
| 641 |
DCH_AM, |
| 642 |
DCH_B_C, |
| 643 |
DCH_BC, |
| 644 |
DCH_CC, |
| 645 |
DCH_DAY, |
| 646 |
DCH_DDD, |
| 647 |
DCH_DD, |
| 648 |
DCH_DY, |
| 649 |
DCH_Day, |
| 650 |
DCH_Dy, |
| 651 |
DCH_D, |
| 652 |
DCH_FF1, /* FFn codes must be consecutive */ |
| 653 |
DCH_FF2, |
| 654 |
DCH_FF3, |
| 655 |
DCH_FF4, |
| 656 |
DCH_FF5, |
| 657 |
DCH_FF6, |
| 658 |
DCH_FX, /* global suffix */ |
| 659 |
DCH_HH24, |
| 660 |
DCH_HH12, |
| 661 |
DCH_HH, |
| 662 |
DCH_IDDD, |
| 663 |
DCH_ID, |
| 664 |
DCH_IW, |
| 665 |
DCH_IYYY, |
| 666 |
DCH_IYY, |
| 667 |
DCH_IY, |
| 668 |
DCH_I, |
| 669 |
DCH_J, |
| 670 |
DCH_MI, |
| 671 |
DCH_MM, |
| 672 |
DCH_MONTH, |
| 673 |
DCH_MON, |
| 674 |
DCH_MS, |
| 675 |
DCH_Month, |
| 676 |
DCH_Mon, |
| 677 |
DCH_OF, |
| 678 |
DCH_P_M, |
| 679 |
DCH_PM, |
| 680 |
DCH_Q, |
| 681 |
DCH_RM, |
| 682 |
DCH_SSSSS, |
| 683 |
DCH_SSSS, |
| 684 |
DCH_SS, |
| 685 |
DCH_TZH, |
| 686 |
DCH_TZM, |
| 687 |
DCH_TZ, |
| 688 |
DCH_US, |
| 689 |
DCH_WW, |
| 690 |
DCH_W, |
| 691 |
DCH_Y_YYY, |
| 692 |
DCH_YYYY, |
| 693 |
DCH_YYY, |
| 694 |
DCH_YY, |
| 695 |
DCH_Y, |
| 696 |
DCH_a_d, |
| 697 |
DCH_a_m, |
| 698 |
DCH_ad, |
| 699 |
DCH_am, |
| 700 |
DCH_b_c, |
| 701 |
DCH_bc, |
| 702 |
DCH_cc, |
| 703 |
DCH_day, |
| 704 |
DCH_ddd, |
| 705 |
DCH_dd, |
| 706 |
DCH_dy, |
| 707 |
DCH_d, |
| 708 |
DCH_ff1, |
| 709 |
DCH_ff2, |
| 710 |
DCH_ff3, |
| 711 |
DCH_ff4, |
| 712 |
DCH_ff5, |
| 713 |
DCH_ff6, |
| 714 |
DCH_fx, |
| 715 |
DCH_hh24, |
| 716 |
DCH_hh12, |
| 717 |
DCH_hh, |
| 718 |
DCH_iddd, |
| 719 |
DCH_id, |
| 720 |
DCH_iw, |
| 721 |
DCH_iyyy, |
| 722 |
DCH_iyy, |
| 723 |
DCH_iy, |
| 724 |
DCH_i, |
| 725 |
DCH_j, |
| 726 |
DCH_mi, |
| 727 |
DCH_mm, |
| 728 |
DCH_month, |
| 729 |
DCH_mon, |
| 730 |
DCH_ms, |
| 731 |
DCH_of, |
| 732 |
DCH_p_m, |
| 733 |
DCH_pm, |
| 734 |
DCH_q, |
| 735 |
DCH_rm, |
| 736 |
DCH_sssss, |
| 737 |
DCH_ssss, |
| 738 |
DCH_ss, |
| 739 |
DCH_tzh, |
| 740 |
DCH_tzm, |
| 741 |
DCH_tz, |
| 742 |
DCH_us, |
| 743 |
DCH_ww, |
| 744 |
DCH_w, |
| 745 |
DCH_y_yyy, |
| 746 |
DCH_yyyy, |
| 747 |
DCH_yyy, |
| 748 |
DCH_yy, |
| 749 |
DCH_y, |
| 750 |
|
| 751 |
/* last */ |
| 752 |
_DCH_last_ |
| 753 |
} DCH_poz; |
| 754 |
|
| 755 |
typedef enum |
| 756 |
{ |
| 757 |
NUM_COMMA, |
| 758 |
NUM_DEC, |
| 759 |
NUM_0, |
| 760 |
NUM_9, |
| 761 |
NUM_B, |
| 762 |
NUM_C, |
| 763 |
NUM_D, |
| 764 |
NUM_E, |
| 765 |
NUM_FM, |
| 766 |
NUM_G, |
| 767 |
NUM_L, |
| 768 |
NUM_MI, |
| 769 |
NUM_PL, |
| 770 |
NUM_PR, |
| 771 |
NUM_RN, |
| 772 |
NUM_SG, |
| 773 |
NUM_SP, |
| 774 |
NUM_S, |
| 775 |
NUM_TH, |
| 776 |
NUM_V, |
| 777 |
NUM_b, |
| 778 |
NUM_c, |
| 779 |
NUM_d, |
| 780 |
NUM_e, |
| 781 |
NUM_fm, |
| 782 |
NUM_g, |
| 783 |
NUM_l, |
| 784 |
NUM_mi, |
| 785 |
NUM_pl, |
| 786 |
NUM_pr, |
| 787 |
NUM_rn, |
| 788 |
NUM_sg, |
| 789 |
NUM_sp, |
| 790 |
NUM_s, |
| 791 |
NUM_th, |
| 792 |
NUM_v, |
| 793 |
|
| 794 |
/* last */ |
| 795 |
_NUM_last_ |
| 796 |
} NUM_poz; |
| 797 |
|
| 798 |
/* ---------- |
| 799 |
* KeyWords for DATE-TIME version |
| 800 |
* ---------- |
| 801 |
*/ |
| 802 |
static const KeyWord DCH_keywords[] = { |
| 803 |
/* name, len, id, is_digit, date_mode */ |
| 804 |
{"A.D.", 4, DCH_A_D, false, FROM_CHAR_DATE_NONE}, /* A */ |
| 805 |
{"A.M.", 4, DCH_A_M, false, FROM_CHAR_DATE_NONE}, |
| 806 |
{"AD", 2, DCH_AD, false, FROM_CHAR_DATE_NONE}, |
| 807 |
{"AM", 2, DCH_AM, false, FROM_CHAR_DATE_NONE}, |
| 808 |
{"B.C.", 4, DCH_B_C, false, FROM_CHAR_DATE_NONE}, /* B */ |
| 809 |
{"BC", 2, DCH_BC, false, FROM_CHAR_DATE_NONE}, |
| 810 |
{"CC", 2, DCH_CC, true, FROM_CHAR_DATE_NONE}, /* C */ |
| 811 |
{"DAY", 3, DCH_DAY, false, FROM_CHAR_DATE_NONE}, /* D */ |
| 812 |
{"DDD", 3, DCH_DDD, true, FROM_CHAR_DATE_GREGORIAN}, |
| 813 |
{"DD", 2, DCH_DD, true, FROM_CHAR_DATE_GREGORIAN}, |
| 814 |
{"DY", 2, DCH_DY, false, FROM_CHAR_DATE_NONE}, |
| 815 |
{"Day", 3, DCH_Day, false, FROM_CHAR_DATE_NONE}, |
| 816 |
{"Dy", 2, DCH_Dy, false, FROM_CHAR_DATE_NONE}, |
| 817 |
{"D", 1, DCH_D, true, FROM_CHAR_DATE_GREGORIAN}, |
| 818 |
{"FF1", 3, DCH_FF1, true, FROM_CHAR_DATE_NONE}, /* F */ |
| 819 |
{"FF2", 3, DCH_FF2, true, FROM_CHAR_DATE_NONE}, |
| 820 |
{"FF3", 3, DCH_FF3, true, FROM_CHAR_DATE_NONE}, |
| 821 |
{"FF4", 3, DCH_FF4, true, FROM_CHAR_DATE_NONE}, |
| 822 |
{"FF5", 3, DCH_FF5, true, FROM_CHAR_DATE_NONE}, |
| 823 |
{"FF6", 3, DCH_FF6, true, FROM_CHAR_DATE_NONE}, |
| 824 |
{"FX", 2, DCH_FX, false, FROM_CHAR_DATE_NONE}, |
| 825 |
{"HH24", 4, DCH_HH24, true, FROM_CHAR_DATE_NONE}, /* H */ |
| 826 |
{"HH12", 4, DCH_HH12, true, FROM_CHAR_DATE_NONE}, |
| 827 |
{"HH", 2, DCH_HH, true, FROM_CHAR_DATE_NONE}, |
| 828 |
{"IDDD", 4, DCH_IDDD, true, FROM_CHAR_DATE_ISOWEEK}, /* I */ |
| 829 |
{"ID", 2, DCH_ID, true, FROM_CHAR_DATE_ISOWEEK}, |
| 830 |
{"IW", 2, DCH_IW, true, FROM_CHAR_DATE_ISOWEEK}, |
| 831 |
{"IYYY", 4, DCH_IYYY, true, FROM_CHAR_DATE_ISOWEEK}, |
| 832 |
{"IYY", 3, DCH_IYY, true, FROM_CHAR_DATE_ISOWEEK}, |
| 833 |
{"IY", 2, DCH_IY, true, FROM_CHAR_DATE_ISOWEEK}, |
| 834 |
{"I", 1, DCH_I, true, FROM_CHAR_DATE_ISOWEEK}, |
| 835 |
{"J", 1, DCH_J, true, FROM_CHAR_DATE_NONE}, /* J */ |
| 836 |
{"MI", 2, DCH_MI, true, FROM_CHAR_DATE_NONE}, /* M */ |
| 837 |
{"MM", 2, DCH_MM, true, FROM_CHAR_DATE_GREGORIAN}, |
| 838 |
{"MONTH", 5, DCH_MONTH, false, FROM_CHAR_DATE_GREGORIAN}, |
| 839 |
{"MON", 3, DCH_MON, false, FROM_CHAR_DATE_GREGORIAN}, |
| 840 |
{"MS", 2, DCH_MS, true, FROM_CHAR_DATE_NONE}, |
| 841 |
{"Month", 5, DCH_Month, false, FROM_CHAR_DATE_GREGORIAN}, |
| 842 |
{"Mon", 3, DCH_Mon, false, FROM_CHAR_DATE_GREGORIAN}, |
| 843 |
{"OF", 2, DCH_OF, false, FROM_CHAR_DATE_NONE}, /* O */ |
| 844 |
{"P.M.", 4, DCH_P_M, false, FROM_CHAR_DATE_NONE}, /* P */ |
| 845 |
{"PM", 2, DCH_PM, false, FROM_CHAR_DATE_NONE}, |
| 846 |
{"Q", 1, DCH_Q, true, FROM_CHAR_DATE_NONE}, /* Q */ |
| 847 |
{"RM", 2, DCH_RM, false, FROM_CHAR_DATE_GREGORIAN}, /* R */ |
| 848 |
{"SSSSS", 5, DCH_SSSS, true, FROM_CHAR_DATE_NONE}, /* S */ |
| 849 |
{"SSSS", 4, DCH_SSSS, true, FROM_CHAR_DATE_NONE}, |
| 850 |
{"SS", 2, DCH_SS, true, FROM_CHAR_DATE_NONE}, |
| 851 |
{"TZH", 3, DCH_TZH, false, FROM_CHAR_DATE_NONE}, /* T */ |
| 852 |
{"TZM", 3, DCH_TZM, true, FROM_CHAR_DATE_NONE}, |
| 853 |
{"TZ", 2, DCH_TZ, false, FROM_CHAR_DATE_NONE}, |
| 854 |
{"US", 2, DCH_US, true, FROM_CHAR_DATE_NONE}, /* U */ |
| 855 |
{"WW", 2, DCH_WW, true, FROM_CHAR_DATE_GREGORIAN}, /* W */ |
| 856 |
{"W", 1, DCH_W, true, FROM_CHAR_DATE_GREGORIAN}, |
| 857 |
{"Y,YYY", 5, DCH_Y_YYY, true, FROM_CHAR_DATE_GREGORIAN}, /* Y */ |
| 858 |
{"YYYY", 4, DCH_YYYY, true, FROM_CHAR_DATE_GREGORIAN}, |
| 859 |
{"YYY", 3, DCH_YYY, true, FROM_CHAR_DATE_GREGORIAN}, |
| 860 |
{"YY", 2, DCH_YY, true, FROM_CHAR_DATE_GREGORIAN}, |
| 861 |
{"Y", 1, DCH_Y, true, FROM_CHAR_DATE_GREGORIAN}, |
| 862 |
{"a.d.", 4, DCH_a_d, false, FROM_CHAR_DATE_NONE}, /* a */ |
| 863 |
{"a.m.", 4, DCH_a_m, false, FROM_CHAR_DATE_NONE}, |
| 864 |
{"ad", 2, DCH_ad, false, FROM_CHAR_DATE_NONE}, |
| 865 |
{"am", 2, DCH_am, false, FROM_CHAR_DATE_NONE}, |
| 866 |
{"b.c.", 4, DCH_b_c, false, FROM_CHAR_DATE_NONE}, /* b */ |
| 867 |
{"bc", 2, DCH_bc, false, FROM_CHAR_DATE_NONE}, |
| 868 |
{"cc", 2, DCH_CC, true, FROM_CHAR_DATE_NONE}, /* c */ |
| 869 |
{"day", 3, DCH_day, false, FROM_CHAR_DATE_NONE}, /* d */ |
| 870 |
{"ddd", 3, DCH_DDD, true, FROM_CHAR_DATE_GREGORIAN}, |
| 871 |
{"dd", 2, DCH_DD, true, FROM_CHAR_DATE_GREGORIAN}, |
| 872 |
{"dy", 2, DCH_dy, false, FROM_CHAR_DATE_NONE}, |
| 873 |
{"d", 1, DCH_D, true, FROM_CHAR_DATE_GREGORIAN}, |
| 874 |
{"ff1", 3, DCH_FF1, true, FROM_CHAR_DATE_NONE}, /* f */ |
| 875 |
{"ff2", 3, DCH_FF2, true, FROM_CHAR_DATE_NONE}, |
| 876 |
{"ff3", 3, DCH_FF3, true, FROM_CHAR_DATE_NONE}, |
| 877 |
{"ff4", 3, DCH_FF4, true, FROM_CHAR_DATE_NONE}, |
| 878 |
{"ff5", 3, DCH_FF5, true, FROM_CHAR_DATE_NONE}, |
| 879 |
{"ff6", 3, DCH_FF6, true, FROM_CHAR_DATE_NONE}, |
| 880 |
{"fx", 2, DCH_FX, false, FROM_CHAR_DATE_NONE}, |
| 881 |
{"hh24", 4, DCH_HH24, true, FROM_CHAR_DATE_NONE}, /* h */ |
| 882 |
{"hh12", 4, DCH_HH12, true, FROM_CHAR_DATE_NONE}, |
| 883 |
{"hh", 2, DCH_HH, true, FROM_CHAR_DATE_NONE}, |
| 884 |
{"iddd", 4, DCH_IDDD, true, FROM_CHAR_DATE_ISOWEEK}, /* i */ |
| 885 |
{"id", 2, DCH_ID, true, FROM_CHAR_DATE_ISOWEEK}, |
| 886 |
{"iw", 2, DCH_IW, true, FROM_CHAR_DATE_ISOWEEK}, |
| 887 |
{"iyyy", 4, DCH_IYYY, true, FROM_CHAR_DATE_ISOWEEK}, |
| 888 |
{"iyy", 3, DCH_IYY, true, FROM_CHAR_DATE_ISOWEEK}, |
| 889 |
{"iy", 2, DCH_IY, true, FROM_CHAR_DATE_ISOWEEK}, |
| 890 |
{"i", 1, DCH_I, true, FROM_CHAR_DATE_ISOWEEK}, |
| 891 |
{"j", 1, DCH_J, true, FROM_CHAR_DATE_NONE}, /* j */ |
| 892 |
{"mi", 2, DCH_MI, true, FROM_CHAR_DATE_NONE}, /* m */ |
| 893 |
{"mm", 2, DCH_MM, true, FROM_CHAR_DATE_GREGORIAN}, |
| 894 |
{"month", 5, DCH_month, false, FROM_CHAR_DATE_GREGORIAN}, |
| 895 |
{"mon", 3, DCH_mon, false, FROM_CHAR_DATE_GREGORIAN}, |
| 896 |
{"ms", 2, DCH_MS, true, FROM_CHAR_DATE_NONE}, |
| 897 |
{"of", 2, DCH_OF, false, FROM_CHAR_DATE_NONE}, /* o */ |
| 898 |
{"p.m.", 4, DCH_p_m, false, FROM_CHAR_DATE_NONE}, /* p */ |
| 899 |
{"pm", 2, DCH_pm, false, FROM_CHAR_DATE_NONE}, |
| 900 |
{"q", 1, DCH_Q, true, FROM_CHAR_DATE_NONE}, /* q */ |
| 901 |
{"rm", 2, DCH_rm, false, FROM_CHAR_DATE_GREGORIAN}, /* r */ |
| 902 |
{"sssss", 5, DCH_SSSS, true, FROM_CHAR_DATE_NONE}, /* s */ |
| 903 |
{"ssss", 4, DCH_SSSS, true, FROM_CHAR_DATE_NONE}, |
| 904 |
{"ss", 2, DCH_SS, true, FROM_CHAR_DATE_NONE}, |
| 905 |
{"tzh", 3, DCH_TZH, false, FROM_CHAR_DATE_NONE}, /* t */ |
| 906 |
{"tzm", 3, DCH_TZM, true, FROM_CHAR_DATE_NONE}, |
| 907 |
{"tz", 2, DCH_tz, false, FROM_CHAR_DATE_NONE}, |
| 908 |
{"us", 2, DCH_US, true, FROM_CHAR_DATE_NONE}, /* u */ |
| 909 |
{"ww", 2, DCH_WW, true, FROM_CHAR_DATE_GREGORIAN}, /* w */ |
| 910 |
{"w", 1, DCH_W, true, FROM_CHAR_DATE_GREGORIAN}, |
| 911 |
{"y,yyy", 5, DCH_Y_YYY, true, FROM_CHAR_DATE_GREGORIAN}, /* y */ |
| 912 |
{"yyyy", 4, DCH_YYYY, true, FROM_CHAR_DATE_GREGORIAN}, |
| 913 |
{"yyy", 3, DCH_YYY, true, FROM_CHAR_DATE_GREGORIAN}, |
| 914 |
{"yy", 2, DCH_YY, true, FROM_CHAR_DATE_GREGORIAN}, |
| 915 |
{"y", 1, DCH_Y, true, FROM_CHAR_DATE_GREGORIAN}, |
| 916 |
|
| 917 |
/* last */ |
| 918 |
{NULL, 0, 0, 0, 0} |
| 919 |
}; |
| 920 |
|
| 921 |
/* ---------- |
| 922 |
* KeyWords for NUMBER version |
| 923 |
* |
| 924 |
* The is_digit and date_mode fields are not relevant here. |
| 925 |
* ---------- |
| 926 |
*/ |
| 927 |
static const KeyWord NUM_keywords[] = { |
| 928 |
/* name, len, id is in Index */ |
| 929 |
{",", 1, NUM_COMMA}, /* , */ |
| 930 |
{".", 1, NUM_DEC}, /* . */ |
| 931 |
{"0", 1, NUM_0}, /* 0 */ |
| 932 |
{"9", 1, NUM_9}, /* 9 */ |
| 933 |
{"B", 1, NUM_B}, /* B */ |
| 934 |
{"C", 1, NUM_C}, /* C */ |
| 935 |
{"D", 1, NUM_D}, /* D */ |
| 936 |
{"EEEE", 4, NUM_E}, /* E */ |
| 937 |
{"FM", 2, NUM_FM}, /* F */ |
| 938 |
{"G", 1, NUM_G}, /* G */ |
| 939 |
{"L", 1, NUM_L}, /* L */ |
| 940 |
{"MI", 2, NUM_MI}, /* M */ |
| 941 |
{"PL", 2, NUM_PL}, /* P */ |
| 942 |
{"PR", 2, NUM_PR}, |
| 943 |
{"RN", 2, NUM_RN}, /* R */ |
| 944 |
{"SG", 2, NUM_SG}, /* S */ |
| 945 |
{"SP", 2, NUM_SP}, |
| 946 |
{"S", 1, NUM_S}, |
| 947 |
{"TH", 2, NUM_TH}, /* T */ |
| 948 |
{"V", 1, NUM_V}, /* V */ |
| 949 |
{"b", 1, NUM_B}, /* b */ |
| 950 |
{"c", 1, NUM_C}, /* c */ |
| 951 |
{"d", 1, NUM_D}, /* d */ |
| 952 |
{"eeee", 4, NUM_E}, /* e */ |
| 953 |
{"fm", 2, NUM_FM}, /* f */ |
| 954 |
{"g", 1, NUM_G}, /* g */ |
| 955 |
{"l", 1, NUM_L}, /* l */ |
| 956 |
{"mi", 2, NUM_MI}, /* m */ |
| 957 |
{"pl", 2, NUM_PL}, /* p */ |
| 958 |
{"pr", 2, NUM_PR}, |
| 959 |
{"rn", 2, NUM_rn}, /* r */ |
| 960 |
{"sg", 2, NUM_SG}, /* s */ |
| 961 |
{"sp", 2, NUM_SP}, |
| 962 |
{"s", 1, NUM_S}, |
| 963 |
{"th", 2, NUM_th}, /* t */ |
| 964 |
{"v", 1, NUM_V}, /* v */ |
| 965 |
|
| 966 |
/* last */ |
| 967 |
{NULL, 0, 0} |
| 968 |
}; |
| 969 |
|
| 970 |
|
| 971 |
/* ---------- |
| 972 |
* KeyWords index for DATE-TIME version |
| 973 |
* ---------- |
| 974 |
*/ |
| 975 |
static const int DCH_index[KeyWord_INDEX_SIZE] = { |
| 976 |
/* |
| 977 |
0 1 2 3 4 5 6 7 8 9 |
| 978 |
*/ |
| 979 |
/*---- first 0..31 chars are skipped ----*/ |
| 980 |
|
| 981 |
-1, -1, -1, -1, -1, -1, -1, -1, |
| 982 |
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 983 |
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 984 |
-1, -1, -1, -1, -1, DCH_A_D, DCH_B_C, DCH_CC, DCH_DAY, -1, |
| 985 |
DCH_FF1, -1, DCH_HH24, DCH_IDDD, DCH_J, -1, -1, DCH_MI, -1, DCH_OF, |
| 986 |
DCH_P_M, DCH_Q, DCH_RM, DCH_SSSSS, DCH_TZH, DCH_US, -1, DCH_WW, -1, DCH_Y_YYY, |
| 987 |
-1, -1, -1, -1, -1, -1, -1, DCH_a_d, DCH_b_c, DCH_cc, |
| 988 |
DCH_day, -1, DCH_ff1, -1, DCH_hh24, DCH_iddd, DCH_j, -1, -1, DCH_mi, |
| 989 |
-1, DCH_of, DCH_p_m, DCH_q, DCH_rm, DCH_sssss, DCH_tzh, DCH_us, -1, DCH_ww, |
| 990 |
-1, DCH_y_yyy, -1, -1, -1, -1 |
| 991 |
|
| 992 |
/*---- chars over 126 are skipped ----*/ |
| 993 |
}; |
| 994 |
|
| 995 |
/* ---------- |
| 996 |
* KeyWords index for NUMBER version |
| 997 |
* ---------- |
| 998 |
*/ |
| 999 |
static const int NUM_index[KeyWord_INDEX_SIZE] = { |
| 1000 |
/* |
| 1001 |
0 1 2 3 4 5 6 7 8 9 |
| 1002 |
*/ |
| 1003 |
/*---- first 0..31 chars are skipped ----*/ |
| 1004 |
|
| 1005 |
-1, -1, -1, -1, -1, -1, -1, -1, |
| 1006 |
-1, -1, -1, -1, NUM_COMMA, -1, NUM_DEC, -1, NUM_0, -1, |
| 1007 |
-1, -1, -1, -1, -1, -1, -1, NUM_9, -1, -1, |
| 1008 |
-1, -1, -1, -1, -1, -1, NUM_B, NUM_C, NUM_D, NUM_E, |
| 1009 |
NUM_FM, NUM_G, -1, -1, -1, -1, NUM_L, NUM_MI, -1, -1, |
| 1010 |
NUM_PL, -1, NUM_RN, NUM_SG, NUM_TH, -1, NUM_V, -1, -1, -1, |
| 1011 |
-1, -1, -1, -1, -1, -1, -1, -1, NUM_b, NUM_c, |
| 1012 |
NUM_d, NUM_e, NUM_fm, NUM_g, -1, -1, -1, -1, NUM_l, NUM_mi, |
| 1013 |
-1, -1, NUM_pl, -1, NUM_rn, NUM_sg, NUM_th, -1, NUM_v, -1, |
| 1014 |
-1, -1, -1, -1, -1, -1 |
| 1015 |
|
| 1016 |
/*---- chars over 126 are skipped ----*/ |
| 1017 |
}; |
| 1018 |
|
| 1019 |
/* ---------- |
| 1020 |
* Number processor struct |
| 1021 |
* ---------- |
| 1022 |
*/ |
| 1023 |
typedef struct NUMProc |
| 1024 |
{ |
| 1025 |
bool is_to_char; |
| 1026 |
NUMDesc *Num; /* number description */ |
| 1027 |
|
| 1028 |
int sign, /* '-' or '+' */ |
| 1029 |
sign_wrote, /* was sign write */ |
| 1030 |
num_count, /* number of write digits */ |
| 1031 |
num_in, /* is inside number */ |
| 1032 |
num_curr, /* current position in number */ |
| 1033 |
out_pre_spaces, /* spaces before first digit */ |
| 1034 |
|
| 1035 |
read_dec, /* to_number - was read dec. point */ |
| 1036 |
read_post, /* to_number - number of dec. digit */ |
| 1037 |
read_pre; /* to_number - number non-dec. digit */ |
| 1038 |
|
| 1039 |
char *number, /* string with number */ |
| 1040 |
*number_p, /* pointer to current number position */ |
| 1041 |
*inout, /* in / out buffer */ |
| 1042 |
*inout_p; /* pointer to current inout position */ |
| 1043 |
|
| 1044 |
const char *last_relevant, /* last relevant number after decimal point */ |
| 1045 |
|
| 1046 |
*L_negative_sign, /* Locale */ |
| 1047 |
*L_positive_sign, |
| 1048 |
*decimal, |
| 1049 |
*L_thousands_sep, |
| 1050 |
*L_currency_symbol; |
| 1051 |
} NUMProc; |
| 1052 |
|
| 1053 |
/* Return flags for DCH_from_char() */ |
| 1054 |
#define DCH_DATED 0x01 |
| 1055 |
#define DCH_TIMED 0x02 |
| 1056 |
#define DCH_ZONED 0x04 |
| 1057 |
|
| 1058 |
/* |
| 1059 |
* These macros are used in NUM_processor() and its subsidiary routines. |
| 1060 |
* OVERLOAD_TEST: true if we've reached end of input string |
| 1061 |
* AMOUNT_TEST(s): true if at least s bytes remain in string |
| 1062 |
*/ |
| 1063 |
#define OVERLOAD_TEST (Np->inout_p >= Np->inout + input_len) |
| 1064 |
#define AMOUNT_TEST(s) (Np->inout_p <= Np->inout + (input_len - (s))) |
| 1065 |
|
| 1066 |
|
| 1067 |
/* ---------- |
| 1068 |
* Functions |
| 1069 |
* ---------- |
| 1070 |
*/ |
| 1071 |
static const KeyWord *index_seq_search(const char *str, const KeyWord *kw, |
| 1072 |
const int *index); |
| 1073 |
static const KeySuffix *suff_search(const char *str, const KeySuffix *suf, int type); |
| 1074 |
static bool is_separator_char(const char *str); |
| 1075 |
static void NUMDesc_prepare(NUMDesc *num, FormatNode *n); |
| 1076 |
static void parse_format(FormatNode *node, const char *str, const KeyWord *kw, |
| 1077 |
const KeySuffix *suf, const int *index, uint32 flags, NUMDesc *Num); |
| 1078 |
|
| 1079 |
static void DCH_to_char(FormatNode *node, bool is_interval, |
| 1080 |
TmToChar *in, char *out, Oid collid); |
| 1081 |
static void DCH_from_char(FormatNode *node, const char *in, TmFromChar *out, |
| 1082 |
Oid collid, bool std, Node *escontext); |
| 1083 |
|
| 1084 |
#ifdef DEBUG_TO_FROM_CHAR |
| 1085 |
static void dump_index(const KeyWord *k, const int *index); |
| 1086 |
static void dump_node(FormatNode *node, int max); |
| 1087 |
#endif |
| 1088 |
|
| 1089 |
static const char *get_th(char *num, int type); |
| 1090 |
static char *str_numth(char *dest, char *num, int type); |
| 1091 |
static int adjust_partial_year_to_2020(int year); |
| 1092 |
static int strspace_len(const char *str); |
| 1093 |
static bool from_char_set_mode(TmFromChar *tmfc, const FromCharDateMode mode, |
| 1094 |
Node *escontext); |
| 1095 |
static bool from_char_set_int(int *dest, const int value, const FormatNode *node, |
| 1096 |
Node *escontext); |
| 1097 |
static int from_char_parse_int_len(int *dest, const char **src, const int len, |
| 1098 |
FormatNode *node, Node *escontext); |
| 1099 |
static int from_char_parse_int(int *dest, const char **src, FormatNode *node, |
| 1100 |
Node *escontext); |
| 1101 |
static int seq_search_ascii(const char *name, const char *const *array, size_t *len); |
| 1102 |
static int seq_search_localized(const char *name, char **array, size_t *len, |
| 1103 |
Oid collid); |
| 1104 |
static bool from_char_seq_search(int *dest, const char **src, |
| 1105 |
const char *const *array, |
| 1106 |
char **localized_array, Oid collid, |
| 1107 |
FormatNode *node, Node *escontext); |
| 1108 |
static bool do_to_timestamp(text *date_txt, text *fmt, Oid collid, bool std, |
| 1109 |
struct pg_tm *tm, fsec_t *fsec, struct fmt_tz *tz, |
| 1110 |
int *fprec, uint32 *flags, Node *escontext); |
| 1111 |
static char *fill_str(char *str, int c, int max); |
| 1112 |
static FormatNode *NUM_cache(int len, NUMDesc *Num, text *pars_str, bool *shouldFree); |
| 1113 |
static char *int_to_roman(int number); |
| 1114 |
static int roman_to_int(NUMProc *Np, int input_len); |
| 1115 |
static void NUM_prepare_locale(NUMProc *Np); |
| 1116 |
static char *get_last_relevant_decnum(char *num); |
| 1117 |
static void NUM_numpart_from_char(NUMProc *Np, int id, int input_len); |
| 1118 |
static void NUM_numpart_to_char(NUMProc *Np, int id); |
| 1119 |
static void NUM_add_locale_symbol(NUMProc *Np, const char *pattern); |
| 1120 |
static char *NUM_processor(FormatNode *node, NUMDesc *Num, char *inout, |
| 1121 |
char *number, int input_len, int to_char_out_pre_spaces, |
| 1122 |
int sign, bool is_to_char, Oid collid); |
| 1123 |
static DCHCacheEntry *DCH_cache_getnew(const char *str, bool std); |
| 1124 |
static DCHCacheEntry *DCH_cache_search(const char *str, bool std); |
| 1125 |
static DCHCacheEntry *DCH_cache_fetch(const char *str, bool std); |
| 1126 |
static NUMCacheEntry *NUM_cache_getnew(const char *str); |
| 1127 |
static NUMCacheEntry *NUM_cache_search(const char *str); |
| 1128 |
static NUMCacheEntry *NUM_cache_fetch(const char *str); |
| 1129 |
|
| 1130 |
|
| 1131 |
/* ---------- |
| 1132 |
* Fast sequential search, use index for data selection which |
| 1133 |
* go to seq. cycle (it is very fast for unwanted strings) |
| 1134 |
* (can't be used binary search in format parsing) |
| 1135 |
* ---------- |
| 1136 |
*/ |
| 1137 |
static const KeyWord * |
| 1138 |
index_seq_search(const char *str, const KeyWord *kw, const int *index) |
| 1139 |
{ |
| 1140 |
int poz; |
| 1141 |
|
| 1142 |
if (!KeyWord_INDEX_FILTER(*str)) |
| 1143 |
return NULL; |
| 1144 |
|
| 1145 |
if ((poz = *(index + (*str - ' '))) > -1) |
| 1146 |
{ |
| 1147 |
const KeyWord *k = kw + poz; |
| 1148 |
|
| 1149 |
do |
| 1150 |
{ |
| 1151 |
if (strncmp(str, k->name, k->len) == 0) |
| 1152 |
return k; |
| 1153 |
k++; |
| 1154 |
if (!k->name) |
| 1155 |
return NULL; |
| 1156 |
} while (*str == *k->name); |
| 1157 |
} |
| 1158 |
return NULL; |
| 1159 |
} |
| 1160 |
|
| 1161 |
static const KeySuffix * |
| 1162 |
suff_search(const char *str, const KeySuffix *suf, int type) |
| 1163 |
{ |
| 1164 |
const KeySuffix *s; |
| 1165 |
|
| 1166 |
for (s = suf; s->name != NULL; s++) |
| 1167 |
{ |
| 1168 |
if (s->type != type) |
| 1169 |
continue; |
| 1170 |
|
| 1171 |
if (strncmp(str, s->name, s->len) == 0) |
| 1172 |
return s; |
| 1173 |
} |
| 1174 |
return NULL; |
| 1175 |
} |
| 1176 |
|
| 1177 |
static bool |
| 1178 |
is_separator_char(const char *str) |
| 1179 |
{ |
| 1180 |
/* ASCII printable character, but not letter or digit */ |
| 1181 |
return (*str > 0x20 && *str < 0x7F && |
| 1182 |
!(*str >= 'A' && *str <= 'Z') && |
| 1183 |
!(*str >= 'a' && *str <= 'z') && |
| 1184 |
!(*str >= '0' && *str <= '9')); |
| 1185 |
} |
| 1186 |
|
| 1187 |
/* ---------- |
| 1188 |
* Prepare NUMDesc (number description struct) via FormatNode struct |
| 1189 |
* ---------- |
| 1190 |
*/ |
| 1191 |
static void |
| 1192 |
NUMDesc_prepare(NUMDesc *num, FormatNode *n) |
| 1193 |
{ |
| 1194 |
if (n->type != NODE_TYPE_ACTION) |
| 1195 |
return; |
| 1196 |
|
| 1197 |
if (IS_EEEE(num) && n->key->id != NUM_E) |
| 1198 |
ereport(ERROR, |
| 1199 |
(errcode(ERRCODE_SYNTAX_ERROR), |
| 1200 |
errmsg("\"EEEE\" must be the last pattern used"))); |
| 1201 |
|
| 1202 |
switch (n->key->id) |
| 1203 |
{ |
| 1204 |
case NUM_9: |
| 1205 |
if (IS_BRACKET(num)) |
| 1206 |
ereport(ERROR, |
| 1207 |
(errcode(ERRCODE_SYNTAX_ERROR), |
| 1208 |
errmsg("\"9\" must be ahead of \"PR\""))); |
| 1209 |
if (IS_MULTI(num)) |
| 1210 |
{ |
| 1211 |
++num->multi; |
| 1212 |
break; |
| 1213 |
} |
| 1214 |
if (IS_DECIMAL(num)) |
| 1215 |
++num->post; |
| 1216 |
else |
| 1217 |
++num->pre; |
| 1218 |
break; |
| 1219 |
|
| 1220 |
case NUM_0: |
| 1221 |
if (IS_BRACKET(num)) |
| 1222 |
ereport(ERROR, |
| 1223 |
(errcode(ERRCODE_SYNTAX_ERROR), |
| 1224 |
errmsg("\"0\" must be ahead of \"PR\""))); |
| 1225 |
if (!IS_ZERO(num) && !IS_DECIMAL(num)) |
| 1226 |
{ |
| 1227 |
num->flag |= NUM_F_ZERO; |
| 1228 |
num->zero_start = num->pre + 1; |
| 1229 |
} |
| 1230 |
if (!IS_DECIMAL(num)) |
| 1231 |
++num->pre; |
| 1232 |
else |
| 1233 |
++num->post; |
| 1234 |
|
| 1235 |
num->zero_end = num->pre + num->post; |
| 1236 |
break; |
| 1237 |
|
| 1238 |
case NUM_B: |
| 1239 |
if (num->pre == 0 && num->post == 0 && (!IS_ZERO(num))) |
| 1240 |
num->flag |= NUM_F_BLANK; |
| 1241 |
break; |
| 1242 |
|
| 1243 |
case NUM_D: |
| 1244 |
num->flag |= NUM_F_LDECIMAL; |
| 1245 |
num->need_locale = true; |
| 1246 |
/* FALLTHROUGH */ |
| 1247 |
case NUM_DEC: |
| 1248 |
if (IS_DECIMAL(num)) |
| 1249 |
ereport(ERROR, |
| 1250 |
(errcode(ERRCODE_SYNTAX_ERROR), |
| 1251 |
errmsg("multiple decimal points"))); |
| 1252 |
if (IS_MULTI(num)) |
| 1253 |
ereport(ERROR, |
| 1254 |
(errcode(ERRCODE_SYNTAX_ERROR), |
| 1255 |
errmsg("cannot use \"V\" and decimal point together"))); |
| 1256 |
num->flag |= NUM_F_DECIMAL; |
| 1257 |
break; |
| 1258 |
|
| 1259 |
case NUM_FM: |
| 1260 |
num->flag |= NUM_F_FILLMODE; |
| 1261 |
break; |
| 1262 |
|
| 1263 |
case NUM_S: |
| 1264 |
if (IS_LSIGN(num)) |
| 1265 |
ereport(ERROR, |
| 1266 |
(errcode(ERRCODE_SYNTAX_ERROR), |
| 1267 |
errmsg("cannot use \"S\" twice"))); |
| 1268 |
if (IS_PLUS(num) || IS_MINUS(num) || IS_BRACKET(num)) |
| 1269 |
ereport(ERROR, |
| 1270 |
(errcode(ERRCODE_SYNTAX_ERROR), |
| 1271 |
errmsg("cannot use \"S\" and \"PL\"/\"MI\"/\"SG\"/\"PR\" together"))); |
| 1272 |
if (!IS_DECIMAL(num)) |
| 1273 |
{ |
| 1274 |
num->lsign = NUM_LSIGN_PRE; |
| 1275 |
num->pre_lsign_num = num->pre; |
| 1276 |
num->need_locale = true; |
| 1277 |
num->flag |= NUM_F_LSIGN; |
| 1278 |
} |
| 1279 |
else if (num->lsign == NUM_LSIGN_NONE) |
| 1280 |
{ |
| 1281 |
num->lsign = NUM_LSIGN_POST; |
| 1282 |
num->need_locale = true; |
| 1283 |
num->flag |= NUM_F_LSIGN; |
| 1284 |
} |
| 1285 |
break; |
| 1286 |
|
| 1287 |
case NUM_MI: |
| 1288 |
if (IS_LSIGN(num)) |
| 1289 |
ereport(ERROR, |
| 1290 |
(errcode(ERRCODE_SYNTAX_ERROR), |
| 1291 |
errmsg("cannot use \"S\" and \"MI\" together"))); |
| 1292 |
num->flag |= NUM_F_MINUS; |
| 1293 |
if (IS_DECIMAL(num)) |
| 1294 |
num->flag |= NUM_F_MINUS_POST; |
| 1295 |
break; |
| 1296 |
|
| 1297 |
case NUM_PL: |
| 1298 |
if (IS_LSIGN(num)) |
| 1299 |
ereport(ERROR, |
| 1300 |
(errcode(ERRCODE_SYNTAX_ERROR), |
| 1301 |
errmsg("cannot use \"S\" and \"PL\" together"))); |
| 1302 |
num->flag |= NUM_F_PLUS; |
| 1303 |
if (IS_DECIMAL(num)) |
| 1304 |
num->flag |= NUM_F_PLUS_POST; |
| 1305 |
break; |
| 1306 |
|
| 1307 |
case NUM_SG: |
| 1308 |
if (IS_LSIGN(num)) |
| 1309 |
ereport(ERROR, |
| 1310 |
(errcode(ERRCODE_SYNTAX_ERROR), |
| 1311 |
errmsg("cannot use \"S\" and \"SG\" together"))); |
| 1312 |
num->flag |= NUM_F_MINUS; |
| 1313 |
num->flag |= NUM_F_PLUS; |
| 1314 |
break; |
| 1315 |
|
| 1316 |
case NUM_PR: |
| 1317 |
if (IS_LSIGN(num) || IS_PLUS(num) || IS_MINUS(num)) |
| 1318 |
ereport(ERROR, |
| 1319 |
(errcode(ERRCODE_SYNTAX_ERROR), |
| 1320 |
errmsg("cannot use \"PR\" and \"S\"/\"PL\"/\"MI\"/\"SG\" together"))); |
| 1321 |
num->flag |= NUM_F_BRACKET; |
| 1322 |
break; |
| 1323 |
|
| 1324 |
case NUM_rn: |
| 1325 |
case NUM_RN: |
| 1326 |
if (IS_ROMAN(num)) |
| 1327 |
ereport(ERROR, |
| 1328 |
(errcode(ERRCODE_SYNTAX_ERROR), |
| 1329 |
errmsg("cannot use \"RN\" twice"))); |
| 1330 |
num->flag |= NUM_F_ROMAN; |
| 1331 |
break; |
| 1332 |
|
| 1333 |
case NUM_L: |
| 1334 |
case NUM_G: |
| 1335 |
num->need_locale = true; |
| 1336 |
break; |
| 1337 |
|
| 1338 |
case NUM_V: |
| 1339 |
if (IS_DECIMAL(num)) |
| 1340 |
ereport(ERROR, |
| 1341 |
(errcode(ERRCODE_SYNTAX_ERROR), |
| 1342 |
errmsg("cannot use \"V\" and decimal point together"))); |
| 1343 |
num->flag |= NUM_F_MULTI; |
| 1344 |
break; |
| 1345 |
|
| 1346 |
case NUM_E: |
| 1347 |
if (IS_EEEE(num)) |
| 1348 |
ereport(ERROR, |
| 1349 |
(errcode(ERRCODE_SYNTAX_ERROR), |
| 1350 |
errmsg("cannot use \"EEEE\" twice"))); |
| 1351 |
if (IS_BLANK(num) || IS_FILLMODE(num) || IS_LSIGN(num) || |
| 1352 |
IS_BRACKET(num) || IS_MINUS(num) || IS_PLUS(num) || |
| 1353 |
IS_ROMAN(num) || IS_MULTI(num)) |
| 1354 |
ereport(ERROR, |
| 1355 |
(errcode(ERRCODE_SYNTAX_ERROR), |
| 1356 |
errmsg("\"EEEE\" is incompatible with other formats"), |
| 1357 |
errdetail("\"EEEE\" may only be used together with digit and decimal point patterns."))); |
| 1358 |
num->flag |= NUM_F_EEEE; |
| 1359 |
break; |
| 1360 |
} |
| 1361 |
|
| 1362 |
if (IS_ROMAN(num) && |
| 1363 |
(num->flag & ~(NUM_F_ROMAN | NUM_F_FILLMODE)) != 0) |
| 1364 |
ereport(ERROR, |
| 1365 |
(errcode(ERRCODE_SYNTAX_ERROR), |
| 1366 |
errmsg("\"RN\" is incompatible with other formats"), |
| 1367 |
errdetail("\"RN\" may only be used together with \"FM\"."))); |
| 1368 |
} |
| 1369 |
|
| 1370 |
/* ---------- |
| 1371 |
* Format parser, search small keywords and keyword's suffixes, and make |
| 1372 |
* format-node tree. |
| 1373 |
* |
| 1374 |
* for DATE-TIME & NUMBER version |
| 1375 |
* ---------- |
| 1376 |
*/ |
| 1377 |
static void |
| 1378 |
parse_format(FormatNode *node, const char *str, const KeyWord *kw, |
| 1379 |
const KeySuffix *suf, const int *index, uint32 flags, NUMDesc *Num) |
| 1380 |
{ |
| 1381 |
FormatNode *n; |
| 1382 |
|
| 1383 |
#ifdef DEBUG_TO_FROM_CHAR |
| 1384 |
elog(DEBUG_elog_output, "to_char/number(): run parser"); |
| 1385 |
#endif |
| 1386 |
|
| 1387 |
n = node; |
| 1388 |
|
| 1389 |
while (*str) |
| 1390 |
{ |
| 1391 |
int suffix = 0; |
| 1392 |
const KeySuffix *s; |
| 1393 |
|
| 1394 |
/* |
| 1395 |
* Prefix |
| 1396 |
*/ |
| 1397 |
if ((flags & DCH_FLAG) && |
| 1398 |
(s = suff_search(str, suf, SUFFTYPE_PREFIX)) != NULL) |
| 1399 |
{ |
| 1400 |
suffix |= s->id; |
| 1401 |
if (s->len) |
| 1402 |
str += s->len; |
| 1403 |
} |
| 1404 |
|
| 1405 |
/* |
| 1406 |
* Keyword |
| 1407 |
*/ |
| 1408 |
if (*str && (n->key = index_seq_search(str, kw, index)) != NULL) |
| 1409 |
{ |
| 1410 |
n->type = NODE_TYPE_ACTION; |
| 1411 |
n->suffix = suffix; |
| 1412 |
if (n->key->len) |
| 1413 |
str += n->key->len; |
| 1414 |
|
| 1415 |
/* |
| 1416 |
* NUM version: Prepare global NUMDesc struct |
| 1417 |
*/ |
| 1418 |
if (flags & NUM_FLAG) |
| 1419 |
NUMDesc_prepare(Num, n); |
| 1420 |
|
| 1421 |
/* |
| 1422 |
* Postfix |
| 1423 |
*/ |
| 1424 |
if ((flags & DCH_FLAG) && *str && |
| 1425 |
(s = suff_search(str, suf, SUFFTYPE_POSTFIX)) != NULL) |
| 1426 |
{ |
| 1427 |
n->suffix |= s->id; |
| 1428 |
if (s->len) |
| 1429 |
str += s->len; |
| 1430 |
} |
| 1431 |
|
| 1432 |
n++; |
| 1433 |
} |
| 1434 |
else if (*str) |
| 1435 |
{ |
| 1436 |
int chlen; |
| 1437 |
|
| 1438 |
if ((flags & STD_FLAG) && *str != '"') |
| 1439 |
{ |
| 1440 |
/* |
| 1441 |
* Standard mode, allow only following separators: "-./,':; ". |
| 1442 |
* However, we support double quotes even in standard mode |
| 1443 |
* (see below). This is our extension of standard mode. |
| 1444 |
*/ |
| 1445 |
if (strchr("-./,':; ", *str) == NULL) |
| 1446 |
ereport(ERROR, |
| 1447 |
(errcode(ERRCODE_INVALID_DATETIME_FORMAT), |
| 1448 |
errmsg("invalid datetime format separator: \"%s\"", |
| 1449 |
pnstrdup(str, pg_mblen_cstr(str))))); |
| 1450 |
|
| 1451 |
if (*str == ' ') |
| 1452 |
n->type = NODE_TYPE_SPACE; |
| 1453 |
else |
| 1454 |
n->type = NODE_TYPE_SEPARATOR; |
| 1455 |
|
| 1456 |
n->character[0] = *str; |
| 1457 |
n->character[1] = '\0'; |
| 1458 |
n->key = NULL; |
| 1459 |
n->suffix = 0; |
| 1460 |
n++; |
| 1461 |
str++; |
| 1462 |
} |
| 1463 |
else if (*str == '"') |
| 1464 |
{ |
| 1465 |
/* |
| 1466 |
* Process double-quoted literal string, if any |
| 1467 |
*/ |
| 1468 |
str++; |
| 1469 |
while (*str) |
| 1470 |
{ |
| 1471 |
if (*str == '"') |
| 1472 |
{ |
| 1473 |
str++; |
| 1474 |
break; |
| 1475 |
} |
| 1476 |
/* backslash quotes the next character, if any */ |
| 1477 |
if (*str == '\\' && *(str + 1)) |
| 1478 |
str++; |
| 1479 |
chlen = pg_mblen_cstr(str); |
| 1480 |
n->type = NODE_TYPE_CHAR; |
| 1481 |
memcpy(n->character, str, chlen); |
| 1482 |
n->character[chlen] = '\0'; |
| 1483 |
n->key = NULL; |
| 1484 |
n->suffix = 0; |
| 1485 |
n++; |
| 1486 |
str += chlen; |
| 1487 |
} |
| 1488 |
} |
| 1489 |
else |
| 1490 |
{ |
| 1491 |
/* |
| 1492 |
* Outside double-quoted strings, backslash is only special if |
| 1493 |
* it immediately precedes a double quote. |
| 1494 |
*/ |
| 1495 |
if (*str == '\\' && *(str + 1) == '"') |
| 1496 |
str++; |
| 1497 |
chlen = pg_mblen_cstr(str); |
| 1498 |
|
| 1499 |
if ((flags & DCH_FLAG) && is_separator_char(str)) |
| 1500 |
n->type = NODE_TYPE_SEPARATOR; |
| 1501 |
else if (isspace((unsigned char) *str)) |
| 1502 |
n->type = NODE_TYPE_SPACE; |
| 1503 |
else |
| 1504 |
n->type = NODE_TYPE_CHAR; |
| 1505 |
|
| 1506 |
memcpy(n->character, str, chlen); |
| 1507 |
n->character[chlen] = '\0'; |
| 1508 |
n->key = NULL; |
| 1509 |
n->suffix = 0; |
| 1510 |
n++; |
| 1511 |
str += chlen; |
| 1512 |
} |
| 1513 |
} |
| 1514 |
} |
| 1515 |
|
| 1516 |
n->type = NODE_TYPE_END; |
| 1517 |
n->suffix = 0; |
| 1518 |
} |
| 1519 |
|
| 1520 |
/* ---------- |
| 1521 |
* DEBUG: Dump the FormatNode Tree (debug) |
| 1522 |
* ---------- |
| 1523 |
*/ |
| 1524 |
#ifdef DEBUG_TO_FROM_CHAR |
| 1525 |
|
| 1526 |
#define DUMP_THth(_suf) (S_TH(_suf) ? "TH" : (S_th(_suf) ? "th" : " ")) |
| 1527 |
#define DUMP_FM(_suf) (S_FM(_suf) ? "FM" : " ") |
| 1528 |
|
| 1529 |
static void |
| 1530 |
dump_node(FormatNode *node, int max) |
| 1531 |
{ |
| 1532 |
FormatNode *n; |
| 1533 |
int a; |
| 1534 |
|
| 1535 |
elog(DEBUG_elog_output, "to_from-char(): DUMP FORMAT"); |
| 1536 |
|
| 1537 |
for (a = 0, n = node; a <= max; n++, a++) |
| 1538 |
{ |
| 1539 |
if (n->type == NODE_TYPE_ACTION) |
| 1540 |
elog(DEBUG_elog_output, "%d:\t NODE_TYPE_ACTION '%s'\t(%s,%s)", |
| 1541 |
a, n->key->name, DUMP_THth(n->suffix), DUMP_FM(n->suffix)); |
| 1542 |
else if (n->type == NODE_TYPE_CHAR) |
| 1543 |
elog(DEBUG_elog_output, "%d:\t NODE_TYPE_CHAR '%s'", |
| 1544 |
a, n->character); |
| 1545 |
else if (n->type == NODE_TYPE_END) |
| 1546 |
{ |
| 1547 |
elog(DEBUG_elog_output, "%d:\t NODE_TYPE_END", a); |
| 1548 |
return; |
| 1549 |
} |
| 1550 |
else |
| 1551 |
elog(DEBUG_elog_output, "%d:\t unknown NODE!", a); |
| 1552 |
} |
| 1553 |
} |
| 1554 |
#endif /* DEBUG */ |
| 1555 |
|
| 1556 |
/***************************************************************************** |
| 1557 |
* Private utils |
| 1558 |
*****************************************************************************/ |
| 1559 |
|
| 1560 |
/* ---------- |
| 1561 |
* Return ST/ND/RD/TH for simple (1..9) numbers |
| 1562 |
* type --> 0 upper, 1 lower |
| 1563 |
* ---------- |
| 1564 |
*/ |
| 1565 |
static const char * |
| 1566 |
get_th(char *num, int type) |
| 1567 |
{ |
| 1568 |
int len = strlen(num), |
| 1569 |
last; |
| 1570 |
|
| 1571 |
last = *(num + (len - 1)); |
| 1572 |
if (!isdigit((unsigned char) last)) |
| 1573 |
ereport(ERROR, |
| 1574 |
(errcode(ERRCODE_INVALID_TEXT_REPRESENTATION), |
| 1575 |
errmsg("\"%s\" is not a number", num))); |
| 1576 |
|
| 1577 |
/* |
| 1578 |
* All "teens" (<x>1[0-9]) get 'TH/th', while <x>[02-9][123] still get |
| 1579 |
* 'ST/st', 'ND/nd', 'RD/rd', respectively |
| 1580 |
*/ |
| 1581 |
if ((len > 1) && (num[len - 2] == '1')) |
| 1582 |
last = 0; |
| 1583 |
|
| 1584 |
switch (last) |
| 1585 |
{ |
| 1586 |
case '1': |
| 1587 |
if (type == TH_UPPER) |
| 1588 |
return numTH[0]; |
| 1589 |
return numth[0]; |
| 1590 |
case '2': |
| 1591 |
if (type == TH_UPPER) |
| 1592 |
return numTH[1]; |
| 1593 |
return numth[1]; |
| 1594 |
case '3': |
| 1595 |
if (type == TH_UPPER) |
| 1596 |
return numTH[2]; |
| 1597 |
return numth[2]; |
| 1598 |
default: |
| 1599 |
if (type == TH_UPPER) |
| 1600 |
return numTH[3]; |
| 1601 |
return numth[3]; |
| 1602 |
} |
| 1603 |
} |
| 1604 |
|
| 1605 |
/* ---------- |
| 1606 |
* Convert string-number to ordinal string-number |
| 1607 |
* type --> 0 upper, 1 lower |
| 1608 |
* ---------- |
| 1609 |
*/ |
| 1610 |
static char * |
| 1611 |
str_numth(char *dest, char *num, int type) |
| 1612 |
{ |
| 1613 |
if (dest != num) |
| 1614 |
strcpy(dest, num); |
| 1615 |
strcat(dest, get_th(num, type)); |
| 1616 |
return dest; |
| 1617 |
} |
| 1618 |
|
| 1619 |
/***************************************************************************** |
| 1620 |
* upper/lower/initcap functions |
| 1621 |
*****************************************************************************/ |
| 1622 |
|
| 1623 |
/* |
| 1624 |
* If the system provides the needed functions for wide-character manipulation |
| 1625 |
* (which are all standardized by C99), then we implement upper/lower/initcap |
| 1626 |
* using wide-character functions, if necessary. Otherwise we use the |
| 1627 |
* traditional <ctype.h> functions, which of course will not work as desired |
| 1628 |
* in multibyte character sets. Note that in either case we are effectively |
| 1629 |
* assuming that the database character encoding matches the encoding implied |
| 1630 |
* by LC_CTYPE. |
| 1631 |
*/ |
| 1632 |
|
| 1633 |
/* |
| 1634 |
* collation-aware, wide-character-aware lower function |
| 1635 |
* |
| 1636 |
* We pass the number of bytes so we can pass varlena and char* |
| 1637 |
* to this function. The result is a palloc'd, null-terminated string. |
| 1638 |
*/ |
| 1639 |
char * |
| 1640 |
str_tolower(const char *buff, size_t nbytes, Oid collid) |
| 1641 |
{ |
| 1642 |
char *result; |
| 1643 |
pg_locale_t mylocale; |
| 1644 |
|
| 1645 |
if (!buff) |
| 1646 |
return NULL; |
| 1647 |
|
| 1648 |
if (!OidIsValid(collid)) |
| 1649 |
{ |
| 1650 |
/* |
| 1651 |
* This typically means that the parser could not resolve a conflict |
| 1652 |
* of implicit collations, so report it that way. |
| 1653 |
*/ |
| 1654 |
ereport(ERROR, |
| 1655 |
(errcode(ERRCODE_INDETERMINATE_COLLATION), |
| 1656 |
errmsg("could not determine which collation to use for %s function", |
| 1657 |
"lower()"), |
| 1658 |
errhint("Use the COLLATE clause to set the collation explicitly."))); |
| 1659 |
} |
| 1660 |
|
| 1661 |
mylocale = pg_newlocale_from_collation(collid); |
| 1662 |
|
| 1663 |
/* C/POSIX collations use this path regardless of database encoding */ |
| 1664 |
if (mylocale->ctype_is_c) |
| 1665 |
{ |
| 1666 |
result = asc_tolower(buff, nbytes); |
| 1667 |
} |
| 1668 |
else |
| 1669 |
{ |
| 1670 |
const char *src = buff; |
| 1671 |
size_t srclen = nbytes; |
| 1672 |
size_t dstsize; |
| 1673 |
char *dst; |
| 1674 |
size_t needed; |
| 1675 |
|
| 1676 |
/* first try buffer of equal size plus terminating NUL */ |
| 1677 |
dstsize = srclen + 1; |
| 1678 |
dst = palloc(dstsize); |
| 1679 |
|
| 1680 |
needed = pg_strlower(dst, dstsize, src, srclen, mylocale); |
| 1681 |
if (needed + 1 > dstsize) |
| 1682 |
{ |
| 1683 |
/* grow buffer if needed and retry */ |
| 1684 |
dstsize = needed + 1; |
| 1685 |
dst = repalloc(dst, dstsize); |
| 1686 |
needed = pg_strlower(dst, dstsize, src, srclen, mylocale); |
| 1687 |
Assert(needed + 1 <= dstsize); |
| 1688 |
} |
| 1689 |
|
| 1690 |
Assert(dst[needed] == '\0'); |
| 1691 |
result = dst; |
| 1692 |
} |
| 1693 |
|
| 1694 |
return result; |
| 1695 |
} |
| 1696 |
|
| 1697 |
/* |
| 1698 |
* collation-aware, wide-character-aware upper function |
| 1699 |
* |
| 1700 |
* We pass the number of bytes so we can pass varlena and char* |
| 1701 |
* to this function. The result is a palloc'd, null-terminated string. |
| 1702 |
*/ |
| 1703 |
char * |
| 1704 |
str_toupper(const char *buff, size_t nbytes, Oid collid) |
| 1705 |
{ |
| 1706 |
char *result; |
| 1707 |
pg_locale_t mylocale; |
| 1708 |
|
| 1709 |
if (!buff) |
| 1710 |
return NULL; |
| 1711 |
|
| 1712 |
if (!OidIsValid(collid)) |
| 1713 |
{ |
| 1714 |
/* |
| 1715 |
* This typically means that the parser could not resolve a conflict |
| 1716 |
* of implicit collations, so report it that way. |
| 1717 |
*/ |
| 1718 |
ereport(ERROR, |
| 1719 |
(errcode(ERRCODE_INDETERMINATE_COLLATION), |
| 1720 |
errmsg("could not determine which collation to use for %s function", |
| 1721 |
"upper()"), |
| 1722 |
errhint("Use the COLLATE clause to set the collation explicitly."))); |
| 1723 |
} |
| 1724 |
|
| 1725 |
mylocale = pg_newlocale_from_collation(collid); |
| 1726 |
|
| 1727 |
/* C/POSIX collations use this path regardless of database encoding */ |
| 1728 |
if (mylocale->ctype_is_c) |
| 1729 |
{ |
| 1730 |
result = asc_toupper(buff, nbytes); |
| 1731 |
} |
| 1732 |
else |
| 1733 |
{ |
| 1734 |
const char *src = buff; |
| 1735 |
size_t srclen = nbytes; |
| 1736 |
size_t dstsize; |
| 1737 |
char *dst; |
| 1738 |
size_t needed; |
| 1739 |
|
| 1740 |
/* first try buffer of equal size plus terminating NUL */ |
| 1741 |
dstsize = srclen + 1; |
| 1742 |
dst = palloc(dstsize); |
| 1743 |
|
| 1744 |
needed = pg_strupper(dst, dstsize, src, srclen, mylocale); |
| 1745 |
if (needed + 1 > dstsize) |
| 1746 |
{ |
| 1747 |
/* grow buffer if needed and retry */ |
| 1748 |
dstsize = needed + 1; |
| 1749 |
dst = repalloc(dst, dstsize); |
| 1750 |
needed = pg_strupper(dst, dstsize, src, srclen, mylocale); |
| 1751 |
Assert(needed + 1 <= dstsize); |
| 1752 |
} |
| 1753 |
|
| 1754 |
Assert(dst[needed] == '\0'); |
| 1755 |
result = dst; |
| 1756 |
} |
| 1757 |
|
| 1758 |
return result; |
| 1759 |
} |
| 1760 |
|
| 1761 |
/* |
| 1762 |
* collation-aware, wide-character-aware initcap function |
| 1763 |
* |
| 1764 |
* We pass the number of bytes so we can pass varlena and char* |
| 1765 |
* to this function. The result is a palloc'd, null-terminated string. |
| 1766 |
*/ |
| 1767 |
char * |
| 1768 |
str_initcap(const char *buff, size_t nbytes, Oid collid) |
| 1769 |
{ |
| 1770 |
char *result; |
| 1771 |
pg_locale_t mylocale; |
| 1772 |
|
| 1773 |
if (!buff) |
| 1774 |
return NULL; |
| 1775 |
|
| 1776 |
if (!OidIsValid(collid)) |
| 1777 |
{ |
| 1778 |
/* |
| 1779 |
* This typically means that the parser could not resolve a conflict |
| 1780 |
* of implicit collations, so report it that way. |
| 1781 |
*/ |
| 1782 |
ereport(ERROR, |
| 1783 |
(errcode(ERRCODE_INDETERMINATE_COLLATION), |
| 1784 |
errmsg("could not determine which collation to use for %s function", |
| 1785 |
"initcap()"), |
| 1786 |
errhint("Use the COLLATE clause to set the collation explicitly."))); |
| 1787 |
} |
| 1788 |
|
| 1789 |
mylocale = pg_newlocale_from_collation(collid); |
| 1790 |
|
| 1791 |
/* C/POSIX collations use this path regardless of database encoding */ |
| 1792 |
if (mylocale->ctype_is_c) |
| 1793 |
{ |
| 1794 |
result = asc_initcap(buff, nbytes); |
| 1795 |
} |
| 1796 |
else |
| 1797 |
{ |
| 1798 |
const char *src = buff; |
| 1799 |
size_t srclen = nbytes; |
| 1800 |
size_t dstsize; |
| 1801 |
char *dst; |
| 1802 |
size_t needed; |
| 1803 |
|
| 1804 |
/* first try buffer of equal size plus terminating NUL */ |
| 1805 |
dstsize = srclen + 1; |
| 1806 |
dst = palloc(dstsize); |
| 1807 |
|
| 1808 |
needed = pg_strtitle(dst, dstsize, src, srclen, mylocale); |
| 1809 |
if (needed + 1 > dstsize) |
| 1810 |
{ |
| 1811 |
/* grow buffer if needed and retry */ |
| 1812 |
dstsize = needed + 1; |
| 1813 |
dst = repalloc(dst, dstsize); |
| 1814 |
needed = pg_strtitle(dst, dstsize, src, srclen, mylocale); |
| 1815 |
Assert(needed + 1 <= dstsize); |
| 1816 |
} |
| 1817 |
|
| 1818 |
Assert(dst[needed] == '\0'); |
| 1819 |
result = dst; |
| 1820 |
} |
| 1821 |
|
| 1822 |
return result; |
| 1823 |
} |
| 1824 |
|
| 1825 |
/* |
| 1826 |
* collation-aware, wide-character-aware case folding |
| 1827 |
* |
| 1828 |
* We pass the number of bytes so we can pass varlena and char* |
| 1829 |
* to this function. The result is a palloc'd, null-terminated string. |
| 1830 |
*/ |
| 1831 |
char * |
| 1832 |
str_casefold(const char *buff, size_t nbytes, Oid collid) |
| 1833 |
{ |
| 1834 |
char *result; |
| 1835 |
pg_locale_t mylocale; |
| 1836 |
|
| 1837 |
if (!buff) |
| 1838 |
return NULL; |
| 1839 |
|
| 1840 |
if (!OidIsValid(collid)) |
| 1841 |
{ |
| 1842 |
/* |
| 1843 |
* This typically means that the parser could not resolve a conflict |
| 1844 |
* of implicit collations, so report it that way. |
| 1845 |
*/ |
| 1846 |
ereport(ERROR, |
| 1847 |
(errcode(ERRCODE_INDETERMINATE_COLLATION), |
| 1848 |
errmsg("could not determine which collation to use for %s function", |
| 1849 |
"lower()"), |
| 1850 |
errhint("Use the COLLATE clause to set the collation explicitly."))); |
| 1851 |
} |
| 1852 |
|
| 1853 |
if (GetDatabaseEncoding() != PG_UTF8) |
| 1854 |
ereport(ERROR, |
| 1855 |
(errcode(ERRCODE_SYNTAX_ERROR), |
| 1856 |
errmsg("Unicode case folding can only be performed if server encoding is UTF8"))); |
| 1857 |
|
| 1858 |
mylocale = pg_newlocale_from_collation(collid); |
| 1859 |
|
| 1860 |
/* C/POSIX collations use this path regardless of database encoding */ |
| 1861 |
if (mylocale->ctype_is_c) |
| 1862 |
{ |
| 1863 |
result = asc_tolower(buff, nbytes); |
| 1864 |
} |
| 1865 |
else |
| 1866 |
{ |
| 1867 |
const char *src = buff; |
| 1868 |
size_t srclen = nbytes; |
| 1869 |
size_t dstsize; |
| 1870 |
char *dst; |
| 1871 |
size_t needed; |
| 1872 |
|
| 1873 |
/* first try buffer of equal size plus terminating NUL */ |
| 1874 |
dstsize = srclen + 1; |
| 1875 |
dst = palloc(dstsize); |
| 1876 |
|
| 1877 |
needed = pg_strfold(dst, dstsize, src, srclen, mylocale); |
| 1878 |
if (needed + 1 > dstsize) |
| 1879 |
{ |
| 1880 |
/* grow buffer if needed and retry */ |
| 1881 |
dstsize = needed + 1; |
| 1882 |
dst = repalloc(dst, dstsize); |
| 1883 |
needed = pg_strfold(dst, dstsize, src, srclen, mylocale); |
| 1884 |
Assert(needed + 1 <= dstsize); |
| 1885 |
} |
| 1886 |
|
| 1887 |
Assert(dst[needed] == '\0'); |
| 1888 |
result = dst; |
| 1889 |
} |
| 1890 |
|
| 1891 |
return result; |
| 1892 |
} |
| 1893 |
|
| 1894 |
/* |
| 1895 |
* ASCII-only lower function |
| 1896 |
* |
| 1897 |
* We pass the number of bytes so we can pass varlena and char* |
| 1898 |
* to this function. The result is a palloc'd, null-terminated string. |
| 1899 |
*/ |
| 1900 |
char * |
| 1901 |
asc_tolower(const char *buff, size_t nbytes) |
| 1902 |
{ |
| 1903 |
char *result; |
| 1904 |
char *p; |
| 1905 |
|
| 1906 |
if (!buff) |
| 1907 |
return NULL; |
| 1908 |
|
| 1909 |
result = pnstrdup(buff, nbytes); |
| 1910 |
|
| 1911 |
for (p = result; *p; p++) |
| 1912 |
*p = pg_ascii_tolower((unsigned char) *p); |
| 1913 |
|
| 1914 |
return result; |
| 1915 |
} |
| 1916 |
|
| 1917 |
/* |
| 1918 |
* ASCII-only upper function |
| 1919 |
* |
| 1920 |
* We pass the number of bytes so we can pass varlena and char* |
| 1921 |
* to this function. The result is a palloc'd, null-terminated string. |
| 1922 |
*/ |
| 1923 |
char * |
| 1924 |
asc_toupper(const char *buff, size_t nbytes) |
| 1925 |
{ |
| 1926 |
char *result; |
| 1927 |
char *p; |
| 1928 |
|
| 1929 |
if (!buff) |
| 1930 |
return NULL; |
| 1931 |
|
| 1932 |
result = pnstrdup(buff, nbytes); |
| 1933 |
|
| 1934 |
for (p = result; *p; p++) |
| 1935 |
*p = pg_ascii_toupper((unsigned char) *p); |
| 1936 |
|
| 1937 |
return result; |
| 1938 |
} |
| 1939 |
|
| 1940 |
/* |
| 1941 |
* ASCII-only initcap function |
| 1942 |
* |
| 1943 |
* We pass the number of bytes so we can pass varlena and char* |
| 1944 |
* to this function. The result is a palloc'd, null-terminated string. |
| 1945 |
*/ |
| 1946 |
char * |
| 1947 |
asc_initcap(const char *buff, size_t nbytes) |
| 1948 |
{ |
| 1949 |
char *result; |
| 1950 |
char *p; |
| 1951 |
int wasalnum = false; |
| 1952 |
|
| 1953 |
if (!buff) |
| 1954 |
return NULL; |
| 1955 |
|
| 1956 |
result = pnstrdup(buff, nbytes); |
| 1957 |
|
| 1958 |
for (p = result; *p; p++) |
| 1959 |
{ |
| 1960 |
char c; |
| 1961 |
|
| 1962 |
if (wasalnum) |
| 1963 |
*p = c = pg_ascii_tolower((unsigned char) *p); |
| 1964 |
else |
| 1965 |
*p = c = pg_ascii_toupper((unsigned char) *p); |
| 1966 |
/* we don't trust isalnum() here */ |
| 1967 |
wasalnum = ((c >= 'A' && c <= 'Z') || |
| 1968 |
(c >= 'a' && c <= 'z') || |
| 1969 |
(c >= '0' && c <= '9')); |
| 1970 |
} |
| 1971 |
|
| 1972 |
return result; |
| 1973 |
} |
| 1974 |
|
| 1975 |
/* convenience routines for when the input is null-terminated */ |
| 1976 |
|
| 1977 |
static char * |
| 1978 |
str_tolower_z(const char *buff, Oid collid) |
| 1979 |
{ |
| 1980 |
return str_tolower(buff, strlen(buff), collid); |
| 1981 |
} |
| 1982 |
|
| 1983 |
static char * |
| 1984 |
str_toupper_z(const char *buff, Oid collid) |
| 1985 |
{ |
| 1986 |
return str_toupper(buff, strlen(buff), collid); |
| 1987 |
} |
| 1988 |
|
| 1989 |
static char * |
| 1990 |
str_initcap_z(const char *buff, Oid collid) |
| 1991 |
{ |
| 1992 |
return str_initcap(buff, strlen(buff), collid); |
| 1993 |
} |
| 1994 |
|
| 1995 |
static char * |
| 1996 |
asc_tolower_z(const char *buff) |
| 1997 |
{ |
| 1998 |
return asc_tolower(buff, strlen(buff)); |
| 1999 |
} |
| 2000 |
|
| 2001 |
static char * |
| 2002 |
asc_toupper_z(const char *buff) |
| 2003 |
{ |
| 2004 |
return asc_toupper(buff, strlen(buff)); |
| 2005 |
} |
| 2006 |
|
| 2007 |
/* asc_initcap_z is not currently needed */ |
| 2008 |
|
| 2009 |
|
| 2010 |
/* ---------- |
| 2011 |
* Skip TM / th in FROM_CHAR |
| 2012 |
* |
| 2013 |
* If S_THth is on, skip two chars, assuming there are two available |
| 2014 |
* ---------- |
| 2015 |
*/ |
| 2016 |
#define SKIP_THth(ptr, _suf) \ |
| 2017 |
do { \ |
| 2018 |
if (S_THth(_suf)) \ |
| 2019 |
{ \ |
| 2020 |
if (*(ptr)) (ptr) += pg_mblen_cstr(ptr); \ |
| 2021 |
if (*(ptr)) (ptr) += pg_mblen_cstr(ptr); \ |
| 2022 |
} \ |
| 2023 |
} while (0) |
| 2024 |
|
| 2025 |
|
| 2026 |
#ifdef DEBUG_TO_FROM_CHAR |
| 2027 |
/* ----------- |
| 2028 |
* DEBUG: Call for debug and for index checking; (Show ASCII char |
| 2029 |
* and defined keyword for each used position |
| 2030 |
* ---------- |
| 2031 |
*/ |
| 2032 |
static void |
| 2033 |
dump_index(const KeyWord *k, const int *index) |
| 2034 |
{ |
| 2035 |
int i, |
| 2036 |
count = 0, |
| 2037 |
free_i = 0; |
| 2038 |
|
| 2039 |
elog(DEBUG_elog_output, "TO-FROM_CHAR: Dump KeyWord Index:"); |
| 2040 |
|
| 2041 |
for (i = 0; i < KeyWord_INDEX_SIZE; i++) |
| 2042 |
{ |
| 2043 |
if (index[i] != -1) |
| 2044 |
{ |
| 2045 |
elog(DEBUG_elog_output, "\t%c: %s, ", i + 32, k[index[i]].name); |
| 2046 |
count++; |
| 2047 |
} |
| 2048 |
else |
| 2049 |
{ |
| 2050 |
free_i++; |
| 2051 |
elog(DEBUG_elog_output, "\t(%d) %c %d", i, i + 32, index[i]); |
| 2052 |
} |
| 2053 |
} |
| 2054 |
elog(DEBUG_elog_output, "\n\t\tUsed positions: %d,\n\t\tFree positions: %d", |
| 2055 |
count, free_i); |
| 2056 |
} |
| 2057 |
#endif /* DEBUG */ |
| 2058 |
|
| 2059 |
/* ---------- |
| 2060 |
* Return true if next format picture is not digit value |
| 2061 |
* ---------- |
| 2062 |
*/ |
| 2063 |
static bool |
| 2064 |
is_next_separator(FormatNode *n) |
| 2065 |
{ |
| 2066 |
if (n->type == NODE_TYPE_END) |
| 2067 |
return false; |
| 2068 |
|
| 2069 |
if (n->type == NODE_TYPE_ACTION && S_THth(n->suffix)) |
| 2070 |
return true; |
| 2071 |
|
| 2072 |
/* |
| 2073 |
* Next node |
| 2074 |
*/ |
| 2075 |
n++; |
| 2076 |
|
| 2077 |
/* end of format string is treated like a non-digit separator */ |
| 2078 |
if (n->type == NODE_TYPE_END) |
| 2079 |
return true; |
| 2080 |
|
| 2081 |
if (n->type == NODE_TYPE_ACTION) |
| 2082 |
{ |
| 2083 |
if (n->key->is_digit) |
| 2084 |
return false; |
| 2085 |
|
| 2086 |
return true; |
| 2087 |
} |
| 2088 |
else if (n->character[1] == '\0' && |
| 2089 |
isdigit((unsigned char) n->character[0])) |
| 2090 |
return false; |
| 2091 |
|
| 2092 |
return true; /* some non-digit input (separator) */ |
| 2093 |
} |
| 2094 |
|
| 2095 |
|
| 2096 |
static int |
| 2097 |
adjust_partial_year_to_2020(int year) |
| 2098 |
{ |
| 2099 |
/* |
| 2100 |
* Adjust all dates toward 2020; this is effectively what happens when we |
| 2101 |
* assume '70' is 1970 and '69' is 2069. |
| 2102 |
*/ |
| 2103 |
/* Force 0-69 into the 2000's */ |
| 2104 |
if (year < 70) |
| 2105 |
return year + 2000; |
| 2106 |
/* Force 70-99 into the 1900's */ |
| 2107 |
else if (year < 100) |
| 2108 |
return year + 1900; |
| 2109 |
/* Force 100-519 into the 2000's */ |
| 2110 |
else if (year < 520) |
| 2111 |
return year + 2000; |
| 2112 |
/* Force 520-999 into the 1000's */ |
| 2113 |
else if (year < 1000) |
| 2114 |
return year + 1000; |
| 2115 |
else |
| 2116 |
return year; |
| 2117 |
} |
| 2118 |
|
| 2119 |
|
| 2120 |
static int |
| 2121 |
strspace_len(const char *str) |
| 2122 |
{ |
| 2123 |
int len = 0; |
| 2124 |
|
| 2125 |
while (*str && isspace((unsigned char) *str)) |
| 2126 |
{ |
| 2127 |
str++; |
| 2128 |
len++; |
| 2129 |
} |
| 2130 |
return len; |
| 2131 |
} |
| 2132 |
|
| 2133 |
/* |
| 2134 |
* Set the date mode of a from-char conversion. |
| 2135 |
* |
| 2136 |
* Puke if the date mode has already been set, and the caller attempts to set |
| 2137 |
* it to a conflicting mode. |
| 2138 |
* |
| 2139 |
* Returns true on success, false on failure (if escontext points to an |
| 2140 |
* ErrorSaveContext; otherwise errors are thrown). |
| 2141 |
*/ |
| 2142 |
static bool |
| 2143 |
from_char_set_mode(TmFromChar *tmfc, const FromCharDateMode mode, |
| 2144 |
Node *escontext) |
| 2145 |
{ |
| 2146 |
if (mode != FROM_CHAR_DATE_NONE) |
| 2147 |
{ |
| 2148 |
if (tmfc->mode == FROM_CHAR_DATE_NONE) |
| 2149 |
tmfc->mode = mode; |
| 2150 |
else if (tmfc->mode != mode) |
| 2151 |
ereturn(escontext, false, |
| 2152 |
(errcode(ERRCODE_INVALID_DATETIME_FORMAT), |
| 2153 |
errmsg("invalid combination of date conventions"), |
| 2154 |
errhint("Do not mix Gregorian and ISO week date " |
| 2155 |
"conventions in a formatting template."))); |
| 2156 |
} |
| 2157 |
return true; |
| 2158 |
} |
| 2159 |
|
| 2160 |
/* |
| 2161 |
* Set the integer pointed to by 'dest' to the given value. |
| 2162 |
* |
| 2163 |
* Puke if the destination integer has previously been set to some other |
| 2164 |
* non-zero value. |
| 2165 |
* |
| 2166 |
* Returns true on success, false on failure (if escontext points to an |
| 2167 |
* ErrorSaveContext; otherwise errors are thrown). |
| 2168 |
*/ |
| 2169 |
static bool |
| 2170 |
from_char_set_int(int *dest, const int value, const FormatNode *node, |
| 2171 |
Node *escontext) |
| 2172 |
{ |
| 2173 |
if (*dest != 0 && *dest != value) |
| 2174 |
ereturn(escontext, false, |
| 2175 |
(errcode(ERRCODE_INVALID_DATETIME_FORMAT), |
| 2176 |
errmsg("conflicting values for \"%s\" field in formatting string", |
| 2177 |
node->key->name), |
| 2178 |
errdetail("This value contradicts a previous setting " |
| 2179 |
"for the same field type."))); |
| 2180 |
*dest = value; |
| 2181 |
return true; |
| 2182 |
} |
| 2183 |
|
| 2184 |
/* |
| 2185 |
* Read a single integer from the source string, into the int pointed to by |
| 2186 |
* 'dest'. If 'dest' is NULL, the result is discarded. |
| 2187 |
* |
| 2188 |
* In fixed-width mode (the node does not have the FM suffix), consume at most |
| 2189 |
* 'len' characters. However, any leading whitespace isn't counted in 'len'. |
| 2190 |
* |
| 2191 |
* We use strtol() to recover the integer value from the source string, in |
| 2192 |
* accordance with the given FormatNode. |
| 2193 |
* |
| 2194 |
* If the conversion completes successfully, src will have been advanced to |
| 2195 |
* point at the character immediately following the last character used in the |
| 2196 |
* conversion. |
| 2197 |
* |
| 2198 |
* Returns the number of characters consumed, or -1 on error (if escontext |
| 2199 |
* points to an ErrorSaveContext; otherwise errors are thrown). |
| 2200 |
* |
| 2201 |
* Note that from_char_parse_int() provides a more convenient wrapper where |
| 2202 |
* the length of the field is the same as the length of the format keyword (as |
| 2203 |
* with DD and MI). |
| 2204 |
*/ |
| 2205 |
static int |
| 2206 |
from_char_parse_int_len(int *dest, const char **src, const int len, FormatNode *node, |
| 2207 |
Node *escontext) |
| 2208 |
{ |
| 2209 |
long result; |
| 2210 |
char copy[DCH_MAX_ITEM_SIZ + 1]; |
| 2211 |
const char *init = *src; |
| 2212 |
int used; |
| 2213 |
|
| 2214 |
/* |
| 2215 |
* Skip any whitespace before parsing the integer. |
| 2216 |
*/ |
| 2217 |
*src += strspace_len(*src); |
| 2218 |
|
| 2219 |
Assert(len <= DCH_MAX_ITEM_SIZ); |
| 2220 |
used = (int) strlcpy(copy, *src, len + 1); |
| 2221 |
|
| 2222 |
if (S_FM(node->suffix) || is_next_separator(node)) |
| 2223 |
{ |
| 2224 |
/* |
| 2225 |
* This node is in Fill Mode, or the next node is known to be a |
| 2226 |
* non-digit value, so we just slurp as many characters as we can get. |
| 2227 |
*/ |
| 2228 |
char *endptr; |
| 2229 |
|
| 2230 |
errno = 0; |
| 2231 |
result = strtol(init, &endptr, 10); |
| 2232 |
*src = endptr; |
| 2233 |
} |
| 2234 |
else |
| 2235 |
{ |
| 2236 |
/* |
| 2237 |
* We need to pull exactly the number of characters given in 'len' out |
| 2238 |
* of the string, and convert those. |
| 2239 |
*/ |
| 2240 |
char *last; |
| 2241 |
|
| 2242 |
if (used < len) |
| 2243 |
ereturn(escontext, -1, |
| 2244 |
(errcode(ERRCODE_INVALID_DATETIME_FORMAT), |
| 2245 |
errmsg("source string too short for \"%s\" formatting field", |
| 2246 |
node->key->name), |
| 2247 |
errdetail("Field requires %d characters, but only %d remain.", |
| 2248 |
len, used), |
| 2249 |
errhint("If your source string is not fixed-width, " |
| 2250 |
"try using the \"FM\" modifier."))); |
| 2251 |
|
| 2252 |
errno = 0; |
| 2253 |
result = strtol(copy, &last, 10); |
| 2254 |
used = last - copy; |
| 2255 |
|
| 2256 |
if (used > 0 && used < len) |
| 2257 |
ereturn(escontext, -1, |
| 2258 |
(errcode(ERRCODE_INVALID_DATETIME_FORMAT), |
| 2259 |
errmsg("invalid value \"%s\" for \"%s\"", |
| 2260 |
copy, node->key->name), |
| 2261 |
errdetail("Field requires %d characters, but only %d could be parsed.", |
| 2262 |
len, used), |
| 2263 |
errhint("If your source string is not fixed-width, " |
| 2264 |
"try using the \"FM\" modifier."))); |
| 2265 |
|
| 2266 |
*src += used; |
| 2267 |
} |
| 2268 |
|
| 2269 |
if (*src == init) |
| 2270 |
ereturn(escontext, -1, |
| 2271 |
(errcode(ERRCODE_INVALID_DATETIME_FORMAT), |
| 2272 |
errmsg("invalid value \"%s\" for \"%s\"", |
| 2273 |
copy, node->key->name), |
| 2274 |
errdetail("Value must be an integer."))); |
| 2275 |
|
| 2276 |
if (errno == ERANGE || result < INT_MIN || result > INT_MAX) |
| 2277 |
ereturn(escontext, -1, |
| 2278 |
(errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), |
| 2279 |
errmsg("value for \"%s\" in source string is out of range", |
| 2280 |
node->key->name), |
| 2281 |
errdetail("Value must be in the range %d to %d.", |
| 2282 |
INT_MIN, INT_MAX))); |
| 2283 |
|
| 2284 |
if (dest != NULL) |
| 2285 |
{ |
| 2286 |
if (!from_char_set_int(dest, (int) result, node, escontext)) |
| 2287 |
return -1; |
| 2288 |
} |
| 2289 |
|
| 2290 |
return *src - init; |
| 2291 |
} |
| 2292 |
|
| 2293 |
/* |
| 2294 |
* Call from_char_parse_int_len(), using the length of the format keyword as |
| 2295 |
* the expected length of the field. |
| 2296 |
* |
| 2297 |
* Don't call this function if the field differs in length from the format |
| 2298 |
* keyword (as with HH24; the keyword length is 4, but the field length is 2). |
| 2299 |
* In such cases, call from_char_parse_int_len() instead to specify the |
| 2300 |
* required length explicitly. |
| 2301 |
*/ |
| 2302 |
static int |
| 2303 |
from_char_parse_int(int *dest, const char **src, FormatNode *node, |
| 2304 |
Node *escontext) |
| 2305 |
{ |
| 2306 |
return from_char_parse_int_len(dest, src, node->key->len, node, escontext); |
| 2307 |
} |
| 2308 |
|
| 2309 |
/* |
| 2310 |
* Sequentially search null-terminated "array" for a case-insensitive match |
| 2311 |
* to the initial character(s) of "name". |
| 2312 |
* |
| 2313 |
* Returns array index of match, or -1 for no match. |
| 2314 |
* |
| 2315 |
* *len is set to the length of the match, or 0 for no match. |
| 2316 |
* |
| 2317 |
* Case-insensitivity is defined per pg_ascii_tolower, so this is only |
| 2318 |
* suitable for comparisons to ASCII strings. |
| 2319 |
*/ |
| 2320 |
static int |
| 2321 |
seq_search_ascii(const char *name, const char *const *array, size_t *len) |
| 2322 |
{ |
| 2323 |
unsigned char firstc; |
| 2324 |
const char *const *a; |
| 2325 |
|
| 2326 |
*len = 0; |
| 2327 |
|
| 2328 |
/* empty string can't match anything */ |
| 2329 |
if (!*name) |
| 2330 |
return -1; |
| 2331 |
|
| 2332 |
/* we handle first char specially to gain some speed */ |
| 2333 |
firstc = pg_ascii_tolower((unsigned char) *name); |
| 2334 |
|
| 2335 |
for (a = array; *a != NULL; a++) |
| 2336 |
{ |
| 2337 |
const char *p; |
| 2338 |
const char *n; |
| 2339 |
|
| 2340 |
/* compare first chars */ |
| 2341 |
if (pg_ascii_tolower((unsigned char) **a) != firstc) |
| 2342 |
continue; |
| 2343 |
|
| 2344 |
/* compare rest of string */ |
| 2345 |
for (p = *a + 1, n = name + 1;; p++, n++) |
| 2346 |
{ |
| 2347 |
/* return success if we matched whole array entry */ |
| 2348 |
if (*p == '\0') |
| 2349 |
{ |
| 2350 |
*len = n - name; |
| 2351 |
return a - array; |
| 2352 |
} |
| 2353 |
/* else, must have another character in "name" ... */ |
| 2354 |
if (*n == '\0') |
| 2355 |
break; |
| 2356 |
/* ... and it must match */ |
| 2357 |
if (pg_ascii_tolower((unsigned char) *p) != |
| 2358 |
pg_ascii_tolower((unsigned char) *n)) |
| 2359 |
break; |
| 2360 |
} |
| 2361 |
} |
| 2362 |
|
| 2363 |
return -1; |
| 2364 |
} |
| 2365 |
|
| 2366 |
/* |
| 2367 |
* Compare 'name' with 'element' in a case-insensitive way, by first |
| 2368 |
* converting 'name' to upper case, then lower case. ('element' is already |
| 2369 |
* case-folded that way.) |
| 2370 |
* |
| 2371 |
* A helper function for seq_search_localized(). |
| 2372 |
*/ |
| 2373 |
static bool |
| 2374 |
casefold_str_cmp(const char *name, size_t name_len, |
| 2375 |
const char *element, size_t element_len, |
| 2376 |
pg_locale_t mylocale) |
| 2377 |
{ |
| 2378 |
/* |
| 2379 |
* 'name' is expected to fit in MAX_L10N_DATA, even with the case |
| 2380 |
* conversions. |
| 2381 |
*/ |
| 2382 |
char upper_substr[MAX_L10N_DATA]; |
| 2383 |
size_t upper_substr_len; |
| 2384 |
char lower_substr[MAX_L10N_DATA]; |
| 2385 |
size_t lower_substr_len; |
| 2386 |
|
| 2387 |
upper_substr_len = pg_strupper(upper_substr, sizeof(upper_substr), |
| 2388 |
name, name_len, |
| 2389 |
mylocale); |
| 2390 |
if (upper_substr_len > sizeof(upper_substr) - 1) |
| 2391 |
return false; /* shouldn't happen */ |
| 2392 |
lower_substr_len = pg_strlower(lower_substr, sizeof(lower_substr), |
| 2393 |
upper_substr, upper_substr_len, |
| 2394 |
mylocale); |
| 2395 |
if (lower_substr_len > sizeof(lower_substr) - 1) |
| 2396 |
return false; /* shouldn't happen */ |
| 2397 |
|
| 2398 |
return strcmp(lower_substr, element) == 0; |
| 2399 |
} |
| 2400 |
|
| 2401 |
/* |
| 2402 |
* Sequentially search an array of possibly non-English words for |
| 2403 |
* a case-insensitive match to the initial character(s) of "name". |
| 2404 |
* |
| 2405 |
* This has the same API as seq_search_ascii(), but we use a more general |
| 2406 |
* case-folding transformation to achieve case-insensitivity. Case folding |
| 2407 |
* is done per the rules of the collation identified by "collid". |
| 2408 |
* |
| 2409 |
* The array is treated as const, but we don't declare it that way because |
| 2410 |
* the arrays exported by pg_locale.c aren't const. |
| 2411 |
*/ |
| 2412 |
static int |
| 2413 |
seq_search_localized(const char *name, char **array, size_t *len, Oid collid) |
| 2414 |
{ |
| 2415 |
size_t name_len = strlen(name); |
| 2416 |
const char *name_end = name + name_len; |
| 2417 |
char *upper_name; |
| 2418 |
char *lower_name; |
| 2419 |
pg_locale_t mylocale; |
| 2420 |
|
| 2421 |
*len = 0; |
| 2422 |
|
| 2423 |
/* empty string can't match anything */ |
| 2424 |
if (!*name) |
| 2425 |
return -1; |
| 2426 |
|
| 2427 |
/* |
| 2428 |
* The case-folding processing done below is fairly expensive, so before |
| 2429 |
* doing that, make a quick pass to see if there is an exact match. |
| 2430 |
*/ |
| 2431 |
for (char **a = array; *a != NULL; a++) |
| 2432 |
{ |
| 2433 |
size_t element_len = strlen(*a); |
| 2434 |
|
| 2435 |
if (strncmp(name, *a, element_len) == 0) |
| 2436 |
{ |
| 2437 |
*len = element_len; |
| 2438 |
return a - array; |
| 2439 |
} |
| 2440 |
} |
| 2441 |
|
| 2442 |
mylocale = pg_newlocale_from_collation(collid); |
| 2443 |
|
| 2444 |
/* |
| 2445 |
* Fold to upper case, then to lower case, so that we can match reliably |
| 2446 |
* even in languages in which case conversions are not injective. |
| 2447 |
*/ |
| 2448 |
upper_name = str_toupper(name, name_len, collid); |
| 2449 |
lower_name = str_tolower(upper_name, strlen(upper_name), collid); |
| 2450 |
pfree(upper_name); |
| 2451 |
|
| 2452 |
for (char **a = array; *a != NULL; a++) |
| 2453 |
{ |
| 2454 |
char upper_element[MAX_L10N_DATA]; |
| 2455 |
size_t upper_element_len; |
| 2456 |
char lower_element[MAX_L10N_DATA]; |
| 2457 |
size_t lower_element_len; |
| 2458 |
|
| 2459 |
/* Likewise upper/lower-case array element */ |
| 2460 |
upper_element_len = pg_strupper(upper_element, sizeof(upper_element), |
| 2461 |
*a, strlen(*a), |
| 2462 |
mylocale); |
| 2463 |
if (upper_element_len > sizeof(upper_element) - 1) |
| 2464 |
continue; /* shouldn't happen */ |
| 2465 |
lower_element_len = pg_strlower(lower_element, sizeof(lower_element), |
| 2466 |
upper_element, upper_element_len, |
| 2467 |
mylocale); |
| 2468 |
if (lower_element_len > sizeof(lower_element) - 1) |
| 2469 |
continue; /* shouldn't happen */ |
| 2470 |
|
| 2471 |
/* Is 'lower_element' a prefix of 'lower_name' ? */ |
| 2472 |
if (strncmp(lower_name, lower_element, lower_element_len) == 0) |
| 2473 |
{ |
| 2474 |
/* |
| 2475 |
* We have a match, but we still need to figure out how long the |
| 2476 |
* match is. The case conversions could have changed the lengths |
| 2477 |
* of either string, or both. |
| 2478 |
*/ |
| 2479 |
const char *ep; |
| 2480 |
const char *element_end; |
| 2481 |
size_t element_nchars; |
| 2482 |
size_t substr_len; |
| 2483 |
size_t substr_nchars; |
| 2484 |
|
| 2485 |
/* |
| 2486 |
* First, check the easy case that the string matches as whole. |
| 2487 |
*/ |
| 2488 |
if (strlen(lower_name) == lower_element_len) |
| 2489 |
{ |
| 2490 |
*len = name_len; |
| 2491 |
pfree(lower_name); |
| 2492 |
return a - array; |
| 2493 |
} |
| 2494 |
|
| 2495 |
/* |
| 2496 |
* Another good guess is that the case conversions did not change |
| 2497 |
* the number of characters. |
| 2498 |
*/ |
| 2499 |
|
| 2500 |
/* count characters in the element */ |
| 2501 |
ep = lower_element; |
| 2502 |
element_end = lower_element + lower_element_len; |
| 2503 |
for (element_nchars = 0; ep < element_end; element_nchars++) |
| 2504 |
ep += pg_mblen_range(ep, element_end); |
| 2505 |
|
| 2506 |
/* |
| 2507 |
* count the byte length of a substring of 'name' having the same |
| 2508 |
* character count as the element |
| 2509 |
*/ |
| 2510 |
substr_len = 0; |
| 2511 |
for (substr_nchars = 0; |
| 2512 |
substr_nchars < element_nchars && substr_len < name_len; |
| 2513 |
substr_nchars++) |
| 2514 |
{ |
| 2515 |
substr_len += pg_mblen_range(name + substr_len, name_end); |
| 2516 |
} |
| 2517 |
|
| 2518 |
if (casefold_str_cmp(name, substr_len, lower_element, lower_element_len, mylocale)) |
| 2519 |
{ |
| 2520 |
*len = substr_len; |
| 2521 |
pfree(lower_name); |
| 2522 |
return a - array; |
| 2523 |
} |
| 2524 |
|
| 2525 |
/* |
| 2526 |
* As last resort, try the case conversion and comparison for |
| 2527 |
* every substring from the beginning of the original string until |
| 2528 |
* we find a match. |
| 2529 |
*/ |
| 2530 |
substr_len = 0; |
| 2531 |
while (substr_len < name_len) |
| 2532 |
{ |
| 2533 |
substr_len += pg_mblen_range(name + substr_len, name_end); |
| 2534 |
|
| 2535 |
if (casefold_str_cmp(name, substr_len, |
| 2536 |
lower_element, lower_element_len, |
| 2537 |
mylocale)) |
| 2538 |
{ |
| 2539 |
*len = substr_len; |
| 2540 |
pfree(lower_name); |
| 2541 |
return a - array; |
| 2542 |
} |
| 2543 |
} |
| 2544 |
} |
| 2545 |
} |
| 2546 |
|
| 2547 |
pfree(lower_name); |
| 2548 |
return -1; |
| 2549 |
} |
| 2550 |
|
| 2551 |
/* |
| 2552 |
* Perform a sequential search in 'array' (or 'localized_array', if that's |
| 2553 |
* not NULL) for an entry matching the first character(s) of the 'src' |
| 2554 |
* string case-insensitively. |
| 2555 |
* |
| 2556 |
* The 'array' is presumed to be English words (all-ASCII), but |
| 2557 |
* if 'localized_array' is supplied, that might be non-English |
| 2558 |
* so we need a more expensive case-folding transformation |
| 2559 |
* (which will follow the rules of the collation 'collid'). |
| 2560 |
* |
| 2561 |
* If a match is found, copy the array index of the match into the integer |
| 2562 |
* pointed to by 'dest' and advance 'src' to the end of the part of the string |
| 2563 |
* which matched. |
| 2564 |
* |
| 2565 |
* Returns true on match, false on failure (if escontext points to an |
| 2566 |
* ErrorSaveContext; otherwise errors are thrown). |
| 2567 |
* |
| 2568 |
* 'node' is used only for error reports: node->key->name identifies the |
| 2569 |
* field type we were searching for. |
| 2570 |
*/ |
| 2571 |
static bool |
| 2572 |
from_char_seq_search(int *dest, const char **src, const char *const *array, |
| 2573 |
char **localized_array, Oid collid, |
| 2574 |
FormatNode *node, Node *escontext) |
| 2575 |
{ |
| 2576 |
size_t len; |
| 2577 |
|
| 2578 |
if (localized_array == NULL) |
| 2579 |
*dest = seq_search_ascii(*src, array, &len); |
| 2580 |
else |
| 2581 |
*dest = seq_search_localized(*src, localized_array, &len, collid); |
| 2582 |
|
| 2583 |
if (len <= 0) |
| 2584 |
{ |
| 2585 |
/* |
| 2586 |
* In the error report, truncate the string at the next whitespace (if |
| 2587 |
* any) to avoid including irrelevant data. |
| 2588 |
*/ |
| 2589 |
char *copy = pstrdup(*src); |
| 2590 |
char *c; |
| 2591 |
|
| 2592 |
for (c = copy; *c; c++) |
| 2593 |
{ |
| 2594 |
if (scanner_isspace(*c)) |
| 2595 |
{ |
| 2596 |
*c = '\0'; |
| 2597 |
break; |
| 2598 |
} |
| 2599 |
} |
| 2600 |
|
| 2601 |
ereturn(escontext, false, |
| 2602 |
(errcode(ERRCODE_INVALID_DATETIME_FORMAT), |
| 2603 |
errmsg("invalid value \"%s\" for \"%s\"", |
| 2604 |
copy, node->key->name), |
| 2605 |
errdetail("The given value did not match any of " |
| 2606 |
"the allowed values for this field."))); |
| 2607 |
} |
| 2608 |
*src += len; |
| 2609 |
return true; |
| 2610 |
} |
| 2611 |
|
| 2612 |
/* ---------- |
| 2613 |
* Process a TmToChar struct as denoted by a list of FormatNodes. |
| 2614 |
* The formatted data is written to the string pointed to by 'out'. |
| 2615 |
* ---------- |
| 2616 |
*/ |
| 2617 |
static void |
| 2618 |
DCH_to_char(FormatNode *node, bool is_interval, TmToChar *in, char *out, Oid collid) |
| 2619 |
{ |
| 2620 |
FormatNode *n; |
| 2621 |
char *s; |
| 2622 |
struct fmt_tm *tm = &in->tm; |
| 2623 |
int i; |
| 2624 |
|
| 2625 |
/* cache localized days and months */ |
| 2626 |
cache_locale_time(); |
| 2627 |
|
| 2628 |
s = out; |
| 2629 |
for (n = node; n->type != NODE_TYPE_END; n++) |
| 2630 |
{ |
| 2631 |
if (n->type != NODE_TYPE_ACTION) |
| 2632 |
{ |
| 2633 |
strcpy(s, n->character); |
| 2634 |
s += strlen(s); |
| 2635 |
continue; |
| 2636 |
} |
| 2637 |
|
| 2638 |
switch (n->key->id) |
| 2639 |
{ |
| 2640 |
case DCH_A_M: |
| 2641 |
case DCH_P_M: |
| 2642 |
strcpy(s, (tm->tm_hour % HOURS_PER_DAY >= HOURS_PER_DAY / 2) |
| 2643 |
? P_M_STR : A_M_STR); |
| 2644 |
s += strlen(s); |
| 2645 |
break; |
| 2646 |
case DCH_AM: |
| 2647 |
case DCH_PM: |
| 2648 |
strcpy(s, (tm->tm_hour % HOURS_PER_DAY >= HOURS_PER_DAY / 2) |
| 2649 |
? PM_STR : AM_STR); |
| 2650 |
s += strlen(s); |
| 2651 |
break; |
| 2652 |
case DCH_a_m: |
| 2653 |
case DCH_p_m: |
| 2654 |
strcpy(s, (tm->tm_hour % HOURS_PER_DAY >= HOURS_PER_DAY / 2) |
| 2655 |
? p_m_STR : a_m_STR); |
| 2656 |
s += strlen(s); |
| 2657 |
break; |
| 2658 |
case DCH_am: |
| 2659 |
case DCH_pm: |
| 2660 |
strcpy(s, (tm->tm_hour % HOURS_PER_DAY >= HOURS_PER_DAY / 2) |
| 2661 |
? pm_STR : am_STR); |
| 2662 |
s += strlen(s); |
| 2663 |
break; |
| 2664 |
case DCH_HH: |
| 2665 |
case DCH_HH12: |
| 2666 |
|
| 2667 |
/* |
| 2668 |
* display time as shown on a 12-hour clock, even for |
| 2669 |
* intervals |
| 2670 |
*/ |
| 2671 |
sprintf(s, "%0*lld", S_FM(n->suffix) ? 0 : (tm->tm_hour >= 0) ? 2 : 3, |
| 2672 |
tm->tm_hour % (HOURS_PER_DAY / 2) == 0 ? |
| 2673 |
(long long) (HOURS_PER_DAY / 2) : |
| 2674 |
(long long) (tm->tm_hour % (HOURS_PER_DAY / 2))); |
| 2675 |
if (S_THth(n->suffix)) |
| 2676 |
str_numth(s, s, S_TH_TYPE(n->suffix)); |
| 2677 |
s += strlen(s); |
| 2678 |
break; |
| 2679 |
case DCH_HH24: |
| 2680 |
sprintf(s, "%0*lld", S_FM(n->suffix) ? 0 : (tm->tm_hour >= 0) ? 2 : 3, |
| 2681 |
(long long) tm->tm_hour); |
| 2682 |
if (S_THth(n->suffix)) |
| 2683 |
str_numth(s, s, S_TH_TYPE(n->suffix)); |
| 2684 |
s += strlen(s); |
| 2685 |
break; |
| 2686 |
case DCH_MI: |
| 2687 |
sprintf(s, "%0*d", S_FM(n->suffix) ? 0 : (tm->tm_min >= 0) ? 2 : 3, |
| 2688 |
tm->tm_min); |
| 2689 |
if (S_THth(n->suffix)) |
| 2690 |
str_numth(s, s, S_TH_TYPE(n->suffix)); |
| 2691 |
s += strlen(s); |
| 2692 |
break; |
| 2693 |
case DCH_SS: |
| 2694 |
sprintf(s, "%0*d", S_FM(n->suffix) ? 0 : (tm->tm_sec >= 0) ? 2 : 3, |
| 2695 |
tm->tm_sec); |
| 2696 |
if (S_THth(n->suffix)) |
| 2697 |
str_numth(s, s, S_TH_TYPE(n->suffix)); |
| 2698 |
s += strlen(s); |
| 2699 |
break; |
| 2700 |
|
| 2701 |
#define DCH_to_char_fsec(frac_fmt, frac_val) \ |
| 2702 |
sprintf(s, frac_fmt, (int) (frac_val)); \ |
| 2703 |
if (S_THth(n->suffix)) \ |
| 2704 |
str_numth(s, s, S_TH_TYPE(n->suffix)); \ |
| 2705 |
s += strlen(s) |
| 2706 |
|
| 2707 |
case DCH_FF1: /* tenth of second */ |
| 2708 |
DCH_to_char_fsec("%01d", in->fsec / 100000); |
| 2709 |
break; |
| 2710 |
case DCH_FF2: /* hundredth of second */ |
| 2711 |
DCH_to_char_fsec("%02d", in->fsec / 10000); |
| 2712 |
break; |
| 2713 |
case DCH_FF3: |
| 2714 |
case DCH_MS: /* millisecond */ |
| 2715 |
DCH_to_char_fsec("%03d", in->fsec / 1000); |
| 2716 |
break; |
| 2717 |
case DCH_FF4: /* tenth of a millisecond */ |
| 2718 |
DCH_to_char_fsec("%04d", in->fsec / 100); |
| 2719 |
break; |
| 2720 |
case DCH_FF5: /* hundredth of a millisecond */ |
| 2721 |
DCH_to_char_fsec("%05d", in->fsec / 10); |
| 2722 |
break; |
| 2723 |
case DCH_FF6: |
| 2724 |
case DCH_US: /* microsecond */ |
| 2725 |
DCH_to_char_fsec("%06d", in->fsec); |
| 2726 |
break; |
| 2727 |
#undef DCH_to_char_fsec |
| 2728 |
case DCH_SSSS: |
| 2729 |
sprintf(s, "%lld", |
| 2730 |
(long long) (tm->tm_hour * SECS_PER_HOUR + |
| 2731 |
tm->tm_min * SECS_PER_MINUTE + |
| 2732 |
tm->tm_sec)); |
| 2733 |
if (S_THth(n->suffix)) |
| 2734 |
str_numth(s, s, S_TH_TYPE(n->suffix)); |
| 2735 |
s += strlen(s); |
| 2736 |
break; |
| 2737 |
case DCH_tz: |
| 2738 |
INVALID_FOR_INTERVAL; |
| 2739 |
if (tmtcTzn(in)) |
| 2740 |
{ |
| 2741 |
/* |
| 2742 |
* We assume here that timezone abbreviations aren't |
| 2743 |
* localized, so ASCII-only downcasing is sufficient. |
| 2744 |
*/ |
| 2745 |
char *p = asc_tolower_z(tmtcTzn(in)); |
| 2746 |
|
| 2747 |
if (strlen(p) <= n->key->len * DCH_MAX_ITEM_SIZ) |
| 2748 |
strcpy(s, p); |
| 2749 |
else |
| 2750 |
ereport(ERROR, |
| 2751 |
(errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), |
| 2752 |
errmsg("time zone format value too long"))); |
| 2753 |
pfree(p); |
| 2754 |
s += strlen(s); |
| 2755 |
} |
| 2756 |
break; |
| 2757 |
case DCH_TZ: |
| 2758 |
INVALID_FOR_INTERVAL; |
| 2759 |
if (tmtcTzn(in)) |
| 2760 |
{ |
| 2761 |
const char *p = tmtcTzn(in); |
| 2762 |
|
| 2763 |
if (strlen(p) <= n->key->len * DCH_MAX_ITEM_SIZ) |
| 2764 |
strcpy(s, p); |
| 2765 |
else |
| 2766 |
ereport(ERROR, |
| 2767 |
(errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), |
| 2768 |
errmsg("time zone format value too long"))); |
| 2769 |
s += strlen(s); |
| 2770 |
} |
| 2771 |
break; |
| 2772 |
case DCH_TZH: |
| 2773 |
INVALID_FOR_INTERVAL; |
| 2774 |
sprintf(s, "%c%02d", |
| 2775 |
(tm->tm_gmtoff >= 0) ? '+' : '-', |
| 2776 |
abs((int) tm->tm_gmtoff) / SECS_PER_HOUR); |
| 2777 |
s += strlen(s); |
| 2778 |
break; |
| 2779 |
case DCH_TZM: |
| 2780 |
INVALID_FOR_INTERVAL; |
| 2781 |
sprintf(s, "%02d", |
| 2782 |
(abs((int) tm->tm_gmtoff) % SECS_PER_HOUR) / SECS_PER_MINUTE); |
| 2783 |
s += strlen(s); |
| 2784 |
break; |
| 2785 |
case DCH_OF: |
| 2786 |
INVALID_FOR_INTERVAL; |
| 2787 |
sprintf(s, "%c%0*d", |
| 2788 |
(tm->tm_gmtoff >= 0) ? '+' : '-', |
| 2789 |
S_FM(n->suffix) ? 0 : 2, |
| 2790 |
abs((int) tm->tm_gmtoff) / SECS_PER_HOUR); |
| 2791 |
s += strlen(s); |
| 2792 |
if (abs((int) tm->tm_gmtoff) % SECS_PER_HOUR != 0) |
| 2793 |
{ |
| 2794 |
sprintf(s, ":%02d", |
| 2795 |
(abs((int) tm->tm_gmtoff) % SECS_PER_HOUR) / SECS_PER_MINUTE); |
| 2796 |
s += strlen(s); |
| 2797 |
} |
| 2798 |
break; |
| 2799 |
case DCH_A_D: |
| 2800 |
case DCH_B_C: |
| 2801 |
INVALID_FOR_INTERVAL; |
| 2802 |
strcpy(s, (tm->tm_year <= 0 ? B_C_STR : A_D_STR)); |
| 2803 |
s += strlen(s); |
| 2804 |
break; |
| 2805 |
case DCH_AD: |
| 2806 |
case DCH_BC: |
| 2807 |
INVALID_FOR_INTERVAL; |
| 2808 |
strcpy(s, (tm->tm_year <= 0 ? BC_STR : AD_STR)); |
| 2809 |
s += strlen(s); |
| 2810 |
break; |
| 2811 |
case DCH_a_d: |
| 2812 |
case DCH_b_c: |
| 2813 |
INVALID_FOR_INTERVAL; |
| 2814 |
strcpy(s, (tm->tm_year <= 0 ? b_c_STR : a_d_STR)); |
| 2815 |
s += strlen(s); |
| 2816 |
break; |
| 2817 |
case DCH_ad: |
| 2818 |
case DCH_bc: |
| 2819 |
INVALID_FOR_INTERVAL; |
| 2820 |
strcpy(s, (tm->tm_year <= 0 ? bc_STR : ad_STR)); |
| 2821 |
s += strlen(s); |
| 2822 |
break; |
| 2823 |
case DCH_MONTH: |
| 2824 |
INVALID_FOR_INTERVAL; |
| 2825 |
if (!tm->tm_mon) |
| 2826 |
break; |
| 2827 |
if (S_TM(n->suffix)) |
| 2828 |
{ |
| 2829 |
char *str = str_toupper_z(localized_full_months[tm->tm_mon - 1], collid); |
| 2830 |
|
| 2831 |
if (strlen(str) <= (n->key->len + TM_SUFFIX_LEN) * DCH_MAX_ITEM_SIZ) |
| 2832 |
strcpy(s, str); |
| 2833 |
else |
| 2834 |
ereport(ERROR, |
| 2835 |
(errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), |
| 2836 |
errmsg("localized string format value too long"))); |
| 2837 |
} |
| 2838 |
else |
| 2839 |
sprintf(s, "%*s", S_FM(n->suffix) ? 0 : -9, |
| 2840 |
asc_toupper_z(months_full[tm->tm_mon - 1])); |
| 2841 |
s += strlen(s); |
| 2842 |
break; |
| 2843 |
case DCH_Month: |
| 2844 |
INVALID_FOR_INTERVAL; |
| 2845 |
if (!tm->tm_mon) |
| 2846 |
break; |
| 2847 |
if (S_TM(n->suffix)) |
| 2848 |
{ |
| 2849 |
char *str = str_initcap_z(localized_full_months[tm->tm_mon - 1], collid); |
| 2850 |
|
| 2851 |
if (strlen(str) <= (n->key->len + TM_SUFFIX_LEN) * DCH_MAX_ITEM_SIZ) |
| 2852 |
strcpy(s, str); |
| 2853 |
else |
| 2854 |
ereport(ERROR, |
| 2855 |
(errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), |
| 2856 |
errmsg("localized string format value too long"))); |
| 2857 |
} |
| 2858 |
else |
| 2859 |
sprintf(s, "%*s", S_FM(n->suffix) ? 0 : -9, |
| 2860 |
months_full[tm->tm_mon - 1]); |
| 2861 |
s += strlen(s); |
| 2862 |
break; |
| 2863 |
case DCH_month: |
| 2864 |
INVALID_FOR_INTERVAL; |
| 2865 |
if (!tm->tm_mon) |
| 2866 |
break; |
| 2867 |
if (S_TM(n->suffix)) |
| 2868 |
{ |
| 2869 |
char *str = str_tolower_z(localized_full_months[tm->tm_mon - 1], collid); |
| 2870 |
|
| 2871 |
if (strlen(str) <= (n->key->len + TM_SUFFIX_LEN) * DCH_MAX_ITEM_SIZ) |
| 2872 |
strcpy(s, str); |
| 2873 |
else |
| 2874 |
ereport(ERROR, |
| 2875 |
(errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), |
| 2876 |
errmsg("localized string format value too long"))); |
| 2877 |
} |
| 2878 |
else |
| 2879 |
sprintf(s, "%*s", S_FM(n->suffix) ? 0 : -9, |
| 2880 |
asc_tolower_z(months_full[tm->tm_mon - 1])); |
| 2881 |
s += strlen(s); |
| 2882 |
break; |
| 2883 |
case DCH_MON: |
| 2884 |
INVALID_FOR_INTERVAL; |
| 2885 |
if (!tm->tm_mon) |
| 2886 |
break; |
| 2887 |
if (S_TM(n->suffix)) |
| 2888 |
{ |
| 2889 |
char *str = str_toupper_z(localized_abbrev_months[tm->tm_mon - 1], collid); |
| 2890 |
|
| 2891 |
if (strlen(str) <= (n->key->len + TM_SUFFIX_LEN) * DCH_MAX_ITEM_SIZ) |
| 2892 |
strcpy(s, str); |
| 2893 |
else |
| 2894 |
ereport(ERROR, |
| 2895 |
(errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), |
| 2896 |
errmsg("localized string format value too long"))); |
| 2897 |
} |
| 2898 |
else |
| 2899 |
strcpy(s, asc_toupper_z(months[tm->tm_mon - 1])); |
| 2900 |
s += strlen(s); |
| 2901 |
break; |
| 2902 |
case DCH_Mon: |
| 2903 |
INVALID_FOR_INTERVAL; |
| 2904 |
if (!tm->tm_mon) |
| 2905 |
break; |
| 2906 |
if (S_TM(n->suffix)) |
| 2907 |
{ |
| 2908 |
char *str = str_initcap_z(localized_abbrev_months[tm->tm_mon - 1], collid); |
| 2909 |
|
| 2910 |
if (strlen(str) <= (n->key->len + TM_SUFFIX_LEN) * DCH_MAX_ITEM_SIZ) |
| 2911 |
strcpy(s, str); |
| 2912 |
else |
| 2913 |
ereport(ERROR, |
| 2914 |
(errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), |
| 2915 |
errmsg("localized string format value too long"))); |
| 2916 |
} |
| 2917 |
else |
| 2918 |
strcpy(s, months[tm->tm_mon - 1]); |
| 2919 |
s += strlen(s); |
| 2920 |
break; |
| 2921 |
case DCH_mon: |
| 2922 |
INVALID_FOR_INTERVAL; |
| 2923 |
if (!tm->tm_mon) |
| 2924 |
break; |
| 2925 |
if (S_TM(n->suffix)) |
| 2926 |
{ |
| 2927 |
char *str = str_tolower_z(localized_abbrev_months[tm->tm_mon - 1], collid); |
| 2928 |
|
| 2929 |
if (strlen(str) <= (n->key->len + TM_SUFFIX_LEN) * DCH_MAX_ITEM_SIZ) |
| 2930 |
strcpy(s, str); |
| 2931 |
else |
| 2932 |
ereport(ERROR, |
| 2933 |
(errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), |
| 2934 |
errmsg("localized string format value too long"))); |
| 2935 |
} |
| 2936 |
else |
| 2937 |
strcpy(s, asc_tolower_z(months[tm->tm_mon - 1])); |
| 2938 |
s += strlen(s); |
| 2939 |
break; |
| 2940 |
case DCH_MM: |
| 2941 |
sprintf(s, "%0*d", S_FM(n->suffix) ? 0 : (tm->tm_mon >= 0) ? 2 : 3, |
| 2942 |
tm->tm_mon); |
| 2943 |
if (S_THth(n->suffix)) |
| 2944 |
str_numth(s, s, S_TH_TYPE(n->suffix)); |
| 2945 |
s += strlen(s); |
| 2946 |
break; |
| 2947 |
case DCH_DAY: |
| 2948 |
INVALID_FOR_INTERVAL; |
| 2949 |
if (S_TM(n->suffix)) |
| 2950 |
{ |
| 2951 |
char *str = str_toupper_z(localized_full_days[tm->tm_wday], collid); |
| 2952 |
|
| 2953 |
if (strlen(str) <= (n->key->len + TM_SUFFIX_LEN) * DCH_MAX_ITEM_SIZ) |
| 2954 |
strcpy(s, str); |
| 2955 |
else |
| 2956 |
ereport(ERROR, |
| 2957 |
(errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), |
| 2958 |
errmsg("localized string format value too long"))); |
| 2959 |
} |
| 2960 |
else |
| 2961 |
sprintf(s, "%*s", S_FM(n->suffix) ? 0 : -9, |
| 2962 |
asc_toupper_z(days[tm->tm_wday])); |
| 2963 |
s += strlen(s); |
| 2964 |
break; |
| 2965 |
case DCH_Day: |
| 2966 |
INVALID_FOR_INTERVAL; |
| 2967 |
if (S_TM(n->suffix)) |
| 2968 |
{ |
| 2969 |
char *str = str_initcap_z(localized_full_days[tm->tm_wday], collid); |
| 2970 |
|
| 2971 |
if (strlen(str) <= (n->key->len + TM_SUFFIX_LEN) * DCH_MAX_ITEM_SIZ) |
| 2972 |
strcpy(s, str); |
| 2973 |
else |
| 2974 |
ereport(ERROR, |
| 2975 |
(errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), |
| 2976 |
errmsg("localized string format value too long"))); |
| 2977 |
} |
| 2978 |
else |
| 2979 |
sprintf(s, "%*s", S_FM(n->suffix) ? 0 : -9, |
| 2980 |
days[tm->tm_wday]); |
| 2981 |
s += strlen(s); |
| 2982 |
break; |
| 2983 |
case DCH_day: |
| 2984 |
INVALID_FOR_INTERVAL; |
| 2985 |
if (S_TM(n->suffix)) |
| 2986 |
{ |
| 2987 |
char *str = str_tolower_z(localized_full_days[tm->tm_wday], collid); |
| 2988 |
|
| 2989 |
if (strlen(str) <= (n->key->len + TM_SUFFIX_LEN) * DCH_MAX_ITEM_SIZ) |
| 2990 |
strcpy(s, str); |
| 2991 |
else |
| 2992 |
ereport(ERROR, |
| 2993 |
(errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), |
| 2994 |
errmsg("localized string format value too long"))); |
| 2995 |
} |
| 2996 |
else |
| 2997 |
sprintf(s, "%*s", S_FM(n->suffix) ? 0 : -9, |
| 2998 |
asc_tolower_z(days[tm->tm_wday])); |
| 2999 |
s += strlen(s); |
| 3000 |
break; |
| 3001 |
case DCH_DY: |
| 3002 |
INVALID_FOR_INTERVAL; |
| 3003 |
if (S_TM(n->suffix)) |
| 3004 |
{ |
| 3005 |
char *str = str_toupper_z(localized_abbrev_days[tm->tm_wday], collid); |
| 3006 |
|
| 3007 |
if (strlen(str) <= (n->key->len + TM_SUFFIX_LEN) * DCH_MAX_ITEM_SIZ) |
| 3008 |
strcpy(s, str); |
| 3009 |
else |
| 3010 |
ereport(ERROR, |
| 3011 |
(errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), |
| 3012 |
errmsg("localized string format value too long"))); |
| 3013 |
} |
| 3014 |
else |
| 3015 |
strcpy(s, asc_toupper_z(days_short[tm->tm_wday])); |
| 3016 |
s += strlen(s); |
| 3017 |
break; |
| 3018 |
case DCH_Dy: |
| 3019 |
INVALID_FOR_INTERVAL; |
| 3020 |
if (S_TM(n->suffix)) |
| 3021 |
{ |
| 3022 |
char *str = str_initcap_z(localized_abbrev_days[tm->tm_wday], collid); |
| 3023 |
|
| 3024 |
if (strlen(str) <= (n->key->len + TM_SUFFIX_LEN) * DCH_MAX_ITEM_SIZ) |
| 3025 |
strcpy(s, str); |
| 3026 |
else |
| 3027 |
ereport(ERROR, |
| 3028 |
(errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), |
| 3029 |
errmsg("localized string format value too long"))); |
| 3030 |
} |
| 3031 |
else |
| 3032 |
strcpy(s, days_short[tm->tm_wday]); |
| 3033 |
s += strlen(s); |
| 3034 |
break; |
| 3035 |
case DCH_dy: |
| 3036 |
INVALID_FOR_INTERVAL; |
| 3037 |
if (S_TM(n->suffix)) |
| 3038 |
{ |
| 3039 |
char *str = str_tolower_z(localized_abbrev_days[tm->tm_wday], collid); |
| 3040 |
|
| 3041 |
if (strlen(str) <= (n->key->len + TM_SUFFIX_LEN) * DCH_MAX_ITEM_SIZ) |
| 3042 |
strcpy(s, str); |
| 3043 |
else |
| 3044 |
ereport(ERROR, |
| 3045 |
(errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), |
| 3046 |
errmsg("localized string format value too long"))); |
| 3047 |
} |
| 3048 |
else |
| 3049 |
strcpy(s, asc_tolower_z(days_short[tm->tm_wday])); |
| 3050 |
s += strlen(s); |
| 3051 |
break; |
| 3052 |
case DCH_DDD: |
| 3053 |
case DCH_IDDD: |
| 3054 |
sprintf(s, "%0*d", S_FM(n->suffix) ? 0 : 3, |
| 3055 |
(n->key->id == DCH_DDD) ? |
| 3056 |
tm->tm_yday : |
| 3057 |
date2isoyearday(tm->tm_year, tm->tm_mon, tm->tm_mday)); |
| 3058 |
if (S_THth(n->suffix)) |
| 3059 |
str_numth(s, s, S_TH_TYPE(n->suffix)); |
| 3060 |
s += strlen(s); |
| 3061 |
break; |
| 3062 |
case DCH_DD: |
| 3063 |
sprintf(s, "%0*d", S_FM(n->suffix) ? 0 : 2, tm->tm_mday); |
| 3064 |
if (S_THth(n->suffix)) |
| 3065 |
str_numth(s, s, S_TH_TYPE(n->suffix)); |
| 3066 |
s += strlen(s); |
| 3067 |
break; |
| 3068 |
case DCH_D: |
| 3069 |
INVALID_FOR_INTERVAL; |
| 3070 |
sprintf(s, "%d", tm->tm_wday + 1); |
| 3071 |
if (S_THth(n->suffix)) |
| 3072 |
str_numth(s, s, S_TH_TYPE(n->suffix)); |
| 3073 |
s += strlen(s); |
| 3074 |
break; |
| 3075 |
case DCH_ID: |
| 3076 |
INVALID_FOR_INTERVAL; |
| 3077 |
sprintf(s, "%d", (tm->tm_wday == 0) ? 7 : tm->tm_wday); |
| 3078 |
if (S_THth(n->suffix)) |
| 3079 |
str_numth(s, s, S_TH_TYPE(n->suffix)); |
| 3080 |
s += strlen(s); |
| 3081 |
break; |
| 3082 |
case DCH_WW: |
| 3083 |
sprintf(s, "%0*d", S_FM(n->suffix) ? 0 : 2, |
| 3084 |
(tm->tm_yday - 1) / 7 + 1); |
| 3085 |
if (S_THth(n->suffix)) |
| 3086 |
str_numth(s, s, S_TH_TYPE(n->suffix)); |
| 3087 |
s += strlen(s); |
| 3088 |
break; |
| 3089 |
case DCH_IW: |
| 3090 |
sprintf(s, "%0*d", S_FM(n->suffix) ? 0 : 2, |
| 3091 |
date2isoweek(tm->tm_year, tm->tm_mon, tm->tm_mday)); |
| 3092 |
if (S_THth(n->suffix)) |
| 3093 |
str_numth(s, s, S_TH_TYPE(n->suffix)); |
| 3094 |
s += strlen(s); |
| 3095 |
break; |
| 3096 |
case DCH_Q: |
| 3097 |
if (!tm->tm_mon) |
| 3098 |
break; |
| 3099 |
sprintf(s, "%d", (tm->tm_mon - 1) / 3 + 1); |
| 3100 |
if (S_THth(n->suffix)) |
| 3101 |
str_numth(s, s, S_TH_TYPE(n->suffix)); |
| 3102 |
s += strlen(s); |
| 3103 |
break; |
| 3104 |
case DCH_CC: |
| 3105 |
if (is_interval) /* straight calculation */ |
| 3106 |
i = tm->tm_year / 100; |
| 3107 |
else |
| 3108 |
{ |
| 3109 |
if (tm->tm_year > 0) |
| 3110 |
/* Century 20 == 1901 - 2000 */ |
| 3111 |
i = (tm->tm_year - 1) / 100 + 1; |
| 3112 |
else |
| 3113 |
/* Century 6BC == 600BC - 501BC */ |
| 3114 |
i = tm->tm_year / 100 - 1; |
| 3115 |
} |
| 3116 |
if (i <= 99 && i >= -99) |
| 3117 |
sprintf(s, "%0*d", S_FM(n->suffix) ? 0 : (i >= 0) ? 2 : 3, i); |
| 3118 |
else |
| 3119 |
sprintf(s, "%d", i); |
| 3120 |
if (S_THth(n->suffix)) |
| 3121 |
str_numth(s, s, S_TH_TYPE(n->suffix)); |
| 3122 |
s += strlen(s); |
| 3123 |
break; |
| 3124 |
case DCH_Y_YYY: |
| 3125 |
i = ADJUST_YEAR(tm->tm_year, is_interval) / 1000; |
| 3126 |
sprintf(s, "%d,%03d", i, |
| 3127 |
ADJUST_YEAR(tm->tm_year, is_interval) - (i * 1000)); |
| 3128 |
if (S_THth(n->suffix)) |
| 3129 |
str_numth(s, s, S_TH_TYPE(n->suffix)); |
| 3130 |
s += strlen(s); |
| 3131 |
break; |
| 3132 |
case DCH_YYYY: |
| 3133 |
case DCH_IYYY: |
| 3134 |
sprintf(s, "%0*d", |
| 3135 |
S_FM(n->suffix) ? 0 : |
| 3136 |
(ADJUST_YEAR(tm->tm_year, is_interval) >= 0) ? 4 : 5, |
| 3137 |
(n->key->id == DCH_YYYY ? |
| 3138 |
ADJUST_YEAR(tm->tm_year, is_interval) : |
| 3139 |
ADJUST_YEAR(date2isoyear(tm->tm_year, |
| 3140 |
tm->tm_mon, |
| 3141 |
tm->tm_mday), |
| 3142 |
is_interval))); |
| 3143 |
if (S_THth(n->suffix)) |
| 3144 |
str_numth(s, s, S_TH_TYPE(n->suffix)); |
| 3145 |
s += strlen(s); |
| 3146 |
break; |
| 3147 |
case DCH_YYY: |
| 3148 |
case DCH_IYY: |
| 3149 |
sprintf(s, "%0*d", |
| 3150 |
S_FM(n->suffix) ? 0 : |
| 3151 |
(ADJUST_YEAR(tm->tm_year, is_interval) >= 0) ? 3 : 4, |
| 3152 |
(n->key->id == DCH_YYY ? |
| 3153 |
ADJUST_YEAR(tm->tm_year, is_interval) : |
| 3154 |
ADJUST_YEAR(date2isoyear(tm->tm_year, |
| 3155 |
tm->tm_mon, |
| 3156 |
tm->tm_mday), |
| 3157 |
is_interval)) % 1000); |
| 3158 |
if (S_THth(n->suffix)) |
| 3159 |
str_numth(s, s, S_TH_TYPE(n->suffix)); |
| 3160 |
s += strlen(s); |
| 3161 |
break; |
| 3162 |
case DCH_YY: |
| 3163 |
case DCH_IY: |
| 3164 |
sprintf(s, "%0*d", |
| 3165 |
S_FM(n->suffix) ? 0 : |
| 3166 |
(ADJUST_YEAR(tm->tm_year, is_interval) >= 0) ? 2 : 3, |
| 3167 |
(n->key->id == DCH_YY ? |
| 3168 |
ADJUST_YEAR(tm->tm_year, is_interval) : |
| 3169 |
ADJUST_YEAR(date2isoyear(tm->tm_year, |
| 3170 |
tm->tm_mon, |
| 3171 |
tm->tm_mday), |
| 3172 |
is_interval)) % 100); |
| 3173 |
if (S_THth(n->suffix)) |
| 3174 |
str_numth(s, s, S_TH_TYPE(n->suffix)); |
| 3175 |
s += strlen(s); |
| 3176 |
break; |
| 3177 |
case DCH_Y: |
| 3178 |
case DCH_I: |
| 3179 |
sprintf(s, "%1d", |
| 3180 |
(n->key->id == DCH_Y ? |
| 3181 |
ADJUST_YEAR(tm->tm_year, is_interval) : |
| 3182 |
ADJUST_YEAR(date2isoyear(tm->tm_year, |
| 3183 |
tm->tm_mon, |
| 3184 |
tm->tm_mday), |
| 3185 |
is_interval)) % 10); |
| 3186 |
if (S_THth(n->suffix)) |
| 3187 |
str_numth(s, s, S_TH_TYPE(n->suffix)); |
| 3188 |
s += strlen(s); |
| 3189 |
break; |
| 3190 |
case DCH_RM: |
| 3191 |
/* FALLTHROUGH */ |
| 3192 |
case DCH_rm: |
| 3193 |
|
| 3194 |
/* |
| 3195 |
* For intervals, values like '12 month' will be reduced to 0 |
| 3196 |
* month and some years. These should be processed. |
| 3197 |
*/ |
| 3198 |
if (!tm->tm_mon && !tm->tm_year) |
| 3199 |
break; |
| 3200 |
else |
| 3201 |
{ |
| 3202 |
int mon = 0; |
| 3203 |
const char *const *months; |
| 3204 |
|
| 3205 |
if (n->key->id == DCH_RM) |
| 3206 |
months = rm_months_upper; |
| 3207 |
else |
| 3208 |
months = rm_months_lower; |
| 3209 |
|
| 3210 |
/* |
| 3211 |
* Compute the position in the roman-numeral array. Note |
| 3212 |
* that the contents of the array are reversed, December |
| 3213 |
* being first and January last. |
| 3214 |
*/ |
| 3215 |
if (tm->tm_mon == 0) |
| 3216 |
{ |
| 3217 |
/* |
| 3218 |
* This case is special, and tracks the case of full |
| 3219 |
* interval years. |
| 3220 |
*/ |
| 3221 |
mon = tm->tm_year >= 0 ? 0 : MONTHS_PER_YEAR - 1; |
| 3222 |
} |
| 3223 |
else if (tm->tm_mon < 0) |
| 3224 |
{ |
| 3225 |
/* |
| 3226 |
* Negative case. In this case, the calculation is |
| 3227 |
* reversed, where -1 means December, -2 November, |
| 3228 |
* etc. |
| 3229 |
*/ |
| 3230 |
mon = -1 * (tm->tm_mon + 1); |
| 3231 |
} |
| 3232 |
else |
| 3233 |
{ |
| 3234 |
/* |
| 3235 |
* Common case, with a strictly positive value. The |
| 3236 |
* position in the array matches with the value of |
| 3237 |
* tm_mon. |
| 3238 |
*/ |
| 3239 |
mon = MONTHS_PER_YEAR - tm->tm_mon; |
| 3240 |
} |
| 3241 |
|
| 3242 |
sprintf(s, "%*s", S_FM(n->suffix) ? 0 : -4, |
| 3243 |
months[mon]); |
| 3244 |
s += strlen(s); |
| 3245 |
} |
| 3246 |
break; |
| 3247 |
case DCH_W: |
| 3248 |
sprintf(s, "%d", (tm->tm_mday - 1) / 7 + 1); |
| 3249 |
if (S_THth(n->suffix)) |
| 3250 |
str_numth(s, s, S_TH_TYPE(n->suffix)); |
| 3251 |
s += strlen(s); |
| 3252 |
break; |
| 3253 |
case DCH_J: |
| 3254 |
sprintf(s, "%d", date2j(tm->tm_year, tm->tm_mon, tm->tm_mday)); |
| 3255 |
if (S_THth(n->suffix)) |
| 3256 |
str_numth(s, s, S_TH_TYPE(n->suffix)); |
| 3257 |
s += strlen(s); |
| 3258 |
break; |
| 3259 |
} |
| 3260 |
} |
| 3261 |
|
| 3262 |
*s = '\0'; |
| 3263 |
} |
| 3264 |
|
| 3265 |
/* |
| 3266 |
* Process the string 'in' as denoted by the array of FormatNodes 'node[]'. |
| 3267 |
* The TmFromChar struct pointed to by 'out' is populated with the results. |
| 3268 |
* |
| 3269 |
* 'collid' identifies the collation to use, if needed. |
| 3270 |
* 'std' specifies standard parsing mode. |
| 3271 |
* |
| 3272 |
* If escontext points to an ErrorSaveContext, data errors will be reported |
| 3273 |
* by filling that struct; the caller must test SOFT_ERROR_OCCURRED() to see |
| 3274 |
* whether an error occurred. Otherwise, errors are thrown. |
| 3275 |
* |
| 3276 |
* Note: we currently don't have any to_interval() function, so there |
| 3277 |
* is no need here for INVALID_FOR_INTERVAL checks. |
| 3278 |
*/ |
| 3279 |
static void |
| 3280 |
DCH_from_char(FormatNode *node, const char *in, TmFromChar *out, |
| 3281 |
Oid collid, bool std, Node *escontext) |
| 3282 |
{ |
| 3283 |
FormatNode *n; |
| 3284 |
const char *s; |
| 3285 |
int len, |
| 3286 |
value; |
| 3287 |
bool fx_mode = std; |
| 3288 |
|
| 3289 |
/* number of extra skipped characters (more than given in format string) */ |
| 3290 |
int extra_skip = 0; |
| 3291 |
|
| 3292 |
/* cache localized days and months */ |
| 3293 |
cache_locale_time(); |
| 3294 |
|
| 3295 |
for (n = node, s = in; n->type != NODE_TYPE_END && *s != '\0'; n++) |
| 3296 |
{ |
| 3297 |
/* |
| 3298 |
* Ignore spaces at the beginning of the string and before fields when |
| 3299 |
* not in FX (fixed width) mode. |
| 3300 |
*/ |
| 3301 |
if (!fx_mode && (n->type != NODE_TYPE_ACTION || n->key->id != DCH_FX) && |
| 3302 |
(n->type == NODE_TYPE_ACTION || n == node)) |
| 3303 |
{ |
| 3304 |
while (*s != '\0' && isspace((unsigned char) *s)) |
| 3305 |
{ |
| 3306 |
s++; |
| 3307 |
extra_skip++; |
| 3308 |
} |
| 3309 |
} |
| 3310 |
|
| 3311 |
if (n->type == NODE_TYPE_SPACE || n->type == NODE_TYPE_SEPARATOR) |
| 3312 |
{ |
| 3313 |
if (std) |
| 3314 |
{ |
| 3315 |
/* |
| 3316 |
* Standard mode requires strict matching between format |
| 3317 |
* string separators/spaces and input string. |
| 3318 |
*/ |
| 3319 |
Assert(n->character[0] && !n->character[1]); |
| 3320 |
|
| 3321 |
if (*s == n->character[0]) |
| 3322 |
s++; |
| 3323 |
else |
| 3324 |
ereturn(escontext,, |
| 3325 |
(errcode(ERRCODE_INVALID_DATETIME_FORMAT), |
| 3326 |
errmsg("unmatched format separator \"%c\"", |
| 3327 |
n->character[0]))); |
| 3328 |
} |
| 3329 |
else if (!fx_mode) |
| 3330 |
{ |
| 3331 |
/* |
| 3332 |
* In non FX (fixed format) mode one format string space or |
| 3333 |
* separator match to one space or separator in input string. |
| 3334 |
* Or match nothing if there is no space or separator in the |
| 3335 |
* current position of input string. |
| 3336 |
*/ |
| 3337 |
extra_skip--; |
| 3338 |
if (isspace((unsigned char) *s) || is_separator_char(s)) |
| 3339 |
{ |
| 3340 |
s++; |
| 3341 |
extra_skip++; |
| 3342 |
} |
| 3343 |
} |
| 3344 |
else |
| 3345 |
{ |
| 3346 |
/* |
| 3347 |
* In FX mode, on format string space or separator we consume |
| 3348 |
* exactly one character from input string. Notice we don't |
| 3349 |
* insist that the consumed character match the format's |
| 3350 |
* character. |
| 3351 |
*/ |
| 3352 |
s += pg_mblen_cstr(s); |
| 3353 |
} |
| 3354 |
continue; |
| 3355 |
} |
| 3356 |
else if (n->type != NODE_TYPE_ACTION) |
| 3357 |
{ |
| 3358 |
/* |
| 3359 |
* Text character, so consume one character from input string. |
| 3360 |
* Notice we don't insist that the consumed character match the |
| 3361 |
* format's character. |
| 3362 |
*/ |
| 3363 |
if (!fx_mode) |
| 3364 |
{ |
| 3365 |
/* |
| 3366 |
* In non FX mode we might have skipped some extra characters |
| 3367 |
* (more than specified in format string) before. In this |
| 3368 |
* case we don't skip input string character, because it might |
| 3369 |
* be part of field. |
| 3370 |
*/ |
| 3371 |
if (extra_skip > 0) |
| 3372 |
extra_skip--; |
| 3373 |
else |
| 3374 |
s += pg_mblen_cstr(s); |
| 3375 |
} |
| 3376 |
else |
| 3377 |
{ |
| 3378 |
int chlen = pg_mblen_cstr(s); |
| 3379 |
|
| 3380 |
/* |
| 3381 |
* Standard mode requires strict match of format characters. |
| 3382 |
*/ |
| 3383 |
if (std && n->type == NODE_TYPE_CHAR && |
| 3384 |
strncmp(s, n->character, chlen) != 0) |
| 3385 |
ereturn(escontext,, |
| 3386 |
(errcode(ERRCODE_INVALID_DATETIME_FORMAT), |
| 3387 |
errmsg("unmatched format character \"%s\"", |
| 3388 |
n->character))); |
| 3389 |
|
| 3390 |
s += chlen; |
| 3391 |
} |
| 3392 |
continue; |
| 3393 |
} |
| 3394 |
|
| 3395 |
if (!from_char_set_mode(out, n->key->date_mode, escontext)) |
| 3396 |
return; |
| 3397 |
|
| 3398 |
switch (n->key->id) |
| 3399 |
{ |
| 3400 |
case DCH_FX: |
| 3401 |
fx_mode = true; |
| 3402 |
break; |
| 3403 |
case DCH_A_M: |
| 3404 |
case DCH_P_M: |
| 3405 |
case DCH_a_m: |
| 3406 |
case DCH_p_m: |
| 3407 |
if (!from_char_seq_search(&value, &s, ampm_strings_long, |
| 3408 |
NULL, InvalidOid, |
| 3409 |
n, escontext)) |
| 3410 |
return; |
| 3411 |
if (!from_char_set_int(&out->pm, value % 2, n, escontext)) |
| 3412 |
return; |
| 3413 |
out->clock = CLOCK_12_HOUR; |
| 3414 |
break; |
| 3415 |
case DCH_AM: |
| 3416 |
case DCH_PM: |
| 3417 |
case DCH_am: |
| 3418 |
case DCH_pm: |
| 3419 |
if (!from_char_seq_search(&value, &s, ampm_strings, |
| 3420 |
NULL, InvalidOid, |
| 3421 |
n, escontext)) |
| 3422 |
return; |
| 3423 |
if (!from_char_set_int(&out->pm, value % 2, n, escontext)) |
| 3424 |
return; |
| 3425 |
out->clock = CLOCK_12_HOUR; |
| 3426 |
break; |
| 3427 |
case DCH_HH: |
| 3428 |
case DCH_HH12: |
| 3429 |
if (from_char_parse_int_len(&out->hh, &s, 2, n, escontext) < 0) |
| 3430 |
return; |
| 3431 |
out->clock = CLOCK_12_HOUR; |
| 3432 |
SKIP_THth(s, n->suffix); |
| 3433 |
break; |
| 3434 |
case DCH_HH24: |
| 3435 |
if (from_char_parse_int_len(&out->hh, &s, 2, n, escontext) < 0) |
| 3436 |
return; |
| 3437 |
SKIP_THth(s, n->suffix); |
| 3438 |
break; |
| 3439 |
case DCH_MI: |
| 3440 |
if (from_char_parse_int(&out->mi, &s, n, escontext) < 0) |
| 3441 |
return; |
| 3442 |
SKIP_THth(s, n->suffix); |
| 3443 |
break; |
| 3444 |
case DCH_SS: |
| 3445 |
if (from_char_parse_int(&out->ss, &s, n, escontext) < 0) |
| 3446 |
return; |
| 3447 |
SKIP_THth(s, n->suffix); |
| 3448 |
break; |
| 3449 |
case DCH_MS: /* millisecond */ |
| 3450 |
len = from_char_parse_int_len(&out->ms, &s, 3, n, escontext); |
| 3451 |
if (len < 0) |
| 3452 |
return; |
| 3453 |
|
| 3454 |
/* |
| 3455 |
* 25 is 0.25 and 250 is 0.25 too; 025 is 0.025 and not 0.25 |
| 3456 |
*/ |
| 3457 |
out->ms *= len == 1 ? 100 : |
| 3458 |
len == 2 ? 10 : 1; |
| 3459 |
|
| 3460 |
SKIP_THth(s, n->suffix); |
| 3461 |
break; |
| 3462 |
case DCH_FF1: |
| 3463 |
case DCH_FF2: |
| 3464 |
case DCH_FF3: |
| 3465 |
case DCH_FF4: |
| 3466 |
case DCH_FF5: |
| 3467 |
case DCH_FF6: |
| 3468 |
out->ff = n->key->id - DCH_FF1 + 1; |
| 3469 |
/* FALLTHROUGH */ |
| 3470 |
case DCH_US: /* microsecond */ |
| 3471 |
len = from_char_parse_int_len(&out->us, &s, |
| 3472 |
n->key->id == DCH_US ? 6 : |
| 3473 |
out->ff, n, escontext); |
| 3474 |
if (len < 0) |
| 3475 |
return; |
| 3476 |
|
| 3477 |
out->us *= len == 1 ? 100000 : |
| 3478 |
len == 2 ? 10000 : |
| 3479 |
len == 3 ? 1000 : |
| 3480 |
len == 4 ? 100 : |
| 3481 |
len == 5 ? 10 : 1; |
| 3482 |
|
| 3483 |
SKIP_THth(s, n->suffix); |
| 3484 |
break; |
| 3485 |
case DCH_SSSS: |
| 3486 |
if (from_char_parse_int(&out->ssss, &s, n, escontext) < 0) |
| 3487 |
return; |
| 3488 |
SKIP_THth(s, n->suffix); |
| 3489 |
break; |
| 3490 |
case DCH_tz: |
| 3491 |
case DCH_TZ: |
| 3492 |
{ |
| 3493 |
int tzlen; |
| 3494 |
|
| 3495 |
tzlen = DecodeTimezoneAbbrevPrefix(s, |
| 3496 |
&out->gmtoffset, |
| 3497 |
&out->tzp); |
| 3498 |
if (tzlen > 0) |
| 3499 |
{ |
| 3500 |
out->has_tz = true; |
| 3501 |
/* we only need the zone abbrev for DYNTZ case */ |
| 3502 |
if (out->tzp) |
| 3503 |
out->abbrev = pnstrdup(s, tzlen); |
| 3504 |
out->tzsign = 0; /* drop any earlier TZH/TZM info */ |
| 3505 |
s += tzlen; |
| 3506 |
break; |
| 3507 |
} |
| 3508 |
else if (isalpha((unsigned char) *s)) |
| 3509 |
{ |
| 3510 |
/* |
| 3511 |
* It doesn't match any abbreviation, but it starts |
| 3512 |
* with a letter. OF format certainly won't succeed; |
| 3513 |
* assume it's a misspelled abbreviation and complain |
| 3514 |
* accordingly. |
| 3515 |
*/ |
| 3516 |
ereturn(escontext,, |
| 3517 |
(errcode(ERRCODE_INVALID_DATETIME_FORMAT), |
| 3518 |
errmsg("invalid value \"%s\" for \"%s\"", |
| 3519 |
s, n->key->name), |
| 3520 |
errdetail("Time zone abbreviation is not recognized."))); |
| 3521 |
} |
| 3522 |
/* otherwise parse it like OF */ |
| 3523 |
} |
| 3524 |
/* FALLTHROUGH */ |
| 3525 |
case DCH_OF: |
| 3526 |
/* OF is equivalent to TZH or TZH:TZM */ |
| 3527 |
/* see TZH comments below */ |
| 3528 |
if (*s == '+' || *s == '-' || *s == ' ') |
| 3529 |
{ |
| 3530 |
out->tzsign = *s == '-' ? -1 : +1; |
| 3531 |
s++; |
| 3532 |
} |
| 3533 |
else |
| 3534 |
{ |
| 3535 |
if (extra_skip > 0 && *(s - 1) == '-') |
| 3536 |
out->tzsign = -1; |
| 3537 |
else |
| 3538 |
out->tzsign = +1; |
| 3539 |
} |
| 3540 |
if (from_char_parse_int_len(&out->tzh, &s, 2, n, escontext) < 0) |
| 3541 |
return; |
| 3542 |
if (*s == ':') |
| 3543 |
{ |
| 3544 |
s++; |
| 3545 |
if (from_char_parse_int_len(&out->tzm, &s, 2, n, |
| 3546 |
escontext) < 0) |
| 3547 |
return; |
| 3548 |
} |
| 3549 |
break; |
| 3550 |
case DCH_TZH: |
| 3551 |
|
| 3552 |
/* |
| 3553 |
* Value of TZH might be negative. And the issue is that we |
| 3554 |
* might swallow minus sign as the separator. So, if we have |
| 3555 |
* skipped more characters than specified in the format |
| 3556 |
* string, then we consider prepending last skipped minus to |
| 3557 |
* TZH. |
| 3558 |
*/ |
| 3559 |
if (*s == '+' || *s == '-' || *s == ' ') |
| 3560 |
{ |
| 3561 |
out->tzsign = *s == '-' ? -1 : +1; |
| 3562 |
s++; |
| 3563 |
} |
| 3564 |
else |
| 3565 |
{ |
| 3566 |
if (extra_skip > 0 && *(s - 1) == '-') |
| 3567 |
out->tzsign = -1; |
| 3568 |
else |
| 3569 |
out->tzsign = +1; |
| 3570 |
} |
| 3571 |
|
| 3572 |
if (from_char_parse_int_len(&out->tzh, &s, 2, n, escontext) < 0) |
| 3573 |
return; |
| 3574 |
break; |
| 3575 |
case DCH_TZM: |
| 3576 |
/* assign positive timezone sign if TZH was not seen before */ |
| 3577 |
if (!out->tzsign) |
| 3578 |
out->tzsign = +1; |
| 3579 |
if (from_char_parse_int_len(&out->tzm, &s, 2, n, escontext) < 0) |
| 3580 |
return; |
| 3581 |
break; |
| 3582 |
case DCH_A_D: |
| 3583 |
case DCH_B_C: |
| 3584 |
case DCH_a_d: |
| 3585 |
case DCH_b_c: |
| 3586 |
if (!from_char_seq_search(&value, &s, adbc_strings_long, |
| 3587 |
NULL, InvalidOid, |
| 3588 |
n, escontext)) |
| 3589 |
return; |
| 3590 |
if (!from_char_set_int(&out->bc, value % 2, n, escontext)) |
| 3591 |
return; |
| 3592 |
break; |
| 3593 |
case DCH_AD: |
| 3594 |
case DCH_BC: |
| 3595 |
case DCH_ad: |
| 3596 |
case DCH_bc: |
| 3597 |
if (!from_char_seq_search(&value, &s, adbc_strings, |
| 3598 |
NULL, InvalidOid, |
| 3599 |
n, escontext)) |
| 3600 |
return; |
| 3601 |
if (!from_char_set_int(&out->bc, value % 2, n, escontext)) |
| 3602 |
return; |
| 3603 |
break; |
| 3604 |
case DCH_MONTH: |
| 3605 |
case DCH_Month: |
| 3606 |
case DCH_month: |
| 3607 |
if (!from_char_seq_search(&value, &s, months_full, |
| 3608 |
S_TM(n->suffix) ? localized_full_months : NULL, |
| 3609 |
collid, |
| 3610 |
n, escontext)) |
| 3611 |
return; |
| 3612 |
if (!from_char_set_int(&out->mm, value + 1, n, escontext)) |
| 3613 |
return; |
| 3614 |
break; |
| 3615 |
case DCH_MON: |
| 3616 |
case DCH_Mon: |
| 3617 |
case DCH_mon: |
| 3618 |
if (!from_char_seq_search(&value, &s, months, |
| 3619 |
S_TM(n->suffix) ? localized_abbrev_months : NULL, |
| 3620 |
collid, |
| 3621 |
n, escontext)) |
| 3622 |
return; |
| 3623 |
if (!from_char_set_int(&out->mm, value + 1, n, escontext)) |
| 3624 |
return; |
| 3625 |
break; |
| 3626 |
case DCH_MM: |
| 3627 |
if (from_char_parse_int(&out->mm, &s, n, escontext) < 0) |
| 3628 |
return; |
| 3629 |
SKIP_THth(s, n->suffix); |
| 3630 |
break; |
| 3631 |
case DCH_DAY: |
| 3632 |
case DCH_Day: |
| 3633 |
case DCH_day: |
| 3634 |
if (!from_char_seq_search(&value, &s, days, |
| 3635 |
S_TM(n->suffix) ? localized_full_days : NULL, |
| 3636 |
collid, |
| 3637 |
n, escontext)) |
| 3638 |
return; |
| 3639 |
if (!from_char_set_int(&out->d, value, n, escontext)) |
| 3640 |
return; |
| 3641 |
out->d++; |
| 3642 |
break; |
| 3643 |
case DCH_DY: |
| 3644 |
case DCH_Dy: |
| 3645 |
case DCH_dy: |
| 3646 |
if (!from_char_seq_search(&value, &s, days_short, |
| 3647 |
S_TM(n->suffix) ? localized_abbrev_days : NULL, |
| 3648 |
collid, |
| 3649 |
n, escontext)) |
| 3650 |
return; |
| 3651 |
if (!from_char_set_int(&out->d, value, n, escontext)) |
| 3652 |
return; |
| 3653 |
out->d++; |
| 3654 |
break; |
| 3655 |
case DCH_DDD: |
| 3656 |
if (from_char_parse_int(&out->ddd, &s, n, escontext) < 0) |
| 3657 |
return; |
| 3658 |
SKIP_THth(s, n->suffix); |
| 3659 |
break; |
| 3660 |
case DCH_IDDD: |
| 3661 |
if (from_char_parse_int_len(&out->ddd, &s, 3, n, escontext) < 0) |
| 3662 |
return; |
| 3663 |
SKIP_THth(s, n->suffix); |
| 3664 |
break; |
| 3665 |
case DCH_DD: |
| 3666 |
if (from_char_parse_int(&out->dd, &s, n, escontext) < 0) |
| 3667 |
return; |
| 3668 |
SKIP_THth(s, n->suffix); |
| 3669 |
break; |
| 3670 |
case DCH_D: |
| 3671 |
if (from_char_parse_int(&out->d, &s, n, escontext) < 0) |
| 3672 |
return; |
| 3673 |
SKIP_THth(s, n->suffix); |
| 3674 |
break; |
| 3675 |
case DCH_ID: |
| 3676 |
if (from_char_parse_int_len(&out->d, &s, 1, n, escontext) < 0) |
| 3677 |
return; |
| 3678 |
/* Shift numbering to match Gregorian where Sunday = 1 */ |
| 3679 |
if (++out->d > 7) |
| 3680 |
out->d = 1; |
| 3681 |
SKIP_THth(s, n->suffix); |
| 3682 |
break; |
| 3683 |
case DCH_WW: |
| 3684 |
case DCH_IW: |
| 3685 |
if (from_char_parse_int(&out->ww, &s, n, escontext) < 0) |
| 3686 |
return; |
| 3687 |
SKIP_THth(s, n->suffix); |
| 3688 |
break; |
| 3689 |
case DCH_Q: |
| 3690 |
|
| 3691 |
/* |
| 3692 |
* We ignore 'Q' when converting to date because it is unclear |
| 3693 |
* which date in the quarter to use, and some people specify |
| 3694 |
* both quarter and month, so if it was honored it might |
| 3695 |
* conflict with the supplied month. That is also why we don't |
| 3696 |
* throw an error. |
| 3697 |
* |
| 3698 |
* We still parse the source string for an integer, but it |
| 3699 |
* isn't stored anywhere in 'out'. |
| 3700 |
*/ |
| 3701 |
if (from_char_parse_int((int *) NULL, &s, n, escontext) < 0) |
| 3702 |
return; |
| 3703 |
SKIP_THth(s, n->suffix); |
| 3704 |
break; |
| 3705 |
case DCH_CC: |
| 3706 |
if (from_char_parse_int(&out->cc, &s, n, escontext) < 0) |
| 3707 |
return; |
| 3708 |
SKIP_THth(s, n->suffix); |
| 3709 |
break; |
| 3710 |
case DCH_Y_YYY: |
| 3711 |
{ |
| 3712 |
int matched, |
| 3713 |
years, |
| 3714 |
millennia, |
| 3715 |
nch; |
| 3716 |
|
| 3717 |
matched = sscanf(s, "%d,%03d%n", &millennia, &years, &nch); |
| 3718 |
if (matched < 2) |
| 3719 |
ereturn(escontext,, |
| 3720 |
(errcode(ERRCODE_INVALID_DATETIME_FORMAT), |
| 3721 |
errmsg("invalid value \"%s\" for \"%s\"", |
| 3722 |
s, "Y,YYY"))); |
| 3723 |
|
| 3724 |
/* years += (millennia * 1000); */ |
| 3725 |
if (pg_mul_s32_overflow(millennia, 1000, &millennia) || |
| 3726 |
pg_add_s32_overflow(years, millennia, &years)) |
| 3727 |
ereturn(escontext,, |
| 3728 |
(errcode(ERRCODE_DATETIME_FIELD_OVERFLOW), |
| 3729 |
errmsg("value for \"%s\" in source string is out of range", "Y,YYY"))); |
| 3730 |
|
| 3731 |
if (!from_char_set_int(&out->year, years, n, escontext)) |
| 3732 |
return; |
| 3733 |
out->yysz = 4; |
| 3734 |
s += nch; |
| 3735 |
SKIP_THth(s, n->suffix); |
| 3736 |
} |
| 3737 |
break; |
| 3738 |
case DCH_YYYY: |
| 3739 |
case DCH_IYYY: |
| 3740 |
if (from_char_parse_int(&out->year, &s, n, escontext) < 0) |
| 3741 |
return; |
| 3742 |
out->yysz = 4; |
| 3743 |
SKIP_THth(s, n->suffix); |
| 3744 |
break; |
| 3745 |
case DCH_YYY: |
| 3746 |
case DCH_IYY: |
| 3747 |
len = from_char_parse_int(&out->year, &s, n, escontext); |
| 3748 |
if (len < 0) |
| 3749 |
return; |
| 3750 |
if (len < 4) |
| 3751 |
out->year = adjust_partial_year_to_2020(out->year); |
| 3752 |
out->yysz = 3; |
| 3753 |
SKIP_THth(s, n->suffix); |
| 3754 |
break; |
| 3755 |
case DCH_YY: |
| 3756 |
case DCH_IY: |
| 3757 |
len = from_char_parse_int(&out->year, &s, n, escontext); |
| 3758 |
if (len < 0) |
| 3759 |
return; |
| 3760 |
if (len < 4) |
| 3761 |
out->year = adjust_partial_year_to_2020(out->year); |
| 3762 |
out->yysz = 2; |
| 3763 |
SKIP_THth(s, n->suffix); |
| 3764 |
break; |
| 3765 |
case DCH_Y: |
| 3766 |
case DCH_I: |
| 3767 |
len = from_char_parse_int(&out->year, &s, n, escontext); |
| 3768 |
if (len < 0) |
| 3769 |
return; |
| 3770 |
if (len < 4) |
| 3771 |
out->year = adjust_partial_year_to_2020(out->year); |
| 3772 |
out->yysz = 1; |
| 3773 |
SKIP_THth(s, n->suffix); |
| 3774 |
break; |
| 3775 |
case DCH_RM: |
| 3776 |
case DCH_rm: |
| 3777 |
if (!from_char_seq_search(&value, &s, rm_months_lower, |
| 3778 |
NULL, InvalidOid, |
| 3779 |
n, escontext)) |
| 3780 |
return; |
| 3781 |
if (!from_char_set_int(&out->mm, MONTHS_PER_YEAR - value, n, |
| 3782 |
escontext)) |
| 3783 |
return; |
| 3784 |
break; |
| 3785 |
case DCH_W: |
| 3786 |
if (from_char_parse_int(&out->w, &s, n, escontext) < 0) |
| 3787 |
return; |
| 3788 |
SKIP_THth(s, n->suffix); |
| 3789 |
break; |
| 3790 |
case DCH_J: |
| 3791 |
if (from_char_parse_int(&out->j, &s, n, escontext) < 0) |
| 3792 |
return; |
| 3793 |
SKIP_THth(s, n->suffix); |
| 3794 |
break; |
| 3795 |
} |
| 3796 |
|
| 3797 |
/* Ignore all spaces after fields */ |
| 3798 |
if (!fx_mode) |
| 3799 |
{ |
| 3800 |
extra_skip = 0; |
| 3801 |
while (*s != '\0' && isspace((unsigned char) *s)) |
| 3802 |
{ |
| 3803 |
s++; |
| 3804 |
extra_skip++; |
| 3805 |
} |
| 3806 |
} |
| 3807 |
} |
| 3808 |
|
| 3809 |
/* |
| 3810 |
* Standard parsing mode doesn't allow unmatched format patterns or |
| 3811 |
* trailing characters in the input string. |
| 3812 |
*/ |
| 3813 |
if (std) |
| 3814 |
{ |
| 3815 |
if (n->type != NODE_TYPE_END) |
| 3816 |
ereturn(escontext,, |
| 3817 |
(errcode(ERRCODE_INVALID_DATETIME_FORMAT), |
| 3818 |
errmsg("input string is too short for datetime format"))); |
| 3819 |
|
| 3820 |
while (*s != '\0' && isspace((unsigned char) *s)) |
| 3821 |
s++; |
| 3822 |
|
| 3823 |
if (*s != '\0') |
| 3824 |
ereturn(escontext,, |
| 3825 |
(errcode(ERRCODE_INVALID_DATETIME_FORMAT), |
| 3826 |
errmsg("trailing characters remain in input string after datetime format"))); |
| 3827 |
} |
| 3828 |
} |
| 3829 |
|
| 3830 |
/* |
| 3831 |
* The invariant for DCH cache entry management is that DCHCounter is equal |
| 3832 |
* to the maximum age value among the existing entries, and we increment it |
| 3833 |
* whenever an access occurs. If we approach overflow, deal with that by |
| 3834 |
* halving all the age values, so that we retain a fairly accurate idea of |
| 3835 |
* which entries are oldest. |
| 3836 |
*/ |
| 3837 |
static inline void |
| 3838 |
DCH_prevent_counter_overflow(void) |
| 3839 |
{ |
| 3840 |
if (DCHCounter >= (INT_MAX - 1)) |
| 3841 |
{ |
| 3842 |
for (int i = 0; i < n_DCHCache; i++) |
| 3843 |
DCHCache[i]->age >>= 1; |
| 3844 |
DCHCounter >>= 1; |
| 3845 |
} |
| 3846 |
} |
| 3847 |
|
| 3848 |
/* |
| 3849 |
* Get mask of date/time/zone components present in format nodes. |
| 3850 |
*/ |
| 3851 |
static int |
| 3852 |
DCH_datetime_type(FormatNode *node) |
| 3853 |
{ |
| 3854 |
FormatNode *n; |
| 3855 |
int flags = 0; |
| 3856 |
|
| 3857 |
for (n = node; n->type != NODE_TYPE_END; n++) |
| 3858 |
{ |
| 3859 |
if (n->type != NODE_TYPE_ACTION) |
| 3860 |
continue; |
| 3861 |
|
| 3862 |
switch (n->key->id) |
| 3863 |
{ |
| 3864 |
case DCH_FX: |
| 3865 |
break; |
| 3866 |
case DCH_A_M: |
| 3867 |
case DCH_P_M: |
| 3868 |
case DCH_a_m: |
| 3869 |
case DCH_p_m: |
| 3870 |
case DCH_AM: |
| 3871 |
case DCH_PM: |
| 3872 |
case DCH_am: |
| 3873 |
case DCH_pm: |
| 3874 |
case DCH_HH: |
| 3875 |
case DCH_HH12: |
| 3876 |
case DCH_HH24: |
| 3877 |
case DCH_MI: |
| 3878 |
case DCH_SS: |
| 3879 |
case DCH_MS: /* millisecond */ |
| 3880 |
case DCH_US: /* microsecond */ |
| 3881 |
case DCH_FF1: |
| 3882 |
case DCH_FF2: |
| 3883 |
case DCH_FF3: |
| 3884 |
case DCH_FF4: |
| 3885 |
case DCH_FF5: |
| 3886 |
case DCH_FF6: |
| 3887 |
case DCH_SSSS: |
| 3888 |
flags |= DCH_TIMED; |
| 3889 |
break; |
| 3890 |
case DCH_tz: |
| 3891 |
case DCH_TZ: |
| 3892 |
case DCH_OF: |
| 3893 |
case DCH_TZH: |
| 3894 |
case DCH_TZM: |
| 3895 |
flags |= DCH_ZONED; |
| 3896 |
break; |
| 3897 |
case DCH_A_D: |
| 3898 |
case DCH_B_C: |
| 3899 |
case DCH_a_d: |
| 3900 |
case DCH_b_c: |
| 3901 |
case DCH_AD: |
| 3902 |
case DCH_BC: |
| 3903 |
case DCH_ad: |
| 3904 |
case DCH_bc: |
| 3905 |
case DCH_MONTH: |
| 3906 |
case DCH_Month: |
| 3907 |
case DCH_month: |
| 3908 |
case DCH_MON: |
| 3909 |
case DCH_Mon: |
| 3910 |
case DCH_mon: |
| 3911 |
case DCH_MM: |
| 3912 |
case DCH_DAY: |
| 3913 |
case DCH_Day: |
| 3914 |
case DCH_day: |
| 3915 |
case DCH_DY: |
| 3916 |
case DCH_Dy: |
| 3917 |
case DCH_dy: |
| 3918 |
case DCH_DDD: |
| 3919 |
case DCH_IDDD: |
| 3920 |
case DCH_DD: |
| 3921 |
case DCH_D: |
| 3922 |
case DCH_ID: |
| 3923 |
case DCH_WW: |
| 3924 |
case DCH_Q: |
| 3925 |
case DCH_CC: |
| 3926 |
case DCH_Y_YYY: |
| 3927 |
case DCH_YYYY: |
| 3928 |
case DCH_IYYY: |
| 3929 |
case DCH_YYY: |
| 3930 |
case DCH_IYY: |
| 3931 |
case DCH_YY: |
| 3932 |
case DCH_IY: |
| 3933 |
case DCH_Y: |
| 3934 |
case DCH_I: |
| 3935 |
case DCH_RM: |
| 3936 |
case DCH_rm: |
| 3937 |
case DCH_W: |
| 3938 |
case DCH_J: |
| 3939 |
flags |= DCH_DATED; |
| 3940 |
break; |
| 3941 |
} |
| 3942 |
} |
| 3943 |
|
| 3944 |
return flags; |
| 3945 |
} |
| 3946 |
|
| 3947 |
/* select a DCHCacheEntry to hold the given format picture */ |
| 3948 |
static DCHCacheEntry * |
| 3949 |
DCH_cache_getnew(const char *str, bool std) |
| 3950 |
{ |
| 3951 |
DCHCacheEntry *ent; |
| 3952 |
|
| 3953 |
/* Ensure we can advance DCHCounter below */ |
| 3954 |
DCH_prevent_counter_overflow(); |
| 3955 |
|
| 3956 |
/* |
| 3957 |
* If cache is full, remove oldest entry (or recycle first not-valid one) |
| 3958 |
*/ |
| 3959 |
if (n_DCHCache >= DCH_CACHE_ENTRIES) |
| 3960 |
{ |
| 3961 |
DCHCacheEntry *old = DCHCache[0]; |
| 3962 |
|
| 3963 |
#ifdef DEBUG_TO_FROM_CHAR |
| 3964 |
elog(DEBUG_elog_output, "cache is full (%d)", n_DCHCache); |
| 3965 |
#endif |
| 3966 |
if (old->valid) |
| 3967 |
{ |
| 3968 |
for (int i = 1; i < DCH_CACHE_ENTRIES; i++) |
| 3969 |
{ |
| 3970 |
ent = DCHCache[i]; |
| 3971 |
if (!ent->valid) |
| 3972 |
{ |
| 3973 |
old = ent; |
| 3974 |
break; |
| 3975 |
} |
| 3976 |
if (ent->age < old->age) |
| 3977 |
old = ent; |
| 3978 |
} |
| 3979 |
} |
| 3980 |
#ifdef DEBUG_TO_FROM_CHAR |
| 3981 |
elog(DEBUG_elog_output, "OLD: '%s' AGE: %d", old->str, old->age); |
| 3982 |
#endif |
| 3983 |
old->valid = false; |
| 3984 |
strlcpy(old->str, str, DCH_CACHE_SIZE + 1); |
| 3985 |
old->age = (++DCHCounter); |
| 3986 |
/* caller is expected to fill format, then set valid */ |
| 3987 |
return old; |
| 3988 |
} |
| 3989 |
else |
| 3990 |
{ |
| 3991 |
#ifdef DEBUG_TO_FROM_CHAR |
| 3992 |
elog(DEBUG_elog_output, "NEW (%d)", n_DCHCache); |
| 3993 |
#endif |
| 3994 |
Assert(DCHCache[n_DCHCache] == NULL); |
| 3995 |
DCHCache[n_DCHCache] = ent = (DCHCacheEntry *) |
| 3996 |
MemoryContextAllocZero(TopMemoryContext, sizeof(DCHCacheEntry)); |
| 3997 |
ent->valid = false; |
| 3998 |
strlcpy(ent->str, str, DCH_CACHE_SIZE + 1); |
| 3999 |
ent->std = std; |
| 4000 |
ent->age = (++DCHCounter); |
| 4001 |
/* caller is expected to fill format, then set valid */ |
| 4002 |
++n_DCHCache; |
| 4003 |
return ent; |
| 4004 |
} |
| 4005 |
} |
| 4006 |
|
| 4007 |
/* look for an existing DCHCacheEntry matching the given format picture */ |
| 4008 |
static DCHCacheEntry * |
| 4009 |
DCH_cache_search(const char *str, bool std) |
| 4010 |
{ |
| 4011 |
/* Ensure we can advance DCHCounter below */ |
| 4012 |
DCH_prevent_counter_overflow(); |
| 4013 |
|
| 4014 |
for (int i = 0; i < n_DCHCache; i++) |
| 4015 |
{ |
| 4016 |
DCHCacheEntry *ent = DCHCache[i]; |
| 4017 |
|
| 4018 |
if (ent->valid && strcmp(ent->str, str) == 0 && ent->std == std) |
| 4019 |
{ |
| 4020 |
ent->age = (++DCHCounter); |
| 4021 |
return ent; |
| 4022 |
} |
| 4023 |
} |
| 4024 |
|
| 4025 |
return NULL; |
| 4026 |
} |
| 4027 |
|
| 4028 |
/* Find or create a DCHCacheEntry for the given format picture */ |
| 4029 |
static DCHCacheEntry * |
| 4030 |
DCH_cache_fetch(const char *str, bool std) |
| 4031 |
{ |
| 4032 |
DCHCacheEntry *ent; |
| 4033 |
|
| 4034 |
if ((ent = DCH_cache_search(str, std)) == NULL) |
| 4035 |
{ |
| 4036 |
/* |
| 4037 |
* Not in the cache, must run parser and save a new format-picture to |
| 4038 |
* the cache. Do not mark the cache entry valid until parsing |
| 4039 |
* succeeds. |
| 4040 |
*/ |
| 4041 |
ent = DCH_cache_getnew(str, std); |
| 4042 |
|
| 4043 |
parse_format(ent->format, str, DCH_keywords, DCH_suff, DCH_index, |
| 4044 |
DCH_FLAG | (std ? STD_FLAG : 0), NULL); |
| 4045 |
|
| 4046 |
ent->valid = true; |
| 4047 |
} |
| 4048 |
return ent; |
| 4049 |
} |
| 4050 |
|
| 4051 |
/* |
| 4052 |
* Format a date/time or interval into a string according to fmt. |
| 4053 |
* We parse fmt into a list of FormatNodes. This is then passed to DCH_to_char |
| 4054 |
* for formatting. |
| 4055 |
*/ |
| 4056 |
static text * |
| 4057 |
datetime_to_char_body(TmToChar *tmtc, text *fmt, bool is_interval, Oid collid) |
| 4058 |
{ |
| 4059 |
FormatNode *format; |
| 4060 |
char *fmt_str, |
| 4061 |
*result; |
| 4062 |
bool incache; |
| 4063 |
int fmt_len; |
| 4064 |
text *res; |
| 4065 |
|
| 4066 |
/* |
| 4067 |
* Convert fmt to C string |
| 4068 |
*/ |
| 4069 |
fmt_str = text_to_cstring(fmt); |
| 4070 |
fmt_len = strlen(fmt_str); |
| 4071 |
|
| 4072 |
/* |
| 4073 |
* Allocate workspace for result as C string |
| 4074 |
*/ |
| 4075 |
result = palloc(mul_size(fmt_len, DCH_MAX_ITEM_SIZ) + 1); |
| 4076 |
*result = '\0'; |
| 4077 |
|
| 4078 |
if (fmt_len > DCH_CACHE_SIZE) |
| 4079 |
{ |
| 4080 |
/* |
| 4081 |
* Allocate new memory if format picture is bigger than static cache |
| 4082 |
* and do not use cache (call parser always) |
| 4083 |
*/ |
| 4084 |
incache = false; |
| 4085 |
|
| 4086 |
format = palloc_array(FormatNode, fmt_len + 1); |
| 4087 |
|
| 4088 |
parse_format(format, fmt_str, DCH_keywords, |
| 4089 |
DCH_suff, DCH_index, DCH_FLAG, NULL); |
| 4090 |
} |
| 4091 |
else |
| 4092 |
{ |
| 4093 |
/* |
| 4094 |
* Use cache buffers |
| 4095 |
*/ |
| 4096 |
DCHCacheEntry *ent = DCH_cache_fetch(fmt_str, false); |
| 4097 |
|
| 4098 |
incache = true; |
| 4099 |
format = ent->format; |
| 4100 |
} |
| 4101 |
|
| 4102 |
/* The real work is here */ |
| 4103 |
DCH_to_char(format, is_interval, tmtc, result, collid); |
| 4104 |
|
| 4105 |
if (!incache) |
| 4106 |
pfree(format); |
| 4107 |
|
| 4108 |
pfree(fmt_str); |
| 4109 |
|
| 4110 |
/* convert C-string result to TEXT format */ |
| 4111 |
res = cstring_to_text(result); |
| 4112 |
|
| 4113 |
pfree(result); |
| 4114 |
return res; |
| 4115 |
} |
| 4116 |
|
| 4117 |
/**************************************************************************** |
| 4118 |
* Public routines |
| 4119 |
***************************************************************************/ |
| 4120 |
|
| 4121 |
/* ------------------- |
| 4122 |
* TIMESTAMP to_char() |
| 4123 |
* ------------------- |
| 4124 |
*/ |
| 4125 |
Datum |
| 4126 |
timestamp_to_char(PG_FUNCTION_ARGS) |
| 4127 |
{ |
| 4128 |
Timestamp dt = PG_GETARG_TIMESTAMP(0); |
| 4129 |
text *fmt = PG_GETARG_TEXT_PP(1), |
| 4130 |
*res; |
| 4131 |
TmToChar tmtc; |
| 4132 |
struct pg_tm tt; |
| 4133 |
struct fmt_tm *tm; |
| 4134 |
int thisdate; |
| 4135 |
|
| 4136 |
if (VARSIZE_ANY_EXHDR(fmt) <= 0 || TIMESTAMP_NOT_FINITE(dt)) |
| 4137 |
PG_RETURN_NULL(); |
| 4138 |
|
| 4139 |
ZERO_tmtc(&tmtc); |
| 4140 |
tm = tmtcTm(&tmtc); |
| 4141 |
|
| 4142 |
if (timestamp2tm(dt, NULL, &tt, &tmtcFsec(&tmtc), NULL, NULL) != 0) |
| 4143 |
ereport(ERROR, |
| 4144 |
(errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), |
| 4145 |
errmsg("timestamp out of range"))); |
| 4146 |
|
| 4147 |
/* calculate wday and yday, because timestamp2tm doesn't */ |
| 4148 |
thisdate = date2j(tt.tm_year, tt.tm_mon, tt.tm_mday); |
| 4149 |
tt.tm_wday = (thisdate + 1) % 7; |
| 4150 |
tt.tm_yday = thisdate - date2j(tt.tm_year, 1, 1) + 1; |
| 4151 |
|
| 4152 |
COPY_tm(tm, &tt); |
| 4153 |
|
| 4154 |
if (!(res = datetime_to_char_body(&tmtc, fmt, false, PG_GET_COLLATION()))) |
| 4155 |
PG_RETURN_NULL(); |
| 4156 |
|
| 4157 |
PG_RETURN_TEXT_P(res); |
| 4158 |
} |
| 4159 |
|
| 4160 |
Datum |
| 4161 |
timestamptz_to_char(PG_FUNCTION_ARGS) |
| 4162 |
{ |
| 4163 |
TimestampTz dt = PG_GETARG_TIMESTAMP(0); |
| 4164 |
text *fmt = PG_GETARG_TEXT_PP(1), |
| 4165 |
*res; |
| 4166 |
TmToChar tmtc; |
| 4167 |
int tz; |
| 4168 |
struct pg_tm tt; |
| 4169 |
struct fmt_tm *tm; |
| 4170 |
int thisdate; |
| 4171 |
|
| 4172 |
if (VARSIZE_ANY_EXHDR(fmt) <= 0 || TIMESTAMP_NOT_FINITE(dt)) |
| 4173 |
PG_RETURN_NULL(); |
| 4174 |
|
| 4175 |
ZERO_tmtc(&tmtc); |
| 4176 |
tm = tmtcTm(&tmtc); |
| 4177 |
|
| 4178 |
if (timestamp2tm(dt, &tz, &tt, &tmtcFsec(&tmtc), &tmtcTzn(&tmtc), NULL) != 0) |
| 4179 |
ereport(ERROR, |
| 4180 |
(errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), |
| 4181 |
errmsg("timestamp out of range"))); |
| 4182 |
|
| 4183 |
/* calculate wday and yday, because timestamp2tm doesn't */ |
| 4184 |
thisdate = date2j(tt.tm_year, tt.tm_mon, tt.tm_mday); |
| 4185 |
tt.tm_wday = (thisdate + 1) % 7; |
| 4186 |
tt.tm_yday = thisdate - date2j(tt.tm_year, 1, 1) + 1; |
| 4187 |
|
| 4188 |
COPY_tm(tm, &tt); |
| 4189 |
|
| 4190 |
if (!(res = datetime_to_char_body(&tmtc, fmt, false, PG_GET_COLLATION()))) |
| 4191 |
PG_RETURN_NULL(); |
| 4192 |
|
| 4193 |
PG_RETURN_TEXT_P(res); |
| 4194 |
} |
| 4195 |
|
| 4196 |
|
| 4197 |
/* ------------------- |
| 4198 |
* INTERVAL to_char() |
| 4199 |
* ------------------- |
| 4200 |
*/ |
| 4201 |
Datum |
| 4202 |
interval_to_char(PG_FUNCTION_ARGS) |
| 4203 |
{ |
| 4204 |
Interval *it = PG_GETARG_INTERVAL_P(0); |
| 4205 |
text *fmt = PG_GETARG_TEXT_PP(1), |
| 4206 |
*res; |
| 4207 |
TmToChar tmtc; |
| 4208 |
struct fmt_tm *tm; |
| 4209 |
struct pg_itm tt, |
| 4210 |
*itm = &tt; |
| 4211 |
|
| 4212 |
if (VARSIZE_ANY_EXHDR(fmt) <= 0 || INTERVAL_NOT_FINITE(it)) |
| 4213 |
PG_RETURN_NULL(); |
| 4214 |
|
| 4215 |
ZERO_tmtc(&tmtc); |
| 4216 |
tm = tmtcTm(&tmtc); |
| 4217 |
|
| 4218 |
interval2itm(*it, itm); |
| 4219 |
tmtc.fsec = itm->tm_usec; |
| 4220 |
tm->tm_sec = itm->tm_sec; |
| 4221 |
tm->tm_min = itm->tm_min; |
| 4222 |
tm->tm_hour = itm->tm_hour; |
| 4223 |
tm->tm_mday = itm->tm_mday; |
| 4224 |
tm->tm_mon = itm->tm_mon; |
| 4225 |
tm->tm_year = itm->tm_year; |
| 4226 |
|
| 4227 |
/* wday is meaningless, yday approximates the total span in days */ |
| 4228 |
tm->tm_yday = (tm->tm_year * MONTHS_PER_YEAR + tm->tm_mon) * DAYS_PER_MONTH + tm->tm_mday; |
| 4229 |
|
| 4230 |
if (!(res = datetime_to_char_body(&tmtc, fmt, true, PG_GET_COLLATION()))) |
| 4231 |
PG_RETURN_NULL(); |
| 4232 |
|
| 4233 |
PG_RETURN_TEXT_P(res); |
| 4234 |
} |
| 4235 |
|
| 4236 |
/* --------------------- |
| 4237 |
* TO_TIMESTAMP() |
| 4238 |
* |
| 4239 |
* Make Timestamp from date_str which is formatted at argument 'fmt' |
| 4240 |
* ( to_timestamp is reverse to_char() ) |
| 4241 |
* --------------------- |
| 4242 |
*/ |
| 4243 |
Datum |
| 4244 |
to_timestamp(PG_FUNCTION_ARGS) |
| 4245 |
{ |
| 4246 |
text *date_txt = PG_GETARG_TEXT_PP(0); |
| 4247 |
text *fmt = PG_GETARG_TEXT_PP(1); |
| 4248 |
Oid collid = PG_GET_COLLATION(); |
| 4249 |
Timestamp result; |
| 4250 |
int tz; |
| 4251 |
struct pg_tm tm; |
| 4252 |
struct fmt_tz ftz; |
| 4253 |
fsec_t fsec; |
| 4254 |
int fprec; |
| 4255 |
|
| 4256 |
do_to_timestamp(date_txt, fmt, collid, false, |
| 4257 |
&tm, &fsec, &ftz, &fprec, NULL, NULL); |
| 4258 |
|
| 4259 |
/* Use the specified time zone, if any. */ |
| 4260 |
if (ftz.has_tz) |
| 4261 |
tz = ftz.gmtoffset; |
| 4262 |
else |
| 4263 |
tz = DetermineTimeZoneOffset(&tm, session_timezone); |
| 4264 |
|
| 4265 |
if (tm2timestamp(&tm, fsec, &tz, &result) != 0) |
| 4266 |
ereport(ERROR, |
| 4267 |
(errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), |
| 4268 |
errmsg("timestamp out of range"))); |
| 4269 |
|
| 4270 |
/* Use the specified fractional precision, if any. */ |
| 4271 |
if (fprec) |
| 4272 |
AdjustTimestampForTypmod(&result, fprec, NULL); |
| 4273 |
|
| 4274 |
PG_RETURN_TIMESTAMP(result); |
| 4275 |
} |
| 4276 |
|
| 4277 |
/* ---------- |
| 4278 |
* TO_DATE |
| 4279 |
* Make Date from date_str which is formatted at argument 'fmt' |
| 4280 |
* ---------- |
| 4281 |
*/ |
| 4282 |
Datum |
| 4283 |
to_date(PG_FUNCTION_ARGS) |
| 4284 |
{ |
| 4285 |
text *date_txt = PG_GETARG_TEXT_PP(0); |
| 4286 |
text *fmt = PG_GETARG_TEXT_PP(1); |
| 4287 |
Oid collid = PG_GET_COLLATION(); |
| 4288 |
DateADT result; |
| 4289 |
struct pg_tm tm; |
| 4290 |
struct fmt_tz ftz; |
| 4291 |
fsec_t fsec; |
| 4292 |
|
| 4293 |
do_to_timestamp(date_txt, fmt, collid, false, |
| 4294 |
&tm, &fsec, &ftz, NULL, NULL, NULL); |
| 4295 |
|
| 4296 |
/* Prevent overflow in Julian-day routines */ |
| 4297 |
if (!IS_VALID_JULIAN(tm.tm_year, tm.tm_mon, tm.tm_mday)) |
| 4298 |
ereport(ERROR, |
| 4299 |
(errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), |
| 4300 |
errmsg("date out of range: \"%s\"", |
| 4301 |
text_to_cstring(date_txt)))); |
| 4302 |
|
| 4303 |
result = date2j(tm.tm_year, tm.tm_mon, tm.tm_mday) - POSTGRES_EPOCH_JDATE; |
| 4304 |
|
| 4305 |
/* Now check for just-out-of-range dates */ |
| 4306 |
if (!IS_VALID_DATE(result)) |
| 4307 |
ereport(ERROR, |
| 4308 |
(errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), |
| 4309 |
errmsg("date out of range: \"%s\"", |
| 4310 |
text_to_cstring(date_txt)))); |
| 4311 |
|
| 4312 |
PG_RETURN_DATEADT(result); |
| 4313 |
} |
| 4314 |
|
| 4315 |
/* |
| 4316 |
* Convert the 'date_txt' input to a datetime type using argument 'fmt' |
| 4317 |
* as a format string. The collation 'collid' may be used for case-folding |
| 4318 |
* rules in some cases. 'strict' specifies standard parsing mode. |
| 4319 |
* |
| 4320 |
* The actual data type (returned in 'typid', 'typmod') is determined by |
| 4321 |
* the presence of date/time/zone components in the format string. |
| 4322 |
* |
| 4323 |
* When a timezone component is present, the corresponding offset is |
| 4324 |
* returned in '*tz'. |
| 4325 |
* |
| 4326 |
* If escontext points to an ErrorSaveContext, data errors will be reported |
| 4327 |
* by filling that struct; the caller must test SOFT_ERROR_OCCURRED() to see |
| 4328 |
* whether an error occurred. Otherwise, errors are thrown. |
| 4329 |
*/ |
| 4330 |
Datum |
| 4331 |
parse_datetime(text *date_txt, text *fmt, Oid collid, bool strict, |
| 4332 |
Oid *typid, int32 *typmod, int *tz, |
| 4333 |
Node *escontext) |
| 4334 |
{ |
| 4335 |
struct pg_tm tm; |
| 4336 |
struct fmt_tz ftz; |
| 4337 |
fsec_t fsec; |
| 4338 |
int fprec; |
| 4339 |
uint32 flags; |
| 4340 |
|
| 4341 |
if (!do_to_timestamp(date_txt, fmt, collid, strict, |
| 4342 |
&tm, &fsec, &ftz, &fprec, &flags, escontext)) |
| 4343 |
return (Datum) 0; |
| 4344 |
|
| 4345 |
*typmod = fprec ? fprec : -1; /* fractional part precision */ |
| 4346 |
|
| 4347 |
if (flags & DCH_DATED) |
| 4348 |
{ |
| 4349 |
if (flags & DCH_TIMED) |
| 4350 |
{ |
| 4351 |
if (flags & DCH_ZONED) |
| 4352 |
{ |
| 4353 |
TimestampTz result; |
| 4354 |
|
| 4355 |
if (ftz.has_tz) |
| 4356 |
{ |
| 4357 |
*tz = ftz.gmtoffset; |
| 4358 |
} |
| 4359 |
else |
| 4360 |
{ |
| 4361 |
/* |
| 4362 |
* Time zone is present in format string, but not in input |
| 4363 |
* string. Assuming do_to_timestamp() triggers no error |
| 4364 |
* this should be possible only in non-strict case. |
| 4365 |
*/ |
| 4366 |
Assert(!strict); |
| 4367 |
|
| 4368 |
ereturn(escontext, (Datum) 0, |
| 4369 |
(errcode(ERRCODE_INVALID_DATETIME_FORMAT), |
| 4370 |
errmsg("missing time zone in input string for type timestamptz"))); |
| 4371 |
} |
| 4372 |
|
| 4373 |
if (tm2timestamp(&tm, fsec, tz, &result) != 0) |
| 4374 |
ereturn(escontext, (Datum) 0, |
| 4375 |
(errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), |
| 4376 |
errmsg("timestamptz out of range"))); |
| 4377 |
|
| 4378 |
AdjustTimestampForTypmod(&result, *typmod, escontext); |
| 4379 |
|
| 4380 |
*typid = TIMESTAMPTZOID; |
| 4381 |
return TimestampTzGetDatum(result); |
| 4382 |
} |
| 4383 |
else |
| 4384 |
{ |
| 4385 |
Timestamp result; |
| 4386 |
|
| 4387 |
if (tm2timestamp(&tm, fsec, NULL, &result) != 0) |
| 4388 |
ereturn(escontext, (Datum) 0, |
| 4389 |
(errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), |
| 4390 |
errmsg("timestamp out of range"))); |
| 4391 |
|
| 4392 |
AdjustTimestampForTypmod(&result, *typmod, escontext); |
| 4393 |
|
| 4394 |
*typid = TIMESTAMPOID; |
| 4395 |
return TimestampGetDatum(result); |
| 4396 |
} |
| 4397 |
} |
| 4398 |
else |
| 4399 |
{ |
| 4400 |
if (flags & DCH_ZONED) |
| 4401 |
{ |
| 4402 |
ereturn(escontext, (Datum) 0, |
| 4403 |
(errcode(ERRCODE_INVALID_DATETIME_FORMAT), |
| 4404 |
errmsg("datetime format is zoned but not timed"))); |
| 4405 |
} |
| 4406 |
else |
| 4407 |
{ |
| 4408 |
DateADT result; |
| 4409 |
|
| 4410 |
/* Prevent overflow in Julian-day routines */ |
| 4411 |
if (!IS_VALID_JULIAN(tm.tm_year, tm.tm_mon, tm.tm_mday)) |
| 4412 |
ereturn(escontext, (Datum) 0, |
| 4413 |
(errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), |
| 4414 |
errmsg("date out of range: \"%s\"", |
| 4415 |
text_to_cstring(date_txt)))); |
| 4416 |
|
| 4417 |
result = date2j(tm.tm_year, tm.tm_mon, tm.tm_mday) - |
| 4418 |
POSTGRES_EPOCH_JDATE; |
| 4419 |
|
| 4420 |
/* Now check for just-out-of-range dates */ |
| 4421 |
if (!IS_VALID_DATE(result)) |
| 4422 |
ereturn(escontext, (Datum) 0, |
| 4423 |
(errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), |
| 4424 |
errmsg("date out of range: \"%s\"", |
| 4425 |
text_to_cstring(date_txt)))); |
| 4426 |
|
| 4427 |
*typid = DATEOID; |
| 4428 |
return DateADTGetDatum(result); |
| 4429 |
} |
| 4430 |
} |
| 4431 |
} |
| 4432 |
else if (flags & DCH_TIMED) |
| 4433 |
{ |
| 4434 |
if (flags & DCH_ZONED) |
| 4435 |
{ |
| 4436 |
TimeTzADT *result = palloc(sizeof(TimeTzADT)); |
| 4437 |
|
| 4438 |
if (ftz.has_tz) |
| 4439 |
{ |
| 4440 |
*tz = ftz.gmtoffset; |
| 4441 |
} |
| 4442 |
else |
| 4443 |
{ |
| 4444 |
/* |
| 4445 |
* Time zone is present in format string, but not in input |
| 4446 |
* string. Assuming do_to_timestamp() triggers no error this |
| 4447 |
* should be possible only in non-strict case. |
| 4448 |
*/ |
| 4449 |
Assert(!strict); |
| 4450 |
|
| 4451 |
ereturn(escontext, (Datum) 0, |
| 4452 |
(errcode(ERRCODE_INVALID_DATETIME_FORMAT), |
| 4453 |
errmsg("missing time zone in input string for type timetz"))); |
| 4454 |
} |
| 4455 |
|
| 4456 |
if (tm2timetz(&tm, fsec, *tz, result) != 0) |
| 4457 |
ereturn(escontext, (Datum) 0, |
| 4458 |
(errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), |
| 4459 |
errmsg("timetz out of range"))); |
| 4460 |
|
| 4461 |
AdjustTimeForTypmod(&result->time, *typmod); |
| 4462 |
|
| 4463 |
*typid = TIMETZOID; |
| 4464 |
return TimeTzADTPGetDatum(result); |
| 4465 |
} |
| 4466 |
else |
| 4467 |
{ |
| 4468 |
TimeADT result; |
| 4469 |
|
| 4470 |
if (tm2time(&tm, fsec, &result) != 0) |
| 4471 |
ereturn(escontext, (Datum) 0, |
| 4472 |
(errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), |
| 4473 |
errmsg("time out of range"))); |
| 4474 |
|
| 4475 |
AdjustTimeForTypmod(&result, *typmod); |
| 4476 |
|
| 4477 |
*typid = TIMEOID; |
| 4478 |
return TimeADTGetDatum(result); |
| 4479 |
} |
| 4480 |
} |
| 4481 |
else |
| 4482 |
{ |
| 4483 |
ereturn(escontext, (Datum) 0, |
| 4484 |
(errcode(ERRCODE_INVALID_DATETIME_FORMAT), |
| 4485 |
errmsg("datetime format is not dated and not timed"))); |
| 4486 |
} |
| 4487 |
} |
| 4488 |
|
| 4489 |
/* |
| 4490 |
* Parses the datetime format string in 'fmt_str' and returns true if it |
| 4491 |
* contains a timezone specifier, false if not. |
| 4492 |
*/ |
| 4493 |
bool |
| 4494 |
datetime_format_has_tz(const char *fmt_str) |
| 4495 |
{ |
| 4496 |
bool incache; |
| 4497 |
int fmt_len = strlen(fmt_str); |
| 4498 |
int result; |
| 4499 |
FormatNode *format; |
| 4500 |
|
| 4501 |
if (fmt_len > DCH_CACHE_SIZE) |
| 4502 |
{ |
| 4503 |
/* |
| 4504 |
* Allocate new memory if format picture is bigger than static cache |
| 4505 |
* and do not use cache (call parser always) |
| 4506 |
*/ |
| 4507 |
incache = false; |
| 4508 |
|
| 4509 |
format = palloc_array(FormatNode, fmt_len + 1); |
| 4510 |
|
| 4511 |
parse_format(format, fmt_str, DCH_keywords, |
| 4512 |
DCH_suff, DCH_index, DCH_FLAG, NULL); |
| 4513 |
} |
| 4514 |
else |
| 4515 |
{ |
| 4516 |
/* |
| 4517 |
* Use cache buffers |
| 4518 |
*/ |
| 4519 |
DCHCacheEntry *ent = DCH_cache_fetch(fmt_str, false); |
| 4520 |
|
| 4521 |
incache = true; |
| 4522 |
format = ent->format; |
| 4523 |
} |
| 4524 |
|
| 4525 |
result = DCH_datetime_type(format); |
| 4526 |
|
| 4527 |
if (!incache) |
| 4528 |
pfree(format); |
| 4529 |
|
| 4530 |
return result & DCH_ZONED; |
| 4531 |
} |
| 4532 |
|
| 4533 |
/* |
| 4534 |
* do_to_timestamp: shared code for to_timestamp and to_date |
| 4535 |
* |
| 4536 |
* Parse the 'date_txt' according to 'fmt', return results as a struct pg_tm, |
| 4537 |
* fractional seconds, struct fmt_tz, and fractional precision. |
| 4538 |
* |
| 4539 |
* 'collid' identifies the collation to use, if needed. |
| 4540 |
* 'std' specifies standard parsing mode. |
| 4541 |
* |
| 4542 |
* Bit mask of date/time/zone components found in 'fmt' is returned in 'flags', |
| 4543 |
* if that is not NULL. |
| 4544 |
* |
| 4545 |
* Returns true on success, false on failure (if escontext points to an |
| 4546 |
* ErrorSaveContext; otherwise errors are thrown). Note that currently, |
| 4547 |
* soft-error behavior is provided for bad data but not bad format. |
| 4548 |
* |
| 4549 |
* We parse 'fmt' into a list of FormatNodes, which is then passed to |
| 4550 |
* DCH_from_char to populate a TmFromChar with the parsed contents of |
| 4551 |
* 'date_txt'. |
| 4552 |
* |
| 4553 |
* The TmFromChar is then analysed and converted into the final results in |
| 4554 |
* struct 'tm', 'fsec', struct 'tz', and 'fprec'. |
| 4555 |
*/ |
| 4556 |
static bool |
| 4557 |
do_to_timestamp(text *date_txt, text *fmt, Oid collid, bool std, |
| 4558 |
struct pg_tm *tm, fsec_t *fsec, struct fmt_tz *tz, |
| 4559 |
int *fprec, uint32 *flags, Node *escontext) |
| 4560 |
{ |
| 4561 |
FormatNode *format = NULL; |
| 4562 |
TmFromChar tmfc; |
| 4563 |
int fmt_len; |
| 4564 |
char *date_str; |
| 4565 |
int fmask; |
| 4566 |
bool incache = false; |
| 4567 |
|
| 4568 |
Assert(tm != NULL); |
| 4569 |
Assert(fsec != NULL); |
| 4570 |
|
| 4571 |
date_str = text_to_cstring(date_txt); |
| 4572 |
|
| 4573 |
ZERO_tmfc(&tmfc); |
| 4574 |
ZERO_tm(tm); |
| 4575 |
*fsec = 0; |
| 4576 |
tz->has_tz = false; |
| 4577 |
if (fprec) |
| 4578 |
*fprec = 0; |
| 4579 |
if (flags) |
| 4580 |
*flags = 0; |
| 4581 |
fmask = 0; /* bit mask for ValidateDate() */ |
| 4582 |
|
| 4583 |
fmt_len = VARSIZE_ANY_EXHDR(fmt); |
| 4584 |
|
| 4585 |
if (fmt_len) |
| 4586 |
{ |
| 4587 |
char *fmt_str; |
| 4588 |
|
| 4589 |
fmt_str = text_to_cstring(fmt); |
| 4590 |
|
| 4591 |
if (fmt_len > DCH_CACHE_SIZE) |
| 4592 |
{ |
| 4593 |
/* |
| 4594 |
* Allocate new memory if format picture is bigger than static |
| 4595 |
* cache and do not use cache (call parser always) |
| 4596 |
*/ |
| 4597 |
format = palloc_array(FormatNode, fmt_len + 1); |
| 4598 |
|
| 4599 |
parse_format(format, fmt_str, DCH_keywords, DCH_suff, DCH_index, |
| 4600 |
DCH_FLAG | (std ? STD_FLAG : 0), NULL); |
| 4601 |
} |
| 4602 |
else |
| 4603 |
{ |
| 4604 |
/* |
| 4605 |
* Use cache buffers |
| 4606 |
*/ |
| 4607 |
DCHCacheEntry *ent = DCH_cache_fetch(fmt_str, std); |
| 4608 |
|
| 4609 |
incache = true; |
| 4610 |
format = ent->format; |
| 4611 |
} |
| 4612 |
|
| 4613 |
#ifdef DEBUG_TO_FROM_CHAR |
| 4614 |
/* dump_node(format, fmt_len); */ |
| 4615 |
/* dump_index(DCH_keywords, DCH_index); */ |
| 4616 |
#endif |
| 4617 |
|
| 4618 |
DCH_from_char(format, date_str, &tmfc, collid, std, escontext); |
| 4619 |
pfree(fmt_str); |
| 4620 |
if (SOFT_ERROR_OCCURRED(escontext)) |
| 4621 |
goto fail; |
| 4622 |
|
| 4623 |
if (flags) |
| 4624 |
*flags = DCH_datetime_type(format); |
| 4625 |
|
| 4626 |
if (!incache) |
| 4627 |
{ |
| 4628 |
pfree(format); |
| 4629 |
format = NULL; |
| 4630 |
} |
| 4631 |
} |
| 4632 |
|
| 4633 |
DEBUG_TMFC(&tmfc); |
| 4634 |
|
| 4635 |
/* |
| 4636 |
* Convert to_date/to_timestamp input fields to standard 'tm' |
| 4637 |
*/ |
| 4638 |
if (tmfc.ssss) |
| 4639 |
{ |
| 4640 |
int x = tmfc.ssss; |
| 4641 |
|
| 4642 |
tm->tm_hour = x / SECS_PER_HOUR; |
| 4643 |
x %= SECS_PER_HOUR; |
| 4644 |
tm->tm_min = x / SECS_PER_MINUTE; |
| 4645 |
x %= SECS_PER_MINUTE; |
| 4646 |
tm->tm_sec = x; |
| 4647 |
} |
| 4648 |
|
| 4649 |
if (tmfc.ss) |
| 4650 |
tm->tm_sec = tmfc.ss; |
| 4651 |
if (tmfc.mi) |
| 4652 |
tm->tm_min = tmfc.mi; |
| 4653 |
if (tmfc.hh) |
| 4654 |
tm->tm_hour = tmfc.hh; |
| 4655 |
|
| 4656 |
if (tmfc.clock == CLOCK_12_HOUR) |
| 4657 |
{ |
| 4658 |
if (tm->tm_hour < 1 || tm->tm_hour > HOURS_PER_DAY / 2) |
| 4659 |
{ |
| 4660 |
errsave(escontext, |
| 4661 |
(errcode(ERRCODE_INVALID_DATETIME_FORMAT), |
| 4662 |
errmsg("hour \"%d\" is invalid for the 12-hour clock", |
| 4663 |
tm->tm_hour), |
| 4664 |
errhint("Use the 24-hour clock, or give an hour between 1 and 12."))); |
| 4665 |
goto fail; |
| 4666 |
} |
| 4667 |
|
| 4668 |
if (tmfc.pm && tm->tm_hour < HOURS_PER_DAY / 2) |
| 4669 |
tm->tm_hour += HOURS_PER_DAY / 2; |
| 4670 |
else if (!tmfc.pm && tm->tm_hour == HOURS_PER_DAY / 2) |
| 4671 |
tm->tm_hour = 0; |
| 4672 |
} |
| 4673 |
|
| 4674 |
if (tmfc.year) |
| 4675 |
{ |
| 4676 |
/* |
| 4677 |
* If CC and YY (or Y) are provided, use YY as 2 low-order digits for |
| 4678 |
* the year in the given century. Keep in mind that the 21st century |
| 4679 |
* AD runs from 2001-2100, not 2000-2099; 6th century BC runs from |
| 4680 |
* 600BC to 501BC. |
| 4681 |
*/ |
| 4682 |
if (tmfc.cc && tmfc.yysz <= 2) |
| 4683 |
{ |
| 4684 |
if (tmfc.bc) |
| 4685 |
tmfc.cc = -tmfc.cc; |
| 4686 |
tm->tm_year = tmfc.year % 100; |
| 4687 |
if (tm->tm_year) |
| 4688 |
{ |
| 4689 |
int tmp; |
| 4690 |
|
| 4691 |
if (tmfc.cc >= 0) |
| 4692 |
{ |
| 4693 |
/* tm->tm_year += (tmfc.cc - 1) * 100; */ |
| 4694 |
tmp = tmfc.cc - 1; |
| 4695 |
if (pg_mul_s32_overflow(tmp, 100, &tmp) || |
| 4696 |
pg_add_s32_overflow(tm->tm_year, tmp, &tm->tm_year)) |
| 4697 |
{ |
| 4698 |
DateTimeParseError(DTERR_FIELD_OVERFLOW, NULL, |
| 4699 |
text_to_cstring(date_txt), "timestamp", |
| 4700 |
escontext); |
| 4701 |
goto fail; |
| 4702 |
} |
| 4703 |
} |
| 4704 |
else |
| 4705 |
{ |
| 4706 |
/* tm->tm_year = (tmfc.cc + 1) * 100 - tm->tm_year + 1; */ |
| 4707 |
tmp = tmfc.cc + 1; |
| 4708 |
if (pg_mul_s32_overflow(tmp, 100, &tmp) || |
| 4709 |
pg_sub_s32_overflow(tmp, tm->tm_year, &tmp) || |
| 4710 |
pg_add_s32_overflow(tmp, 1, &tm->tm_year)) |
| 4711 |
{ |
| 4712 |
DateTimeParseError(DTERR_FIELD_OVERFLOW, NULL, |
| 4713 |
text_to_cstring(date_txt), "timestamp", |
| 4714 |
escontext); |
| 4715 |
goto fail; |
| 4716 |
} |
| 4717 |
} |
| 4718 |
} |
| 4719 |
else |
| 4720 |
{ |
| 4721 |
/* find century year for dates ending in "00" */ |
| 4722 |
tm->tm_year = tmfc.cc * 100 + ((tmfc.cc >= 0) ? 0 : 1); |
| 4723 |
} |
| 4724 |
} |
| 4725 |
else |
| 4726 |
{ |
| 4727 |
/* If a 4-digit year is provided, we use that and ignore CC. */ |
| 4728 |
tm->tm_year = tmfc.year; |
| 4729 |
if (tmfc.bc) |
| 4730 |
tm->tm_year = -tm->tm_year; |
| 4731 |
/* correct for our representation of BC years */ |
| 4732 |
if (tm->tm_year < 0) |
| 4733 |
tm->tm_year++; |
| 4734 |
} |
| 4735 |
fmask |= DTK_M(YEAR); |
| 4736 |
} |
| 4737 |
else if (tmfc.cc) |
| 4738 |
{ |
| 4739 |
/* use first year of century */ |
| 4740 |
if (tmfc.bc) |
| 4741 |
tmfc.cc = -tmfc.cc; |
| 4742 |
if (tmfc.cc >= 0) |
| 4743 |
{ |
| 4744 |
/* +1 because 21st century started in 2001 */ |
| 4745 |
/* tm->tm_year = (tmfc.cc - 1) * 100 + 1; */ |
| 4746 |
if (pg_mul_s32_overflow(tmfc.cc - 1, 100, &tm->tm_year) || |
| 4747 |
pg_add_s32_overflow(tm->tm_year, 1, &tm->tm_year)) |
| 4748 |
{ |
| 4749 |
DateTimeParseError(DTERR_FIELD_OVERFLOW, NULL, |
| 4750 |
text_to_cstring(date_txt), "timestamp", |
| 4751 |
escontext); |
| 4752 |
goto fail; |
| 4753 |
} |
| 4754 |
} |
| 4755 |
else |
| 4756 |
{ |
| 4757 |
/* +1 because year == 599 is 600 BC */ |
| 4758 |
/* tm->tm_year = tmfc.cc * 100 + 1; */ |
| 4759 |
if (pg_mul_s32_overflow(tmfc.cc, 100, &tm->tm_year) || |
| 4760 |
pg_add_s32_overflow(tm->tm_year, 1, &tm->tm_year)) |
| 4761 |
{ |
| 4762 |
DateTimeParseError(DTERR_FIELD_OVERFLOW, NULL, |
| 4763 |
text_to_cstring(date_txt), "timestamp", |
| 4764 |
escontext); |
| 4765 |
goto fail; |
| 4766 |
} |
| 4767 |
} |
| 4768 |
fmask |= DTK_M(YEAR); |
| 4769 |
} |
| 4770 |
|
| 4771 |
if (tmfc.j) |
| 4772 |
{ |
| 4773 |
j2date(tmfc.j, &tm->tm_year, &tm->tm_mon, &tm->tm_mday); |
| 4774 |
fmask |= DTK_DATE_M; |
| 4775 |
} |
| 4776 |
|
| 4777 |
if (tmfc.ww) |
| 4778 |
{ |
| 4779 |
if (tmfc.mode == FROM_CHAR_DATE_ISOWEEK) |
| 4780 |
{ |
| 4781 |
/* |
| 4782 |
* If tmfc.d is not set, then the date is left at the beginning of |
| 4783 |
* the ISO week (Monday). |
| 4784 |
*/ |
| 4785 |
if (tmfc.d) |
| 4786 |
isoweekdate2date(tmfc.ww, tmfc.d, &tm->tm_year, &tm->tm_mon, &tm->tm_mday); |
| 4787 |
else |
| 4788 |
isoweek2date(tmfc.ww, &tm->tm_year, &tm->tm_mon, &tm->tm_mday); |
| 4789 |
fmask |= DTK_DATE_M; |
| 4790 |
} |
| 4791 |
else |
| 4792 |
{ |
| 4793 |
/* tmfc.ddd = (tmfc.ww - 1) * 7 + 1; */ |
| 4794 |
if (pg_sub_s32_overflow(tmfc.ww, 1, &tmfc.ddd) || |
| 4795 |
pg_mul_s32_overflow(tmfc.ddd, 7, &tmfc.ddd) || |
| 4796 |
pg_add_s32_overflow(tmfc.ddd, 1, &tmfc.ddd)) |
| 4797 |
{ |
| 4798 |
DateTimeParseError(DTERR_FIELD_OVERFLOW, NULL, |
| 4799 |
date_str, "timestamp", escontext); |
| 4800 |
goto fail; |
| 4801 |
} |
| 4802 |
} |
| 4803 |
} |
| 4804 |
|
| 4805 |
if (tmfc.w) |
| 4806 |
{ |
| 4807 |
/* tmfc.dd = (tmfc.w - 1) * 7 + 1; */ |
| 4808 |
if (pg_sub_s32_overflow(tmfc.w, 1, &tmfc.dd) || |
| 4809 |
pg_mul_s32_overflow(tmfc.dd, 7, &tmfc.dd) || |
| 4810 |
pg_add_s32_overflow(tmfc.dd, 1, &tmfc.dd)) |
| 4811 |
{ |
| 4812 |
DateTimeParseError(DTERR_FIELD_OVERFLOW, NULL, |
| 4813 |
date_str, "timestamp", escontext); |
| 4814 |
goto fail; |
| 4815 |
} |
| 4816 |
} |
| 4817 |
if (tmfc.dd) |
| 4818 |
{ |
| 4819 |
tm->tm_mday = tmfc.dd; |
| 4820 |
fmask |= DTK_M(DAY); |
| 4821 |
} |
| 4822 |
if (tmfc.mm) |
| 4823 |
{ |
| 4824 |
tm->tm_mon = tmfc.mm; |
| 4825 |
fmask |= DTK_M(MONTH); |
| 4826 |
} |
| 4827 |
|
| 4828 |
if (tmfc.ddd && (tm->tm_mon <= 1 || tm->tm_mday <= 1)) |
| 4829 |
{ |
| 4830 |
/* |
| 4831 |
* The month and day field have not been set, so we use the |
| 4832 |
* day-of-year field to populate them. Depending on the date mode, |
| 4833 |
* this field may be interpreted as a Gregorian day-of-year, or an ISO |
| 4834 |
* week date day-of-year. |
| 4835 |
*/ |
| 4836 |
|
| 4837 |
if (!tm->tm_year && !tmfc.bc) |
| 4838 |
{ |
| 4839 |
errsave(escontext, |
| 4840 |
(errcode(ERRCODE_INVALID_DATETIME_FORMAT), |
| 4841 |
errmsg("cannot calculate day of year without year information"))); |
| 4842 |
goto fail; |
| 4843 |
} |
| 4844 |
|
| 4845 |
if (tmfc.mode == FROM_CHAR_DATE_ISOWEEK) |
| 4846 |
{ |
| 4847 |
int j0; /* zeroth day of the ISO year, in Julian */ |
| 4848 |
|
| 4849 |
j0 = isoweek2j(tm->tm_year, 1) - 1; |
| 4850 |
|
| 4851 |
j2date(j0 + tmfc.ddd, &tm->tm_year, &tm->tm_mon, &tm->tm_mday); |
| 4852 |
fmask |= DTK_DATE_M; |
| 4853 |
} |
| 4854 |
else |
| 4855 |
{ |
| 4856 |
const int *y; |
| 4857 |
int i; |
| 4858 |
|
| 4859 |
static const int ysum[2][13] = { |
| 4860 |
{0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365}, |
| 4861 |
{0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366}}; |
| 4862 |
|
| 4863 |
y = ysum[isleap(tm->tm_year)]; |
| 4864 |
|
| 4865 |
for (i = 1; i <= MONTHS_PER_YEAR; i++) |
| 4866 |
{ |
| 4867 |
if (tmfc.ddd <= y[i]) |
| 4868 |
break; |
| 4869 |
} |
| 4870 |
if (tm->tm_mon <= 1) |
| 4871 |
tm->tm_mon = i; |
| 4872 |
|
| 4873 |
if (tm->tm_mday <= 1) |
| 4874 |
tm->tm_mday = tmfc.ddd - y[i - 1]; |
| 4875 |
|
| 4876 |
fmask |= DTK_M(MONTH) | DTK_M(DAY); |
| 4877 |
} |
| 4878 |
} |
| 4879 |
|
| 4880 |
if (tmfc.ms) |
| 4881 |
{ |
| 4882 |
int tmp = 0; |
| 4883 |
|
| 4884 |
/* *fsec += tmfc.ms * 1000; */ |
| 4885 |
if (pg_mul_s32_overflow(tmfc.ms, 1000, &tmp) || |
| 4886 |
pg_add_s32_overflow(*fsec, tmp, fsec)) |
| 4887 |
{ |
| 4888 |
DateTimeParseError(DTERR_FIELD_OVERFLOW, NULL, |
| 4889 |
date_str, "timestamp", escontext); |
| 4890 |
goto fail; |
| 4891 |
} |
| 4892 |
} |
| 4893 |
if (tmfc.us) |
| 4894 |
*fsec += tmfc.us; |
| 4895 |
if (fprec) |
| 4896 |
*fprec = tmfc.ff; /* fractional precision, if specified */ |
| 4897 |
|
| 4898 |
/* Range-check date fields according to bit mask computed above */ |
| 4899 |
if (fmask != 0) |
| 4900 |
{ |
| 4901 |
/* We already dealt with AD/BC, so pass isjulian = true */ |
| 4902 |
int dterr = ValidateDate(fmask, true, false, false, tm); |
| 4903 |
|
| 4904 |
if (dterr != 0) |
| 4905 |
{ |
| 4906 |
/* |
| 4907 |
* Force the error to be DTERR_FIELD_OVERFLOW even if ValidateDate |
| 4908 |
* said DTERR_MD_FIELD_OVERFLOW, because we don't want to print an |
| 4909 |
* irrelevant hint about datestyle. |
| 4910 |
*/ |
| 4911 |
DateTimeParseError(DTERR_FIELD_OVERFLOW, NULL, |
| 4912 |
date_str, "timestamp", escontext); |
| 4913 |
goto fail; |
| 4914 |
} |
| 4915 |
} |
| 4916 |
|
| 4917 |
/* Range-check time fields too */ |
| 4918 |
if (tm->tm_hour < 0 || tm->tm_hour >= HOURS_PER_DAY || |
| 4919 |
tm->tm_min < 0 || tm->tm_min >= MINS_PER_HOUR || |
| 4920 |
tm->tm_sec < 0 || tm->tm_sec >= SECS_PER_MINUTE || |
| 4921 |
*fsec < INT64CONST(0) || *fsec >= USECS_PER_SEC) |
| 4922 |
{ |
| 4923 |
DateTimeParseError(DTERR_FIELD_OVERFLOW, NULL, |
| 4924 |
date_str, "timestamp", escontext); |
| 4925 |
goto fail; |
| 4926 |
} |
| 4927 |
|
| 4928 |
/* |
| 4929 |
* If timezone info was present, reduce it to a GMT offset. (We cannot do |
| 4930 |
* this until we've filled all of the tm struct, since the zone's offset |
| 4931 |
* might be time-varying.) |
| 4932 |
*/ |
| 4933 |
if (tmfc.tzsign) |
| 4934 |
{ |
| 4935 |
/* TZH and/or TZM fields */ |
| 4936 |
if (tmfc.tzh < 0 || tmfc.tzh > MAX_TZDISP_HOUR || |
| 4937 |
tmfc.tzm < 0 || tmfc.tzm >= MINS_PER_HOUR) |
| 4938 |
{ |
| 4939 |
DateTimeParseError(DTERR_TZDISP_OVERFLOW, NULL, |
| 4940 |
date_str, "timestamp", escontext); |
| 4941 |
goto fail; |
| 4942 |
} |
| 4943 |
|
| 4944 |
tz->has_tz = true; |
| 4945 |
tz->gmtoffset = (tmfc.tzh * MINS_PER_HOUR + tmfc.tzm) * SECS_PER_MINUTE; |
| 4946 |
/* note we are flipping the sign convention here */ |
| 4947 |
if (tmfc.tzsign > 0) |
| 4948 |
tz->gmtoffset = -tz->gmtoffset; |
| 4949 |
} |
| 4950 |
else if (tmfc.has_tz) |
| 4951 |
{ |
| 4952 |
/* TZ field */ |
| 4953 |
tz->has_tz = true; |
| 4954 |
if (tmfc.tzp == NULL) |
| 4955 |
{ |
| 4956 |
/* fixed-offset abbreviation; flip the sign convention */ |
| 4957 |
tz->gmtoffset = -tmfc.gmtoffset; |
| 4958 |
} |
| 4959 |
else |
| 4960 |
{ |
| 4961 |
/* dynamic-offset abbreviation, resolve using specified time */ |
| 4962 |
tz->gmtoffset = DetermineTimeZoneAbbrevOffset(tm, tmfc.abbrev, |
| 4963 |
tmfc.tzp); |
| 4964 |
} |
| 4965 |
} |
| 4966 |
|
| 4967 |
DEBUG_TM(tm); |
| 4968 |
|
| 4969 |
if (format && !incache) |
| 4970 |
pfree(format); |
| 4971 |
pfree(date_str); |
| 4972 |
|
| 4973 |
return true; |
| 4974 |
|
| 4975 |
fail: |
| 4976 |
if (format && !incache) |
| 4977 |
pfree(format); |
| 4978 |
pfree(date_str); |
| 4979 |
|
| 4980 |
return false; |
| 4981 |
} |
| 4982 |
|
| 4983 |
|
| 4984 |
/********************************************************************** |
| 4985 |
* the NUMBER version part |
| 4986 |
*********************************************************************/ |
| 4987 |
|
| 4988 |
|
| 4989 |
static char * |
| 4990 |
fill_str(char *str, int c, int max) |
| 4991 |
{ |
| 4992 |
memset(str, c, max); |
| 4993 |
*(str + max) = '\0'; |
| 4994 |
return str; |
| 4995 |
} |
| 4996 |
|
| 4997 |
#define zeroize_NUM(_n) \ |
| 4998 |
do { \ |
| 4999 |
(_n)->flag = 0; \ |
| 5000 |
(_n)->lsign = 0; \ |
| 5001 |
(_n)->pre = 0; \ |
| 5002 |
(_n)->post = 0; \ |
| 5003 |
(_n)->pre_lsign_num = 0; \ |
| 5004 |
(_n)->need_locale = 0; \ |
| 5005 |
(_n)->multi = 0; \ |
| 5006 |
(_n)->zero_start = 0; \ |
| 5007 |
(_n)->zero_end = 0; \ |
| 5008 |
} while(0) |
| 5009 |
|
| 5010 |
/* This works the same as DCH_prevent_counter_overflow */ |
| 5011 |
static inline void |
| 5012 |
NUM_prevent_counter_overflow(void) |
| 5013 |
{ |
| 5014 |
if (NUMCounter >= (INT_MAX - 1)) |
| 5015 |
{ |
| 5016 |
for (int i = 0; i < n_NUMCache; i++) |
| 5017 |
NUMCache[i]->age >>= 1; |
| 5018 |
NUMCounter >>= 1; |
| 5019 |
} |
| 5020 |
} |
| 5021 |
|
| 5022 |
/* select a NUMCacheEntry to hold the given format picture */ |
| 5023 |
static NUMCacheEntry * |
| 5024 |
NUM_cache_getnew(const char *str) |
| 5025 |
{ |
| 5026 |
NUMCacheEntry *ent; |
| 5027 |
|
| 5028 |
/* Ensure we can advance NUMCounter below */ |
| 5029 |
NUM_prevent_counter_overflow(); |
| 5030 |
|
| 5031 |
/* |
| 5032 |
* If cache is full, remove oldest entry (or recycle first not-valid one) |
| 5033 |
*/ |
| 5034 |
if (n_NUMCache >= NUM_CACHE_ENTRIES) |
| 5035 |
{ |
| 5036 |
NUMCacheEntry *old = NUMCache[0]; |
| 5037 |
|
| 5038 |
#ifdef DEBUG_TO_FROM_CHAR |
| 5039 |
elog(DEBUG_elog_output, "Cache is full (%d)", n_NUMCache); |
| 5040 |
#endif |
| 5041 |
if (old->valid) |
| 5042 |
{ |
| 5043 |
for (int i = 1; i < NUM_CACHE_ENTRIES; i++) |
| 5044 |
{ |
| 5045 |
ent = NUMCache[i]; |
| 5046 |
if (!ent->valid) |
| 5047 |
{ |
| 5048 |
old = ent; |
| 5049 |
break; |
| 5050 |
} |
| 5051 |
if (ent->age < old->age) |
| 5052 |
old = ent; |
| 5053 |
} |
| 5054 |
} |
| 5055 |
#ifdef DEBUG_TO_FROM_CHAR |
| 5056 |
elog(DEBUG_elog_output, "OLD: \"%s\" AGE: %d", old->str, old->age); |
| 5057 |
#endif |
| 5058 |
old->valid = false; |
| 5059 |
strlcpy(old->str, str, NUM_CACHE_SIZE + 1); |
| 5060 |
old->age = (++NUMCounter); |
| 5061 |
/* caller is expected to fill format and Num, then set valid */ |
| 5062 |
return old; |
| 5063 |
} |
| 5064 |
else |
| 5065 |
{ |
| 5066 |
#ifdef DEBUG_TO_FROM_CHAR |
| 5067 |
elog(DEBUG_elog_output, "NEW (%d)", n_NUMCache); |
| 5068 |
#endif |
| 5069 |
Assert(NUMCache[n_NUMCache] == NULL); |
| 5070 |
NUMCache[n_NUMCache] = ent = (NUMCacheEntry *) |
| 5071 |
MemoryContextAllocZero(TopMemoryContext, sizeof(NUMCacheEntry)); |
| 5072 |
ent->valid = false; |
| 5073 |
strlcpy(ent->str, str, NUM_CACHE_SIZE + 1); |
| 5074 |
ent->age = (++NUMCounter); |
| 5075 |
/* caller is expected to fill format and Num, then set valid */ |
| 5076 |
++n_NUMCache; |
| 5077 |
return ent; |
| 5078 |
} |
| 5079 |
} |
| 5080 |
|
| 5081 |
/* look for an existing NUMCacheEntry matching the given format picture */ |
| 5082 |
static NUMCacheEntry * |
| 5083 |
NUM_cache_search(const char *str) |
| 5084 |
{ |
| 5085 |
/* Ensure we can advance NUMCounter below */ |
| 5086 |
NUM_prevent_counter_overflow(); |
| 5087 |
|
| 5088 |
for (int i = 0; i < n_NUMCache; i++) |
| 5089 |
{ |
| 5090 |
NUMCacheEntry *ent = NUMCache[i]; |
| 5091 |
|
| 5092 |
if (ent->valid && strcmp(ent->str, str) == 0) |
| 5093 |
{ |
| 5094 |
ent->age = (++NUMCounter); |
| 5095 |
return ent; |
| 5096 |
} |
| 5097 |
} |
| 5098 |
|
| 5099 |
return NULL; |
| 5100 |
} |
| 5101 |
|
| 5102 |
/* Find or create a NUMCacheEntry for the given format picture */ |
| 5103 |
static NUMCacheEntry * |
| 5104 |
NUM_cache_fetch(const char *str) |
| 5105 |
{ |
| 5106 |
NUMCacheEntry *ent; |
| 5107 |
|
| 5108 |
if ((ent = NUM_cache_search(str)) == NULL) |
| 5109 |
{ |
| 5110 |
/* |
| 5111 |
* Not in the cache, must run parser and save a new format-picture to |
| 5112 |
* the cache. Do not mark the cache entry valid until parsing |
| 5113 |
* succeeds. |
| 5114 |
*/ |
| 5115 |
ent = NUM_cache_getnew(str); |
| 5116 |
|
| 5117 |
zeroize_NUM(&ent->Num); |
| 5118 |
|
| 5119 |
parse_format(ent->format, str, NUM_keywords, |
| 5120 |
NULL, NUM_index, NUM_FLAG, &ent->Num); |
| 5121 |
|
| 5122 |
ent->valid = true; |
| 5123 |
} |
| 5124 |
return ent; |
| 5125 |
} |
| 5126 |
|
| 5127 |
/* ---------- |
| 5128 |
* Cache routine for NUM to_char version |
| 5129 |
* ---------- |
| 5130 |
*/ |
| 5131 |
static FormatNode * |
| 5132 |
NUM_cache(int len, NUMDesc *Num, text *pars_str, bool *shouldFree) |
| 5133 |
{ |
| 5134 |
FormatNode *format = NULL; |
| 5135 |
char *str; |
| 5136 |
|
| 5137 |
str = text_to_cstring(pars_str); |
| 5138 |
|
| 5139 |
if (len > NUM_CACHE_SIZE) |
| 5140 |
{ |
| 5141 |
/* |
| 5142 |
* Allocate new memory if format picture is bigger than static cache |
| 5143 |
* and do not use cache (call parser always) |
| 5144 |
*/ |
| 5145 |
format = palloc_array(FormatNode, len + 1); |
| 5146 |
|
| 5147 |
*shouldFree = true; |
| 5148 |
|
| 5149 |
zeroize_NUM(Num); |
| 5150 |
|
| 5151 |
parse_format(format, str, NUM_keywords, |
| 5152 |
NULL, NUM_index, NUM_FLAG, Num); |
| 5153 |
} |
| 5154 |
else |
| 5155 |
{ |
| 5156 |
/* |
| 5157 |
* Use cache buffers |
| 5158 |
*/ |
| 5159 |
NUMCacheEntry *ent = NUM_cache_fetch(str); |
| 5160 |
|
| 5161 |
*shouldFree = false; |
| 5162 |
|
| 5163 |
format = ent->format; |
| 5164 |
|
| 5165 |
/* |
| 5166 |
* Copy cache to used struct |
| 5167 |
*/ |
| 5168 |
Num->flag = ent->Num.flag; |
| 5169 |
Num->lsign = ent->Num.lsign; |
| 5170 |
Num->pre = ent->Num.pre; |
| 5171 |
Num->post = ent->Num.post; |
| 5172 |
Num->pre_lsign_num = ent->Num.pre_lsign_num; |
| 5173 |
Num->need_locale = ent->Num.need_locale; |
| 5174 |
Num->multi = ent->Num.multi; |
| 5175 |
Num->zero_start = ent->Num.zero_start; |
| 5176 |
Num->zero_end = ent->Num.zero_end; |
| 5177 |
} |
| 5178 |
|
| 5179 |
#ifdef DEBUG_TO_FROM_CHAR |
| 5180 |
/* dump_node(format, len); */ |
| 5181 |
dump_index(NUM_keywords, NUM_index); |
| 5182 |
#endif |
| 5183 |
|
| 5184 |
pfree(str); |
| 5185 |
return format; |
| 5186 |
} |
| 5187 |
|
| 5188 |
|
| 5189 |
/* |
| 5190 |
* Convert integer to Roman numerals |
| 5191 |
* Result is upper-case and not blank-padded (NUM_processor converts as needed) |
| 5192 |
* If input is out-of-range, produce '###############' |
| 5193 |
*/ |
| 5194 |
static char * |
| 5195 |
int_to_roman(int number) |
| 5196 |
{ |
| 5197 |
int len, |
| 5198 |
num; |
| 5199 |
char *p, |
| 5200 |
*result, |
| 5201 |
numstr[12]; |
| 5202 |
|
| 5203 |
result = (char *) palloc(MAX_ROMAN_LEN + 1); |
| 5204 |
*result = '\0'; |
| 5205 |
|
| 5206 |
/* |
| 5207 |
* This range limit is the same as in Oracle(TM). The difficulty with |
| 5208 |
* handling 4000 or more is that we'd need to use more than 3 "M"'s, and |
| 5209 |
* more than 3 of the same digit isn't considered a valid Roman string. |
| 5210 |
*/ |
| 5211 |
if (number > 3999 || number < 1) |
| 5212 |
{ |
| 5213 |
fill_str(result, '#', MAX_ROMAN_LEN); |
| 5214 |
return result; |
| 5215 |
} |
| 5216 |
|
| 5217 |
/* Convert to decimal, then examine each digit */ |
| 5218 |
len = snprintf(numstr, sizeof(numstr), "%d", number); |
| 5219 |
Assert(len > 0 && len <= 4); |
| 5220 |
|
| 5221 |
for (p = numstr; *p != '\0'; p++, --len) |
| 5222 |
{ |
| 5223 |
num = *p - ('0' + 1); |
| 5224 |
if (num < 0) |
| 5225 |
continue; /* ignore zeroes */ |
| 5226 |
/* switch on current column position */ |
| 5227 |
switch (len) |
| 5228 |
{ |
| 5229 |
case 4: |
| 5230 |
while (num-- >= 0) |
| 5231 |
strcat(result, "M"); |
| 5232 |
break; |
| 5233 |
case 3: |
| 5234 |
strcat(result, rm100[num]); |
| 5235 |
break; |
| 5236 |
case 2: |
| 5237 |
strcat(result, rm10[num]); |
| 5238 |
break; |
| 5239 |
case 1: |
| 5240 |
strcat(result, rm1[num]); |
| 5241 |
break; |
| 5242 |
} |
| 5243 |
} |
| 5244 |
return result; |
| 5245 |
} |
| 5246 |
|
| 5247 |
/* |
| 5248 |
* Convert a roman numeral (standard form) to an integer. |
| 5249 |
* Result is an integer between 1 and 3999. |
| 5250 |
* Np->inout_p is advanced past the characters consumed. |
| 5251 |
* |
| 5252 |
* If input is invalid, return -1. |
| 5253 |
*/ |
| 5254 |
static int |
| 5255 |
roman_to_int(NUMProc *Np, int input_len) |
| 5256 |
{ |
| 5257 |
int result = 0; |
| 5258 |
int len; |
| 5259 |
char romanChars[MAX_ROMAN_LEN]; |
| 5260 |
int romanValues[MAX_ROMAN_LEN]; |
| 5261 |
int repeatCount = 1; |
| 5262 |
int vCount = 0, |
| 5263 |
lCount = 0, |
| 5264 |
dCount = 0; |
| 5265 |
bool subtractionEncountered = false; |
| 5266 |
int lastSubtractedValue = 0; |
| 5267 |
|
| 5268 |
/* |
| 5269 |
* Skip any leading whitespace. Perhaps we should limit the amount of |
| 5270 |
* space skipped to MAX_ROMAN_LEN, but that seems unnecessarily picky. |
| 5271 |
*/ |
| 5272 |
while (!OVERLOAD_TEST && isspace((unsigned char) *Np->inout_p)) |
| 5273 |
Np->inout_p++; |
| 5274 |
|
| 5275 |
/* |
| 5276 |
* Collect and decode valid roman numerals, consuming at most |
| 5277 |
* MAX_ROMAN_LEN characters. We do this in a separate loop to avoid |
| 5278 |
* repeated decoding and because the main loop needs to know when it's at |
| 5279 |
* the last numeral. |
| 5280 |
*/ |
| 5281 |
for (len = 0; len < MAX_ROMAN_LEN && !OVERLOAD_TEST; len++) |
| 5282 |
{ |
| 5283 |
char currChar = pg_ascii_toupper(*Np->inout_p); |
| 5284 |
int currValue = ROMAN_VAL(currChar); |
| 5285 |
|
| 5286 |
if (currValue == 0) |
| 5287 |
break; /* Not a valid roman numeral. */ |
| 5288 |
romanChars[len] = currChar; |
| 5289 |
romanValues[len] = currValue; |
| 5290 |
Np->inout_p++; |
| 5291 |
} |
| 5292 |
|
| 5293 |
if (len == 0) |
| 5294 |
return -1; /* No valid roman numerals. */ |
| 5295 |
|
| 5296 |
/* Check for valid combinations and compute the represented value. */ |
| 5297 |
for (int i = 0; i < len; i++) |
| 5298 |
{ |
| 5299 |
char currChar = romanChars[i]; |
| 5300 |
int currValue = romanValues[i]; |
| 5301 |
|
| 5302 |
/* |
| 5303 |
* Ensure no numeral greater than or equal to the subtracted numeral |
| 5304 |
* appears after a subtraction. |
| 5305 |
*/ |
| 5306 |
if (subtractionEncountered && currValue >= lastSubtractedValue) |
| 5307 |
return -1; |
| 5308 |
|
| 5309 |
/* |
| 5310 |
* V, L, and D should not appear before a larger numeral, nor should |
| 5311 |
* they be repeated. |
| 5312 |
*/ |
| 5313 |
if ((vCount && currValue >= ROMAN_VAL('V')) || |
| 5314 |
(lCount && currValue >= ROMAN_VAL('L')) || |
| 5315 |
(dCount && currValue >= ROMAN_VAL('D'))) |
| 5316 |
return -1; |
| 5317 |
if (currChar == 'V') |
| 5318 |
vCount++; |
| 5319 |
else if (currChar == 'L') |
| 5320 |
lCount++; |
| 5321 |
else if (currChar == 'D') |
| 5322 |
dCount++; |
| 5323 |
|
| 5324 |
if (i < len - 1) |
| 5325 |
{ |
| 5326 |
/* Compare current numeral to next numeral. */ |
| 5327 |
char nextChar = romanChars[i + 1]; |
| 5328 |
int nextValue = romanValues[i + 1]; |
| 5329 |
|
| 5330 |
/* |
| 5331 |
* If the current value is less than the next value, handle |
| 5332 |
* subtraction. Verify valid subtractive combinations and update |
| 5333 |
* the result accordingly. |
| 5334 |
*/ |
| 5335 |
if (currValue < nextValue) |
| 5336 |
{ |
| 5337 |
if (!IS_VALID_SUB_COMB(currChar, nextChar)) |
| 5338 |
return -1; |
| 5339 |
|
| 5340 |
/* |
| 5341 |
* Reject cases where same numeral is repeated with |
| 5342 |
* subtraction (e.g. 'MCCM' or 'DCCCD'). |
| 5343 |
*/ |
| 5344 |
if (repeatCount > 1) |
| 5345 |
return -1; |
| 5346 |
|
| 5347 |
/* |
| 5348 |
* We are going to skip nextChar, so first make checks needed |
| 5349 |
* for V, L, and D. These are the same as we'd have applied |
| 5350 |
* if we reached nextChar without a subtraction. |
| 5351 |
*/ |
| 5352 |
if ((vCount && nextValue >= ROMAN_VAL('V')) || |
| 5353 |
(lCount && nextValue >= ROMAN_VAL('L')) || |
| 5354 |
(dCount && nextValue >= ROMAN_VAL('D'))) |
| 5355 |
return -1; |
| 5356 |
if (nextChar == 'V') |
| 5357 |
vCount++; |
| 5358 |
else if (nextChar == 'L') |
| 5359 |
lCount++; |
| 5360 |
else if (nextChar == 'D') |
| 5361 |
dCount++; |
| 5362 |
|
| 5363 |
/* |
| 5364 |
* Skip the next numeral as it is part of the subtractive |
| 5365 |
* combination. |
| 5366 |
*/ |
| 5367 |
i++; |
| 5368 |
|
| 5369 |
/* Update state. */ |
| 5370 |
repeatCount = 1; |
| 5371 |
subtractionEncountered = true; |
| 5372 |
lastSubtractedValue = currValue; |
| 5373 |
result += (nextValue - currValue); |
| 5374 |
} |
| 5375 |
else |
| 5376 |
{ |
| 5377 |
/* For same numerals, check for repetition. */ |
| 5378 |
if (currChar == nextChar) |
| 5379 |
{ |
| 5380 |
repeatCount++; |
| 5381 |
if (repeatCount > 3) |
| 5382 |
return -1; |
| 5383 |
} |
| 5384 |
else |
| 5385 |
repeatCount = 1; |
| 5386 |
result += currValue; |
| 5387 |
} |
| 5388 |
} |
| 5389 |
else |
| 5390 |
{ |
| 5391 |
/* This is the last numeral; just add it to the result. */ |
| 5392 |
result += currValue; |
| 5393 |
} |
| 5394 |
} |
| 5395 |
|
| 5396 |
return result; |
| 5397 |
} |
| 5398 |
|
| 5399 |
|
| 5400 |
/* ---------- |
| 5401 |
* Locale |
| 5402 |
* ---------- |
| 5403 |
*/ |
| 5404 |
static void |
| 5405 |
NUM_prepare_locale(NUMProc *Np) |
| 5406 |
{ |
| 5407 |
if (Np->Num->need_locale) |
| 5408 |
{ |
| 5409 |
struct lconv *lconv; |
| 5410 |
|
| 5411 |
/* |
| 5412 |
* Get locales |
| 5413 |
*/ |
| 5414 |
lconv = PGLC_localeconv(); |
| 5415 |
|
| 5416 |
/* |
| 5417 |
* Positive / Negative number sign |
| 5418 |
*/ |
| 5419 |
if (lconv->negative_sign && *lconv->negative_sign) |
| 5420 |
Np->L_negative_sign = lconv->negative_sign; |
| 5421 |
else |
| 5422 |
Np->L_negative_sign = "-"; |
| 5423 |
|
| 5424 |
if (lconv->positive_sign && *lconv->positive_sign) |
| 5425 |
Np->L_positive_sign = lconv->positive_sign; |
| 5426 |
else |
| 5427 |
Np->L_positive_sign = "+"; |
| 5428 |
|
| 5429 |
/* |
| 5430 |
* Number decimal point |
| 5431 |
*/ |
| 5432 |
if (lconv->decimal_point && *lconv->decimal_point) |
| 5433 |
Np->decimal = lconv->decimal_point; |
| 5434 |
|
| 5435 |
else |
| 5436 |
Np->decimal = "."; |
| 5437 |
|
| 5438 |
if (!IS_LDECIMAL(Np->Num)) |
| 5439 |
Np->decimal = "."; |
| 5440 |
|
| 5441 |
/* |
| 5442 |
* Number thousands separator |
| 5443 |
* |
| 5444 |
* Some locales (e.g. broken glibc pt_BR), have a comma for decimal, |
| 5445 |
* but "" for thousands_sep, so we set the thousands_sep too. |
| 5446 |
* http://archives.postgresql.org/pgsql-hackers/2007-11/msg00772.php |
| 5447 |
*/ |
| 5448 |
if (lconv->thousands_sep && *lconv->thousands_sep) |
| 5449 |
Np->L_thousands_sep = lconv->thousands_sep; |
| 5450 |
/* Make sure thousands separator doesn't match decimal point symbol. */ |
| 5451 |
else if (strcmp(Np->decimal, ",") != 0) |
| 5452 |
Np->L_thousands_sep = ","; |
| 5453 |
else |
| 5454 |
Np->L_thousands_sep = "."; |
| 5455 |
|
| 5456 |
/* |
| 5457 |
* Currency symbol |
| 5458 |
*/ |
| 5459 |
if (lconv->currency_symbol && *lconv->currency_symbol) |
| 5460 |
Np->L_currency_symbol = lconv->currency_symbol; |
| 5461 |
else |
| 5462 |
Np->L_currency_symbol = " "; |
| 5463 |
} |
| 5464 |
else |
| 5465 |
{ |
| 5466 |
/* |
| 5467 |
* Default values |
| 5468 |
*/ |
| 5469 |
Np->L_negative_sign = "-"; |
| 5470 |
Np->L_positive_sign = "+"; |
| 5471 |
Np->decimal = "."; |
| 5472 |
|
| 5473 |
Np->L_thousands_sep = ","; |
| 5474 |
Np->L_currency_symbol = " "; |
| 5475 |
} |
| 5476 |
} |
| 5477 |
|
| 5478 |
/* ---------- |
| 5479 |
* Return pointer of last relevant number after decimal point |
| 5480 |
* 12.0500 --> last relevant is '5' |
| 5481 |
* 12.0000 --> last relevant is '.' |
| 5482 |
* If there is no decimal point, return NULL (which will result in same |
| 5483 |
* behavior as if FM hadn't been specified). |
| 5484 |
* ---------- |
| 5485 |
*/ |
| 5486 |
static char * |
| 5487 |
get_last_relevant_decnum(char *num) |
| 5488 |
{ |
| 5489 |
char *result, |
| 5490 |
*p = strchr(num, '.'); |
| 5491 |
|
| 5492 |
#ifdef DEBUG_TO_FROM_CHAR |
| 5493 |
elog(DEBUG_elog_output, "get_last_relevant_decnum()"); |
| 5494 |
#endif |
| 5495 |
|
| 5496 |
if (!p) |
| 5497 |
return NULL; |
| 5498 |
|
| 5499 |
result = p; |
| 5500 |
|
| 5501 |
while (*(++p)) |
| 5502 |
{ |
| 5503 |
if (*p != '0') |
| 5504 |
result = p; |
| 5505 |
} |
| 5506 |
|
| 5507 |
return result; |
| 5508 |
} |
| 5509 |
|
| 5510 |
/* ---------- |
| 5511 |
* Number extraction for TO_NUMBER() |
| 5512 |
* ---------- |
| 5513 |
*/ |
| 5514 |
static void |
| 5515 |
NUM_numpart_from_char(NUMProc *Np, int id, int input_len) |
| 5516 |
{ |
| 5517 |
bool isread = false; |
| 5518 |
|
| 5519 |
#ifdef DEBUG_TO_FROM_CHAR |
| 5520 |
elog(DEBUG_elog_output, " --- scan start --- id=%s", |
| 5521 |
(id == NUM_0 || id == NUM_9) ? "NUM_0/9" : id == NUM_DEC ? "NUM_DEC" : "???"); |
| 5522 |
#endif |
| 5523 |
|
| 5524 |
if (OVERLOAD_TEST) |
| 5525 |
return; |
| 5526 |
|
| 5527 |
if (*Np->inout_p == ' ') |
| 5528 |
Np->inout_p++; |
| 5529 |
|
| 5530 |
if (OVERLOAD_TEST) |
| 5531 |
return; |
| 5532 |
|
| 5533 |
/* |
| 5534 |
* read sign before number |
| 5535 |
*/ |
| 5536 |
if (*Np->number == ' ' && (id == NUM_0 || id == NUM_9) && |
| 5537 |
(Np->read_pre + Np->read_post) == 0) |
| 5538 |
{ |
| 5539 |
#ifdef DEBUG_TO_FROM_CHAR |
| 5540 |
elog(DEBUG_elog_output, "Try read sign (%c), locale positive: %s, negative: %s", |
| 5541 |
*Np->inout_p, Np->L_positive_sign, Np->L_negative_sign); |
| 5542 |
#endif |
| 5543 |
|
| 5544 |
/* |
| 5545 |
* locale sign |
| 5546 |
*/ |
| 5547 |
if (IS_LSIGN(Np->Num) && Np->Num->lsign == NUM_LSIGN_PRE) |
| 5548 |
{ |
| 5549 |
int x = 0; |
| 5550 |
|
| 5551 |
#ifdef DEBUG_TO_FROM_CHAR |
| 5552 |
elog(DEBUG_elog_output, "Try read locale pre-sign (%c)", *Np->inout_p); |
| 5553 |
#endif |
| 5554 |
if ((x = strlen(Np->L_negative_sign)) && |
| 5555 |
AMOUNT_TEST(x) && |
| 5556 |
strncmp(Np->inout_p, Np->L_negative_sign, x) == 0) |
| 5557 |
{ |
| 5558 |
Np->inout_p += x; |
| 5559 |
*Np->number = '-'; |
| 5560 |
} |
| 5561 |
else if ((x = strlen(Np->L_positive_sign)) && |
| 5562 |
AMOUNT_TEST(x) && |
| 5563 |
strncmp(Np->inout_p, Np->L_positive_sign, x) == 0) |
| 5564 |
{ |
| 5565 |
Np->inout_p += x; |
| 5566 |
*Np->number = '+'; |
| 5567 |
} |
| 5568 |
} |
| 5569 |
else |
| 5570 |
{ |
| 5571 |
#ifdef DEBUG_TO_FROM_CHAR |
| 5572 |
elog(DEBUG_elog_output, "Try read simple sign (%c)", *Np->inout_p); |
| 5573 |
#endif |
| 5574 |
|
| 5575 |
/* |
| 5576 |
* simple + - < > |
| 5577 |
*/ |
| 5578 |
if (*Np->inout_p == '-' || (IS_BRACKET(Np->Num) && |
| 5579 |
*Np->inout_p == '<')) |
| 5580 |
{ |
| 5581 |
*Np->number = '-'; /* set - */ |
| 5582 |
Np->inout_p++; |
| 5583 |
} |
| 5584 |
else if (*Np->inout_p == '+') |
| 5585 |
{ |
| 5586 |
*Np->number = '+'; /* set + */ |
| 5587 |
Np->inout_p++; |
| 5588 |
} |
| 5589 |
} |
| 5590 |
} |
| 5591 |
|
| 5592 |
if (OVERLOAD_TEST) |
| 5593 |
return; |
| 5594 |
|
| 5595 |
#ifdef DEBUG_TO_FROM_CHAR |
| 5596 |
elog(DEBUG_elog_output, "Scan for numbers (%c), current number: '%s'", *Np->inout_p, Np->number); |
| 5597 |
#endif |
| 5598 |
|
| 5599 |
/* |
| 5600 |
* read digit or decimal point |
| 5601 |
*/ |
| 5602 |
if (isdigit((unsigned char) *Np->inout_p)) |
| 5603 |
{ |
| 5604 |
if (Np->read_dec && Np->read_post == Np->Num->post) |
| 5605 |
return; |
| 5606 |
|
| 5607 |
*Np->number_p = *Np->inout_p; |
| 5608 |
Np->number_p++; |
| 5609 |
|
| 5610 |
if (Np->read_dec) |
| 5611 |
Np->read_post++; |
| 5612 |
else |
| 5613 |
Np->read_pre++; |
| 5614 |
|
| 5615 |
isread = true; |
| 5616 |
|
| 5617 |
#ifdef DEBUG_TO_FROM_CHAR |
| 5618 |
elog(DEBUG_elog_output, "Read digit (%c)", *Np->inout_p); |
| 5619 |
#endif |
| 5620 |
} |
| 5621 |
else if (IS_DECIMAL(Np->Num) && Np->read_dec == false) |
| 5622 |
{ |
| 5623 |
/* |
| 5624 |
* We need not test IS_LDECIMAL(Np->Num) explicitly here, because |
| 5625 |
* Np->decimal is always just "." if we don't have a D format token. |
| 5626 |
* So we just unconditionally match to Np->decimal. |
| 5627 |
*/ |
| 5628 |
int x = strlen(Np->decimal); |
| 5629 |
|
| 5630 |
#ifdef DEBUG_TO_FROM_CHAR |
| 5631 |
elog(DEBUG_elog_output, "Try read decimal point (%c)", |
| 5632 |
*Np->inout_p); |
| 5633 |
#endif |
| 5634 |
if (x && AMOUNT_TEST(x) && strncmp(Np->inout_p, Np->decimal, x) == 0) |
| 5635 |
{ |
| 5636 |
Np->inout_p += x - 1; |
| 5637 |
*Np->number_p = '.'; |
| 5638 |
Np->number_p++; |
| 5639 |
Np->read_dec = true; |
| 5640 |
isread = true; |
| 5641 |
} |
| 5642 |
} |
| 5643 |
|
| 5644 |
if (OVERLOAD_TEST) |
| 5645 |
return; |
| 5646 |
|
| 5647 |
/* |
| 5648 |
* Read sign behind "last" number |
| 5649 |
* |
| 5650 |
* We need sign detection because determine exact position of post-sign is |
| 5651 |
* difficult: |
| 5652 |
* |
| 5653 |
* FM9999.9999999S -> 123.001- 9.9S -> .5- FM9.999999MI -> |
| 5654 |
* 5.01- |
| 5655 |
*/ |
| 5656 |
if (*Np->number == ' ' && Np->read_pre + Np->read_post > 0) |
| 5657 |
{ |
| 5658 |
/* |
| 5659 |
* locale sign (NUM_S) is always anchored behind a last number, if: - |
| 5660 |
* locale sign expected - last read char was NUM_0/9 or NUM_DEC - and |
| 5661 |
* next char is not digit |
| 5662 |
*/ |
| 5663 |
if (IS_LSIGN(Np->Num) && isread && |
| 5664 |
(Np->inout_p + 1) < Np->inout + input_len && |
| 5665 |
!isdigit((unsigned char) *(Np->inout_p + 1))) |
| 5666 |
{ |
| 5667 |
int x; |
| 5668 |
char *tmp = Np->inout_p++; |
| 5669 |
|
| 5670 |
#ifdef DEBUG_TO_FROM_CHAR |
| 5671 |
elog(DEBUG_elog_output, "Try read locale post-sign (%c)", *Np->inout_p); |
| 5672 |
#endif |
| 5673 |
if ((x = strlen(Np->L_negative_sign)) && |
| 5674 |
AMOUNT_TEST(x) && |
| 5675 |
strncmp(Np->inout_p, Np->L_negative_sign, x) == 0) |
| 5676 |
{ |
| 5677 |
Np->inout_p += x - 1; /* -1 .. NUM_processor() do inout_p++ */ |
| 5678 |
*Np->number = '-'; |
| 5679 |
} |
| 5680 |
else if ((x = strlen(Np->L_positive_sign)) && |
| 5681 |
AMOUNT_TEST(x) && |
| 5682 |
strncmp(Np->inout_p, Np->L_positive_sign, x) == 0) |
| 5683 |
{ |
| 5684 |
Np->inout_p += x - 1; /* -1 .. NUM_processor() do inout_p++ */ |
| 5685 |
*Np->number = '+'; |
| 5686 |
} |
| 5687 |
if (*Np->number == ' ') |
| 5688 |
/* no sign read */ |
| 5689 |
Np->inout_p = tmp; |
| 5690 |
} |
| 5691 |
|
| 5692 |
/* |
| 5693 |
* try read non-locale sign, which happens only if format is not exact |
| 5694 |
* and we cannot determine sign position of MI/PL/SG, an example: |
| 5695 |
* |
| 5696 |
* FM9.999999MI -> 5.01- |
| 5697 |
* |
| 5698 |
* if (.... && IS_LSIGN(Np->Num)==false) prevents read wrong formats |
| 5699 |
* like to_number('1 -', '9S') where sign is not anchored to last |
| 5700 |
* number. |
| 5701 |
*/ |
| 5702 |
else if (isread == false && IS_LSIGN(Np->Num) == false && |
| 5703 |
(IS_PLUS(Np->Num) || IS_MINUS(Np->Num))) |
| 5704 |
{ |
| 5705 |
#ifdef DEBUG_TO_FROM_CHAR |
| 5706 |
elog(DEBUG_elog_output, "Try read simple post-sign (%c)", *Np->inout_p); |
| 5707 |
#endif |
| 5708 |
|
| 5709 |
/* |
| 5710 |
* simple + - |
| 5711 |
*/ |
| 5712 |
if (*Np->inout_p == '-' || *Np->inout_p == '+') |
| 5713 |
/* NUM_processor() do inout_p++ */ |
| 5714 |
*Np->number = *Np->inout_p; |
| 5715 |
} |
| 5716 |
} |
| 5717 |
} |
| 5718 |
|
| 5719 |
#define IS_PREDEC_SPACE(_n) \ |
| 5720 |
(IS_ZERO((_n)->Num)==false && \ |
| 5721 |
(_n)->number == (_n)->number_p && \ |
| 5722 |
*(_n)->number == '0' && \ |
| 5723 |
(_n)->Num->post != 0) |
| 5724 |
|
| 5725 |
/* ---------- |
| 5726 |
* Add digit or sign to number-string |
| 5727 |
* ---------- |
| 5728 |
*/ |
| 5729 |
static void |
| 5730 |
NUM_numpart_to_char(NUMProc *Np, int id) |
| 5731 |
{ |
| 5732 |
int end; |
| 5733 |
|
| 5734 |
if (IS_ROMAN(Np->Num)) |
| 5735 |
return; |
| 5736 |
|
| 5737 |
/* Note: in this elog() output not set '\0' in 'inout' */ |
| 5738 |
|
| 5739 |
#ifdef DEBUG_TO_FROM_CHAR |
| 5740 |
|
| 5741 |
/* |
| 5742 |
* Np->num_curr is number of current item in format-picture, it is not |
| 5743 |
* current position in inout! |
| 5744 |
*/ |
| 5745 |
elog(DEBUG_elog_output, |
| 5746 |
"SIGN_WROTE: %d, CURRENT: %d, NUMBER_P: \"%s\", INOUT: \"%s\"", |
| 5747 |
Np->sign_wrote, |
| 5748 |
Np->num_curr, |
| 5749 |
Np->number_p, |
| 5750 |
Np->inout); |
| 5751 |
#endif |
| 5752 |
Np->num_in = false; |
| 5753 |
|
| 5754 |
/* |
| 5755 |
* Write sign if real number will write to output Note: IS_PREDEC_SPACE() |
| 5756 |
* handle "9.9" --> " .1" |
| 5757 |
*/ |
| 5758 |
if (Np->sign_wrote == false && |
| 5759 |
(Np->num_curr >= Np->out_pre_spaces || (IS_ZERO(Np->Num) && Np->Num->zero_start == Np->num_curr)) && |
| 5760 |
(IS_PREDEC_SPACE(Np) == false || (Np->last_relevant && *Np->last_relevant == '.'))) |
| 5761 |
{ |
| 5762 |
if (IS_LSIGN(Np->Num)) |
| 5763 |
{ |
| 5764 |
if (Np->Num->lsign == NUM_LSIGN_PRE) |
| 5765 |
{ |
| 5766 |
NUM_add_locale_symbol(Np, (Np->sign == '-') ? |
| 5767 |
Np->L_negative_sign : |
| 5768 |
Np->L_positive_sign); |
| 5769 |
Np->sign_wrote = true; |
| 5770 |
} |
| 5771 |
} |
| 5772 |
else if (IS_BRACKET(Np->Num)) |
| 5773 |
{ |
| 5774 |
*Np->inout_p = Np->sign == '+' ? ' ' : '<'; |
| 5775 |
++Np->inout_p; |
| 5776 |
Np->sign_wrote = true; |
| 5777 |
} |
| 5778 |
else if (Np->sign == '+') |
| 5779 |
{ |
| 5780 |
if (!IS_FILLMODE(Np->Num)) |
| 5781 |
{ |
| 5782 |
*Np->inout_p = ' '; /* Write + */ |
| 5783 |
++Np->inout_p; |
| 5784 |
} |
| 5785 |
Np->sign_wrote = true; |
| 5786 |
} |
| 5787 |
else if (Np->sign == '-') |
| 5788 |
{ /* Write - */ |
| 5789 |
*Np->inout_p = '-'; |
| 5790 |
++Np->inout_p; |
| 5791 |
Np->sign_wrote = true; |
| 5792 |
} |
| 5793 |
} |
| 5794 |
|
| 5795 |
|
| 5796 |
/* |
| 5797 |
* digits / FM / Zero / Dec. point |
| 5798 |
*/ |
| 5799 |
if (id == NUM_9 || id == NUM_0 || id == NUM_D || id == NUM_DEC) |
| 5800 |
{ |
| 5801 |
if (Np->num_curr < Np->out_pre_spaces && |
| 5802 |
(Np->Num->zero_start > Np->num_curr || !IS_ZERO(Np->Num))) |
| 5803 |
{ |
| 5804 |
/* |
| 5805 |
* Write blank space |
| 5806 |
*/ |
| 5807 |
if (!IS_FILLMODE(Np->Num)) |
| 5808 |
{ |
| 5809 |
*Np->inout_p = ' '; /* Write ' ' */ |
| 5810 |
++Np->inout_p; |
| 5811 |
} |
| 5812 |
} |
| 5813 |
else if (IS_ZERO(Np->Num) && |
| 5814 |
Np->num_curr < Np->out_pre_spaces && |
| 5815 |
Np->Num->zero_start <= Np->num_curr) |
| 5816 |
{ |
| 5817 |
/* |
| 5818 |
* Write ZERO |
| 5819 |
*/ |
| 5820 |
*Np->inout_p = '0'; /* Write '0' */ |
| 5821 |
++Np->inout_p; |
| 5822 |
Np->num_in = true; |
| 5823 |
} |
| 5824 |
else |
| 5825 |
{ |
| 5826 |
/* |
| 5827 |
* Write Decimal point |
| 5828 |
*/ |
| 5829 |
if (*Np->number_p == '.') |
| 5830 |
{ |
| 5831 |
if (!Np->last_relevant || *Np->last_relevant != '.') |
| 5832 |
{ |
| 5833 |
NUM_add_locale_symbol(Np, Np->decimal); /* Write DEC/D */ |
| 5834 |
} |
| 5835 |
|
| 5836 |
/* |
| 5837 |
* Ora 'n' -- FM9.9 --> 'n.' |
| 5838 |
*/ |
| 5839 |
else if (IS_FILLMODE(Np->Num) && |
| 5840 |
Np->last_relevant && *Np->last_relevant == '.') |
| 5841 |
{ |
| 5842 |
NUM_add_locale_symbol(Np, Np->decimal); /* Write DEC/D */ |
| 5843 |
} |
| 5844 |
} |
| 5845 |
else |
| 5846 |
{ |
| 5847 |
/* |
| 5848 |
* Write Digits |
| 5849 |
*/ |
| 5850 |
if (Np->last_relevant && Np->number_p > Np->last_relevant && |
| 5851 |
id != NUM_0) |
| 5852 |
; |
| 5853 |
|
| 5854 |
/* |
| 5855 |
* '0.1' -- 9.9 --> ' .1' |
| 5856 |
*/ |
| 5857 |
else if (IS_PREDEC_SPACE(Np)) |
| 5858 |
{ |
| 5859 |
if (!IS_FILLMODE(Np->Num)) |
| 5860 |
{ |
| 5861 |
*Np->inout_p = ' '; |
| 5862 |
++Np->inout_p; |
| 5863 |
} |
| 5864 |
|
| 5865 |
/* |
| 5866 |
* '0' -- FM9.9 --> '0.' |
| 5867 |
*/ |
| 5868 |
else if (Np->last_relevant && *Np->last_relevant == '.') |
| 5869 |
{ |
| 5870 |
*Np->inout_p = '0'; |
| 5871 |
++Np->inout_p; |
| 5872 |
} |
| 5873 |
} |
| 5874 |
else |
| 5875 |
{ |
| 5876 |
*Np->inout_p = *Np->number_p; /* Write DIGIT */ |
| 5877 |
++Np->inout_p; |
| 5878 |
Np->num_in = true; |
| 5879 |
} |
| 5880 |
} |
| 5881 |
/* do no exceed string length */ |
| 5882 |
if (*Np->number_p) |
| 5883 |
++Np->number_p; |
| 5884 |
} |
| 5885 |
|
| 5886 |
end = Np->num_count + (Np->out_pre_spaces ? 1 : 0) + (IS_DECIMAL(Np->Num) ? 1 : 0); |
| 5887 |
|
| 5888 |
if (Np->last_relevant && Np->last_relevant == Np->number_p) |
| 5889 |
end = Np->num_curr; |
| 5890 |
|
| 5891 |
if (Np->num_curr + 1 == end) |
| 5892 |
{ |
| 5893 |
if (Np->sign_wrote == true && IS_BRACKET(Np->Num)) |
| 5894 |
{ |
| 5895 |
*Np->inout_p = Np->sign == '+' ? ' ' : '>'; |
| 5896 |
++Np->inout_p; |
| 5897 |
} |
| 5898 |
else if (IS_LSIGN(Np->Num) && Np->Num->lsign == NUM_LSIGN_POST) |
| 5899 |
{ |
| 5900 |
NUM_add_locale_symbol(Np, (Np->sign == '-') ? |
| 5901 |
Np->L_negative_sign : |
| 5902 |
Np->L_positive_sign); |
| 5903 |
} |
| 5904 |
} |
| 5905 |
} |
| 5906 |
|
| 5907 |
++Np->num_curr; |
| 5908 |
} |
| 5909 |
|
| 5910 |
/* |
| 5911 |
* Append locale-specific symbol to Np->inout. |
| 5912 |
* Note we don't null-terminate the output |
| 5913 |
*/ |
| 5914 |
static void |
| 5915 |
NUM_add_locale_symbol(NUMProc *Np, const char *pattern) |
| 5916 |
{ |
| 5917 |
size_t pattern_len = strlen(pattern); |
| 5918 |
|
| 5919 |
/* Truncate symbol if it's potentially too long */ |
| 5920 |
if (unlikely(pattern_len > NUM_MAX_ITEM_SIZ)) |
| 5921 |
pattern_len = pg_mbcliplen(pattern, pattern_len, |
| 5922 |
NUM_MAX_ITEM_SIZ); |
| 5923 |
memcpy(Np->inout_p, pattern, pattern_len); |
| 5924 |
Np->inout_p += pattern_len; |
| 5925 |
} |
| 5926 |
|
| 5927 |
/* |
| 5928 |
* Skip over "n" input characters, but only if they aren't numeric data |
| 5929 |
*/ |
| 5930 |
static void |
| 5931 |
NUM_eat_non_data_chars(NUMProc *Np, int n, int input_len) |
| 5932 |
{ |
| 5933 |
const char *end = Np->inout + input_len; |
| 5934 |
|
| 5935 |
while (n-- > 0) |
| 5936 |
{ |
| 5937 |
if (OVERLOAD_TEST) |
| 5938 |
break; /* end of input */ |
| 5939 |
if (strchr("0123456789.,+-", *Np->inout_p) != NULL) |
| 5940 |
break; /* it's a data character */ |
| 5941 |
Np->inout_p += pg_mblen_range(Np->inout_p, end); |
| 5942 |
} |
| 5943 |
} |
| 5944 |
|
| 5945 |
static char * |
| 5946 |
NUM_processor(FormatNode *node, NUMDesc *Num, char *inout, |
| 5947 |
char *number, int input_len, int to_char_out_pre_spaces, |
| 5948 |
int sign, bool is_to_char, Oid collid) |
| 5949 |
{ |
| 5950 |
FormatNode *n; |
| 5951 |
NUMProc _Np, |
| 5952 |
*Np = &_Np; |
| 5953 |
const char *pattern; |
| 5954 |
int pattern_len; |
| 5955 |
|
| 5956 |
MemSet(Np, 0, sizeof(NUMProc)); |
| 5957 |
|
| 5958 |
Np->Num = Num; |
| 5959 |
Np->is_to_char = is_to_char; |
| 5960 |
Np->number = number; |
| 5961 |
Np->inout = inout; |
| 5962 |
Np->last_relevant = NULL; |
| 5963 |
Np->read_post = 0; |
| 5964 |
Np->read_pre = 0; |
| 5965 |
Np->read_dec = false; |
| 5966 |
|
| 5967 |
if (Np->Num->zero_start) |
| 5968 |
--Np->Num->zero_start; |
| 5969 |
|
| 5970 |
if (IS_EEEE(Np->Num)) |
| 5971 |
{ |
| 5972 |
if (!Np->is_to_char) |
| 5973 |
ereport(ERROR, |
| 5974 |
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED), |
| 5975 |
errmsg("\"EEEE\" not supported for input"))); |
| 5976 |
return strcpy(inout, number); |
| 5977 |
} |
| 5978 |
|
| 5979 |
/* |
| 5980 |
* Sign |
| 5981 |
*/ |
| 5982 |
if (is_to_char) |
| 5983 |
{ |
| 5984 |
Np->sign = sign; |
| 5985 |
|
| 5986 |
/* MI/PL/SG - write sign itself and not in number */ |
| 5987 |
if (IS_PLUS(Np->Num) || IS_MINUS(Np->Num)) |
| 5988 |
{ |
| 5989 |
if (IS_PLUS(Np->Num) && IS_MINUS(Np->Num) == false) |
| 5990 |
Np->sign_wrote = false; /* need sign */ |
| 5991 |
else |
| 5992 |
Np->sign_wrote = true; /* needn't sign */ |
| 5993 |
} |
| 5994 |
else |
| 5995 |
{ |
| 5996 |
if (Np->sign != '-') |
| 5997 |
{ |
| 5998 |
if (IS_FILLMODE(Np->Num)) |
| 5999 |
Np->Num->flag &= ~NUM_F_BRACKET; |
| 6000 |
} |
| 6001 |
|
| 6002 |
if (Np->sign == '+' && IS_FILLMODE(Np->Num) && IS_LSIGN(Np->Num) == false) |
| 6003 |
Np->sign_wrote = true; /* needn't sign */ |
| 6004 |
else |
| 6005 |
Np->sign_wrote = false; /* need sign */ |
| 6006 |
|
| 6007 |
if (Np->Num->lsign == NUM_LSIGN_PRE && Np->Num->pre == Np->Num->pre_lsign_num) |
| 6008 |
Np->Num->lsign = NUM_LSIGN_POST; |
| 6009 |
} |
| 6010 |
} |
| 6011 |
else |
| 6012 |
Np->sign = false; |
| 6013 |
|
| 6014 |
/* |
| 6015 |
* Count |
| 6016 |
*/ |
| 6017 |
Np->num_count = Np->Num->post + Np->Num->pre - 1; |
| 6018 |
|
| 6019 |
if (is_to_char) |
| 6020 |
{ |
| 6021 |
Np->out_pre_spaces = to_char_out_pre_spaces; |
| 6022 |
|
| 6023 |
if (IS_FILLMODE(Np->Num) && IS_DECIMAL(Np->Num)) |
| 6024 |
{ |
| 6025 |
Np->last_relevant = get_last_relevant_decnum(Np->number); |
| 6026 |
|
| 6027 |
/* |
| 6028 |
* If any '0' specifiers are present, make sure we don't strip |
| 6029 |
* those digits. But don't advance last_relevant beyond the last |
| 6030 |
* character of the Np->number string, which is a hazard if the |
| 6031 |
* number got shortened due to precision limitations. |
| 6032 |
*/ |
| 6033 |
if (Np->last_relevant && Np->Num->zero_end > Np->out_pre_spaces) |
| 6034 |
{ |
| 6035 |
int last_zero_pos; |
| 6036 |
char *last_zero; |
| 6037 |
|
| 6038 |
/* note that Np->number cannot be zero-length here */ |
| 6039 |
last_zero_pos = strlen(Np->number) - 1; |
| 6040 |
last_zero_pos = Min(last_zero_pos, |
| 6041 |
Np->Num->zero_end - Np->out_pre_spaces); |
| 6042 |
last_zero = Np->number + last_zero_pos; |
| 6043 |
if (Np->last_relevant < last_zero) |
| 6044 |
Np->last_relevant = last_zero; |
| 6045 |
} |
| 6046 |
} |
| 6047 |
|
| 6048 |
if (Np->sign_wrote == false && Np->out_pre_spaces == 0) |
| 6049 |
++Np->num_count; |
| 6050 |
} |
| 6051 |
else |
| 6052 |
{ |
| 6053 |
Np->out_pre_spaces = 0; |
| 6054 |
*Np->number = ' '; /* sign space */ |
| 6055 |
*(Np->number + 1) = '\0'; |
| 6056 |
} |
| 6057 |
|
| 6058 |
Np->num_in = 0; |
| 6059 |
Np->num_curr = 0; |
| 6060 |
|
| 6061 |
#ifdef DEBUG_TO_FROM_CHAR |
| 6062 |
elog(DEBUG_elog_output, |
| 6063 |
"\n\tSIGN: '%c'\n\tNUM: '%s'\n\tPRE: %d\n\tPOST: %d\n\tNUM_COUNT: %d\n\tNUM_PRE: %d\n\tSIGN_WROTE: %s\n\tZERO: %s\n\tZERO_START: %d\n\tZERO_END: %d\n\tLAST_RELEVANT: %s\n\tBRACKET: %s\n\tPLUS: %s\n\tMINUS: %s\n\tFILLMODE: %s\n\tROMAN: %s\n\tEEEE: %s", |
| 6064 |
Np->sign, |
| 6065 |
Np->number, |
| 6066 |
Np->Num->pre, |
| 6067 |
Np->Num->post, |
| 6068 |
Np->num_count, |
| 6069 |
Np->out_pre_spaces, |
| 6070 |
Np->sign_wrote ? "Yes" : "No", |
| 6071 |
IS_ZERO(Np->Num) ? "Yes" : "No", |
| 6072 |
Np->Num->zero_start, |
| 6073 |
Np->Num->zero_end, |
| 6074 |
Np->last_relevant ? Np->last_relevant : "<not set>", |
| 6075 |
IS_BRACKET(Np->Num) ? "Yes" : "No", |
| 6076 |
IS_PLUS(Np->Num) ? "Yes" : "No", |
| 6077 |
IS_MINUS(Np->Num) ? "Yes" : "No", |
| 6078 |
IS_FILLMODE(Np->Num) ? "Yes" : "No", |
| 6079 |
IS_ROMAN(Np->Num) ? "Yes" : "No", |
| 6080 |
IS_EEEE(Np->Num) ? "Yes" : "No" |
| 6081 |
); |
| 6082 |
#endif |
| 6083 |
|
| 6084 |
/* |
| 6085 |
* Locale |
| 6086 |
*/ |
| 6087 |
NUM_prepare_locale(Np); |
| 6088 |
|
| 6089 |
/* |
| 6090 |
* Processor direct cycle |
| 6091 |
*/ |
| 6092 |
if (Np->is_to_char) |
| 6093 |
Np->number_p = Np->number; |
| 6094 |
else |
| 6095 |
Np->number_p = Np->number + 1; /* first char is space for sign */ |
| 6096 |
|
| 6097 |
for (n = node, Np->inout_p = Np->inout; n->type != NODE_TYPE_END; n++) |
| 6098 |
{ |
| 6099 |
if (!Np->is_to_char) |
| 6100 |
{ |
| 6101 |
/* |
| 6102 |
* Check at least one byte remains to be scanned. (In actions |
| 6103 |
* below, must use AMOUNT_TEST if we want to read more bytes than |
| 6104 |
* that.) |
| 6105 |
*/ |
| 6106 |
if (OVERLOAD_TEST) |
| 6107 |
break; |
| 6108 |
} |
| 6109 |
|
| 6110 |
/* |
| 6111 |
* Format pictures actions |
| 6112 |
*/ |
| 6113 |
if (n->type == NODE_TYPE_ACTION) |
| 6114 |
{ |
| 6115 |
/* |
| 6116 |
* Create/read digit/zero/blank/sign/special-case |
| 6117 |
* |
| 6118 |
* 'NUM_S' note: The locale sign is anchored to number and we |
| 6119 |
* read/write it when we work with first or last number |
| 6120 |
* (NUM_0/NUM_9). This is why NUM_S is missing in switch(). |
| 6121 |
* |
| 6122 |
* Notice the "Np->inout_p++" at the bottom of the loop. This is |
| 6123 |
* why most of the actions advance inout_p one less than you might |
| 6124 |
* expect. In cases where we don't want that increment to happen, |
| 6125 |
* a switch case ends with "continue" not "break". |
| 6126 |
*/ |
| 6127 |
switch (n->key->id) |
| 6128 |
{ |
| 6129 |
case NUM_9: |
| 6130 |
case NUM_0: |
| 6131 |
case NUM_DEC: |
| 6132 |
case NUM_D: |
| 6133 |
if (Np->is_to_char) |
| 6134 |
{ |
| 6135 |
NUM_numpart_to_char(Np, n->key->id); |
| 6136 |
continue; /* for() */ |
| 6137 |
} |
| 6138 |
else |
| 6139 |
{ |
| 6140 |
NUM_numpart_from_char(Np, n->key->id, input_len); |
| 6141 |
break; /* switch() case: */ |
| 6142 |
} |
| 6143 |
|
| 6144 |
case NUM_COMMA: |
| 6145 |
if (Np->is_to_char) |
| 6146 |
{ |
| 6147 |
if (!Np->num_in) |
| 6148 |
{ |
| 6149 |
if (IS_FILLMODE(Np->Num)) |
| 6150 |
continue; |
| 6151 |
else |
| 6152 |
*Np->inout_p = ' '; |
| 6153 |
} |
| 6154 |
else |
| 6155 |
*Np->inout_p = ','; |
| 6156 |
} |
| 6157 |
else |
| 6158 |
{ |
| 6159 |
if (!Np->num_in) |
| 6160 |
{ |
| 6161 |
if (IS_FILLMODE(Np->Num)) |
| 6162 |
continue; |
| 6163 |
} |
| 6164 |
if (*Np->inout_p != ',') |
| 6165 |
continue; |
| 6166 |
} |
| 6167 |
break; |
| 6168 |
|
| 6169 |
case NUM_G: |
| 6170 |
pattern = Np->L_thousands_sep; |
| 6171 |
pattern_len = strlen(pattern); |
| 6172 |
if (Np->is_to_char) |
| 6173 |
{ |
| 6174 |
/* Truncate symbol if it's potentially too long */ |
| 6175 |
if (unlikely(pattern_len > NUM_MAX_ITEM_SIZ)) |
| 6176 |
pattern_len = pg_mbcliplen(pattern, pattern_len, |
| 6177 |
NUM_MAX_ITEM_SIZ); |
| 6178 |
if (!Np->num_in) |
| 6179 |
{ |
| 6180 |
if (IS_FILLMODE(Np->Num)) |
| 6181 |
continue; |
| 6182 |
else |
| 6183 |
{ |
| 6184 |
/* just in case there are MB chars */ |
| 6185 |
pattern_len = pg_mbstrlen_with_len(pattern, |
| 6186 |
pattern_len); |
| 6187 |
memset(Np->inout_p, ' ', pattern_len); |
| 6188 |
Np->inout_p += pattern_len - 1; |
| 6189 |
} |
| 6190 |
} |
| 6191 |
else |
| 6192 |
{ |
| 6193 |
memcpy(Np->inout_p, pattern, pattern_len); |
| 6194 |
Np->inout_p += pattern_len - 1; |
| 6195 |
} |
| 6196 |
} |
| 6197 |
else |
| 6198 |
{ |
| 6199 |
/* Here we do not truncate the symbol ... */ |
| 6200 |
if (!Np->num_in) |
| 6201 |
{ |
| 6202 |
if (IS_FILLMODE(Np->Num)) |
| 6203 |
continue; |
| 6204 |
} |
| 6205 |
|
| 6206 |
/* |
| 6207 |
* Because L_thousands_sep typically contains data |
| 6208 |
* characters (either '.' or ','), we can't use |
| 6209 |
* NUM_eat_non_data_chars here. Instead skip only if |
| 6210 |
* the input matches L_thousands_sep. |
| 6211 |
*/ |
| 6212 |
if (AMOUNT_TEST(pattern_len) && |
| 6213 |
strncmp(Np->inout_p, pattern, pattern_len) == 0) |
| 6214 |
Np->inout_p += pattern_len - 1; |
| 6215 |
else |
| 6216 |
continue; |
| 6217 |
} |
| 6218 |
break; |
| 6219 |
|
| 6220 |
case NUM_L: |
| 6221 |
pattern = Np->L_currency_symbol; |
| 6222 |
if (Np->is_to_char) |
| 6223 |
{ |
| 6224 |
/* Truncate symbol if it's potentially too long */ |
| 6225 |
pattern_len = strlen(pattern); |
| 6226 |
if (unlikely(pattern_len > NUM_MAX_ITEM_SIZ)) |
| 6227 |
pattern_len = pg_mbcliplen(pattern, pattern_len, |
| 6228 |
NUM_MAX_ITEM_SIZ); |
| 6229 |
|
| 6230 |
memcpy(Np->inout_p, pattern, pattern_len); |
| 6231 |
Np->inout_p += pattern_len - 1; |
| 6232 |
} |
| 6233 |
else |
| 6234 |
{ |
| 6235 |
/* Here we do not truncate the symbol ... */ |
| 6236 |
NUM_eat_non_data_chars(Np, pg_mbstrlen(pattern), input_len); |
| 6237 |
continue; |
| 6238 |
} |
| 6239 |
break; |
| 6240 |
|
| 6241 |
case NUM_RN: |
| 6242 |
case NUM_rn: |
| 6243 |
if (Np->is_to_char) |
| 6244 |
{ |
| 6245 |
const char *number_p; |
| 6246 |
|
| 6247 |
if (n->key->id == NUM_rn) |
| 6248 |
number_p = asc_tolower_z(Np->number_p); |
| 6249 |
else |
| 6250 |
number_p = Np->number_p; |
| 6251 |
if (IS_FILLMODE(Np->Num)) |
| 6252 |
strcpy(Np->inout_p, number_p); |
| 6253 |
else |
| 6254 |
sprintf(Np->inout_p, "%15s", number_p); |
| 6255 |
Np->inout_p += strlen(Np->inout_p) - 1; |
| 6256 |
} |
| 6257 |
else |
| 6258 |
{ |
| 6259 |
int roman_result = roman_to_int(Np, input_len); |
| 6260 |
int numlen; |
| 6261 |
|
| 6262 |
if (roman_result < 0) |
| 6263 |
ereport(ERROR, |
| 6264 |
(errcode(ERRCODE_INVALID_TEXT_REPRESENTATION), |
| 6265 |
errmsg("invalid Roman numeral"))); |
| 6266 |
numlen = sprintf(Np->number_p, "%d", roman_result); |
| 6267 |
Np->number_p += numlen; |
| 6268 |
Np->Num->pre = numlen; |
| 6269 |
Np->Num->post = 0; |
| 6270 |
continue; /* roman_to_int ate all the chars */ |
| 6271 |
} |
| 6272 |
break; |
| 6273 |
|
| 6274 |
case NUM_th: |
| 6275 |
if (IS_ROMAN(Np->Num) || *Np->number == '#' || |
| 6276 |
Np->sign == '-' || IS_DECIMAL(Np->Num)) |
| 6277 |
continue; |
| 6278 |
|
| 6279 |
if (Np->is_to_char) |
| 6280 |
{ |
| 6281 |
strcpy(Np->inout_p, get_th(Np->number, TH_LOWER)); |
| 6282 |
Np->inout_p += 1; |
| 6283 |
} |
| 6284 |
else |
| 6285 |
{ |
| 6286 |
/* All variants of 'th' occupy 2 characters */ |
| 6287 |
NUM_eat_non_data_chars(Np, 2, input_len); |
| 6288 |
continue; |
| 6289 |
} |
| 6290 |
break; |
| 6291 |
|
| 6292 |
case NUM_TH: |
| 6293 |
if (IS_ROMAN(Np->Num) || *Np->number == '#' || |
| 6294 |
Np->sign == '-' || IS_DECIMAL(Np->Num)) |
| 6295 |
continue; |
| 6296 |
|
| 6297 |
if (Np->is_to_char) |
| 6298 |
{ |
| 6299 |
strcpy(Np->inout_p, get_th(Np->number, TH_UPPER)); |
| 6300 |
Np->inout_p += 1; |
| 6301 |
} |
| 6302 |
else |
| 6303 |
{ |
| 6304 |
/* All variants of 'TH' occupy 2 characters */ |
| 6305 |
NUM_eat_non_data_chars(Np, 2, input_len); |
| 6306 |
continue; |
| 6307 |
} |
| 6308 |
break; |
| 6309 |
|
| 6310 |
case NUM_MI: |
| 6311 |
if (Np->is_to_char) |
| 6312 |
{ |
| 6313 |
if (Np->sign == '-') |
| 6314 |
*Np->inout_p = '-'; |
| 6315 |
else if (IS_FILLMODE(Np->Num)) |
| 6316 |
continue; |
| 6317 |
else |
| 6318 |
*Np->inout_p = ' '; |
| 6319 |
} |
| 6320 |
else |
| 6321 |
{ |
| 6322 |
if (*Np->inout_p == '-') |
| 6323 |
*Np->number = '-'; |
| 6324 |
else |
| 6325 |
{ |
| 6326 |
NUM_eat_non_data_chars(Np, 1, input_len); |
| 6327 |
continue; |
| 6328 |
} |
| 6329 |
} |
| 6330 |
break; |
| 6331 |
|
| 6332 |
case NUM_PL: |
| 6333 |
if (Np->is_to_char) |
| 6334 |
{ |
| 6335 |
if (Np->sign == '+') |
| 6336 |
*Np->inout_p = '+'; |
| 6337 |
else if (IS_FILLMODE(Np->Num)) |
| 6338 |
continue; |
| 6339 |
else |
| 6340 |
*Np->inout_p = ' '; |
| 6341 |
} |
| 6342 |
else |
| 6343 |
{ |
| 6344 |
if (*Np->inout_p == '+') |
| 6345 |
*Np->number = '+'; |
| 6346 |
else |
| 6347 |
{ |
| 6348 |
NUM_eat_non_data_chars(Np, 1, input_len); |
| 6349 |
continue; |
| 6350 |
} |
| 6351 |
} |
| 6352 |
break; |
| 6353 |
|
| 6354 |
case NUM_SG: |
| 6355 |
if (Np->is_to_char) |
| 6356 |
*Np->inout_p = Np->sign; |
| 6357 |
else |
| 6358 |
{ |
| 6359 |
if (*Np->inout_p == '-') |
| 6360 |
*Np->number = '-'; |
| 6361 |
else if (*Np->inout_p == '+') |
| 6362 |
*Np->number = '+'; |
| 6363 |
else |
| 6364 |
{ |
| 6365 |
NUM_eat_non_data_chars(Np, 1, input_len); |
| 6366 |
continue; |
| 6367 |
} |
| 6368 |
} |
| 6369 |
break; |
| 6370 |
|
| 6371 |
default: |
| 6372 |
continue; |
| 6373 |
break; |
| 6374 |
} |
| 6375 |
} |
| 6376 |
else |
| 6377 |
{ |
| 6378 |
/* |
| 6379 |
* In TO_CHAR, non-pattern characters in the format are copied to |
| 6380 |
* the output. In TO_NUMBER, we skip one input character for each |
| 6381 |
* non-pattern format character, whether or not it matches the |
| 6382 |
* format character. |
| 6383 |
*/ |
| 6384 |
if (Np->is_to_char) |
| 6385 |
{ |
| 6386 |
strcpy(Np->inout_p, n->character); |
| 6387 |
Np->inout_p += strlen(Np->inout_p); |
| 6388 |
} |
| 6389 |
else |
| 6390 |
{ |
| 6391 |
Np->inout_p += pg_mblen_range(Np->inout_p, Np->inout + input_len); |
| 6392 |
} |
| 6393 |
continue; |
| 6394 |
} |
| 6395 |
Np->inout_p++; |
| 6396 |
} |
| 6397 |
|
| 6398 |
if (Np->is_to_char) |
| 6399 |
{ |
| 6400 |
*Np->inout_p = '\0'; |
| 6401 |
return Np->inout; |
| 6402 |
} |
| 6403 |
else |
| 6404 |
{ |
| 6405 |
if (*(Np->number_p - 1) == '.') |
| 6406 |
*(Np->number_p - 1) = '\0'; |
| 6407 |
else |
| 6408 |
*Np->number_p = '\0'; |
| 6409 |
|
| 6410 |
/* |
| 6411 |
* Correction - precision of dec. number |
| 6412 |
*/ |
| 6413 |
Np->Num->post = Np->read_post; |
| 6414 |
|
| 6415 |
#ifdef DEBUG_TO_FROM_CHAR |
| 6416 |
elog(DEBUG_elog_output, "TO_NUMBER (number): '%s'", Np->number); |
| 6417 |
#endif |
| 6418 |
return Np->number; |
| 6419 |
} |
| 6420 |
} |
| 6421 |
|
| 6422 |
/* ---------- |
| 6423 |
* MACRO: Start part of NUM - for all NUM's to_char variants |
| 6424 |
* (sorry, but I hate copy same code - macro is better..) |
| 6425 |
* ---------- |
| 6426 |
*/ |
| 6427 |
#define NUM_TOCHAR_prepare \ |
| 6428 |
do { \ |
| 6429 |
int len = VARSIZE_ANY_EXHDR(fmt); \ |
| 6430 |
if (len <= 0 || len >= (INT_MAX-VARHDRSZ)/NUM_MAX_ITEM_SIZ) \ |
| 6431 |
PG_RETURN_TEXT_P(cstring_to_text("")); \ |
| 6432 |
result = (text *) palloc0((len * NUM_MAX_ITEM_SIZ) + 1 + VARHDRSZ); \ |
| 6433 |
format = NUM_cache(len, &Num, fmt, &shouldFree); \ |
| 6434 |
} while (0) |
| 6435 |
|
| 6436 |
/* ---------- |
| 6437 |
* MACRO: Finish part of NUM |
| 6438 |
* ---------- |
| 6439 |
*/ |
| 6440 |
#define NUM_TOCHAR_finish \ |
| 6441 |
do { \ |
| 6442 |
int len; \ |
| 6443 |
\ |
| 6444 |
NUM_processor(format, &Num, VARDATA(result), numstr, 0, out_pre_spaces, sign, true, PG_GET_COLLATION()); \ |
| 6445 |
\ |
| 6446 |
if (shouldFree) \ |
| 6447 |
pfree(format); \ |
| 6448 |
\ |
| 6449 |
/* \ |
| 6450 |
* Convert null-terminated representation of result to standard text. \ |
| 6451 |
* The result is usually much bigger than it needs to be, but there \ |
| 6452 |
* seems little point in realloc'ing it smaller. \ |
| 6453 |
*/ \ |
| 6454 |
len = strlen(VARDATA(result)); \ |
| 6455 |
SET_VARSIZE(result, len + VARHDRSZ); \ |
| 6456 |
} while (0) |
| 6457 |
|
| 6458 |
/* ------------------- |
| 6459 |
* NUMERIC to_number() (convert string to numeric) |
| 6460 |
* ------------------- |
| 6461 |
*/ |
| 6462 |
Datum |
| 6463 |
numeric_to_number(PG_FUNCTION_ARGS) |
| 6464 |
{ |
| 6465 |
text *value = PG_GETARG_TEXT_PP(0); |
| 6466 |
text *fmt = PG_GETARG_TEXT_PP(1); |
| 6467 |
NUMDesc Num; |
| 6468 |
Datum result; |
| 6469 |
FormatNode *format; |
| 6470 |
char *numstr; |
| 6471 |
bool shouldFree; |
| 6472 |
int len = 0; |
| 6473 |
int scale, |
| 6474 |
precision; |
| 6475 |
|
| 6476 |
len = VARSIZE_ANY_EXHDR(fmt); |
| 6477 |
|
| 6478 |
if (len <= 0 || len >= INT_MAX / NUM_MAX_ITEM_SIZ) |
| 6479 |
PG_RETURN_NULL(); |
| 6480 |
|
| 6481 |
format = NUM_cache(len, &Num, fmt, &shouldFree); |
| 6482 |
|
| 6483 |
numstr = (char *) palloc((len * NUM_MAX_ITEM_SIZ) + 1); |
| 6484 |
|
| 6485 |
NUM_processor(format, &Num, VARDATA_ANY(value), numstr, |
| 6486 |
VARSIZE_ANY_EXHDR(value), 0, 0, false, PG_GET_COLLATION()); |
| 6487 |
|
| 6488 |
scale = Num.post; |
| 6489 |
precision = Num.pre + Num.multi + scale; |
| 6490 |
|
| 6491 |
if (shouldFree) |
| 6492 |
pfree(format); |
| 6493 |
|
| 6494 |
result = DirectFunctionCall3(numeric_in, |
| 6495 |
CStringGetDatum(numstr), |
| 6496 |
ObjectIdGetDatum(InvalidOid), |
| 6497 |
Int32GetDatum(((precision << 16) | scale) + VARHDRSZ)); |
| 6498 |
|
| 6499 |
if (IS_MULTI(&Num)) |
| 6500 |
{ |
| 6501 |
Numeric x; |
| 6502 |
Numeric a = int64_to_numeric(10); |
| 6503 |
Numeric b = int64_to_numeric(-Num.multi); |
| 6504 |
|
| 6505 |
x = DatumGetNumeric(DirectFunctionCall2(numeric_power, |
| 6506 |
NumericGetDatum(a), |
| 6507 |
NumericGetDatum(b))); |
| 6508 |
result = DirectFunctionCall2(numeric_mul, |
| 6509 |
result, |
| 6510 |
NumericGetDatum(x)); |
| 6511 |
} |
| 6512 |
|
| 6513 |
pfree(numstr); |
| 6514 |
return result; |
| 6515 |
} |
| 6516 |
|
| 6517 |
/* ------------------ |
| 6518 |
* NUMERIC to_char() |
| 6519 |
* ------------------ |
| 6520 |
*/ |
| 6521 |
Datum |
| 6522 |
numeric_to_char(PG_FUNCTION_ARGS) |
| 6523 |
{ |
| 6524 |
Numeric value = PG_GETARG_NUMERIC(0); |
| 6525 |
text *fmt = PG_GETARG_TEXT_PP(1); |
| 6526 |
NUMDesc Num; |
| 6527 |
FormatNode *format; |
| 6528 |
text *result; |
| 6529 |
bool shouldFree; |
| 6530 |
int out_pre_spaces = 0, |
| 6531 |
sign = 0; |
| 6532 |
char *numstr, |
| 6533 |
*orgnum, |
| 6534 |
*p; |
| 6535 |
|
| 6536 |
NUM_TOCHAR_prepare; |
| 6537 |
|
| 6538 |
/* |
| 6539 |
* On DateType depend part (numeric) |
| 6540 |
*/ |
| 6541 |
if (IS_ROMAN(&Num)) |
| 6542 |
{ |
| 6543 |
int32 intvalue; |
| 6544 |
bool err; |
| 6545 |
|
| 6546 |
/* Round and convert to int */ |
| 6547 |
intvalue = numeric_int4_opt_error(value, &err); |
| 6548 |
/* On overflow, just use PG_INT32_MAX; int_to_roman will cope */ |
| 6549 |
if (err) |
| 6550 |
intvalue = PG_INT32_MAX; |
| 6551 |
numstr = int_to_roman(intvalue); |
| 6552 |
} |
| 6553 |
else if (IS_EEEE(&Num)) |
| 6554 |
{ |
| 6555 |
orgnum = numeric_out_sci(value, Num.post); |
| 6556 |
|
| 6557 |
/* |
| 6558 |
* numeric_out_sci() does not emit a sign for positive numbers. We |
| 6559 |
* need to add a space in this case so that positive and negative |
| 6560 |
* numbers are aligned. Also must check for NaN/infinity cases, which |
| 6561 |
* we handle the same way as in float8_to_char. |
| 6562 |
*/ |
| 6563 |
if (strcmp(orgnum, "NaN") == 0 || |
| 6564 |
strcmp(orgnum, "Infinity") == 0 || |
| 6565 |
strcmp(orgnum, "-Infinity") == 0) |
| 6566 |
{ |
| 6567 |
/* |
| 6568 |
* Allow 6 characters for the leading sign, the decimal point, |
| 6569 |
* "e", the exponent's sign and two exponent digits. |
| 6570 |
*/ |
| 6571 |
numstr = (char *) palloc(Num.pre + Num.post + 7); |
| 6572 |
fill_str(numstr, '#', Num.pre + Num.post + 6); |
| 6573 |
*numstr = ' '; |
| 6574 |
*(numstr + Num.pre + 1) = '.'; |
| 6575 |
} |
| 6576 |
else if (*orgnum != '-') |
| 6577 |
{ |
| 6578 |
numstr = (char *) palloc(strlen(orgnum) + 2); |
| 6579 |
*numstr = ' '; |
| 6580 |
strcpy(numstr + 1, orgnum); |
| 6581 |
} |
| 6582 |
else |
| 6583 |
{ |
| 6584 |
numstr = orgnum; |
| 6585 |
} |
| 6586 |
} |
| 6587 |
else |
| 6588 |
{ |
| 6589 |
int numstr_pre_len; |
| 6590 |
Numeric val = value; |
| 6591 |
Numeric x; |
| 6592 |
|
| 6593 |
if (IS_MULTI(&Num)) |
| 6594 |
{ |
| 6595 |
Numeric a = int64_to_numeric(10); |
| 6596 |
Numeric b = int64_to_numeric(Num.multi); |
| 6597 |
|
| 6598 |
x = DatumGetNumeric(DirectFunctionCall2(numeric_power, |
| 6599 |
NumericGetDatum(a), |
| 6600 |
NumericGetDatum(b))); |
| 6601 |
val = DatumGetNumeric(DirectFunctionCall2(numeric_mul, |
| 6602 |
NumericGetDatum(value), |
| 6603 |
NumericGetDatum(x))); |
| 6604 |
Num.pre += Num.multi; |
| 6605 |
} |
| 6606 |
|
| 6607 |
x = DatumGetNumeric(DirectFunctionCall2(numeric_round, |
| 6608 |
NumericGetDatum(val), |
| 6609 |
Int32GetDatum(Num.post))); |
| 6610 |
orgnum = DatumGetCString(DirectFunctionCall1(numeric_out, |
| 6611 |
NumericGetDatum(x))); |
| 6612 |
|
| 6613 |
if (*orgnum == '-') |
| 6614 |
{ |
| 6615 |
sign = '-'; |
| 6616 |
numstr = orgnum + 1; |
| 6617 |
} |
| 6618 |
else |
| 6619 |
{ |
| 6620 |
sign = '+'; |
| 6621 |
numstr = orgnum; |
| 6622 |
} |
| 6623 |
|
| 6624 |
if ((p = strchr(numstr, '.'))) |
| 6625 |
numstr_pre_len = p - numstr; |
| 6626 |
else |
| 6627 |
numstr_pre_len = strlen(numstr); |
| 6628 |
|
| 6629 |
/* needs padding? */ |
| 6630 |
if (numstr_pre_len < Num.pre) |
| 6631 |
out_pre_spaces = Num.pre - numstr_pre_len; |
| 6632 |
/* overflowed prefix digit format? */ |
| 6633 |
else if (numstr_pre_len > Num.pre) |
| 6634 |
{ |
| 6635 |
numstr = (char *) palloc(Num.pre + Num.post + 2); |
| 6636 |
fill_str(numstr, '#', Num.pre + Num.post + 1); |
| 6637 |
*(numstr + Num.pre) = '.'; |
| 6638 |
} |
| 6639 |
} |
| 6640 |
|
| 6641 |
NUM_TOCHAR_finish; |
| 6642 |
PG_RETURN_TEXT_P(result); |
| 6643 |
} |
| 6644 |
|
| 6645 |
/* --------------- |
| 6646 |
* INT4 to_char() |
| 6647 |
* --------------- |
| 6648 |
*/ |
| 6649 |
Datum |
| 6650 |
int4_to_char(PG_FUNCTION_ARGS) |
| 6651 |
{ |
| 6652 |
int32 value = PG_GETARG_INT32(0); |
| 6653 |
text *fmt = PG_GETARG_TEXT_PP(1); |
| 6654 |
NUMDesc Num; |
| 6655 |
FormatNode *format; |
| 6656 |
text *result; |
| 6657 |
bool shouldFree; |
| 6658 |
int out_pre_spaces = 0, |
| 6659 |
sign = 0; |
| 6660 |
char *numstr, |
| 6661 |
*orgnum; |
| 6662 |
|
| 6663 |
NUM_TOCHAR_prepare; |
| 6664 |
|
| 6665 |
/* |
| 6666 |
* On DateType depend part (int32) |
| 6667 |
*/ |
| 6668 |
if (IS_ROMAN(&Num)) |
| 6669 |
numstr = int_to_roman(value); |
| 6670 |
else if (IS_EEEE(&Num)) |
| 6671 |
{ |
| 6672 |
/* we can do it easily because float8 won't lose any precision */ |
| 6673 |
float8 val = (float8) value; |
| 6674 |
|
| 6675 |
orgnum = (char *) psprintf("%+.*e", Num.post, val); |
| 6676 |
|
| 6677 |
/* |
| 6678 |
* Swap a leading positive sign for a space. |
| 6679 |
*/ |
| 6680 |
if (*orgnum == '+') |
| 6681 |
*orgnum = ' '; |
| 6682 |
|
| 6683 |
numstr = orgnum; |
| 6684 |
} |
| 6685 |
else |
| 6686 |
{ |
| 6687 |
int numstr_pre_len; |
| 6688 |
|
| 6689 |
if (IS_MULTI(&Num)) |
| 6690 |
{ |
| 6691 |
orgnum = DatumGetCString(DirectFunctionCall1(int4out, |
| 6692 |
Int32GetDatum(value * ((int32) pow((double) 10, (double) Num.multi))))); |
| 6693 |
Num.pre += Num.multi; |
| 6694 |
} |
| 6695 |
else |
| 6696 |
{ |
| 6697 |
orgnum = DatumGetCString(DirectFunctionCall1(int4out, |
| 6698 |
Int32GetDatum(value))); |
| 6699 |
} |
| 6700 |
|
| 6701 |
if (*orgnum == '-') |
| 6702 |
{ |
| 6703 |
sign = '-'; |
| 6704 |
orgnum++; |
| 6705 |
} |
| 6706 |
else |
| 6707 |
sign = '+'; |
| 6708 |
|
| 6709 |
numstr_pre_len = strlen(orgnum); |
| 6710 |
|
| 6711 |
/* post-decimal digits? Pad out with zeros. */ |
| 6712 |
if (Num.post) |
| 6713 |
{ |
| 6714 |
numstr = (char *) palloc(numstr_pre_len + Num.post + 2); |
| 6715 |
strcpy(numstr, orgnum); |
| 6716 |
*(numstr + numstr_pre_len) = '.'; |
| 6717 |
memset(numstr + numstr_pre_len + 1, '0', Num.post); |
| 6718 |
*(numstr + numstr_pre_len + Num.post + 1) = '\0'; |
| 6719 |
} |
| 6720 |
else |
| 6721 |
numstr = orgnum; |
| 6722 |
|
| 6723 |
/* needs padding? */ |
| 6724 |
if (numstr_pre_len < Num.pre) |
| 6725 |
out_pre_spaces = Num.pre - numstr_pre_len; |
| 6726 |
/* overflowed prefix digit format? */ |
| 6727 |
else if (numstr_pre_len > Num.pre) |
| 6728 |
{ |
| 6729 |
numstr = (char *) palloc(Num.pre + Num.post + 2); |
| 6730 |
fill_str(numstr, '#', Num.pre + Num.post + 1); |
| 6731 |
*(numstr + Num.pre) = '.'; |
| 6732 |
} |
| 6733 |
} |
| 6734 |
|
| 6735 |
NUM_TOCHAR_finish; |
| 6736 |
PG_RETURN_TEXT_P(result); |
| 6737 |
} |
| 6738 |
|
| 6739 |
/* --------------- |
| 6740 |
* INT8 to_char() |
| 6741 |
* --------------- |
| 6742 |
*/ |
| 6743 |
Datum |
| 6744 |
int8_to_char(PG_FUNCTION_ARGS) |
| 6745 |
{ |
| 6746 |
int64 value = PG_GETARG_INT64(0); |
| 6747 |
text *fmt = PG_GETARG_TEXT_PP(1); |
| 6748 |
NUMDesc Num; |
| 6749 |
FormatNode *format; |
| 6750 |
text *result; |
| 6751 |
bool shouldFree; |
| 6752 |
int out_pre_spaces = 0, |
| 6753 |
sign = 0; |
| 6754 |
char *numstr, |
| 6755 |
*orgnum; |
| 6756 |
|
| 6757 |
NUM_TOCHAR_prepare; |
| 6758 |
|
| 6759 |
/* |
| 6760 |
* On DateType depend part (int64) |
| 6761 |
*/ |
| 6762 |
if (IS_ROMAN(&Num)) |
| 6763 |
{ |
| 6764 |
int32 intvalue; |
| 6765 |
|
| 6766 |
/* On overflow, just use PG_INT32_MAX; int_to_roman will cope */ |
| 6767 |
if (value <= PG_INT32_MAX && value >= PG_INT32_MIN) |
| 6768 |
intvalue = (int32) value; |
| 6769 |
else |
| 6770 |
intvalue = PG_INT32_MAX; |
| 6771 |
numstr = int_to_roman(intvalue); |
| 6772 |
} |
| 6773 |
else if (IS_EEEE(&Num)) |
| 6774 |
{ |
| 6775 |
/* to avoid loss of precision, must go via numeric not float8 */ |
| 6776 |
orgnum = numeric_out_sci(int64_to_numeric(value), |
| 6777 |
Num.post); |
| 6778 |
|
| 6779 |
/* |
| 6780 |
* numeric_out_sci() does not emit a sign for positive numbers. We |
| 6781 |
* need to add a space in this case so that positive and negative |
| 6782 |
* numbers are aligned. We don't have to worry about NaN/inf here. |
| 6783 |
*/ |
| 6784 |
if (*orgnum != '-') |
| 6785 |
{ |
| 6786 |
numstr = (char *) palloc(strlen(orgnum) + 2); |
| 6787 |
*numstr = ' '; |
| 6788 |
strcpy(numstr + 1, orgnum); |
| 6789 |
} |
| 6790 |
else |
| 6791 |
{ |
| 6792 |
numstr = orgnum; |
| 6793 |
} |
| 6794 |
} |
| 6795 |
else |
| 6796 |
{ |
| 6797 |
int numstr_pre_len; |
| 6798 |
|
| 6799 |
if (IS_MULTI(&Num)) |
| 6800 |
{ |
| 6801 |
double multi = pow((double) 10, (double) Num.multi); |
| 6802 |
|
| 6803 |
value = DatumGetInt64(DirectFunctionCall2(int8mul, |
| 6804 |
Int64GetDatum(value), |
| 6805 |
DirectFunctionCall1(dtoi8, |
| 6806 |
Float8GetDatum(multi)))); |
| 6807 |
Num.pre += Num.multi; |
| 6808 |
} |
| 6809 |
|
| 6810 |
orgnum = DatumGetCString(DirectFunctionCall1(int8out, |
| 6811 |
Int64GetDatum(value))); |
| 6812 |
|
| 6813 |
if (*orgnum == '-') |
| 6814 |
{ |
| 6815 |
sign = '-'; |
| 6816 |
orgnum++; |
| 6817 |
} |
| 6818 |
else |
| 6819 |
sign = '+'; |
| 6820 |
|
| 6821 |
numstr_pre_len = strlen(orgnum); |
| 6822 |
|
| 6823 |
/* post-decimal digits? Pad out with zeros. */ |
| 6824 |
if (Num.post) |
| 6825 |
{ |
| 6826 |
numstr = (char *) palloc(numstr_pre_len + Num.post + 2); |
| 6827 |
strcpy(numstr, orgnum); |
| 6828 |
*(numstr + numstr_pre_len) = '.'; |
| 6829 |
memset(numstr + numstr_pre_len + 1, '0', Num.post); |
| 6830 |
*(numstr + numstr_pre_len + Num.post + 1) = '\0'; |
| 6831 |
} |
| 6832 |
else |
| 6833 |
numstr = orgnum; |
| 6834 |
|
| 6835 |
/* needs padding? */ |
| 6836 |
if (numstr_pre_len < Num.pre) |
| 6837 |
out_pre_spaces = Num.pre - numstr_pre_len; |
| 6838 |
/* overflowed prefix digit format? */ |
| 6839 |
else if (numstr_pre_len > Num.pre) |
| 6840 |
{ |
| 6841 |
numstr = (char *) palloc(Num.pre + Num.post + 2); |
| 6842 |
fill_str(numstr, '#', Num.pre + Num.post + 1); |
| 6843 |
*(numstr + Num.pre) = '.'; |
| 6844 |
} |
| 6845 |
} |
| 6846 |
|
| 6847 |
NUM_TOCHAR_finish; |
| 6848 |
PG_RETURN_TEXT_P(result); |
| 6849 |
} |
| 6850 |
|
| 6851 |
/* ----------------- |
| 6852 |
* FLOAT4 to_char() |
| 6853 |
* ----------------- |
| 6854 |
*/ |
| 6855 |
Datum |
| 6856 |
float4_to_char(PG_FUNCTION_ARGS) |
| 6857 |
{ |
| 6858 |
float4 value = PG_GETARG_FLOAT4(0); |
| 6859 |
text *fmt = PG_GETARG_TEXT_PP(1); |
| 6860 |
NUMDesc Num; |
| 6861 |
FormatNode *format; |
| 6862 |
text *result; |
| 6863 |
bool shouldFree; |
| 6864 |
int out_pre_spaces = 0, |
| 6865 |
sign = 0; |
| 6866 |
char *numstr, |
| 6867 |
*p; |
| 6868 |
|
| 6869 |
NUM_TOCHAR_prepare; |
| 6870 |
|
| 6871 |
if (IS_ROMAN(&Num)) |
| 6872 |
{ |
| 6873 |
int32 intvalue; |
| 6874 |
|
| 6875 |
/* See notes in ftoi4() */ |
| 6876 |
value = rint(value); |
| 6877 |
/* On overflow, just use PG_INT32_MAX; int_to_roman will cope */ |
| 6878 |
if (!isnan(value) && FLOAT4_FITS_IN_INT32(value)) |
| 6879 |
intvalue = (int32) value; |
| 6880 |
else |
| 6881 |
intvalue = PG_INT32_MAX; |
| 6882 |
numstr = int_to_roman(intvalue); |
| 6883 |
} |
| 6884 |
else if (IS_EEEE(&Num)) |
| 6885 |
{ |
| 6886 |
if (isnan(value) || isinf(value)) |
| 6887 |
{ |
| 6888 |
/* |
| 6889 |
* Allow 6 characters for the leading sign, the decimal point, |
| 6890 |
* "e", the exponent's sign and two exponent digits. |
| 6891 |
*/ |
| 6892 |
numstr = (char *) palloc(Num.pre + Num.post + 7); |
| 6893 |
fill_str(numstr, '#', Num.pre + Num.post + 6); |
| 6894 |
*numstr = ' '; |
| 6895 |
*(numstr + Num.pre + 1) = '.'; |
| 6896 |
} |
| 6897 |
else |
| 6898 |
{ |
| 6899 |
numstr = psprintf("%+.*e", Num.post, value); |
| 6900 |
|
| 6901 |
/* |
| 6902 |
* Swap a leading positive sign for a space. |
| 6903 |
*/ |
| 6904 |
if (*numstr == '+') |
| 6905 |
*numstr = ' '; |
| 6906 |
} |
| 6907 |
} |
| 6908 |
else |
| 6909 |
{ |
| 6910 |
float4 val = value; |
| 6911 |
char *orgnum; |
| 6912 |
int numstr_pre_len; |
| 6913 |
|
| 6914 |
if (IS_MULTI(&Num)) |
| 6915 |
{ |
| 6916 |
float multi = pow((double) 10, (double) Num.multi); |
| 6917 |
|
| 6918 |
val = value * multi; |
| 6919 |
Num.pre += Num.multi; |
| 6920 |
} |
| 6921 |
|
| 6922 |
orgnum = psprintf("%.0f", fabs(val)); |
| 6923 |
numstr_pre_len = strlen(orgnum); |
| 6924 |
|
| 6925 |
/* adjust post digits to fit max float digits */ |
| 6926 |
if (numstr_pre_len >= FLT_DIG) |
| 6927 |
Num.post = 0; |
| 6928 |
else if (numstr_pre_len + Num.post > FLT_DIG) |
| 6929 |
Num.post = FLT_DIG - numstr_pre_len; |
| 6930 |
orgnum = psprintf("%.*f", Num.post, val); |
| 6931 |
|
| 6932 |
if (*orgnum == '-') |
| 6933 |
{ /* < 0 */ |
| 6934 |
sign = '-'; |
| 6935 |
numstr = orgnum + 1; |
| 6936 |
} |
| 6937 |
else |
| 6938 |
{ |
| 6939 |
sign = '+'; |
| 6940 |
numstr = orgnum; |
| 6941 |
} |
| 6942 |
|
| 6943 |
if ((p = strchr(numstr, '.'))) |
| 6944 |
numstr_pre_len = p - numstr; |
| 6945 |
else |
| 6946 |
numstr_pre_len = strlen(numstr); |
| 6947 |
|
| 6948 |
/* needs padding? */ |
| 6949 |
if (numstr_pre_len < Num.pre) |
| 6950 |
out_pre_spaces = Num.pre - numstr_pre_len; |
| 6951 |
/* overflowed prefix digit format? */ |
| 6952 |
else if (numstr_pre_len > Num.pre) |
| 6953 |
{ |
| 6954 |
numstr = (char *) palloc(Num.pre + Num.post + 2); |
| 6955 |
fill_str(numstr, '#', Num.pre + Num.post + 1); |
| 6956 |
*(numstr + Num.pre) = '.'; |
| 6957 |
} |
| 6958 |
} |
| 6959 |
|
| 6960 |
NUM_TOCHAR_finish; |
| 6961 |
PG_RETURN_TEXT_P(result); |
| 6962 |
} |
| 6963 |
|
| 6964 |
/* ----------------- |
| 6965 |
* FLOAT8 to_char() |
| 6966 |
* ----------------- |
| 6967 |
*/ |
| 6968 |
Datum |
| 6969 |
float8_to_char(PG_FUNCTION_ARGS) |
| 6970 |
{ |
| 6971 |
float8 value = PG_GETARG_FLOAT8(0); |
| 6972 |
text *fmt = PG_GETARG_TEXT_PP(1); |
| 6973 |
NUMDesc Num; |
| 6974 |
FormatNode *format; |
| 6975 |
text *result; |
| 6976 |
bool shouldFree; |
| 6977 |
int out_pre_spaces = 0, |
| 6978 |
sign = 0; |
| 6979 |
char *numstr, |
| 6980 |
*p; |
| 6981 |
|
| 6982 |
NUM_TOCHAR_prepare; |
| 6983 |
|
| 6984 |
if (IS_ROMAN(&Num)) |
| 6985 |
{ |
| 6986 |
int32 intvalue; |
| 6987 |
|
| 6988 |
/* See notes in dtoi4() */ |
| 6989 |
value = rint(value); |
| 6990 |
/* On overflow, just use PG_INT32_MAX; int_to_roman will cope */ |
| 6991 |
if (!isnan(value) && FLOAT8_FITS_IN_INT32(value)) |
| 6992 |
intvalue = (int32) value; |
| 6993 |
else |
| 6994 |
intvalue = PG_INT32_MAX; |
| 6995 |
numstr = int_to_roman(intvalue); |
| 6996 |
} |
| 6997 |
else if (IS_EEEE(&Num)) |
| 6998 |
{ |
| 6999 |
if (isnan(value) || isinf(value)) |
| 7000 |
{ |
| 7001 |
/* |
| 7002 |
* Allow 6 characters for the leading sign, the decimal point, |
| 7003 |
* "e", the exponent's sign and two exponent digits. |
| 7004 |
*/ |
| 7005 |
numstr = (char *) palloc(Num.pre + Num.post + 7); |
| 7006 |
fill_str(numstr, '#', Num.pre + Num.post + 6); |
| 7007 |
*numstr = ' '; |
| 7008 |
*(numstr + Num.pre + 1) = '.'; |
| 7009 |
} |
| 7010 |
else |
| 7011 |
{ |
| 7012 |
numstr = psprintf("%+.*e", Num.post, value); |
| 7013 |
|
| 7014 |
/* |
| 7015 |
* Swap a leading positive sign for a space. |
| 7016 |
*/ |
| 7017 |
if (*numstr == '+') |
| 7018 |
*numstr = ' '; |
| 7019 |
} |
| 7020 |
} |
| 7021 |
else |
| 7022 |
{ |
| 7023 |
float8 val = value; |
| 7024 |
char *orgnum; |
| 7025 |
int numstr_pre_len; |
| 7026 |
|
| 7027 |
if (IS_MULTI(&Num)) |
| 7028 |
{ |
| 7029 |
double multi = pow((double) 10, (double) Num.multi); |
| 7030 |
|
| 7031 |
val = value * multi; |
| 7032 |
Num.pre += Num.multi; |
| 7033 |
} |
| 7034 |
|
| 7035 |
orgnum = psprintf("%.0f", fabs(val)); |
| 7036 |
numstr_pre_len = strlen(orgnum); |
| 7037 |
|
| 7038 |
/* adjust post digits to fit max double digits */ |
| 7039 |
if (numstr_pre_len >= DBL_DIG) |
| 7040 |
Num.post = 0; |
| 7041 |
else if (numstr_pre_len + Num.post > DBL_DIG) |
| 7042 |
Num.post = DBL_DIG - numstr_pre_len; |
| 7043 |
orgnum = psprintf("%.*f", Num.post, val); |
| 7044 |
|
| 7045 |
if (*orgnum == '-') |
| 7046 |
{ /* < 0 */ |
| 7047 |
sign = '-'; |
| 7048 |
numstr = orgnum + 1; |
| 7049 |
} |
| 7050 |
else |
| 7051 |
{ |
| 7052 |
sign = '+'; |
| 7053 |
numstr = orgnum; |
| 7054 |
} |
| 7055 |
|
| 7056 |
if ((p = strchr(numstr, '.'))) |
| 7057 |
numstr_pre_len = p - numstr; |
| 7058 |
else |
| 7059 |
numstr_pre_len = strlen(numstr); |
| 7060 |
|
| 7061 |
/* needs padding? */ |
| 7062 |
if (numstr_pre_len < Num.pre) |
| 7063 |
out_pre_spaces = Num.pre - numstr_pre_len; |
| 7064 |
/* overflowed prefix digit format? */ |
| 7065 |
else if (numstr_pre_len > Num.pre) |
| 7066 |
{ |
| 7067 |
numstr = (char *) palloc(Num.pre + Num.post + 2); |
| 7068 |
fill_str(numstr, '#', Num.pre + Num.post + 1); |
| 7069 |
*(numstr + Num.pre) = '.'; |
| 7070 |
} |
| 7071 |
} |
| 7072 |
|
| 7073 |
NUM_TOCHAR_finish; |
| 7074 |
PG_RETURN_TEXT_P(result); |
| 7075 |
} |