Skip to content

Commit 94701fb

Browse files
committed
Peter found bug in the to_char() routine for PL/MI options. This
patch fix it -- but this patch doesn't contains tests or docs fixes. I will send it later. Fixed outputs: select to_char(x, '9999.999') as x, to_char(x, 'S9999.999') as s, to_char(x, 'SG9999.999') as sg, to_char(x, 'MI9999.999') as mi, to_char(x, 'PL9999.999') as pl, to_char(x, 'PLMI9999.999') as plmi, to_char(x, '9999.999SG') as sg2, to_char(x, '9999.999PL') as pl2, to_char(x, '9999.999MI') as mi2 from num; Karel Zak
1 parent 8000fdd commit 94701fb

File tree

1 file changed

+53
-38
lines changed

1 file changed

+53
-38
lines changed

src/backend/utils/adt/formatting.c

Lines changed: 53 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/* -----------------------------------------------------------------------
22
* formatting.c
33
*
4-
* $Header: /cvsroot/pgsql/src/backend/utils/adt/formatting.c,v 1.58 2003/03/10 22:28:18 tgl Exp $
4+
* $Header: /cvsroot/pgsql/src/backend/utils/adt/formatting.c,v 1.59 2003/03/20 05:19:26 momjian Exp $
55
*
66
*
77
* Portions Copyright (c) 1999-2002, PostgreSQL Global Development Group
@@ -276,17 +276,19 @@ typedef struct
276276
* Flags for NUMBER version
277277
* ----------
278278
*/
279-
#define NUM_F_DECIMAL 0x01
280-
#define NUM_F_LDECIMAL 0x02
281-
#define NUM_F_ZERO 0x04
282-
#define NUM_F_BLANK 0x08
283-
#define NUM_F_FILLMODE 0x10
284-
#define NUM_F_LSIGN 0x20
285-
#define NUM_F_BRACKET 0x40
286-
#define NUM_F_MINUS 0x80
287-
#define NUM_F_PLUS 0x100
288-
#define NUM_F_ROMAN 0x200
289-
#define NUM_F_MULTI 0x400
279+
#define NUM_F_DECIMAL 1 << 1
280+
#define NUM_F_LDECIMAL 1 << 2
281+
#define NUM_F_ZERO 1 << 3
282+
#define NUM_F_BLANK 1 << 4
283+
#define NUM_F_FILLMODE 1 << 5
284+
#define NUM_F_LSIGN 1 << 6
285+
#define NUM_F_BRACKET 1 << 7
286+
#define NUM_F_MINUS 1 << 8
287+
#define NUM_F_PLUS 1 << 9
288+
#define NUM_F_ROMAN 1 << 10
289+
#define NUM_F_MULTI 1 << 11
290+
#define NUM_F_PLUS_POST 1 << 12
291+
#define NUM_F_MINUS_POST 1 << 13
290292

291293
#define NUM_LSIGN_PRE -1
292294
#define NUM_LSIGN_POST 1
@@ -1052,6 +1054,8 @@ NUMDesc_prepare(NUMDesc *num, FormatNode *n)
10521054
elog(ERROR, "to_char/to_number(): can't use 'S' and 'MI' together.");
10531055
}
10541056
num->flag |= NUM_F_MINUS;
1057+
if (IS_DECIMAL(num))
1058+
num->flag |= NUM_F_MINUS_POST;
10551059
break;
10561060

10571061
case NUM_PL:
@@ -1061,6 +1065,8 @@ NUMDesc_prepare(NUMDesc *num, FormatNode *n)
10611065
elog(ERROR, "to_char/to_number(): can't use 'S' and 'PL' together.");
10621066
}
10631067
num->flag |= NUM_F_PLUS;
1068+
if (IS_DECIMAL(num))
1069+
num->flag |= NUM_F_PLUS_POST;
10641070
break;
10651071

10661072
case NUM_SG:
@@ -3880,28 +3886,36 @@ NUM_processor(FormatNode *node, NUMDesc *Num, char *inout, char *number,
38803886
else
38813887
{
38823888
Np->sign = sign;
3883-
3884-
if (Np->sign != '-')
3889+
3890+
/* MI/PL/SG - write sign itself and not in number */
3891+
if (IS_PLUS(Np->Num) || IS_MINUS(Np->Num))
38853892
{
3886-
Np->Num->flag &= ~NUM_F_BRACKET;
3887-
Np->Num->flag &= ~NUM_F_MINUS;
3893+
if (IS_PLUS(Np->Num) && IS_MINUS(Np->Num)==FALSE)
3894+
Np->sign_wrote = FALSE;
3895+
Np->sign_pos = -1;
38883896
}
3889-
else if (Np->sign != '+')
3890-
Np->Num->flag &= ~NUM_F_PLUS;
3891-
3892-
if (Np->sign == '+' && IS_FILLMODE(Np->Num) && !IS_LSIGN(Np->Num))
3893-
Np->sign_wrote = TRUE; /* needn't sign */
38943897
else
3895-
Np->sign_wrote = FALSE; /* need sign */
3898+
{
3899+
if (Np->sign != '-')
3900+
{
3901+
if (IS_BRACKET(Np->Num))
3902+
Np->Num->flag &= ~NUM_F_BRACKET;
3903+
if (IS_MINUS(Np->Num))
3904+
Np->Num->flag &= ~NUM_F_MINUS;
3905+
}
3906+
else if (Np->sign != '+' && IS_PLUS(Np->Num))
3907+
Np->Num->flag &= ~NUM_F_PLUS;
38963908

3897-
Np->sign_pos = -1;
3909+
if (Np->sign == '+' && IS_FILLMODE(Np->Num) && !IS_LSIGN(Np->Num))
3910+
Np->sign_wrote = TRUE; /* needn't sign */
3911+
else
3912+
Np->sign_wrote = FALSE; /* need sign */
38983913

3899-
if (Np->Num->lsign == NUM_LSIGN_PRE && Np->Num->pre == Np->Num->pre_lsign_num)
3900-
Np->Num->lsign = NUM_LSIGN_POST;
3914+
Np->sign_pos = -1;
39013915

3902-
/* MI/PL/SG - write sign itself and not in number */
3903-
if (IS_PLUS(Np->Num) || IS_MINUS(Np->Num))
3904-
Np->sign_wrote = TRUE; /* needn't sign */
3916+
if (Np->Num->lsign == NUM_LSIGN_PRE && Np->Num->pre == Np->Num->pre_lsign_num)
3917+
Np->Num->lsign = NUM_LSIGN_POST;
3918+
}
39053919
}
39063920

39073921
/*
@@ -3917,7 +3931,7 @@ NUM_processor(FormatNode *node, NUMDesc *Num, char *inout, char *number,
39173931
{
39183932
if (IS_DECIMAL(Np->Num))
39193933
Np->last_relevant = get_last_relevant_decnum(
3920-
Np->number +
3934+
Np->number +
39213935
((Np->Num->zero_end - Np->num_pre > 0) ?
39223936
Np->Num->zero_end - Np->num_pre : 0));
39233937
}
@@ -3946,17 +3960,15 @@ NUM_processor(FormatNode *node, NUMDesc *Num, char *inout, char *number,
39463960
/*
39473961
* terrible Ora format
39483962
*/
3949-
if (!IS_ZERO(Np->Num) && *Np->number == '0' &&
3950-
!IS_FILLMODE(Np->Num) && Np->Num->post != 0)
3963+
if (IS_ZERO(Np->Num)==FALSE && *Np->number == '0' &&
3964+
IS_FILLMODE(Np->Num)==FALSE && Np->Num->post)
39513965
{
39523966

39533967
++Np->sign_pos;
39543968

39553969
if (IS_LSIGN(Np->Num))
39563970
{
3957-
if (Np->Num->lsign == NUM_LSIGN_PRE)
3958-
++Np->sign_pos;
3959-
else
3971+
if (Np->Num->lsign != NUM_LSIGN_PRE)
39603972
--Np->sign_pos;
39613973
}
39623974
}
@@ -3975,8 +3987,8 @@ NUM_processor(FormatNode *node, NUMDesc *Num, char *inout, char *number,
39753987

39763988
#ifdef DEBUG_TO_FROM_CHAR
39773989
elog(DEBUG_elog_output,
3978-
3979-
"\n\tNUM: '%s'\n\tPRE: %d\n\tPOST: %d\n\tNUM_COUNT: %d\n\tNUM_PRE: %d\n\tSIGN_POS: %d\n\tSIGN_WROTE: %s\n\tZERO: %s\n\tZERO_START: %d\n\tZERO_END: %d\n\tLAST_RELEVANT: %s",
3990+
"\n\tSIGN: '%c'\n\tNUM: '%s'\n\tPRE: %d\n\tPOST: %d\n\tNUM_COUNT: %d\n\tNUM_PRE: %d\n\tSIGN_POS: %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",
3991+
Np->sign,
39803992
Np->number,
39813993
Np->Num->pre,
39823994
Np->Num->post,
@@ -3987,8 +3999,11 @@ NUM_processor(FormatNode *node, NUMDesc *Num, char *inout, char *number,
39873999
IS_ZERO(Np->Num) ? "Yes" : "No",
39884000
Np->Num->zero_start,
39894001
Np->Num->zero_end,
3990-
Np->last_relevant ? Np->last_relevant : "<not set>"
3991-
);
4002+
Np->last_relevant ? Np->last_relevant : "<not set>",
4003+
IS_BRACKET(Np->Num) ? "Yes" : "No",
4004+
IS_PLUS(Np->Num) ? "Yes" : "No",
4005+
IS_MINUS(Np->Num) ? "Yes" : "No"
4006+
);
39924007
#endif
39934008

39944009
/*

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy