Skip to content

Commit d1d8462

Browse files
committed
Assorted further cleanup for integer-conversion patch.
Avoid depending on LL notation, which is likely to not work in pre-C99 compilers; don't pointlessly use INT32_MIN/INT64_MIN in code that has the numerical value hard-wired into it anyway; remove some gratuitous style inconsistencies between pg_ltoa and pg_lltoa; fix int2 test case so it actually tests int2.
1 parent 4343c0e commit d1d8462

File tree

3 files changed

+26
-31
lines changed

3 files changed

+26
-31
lines changed

src/backend/utils/adt/numutils.c

Lines changed: 24 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,6 @@
1818
#include <limits.h>
1919
#include <ctype.h>
2020

21-
/*
22-
* Defining INT64_MIN as -9223372036854775808LL may not work; the compiler's
23-
* tokenizer may see - as a separate token and then be unable to view
24-
* 9223372036854775808 as a number. This is the standard workaround for that
25-
* problem.
26-
*/
27-
#ifndef INT64_MIN
28-
#define INT64_MIN (-9223372036854775807LL - 1)
29-
#endif
30-
3121
#include "utils/builtins.h"
3222

3323
/*
@@ -127,7 +117,7 @@ pg_atoi(char *s, int size, int c)
127117
void
128118
pg_itoa(int16 i, char *a)
129119
{
130-
pg_ltoa((int32)i, a);
120+
pg_ltoa((int32) i, a);
131121
}
132122

133123
/*
@@ -139,14 +129,14 @@ pg_itoa(int16 i, char *a)
139129
void
140130
pg_ltoa(int32 value, char *a)
141131
{
142-
char *start = a;
143-
bool neg = false;
132+
char *start = a;
133+
bool neg = false;
144134

145135
/*
146136
* Avoid problems with the most negative integer not being representable
147137
* as a positive integer.
148138
*/
149-
if (value == INT_MIN)
139+
if (value == (-2147483647-1))
150140
{
151141
memcpy(a, "-2147483648", 12);
152142
return;
@@ -157,47 +147,50 @@ pg_ltoa(int32 value, char *a)
157147
neg = true;
158148
}
159149

160-
/* Compute the result backwards. */
150+
/* Compute the result string backwards. */
161151
do
162152
{
163-
int32 remainder;
164-
int32 oldval = value;
153+
int32 remainder;
154+
int32 oldval = value;
155+
165156
value /= 10;
166157
remainder = oldval - value * 10;
167158
*a++ = '0' + remainder;
168159
} while (value != 0);
160+
169161
if (neg)
170162
*a++ = '-';
171163

172-
/* Add trailing NUL byte. */
164+
/* Add trailing NUL byte, and back up 'a' to the last character. */
173165
*a-- = '\0';
174166

175-
/* reverse string */
167+
/* Reverse string. */
176168
while (start < a)
177169
{
178-
char swap = *start;
170+
char swap = *start;
171+
179172
*start++ = *a;
180173
*a-- = swap;
181174
}
182175
}
183176

184177
/*
185-
* pg_lltoa: convert a signed 64bit integer to its string representation
178+
* pg_lltoa: convert a signed 64-bit integer to its string representation
186179
*
187180
* Caller must ensure that 'a' points to enough memory to hold the result
188181
* (at least MAXINT8LEN+1 bytes, counting a leading sign and trailing NUL).
189182
*/
190183
void
191184
pg_lltoa(int64 value, char *a)
192185
{
193-
char *start = a;
194-
bool neg = false;
186+
char *start = a;
187+
bool neg = false;
195188

196189
/*
197190
* Avoid problems with the most negative integer not being representable
198191
* as a positive integer.
199192
*/
200-
if (value == INT64_MIN)
193+
if (value == (-INT64CONST(0x7FFFFFFFFFFFFFFF)-1))
201194
{
202195
memcpy(a, "-9223372036854775808", 21);
203196
return;
@@ -208,11 +201,12 @@ pg_lltoa(int64 value, char *a)
208201
neg = true;
209202
}
210203

211-
/* Build the string by computing the wanted string backwards. */
204+
/* Compute the result string backwards. */
212205
do
213206
{
214-
int64 remainder;
215-
int64 oldval = value;
207+
int64 remainder;
208+
int64 oldval = value;
209+
216210
value /= 10;
217211
remainder = oldval - value * 10;
218212
*a++ = '0' + remainder;
@@ -221,13 +215,14 @@ pg_lltoa(int64 value, char *a)
221215
if (neg)
222216
*a++ = '-';
223217

224-
/* Add trailing NUL byte. */
218+
/* Add trailing NUL byte, and back up 'a' to the last character. */
225219
*a-- = '\0';
226220

227221
/* Reverse string. */
228222
while (start < a)
229223
{
230-
char swap = *start;
224+
char swap = *start;
225+
231226
*start++ = *a;
232227
*a-- = swap;
233228
}

src/test/regress/expected/int2.out

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ SELECT (-1::int2<<15)::text;
249249
-32768
250250
(1 row)
251251

252-
SELECT ((-1::int2<<15)+1)::text;
252+
SELECT ((-1::int2<<15)+1::int2)::text;
253253
text
254254
--------
255255
-32767

src/test/regress/sql/int2.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,4 +86,4 @@ SELECT '' AS five, i.f1, i.f1 / int4 '2' AS x FROM INT2_TBL i;
8686

8787
-- corner cases
8888
SELECT (-1::int2<<15)::text;
89-
SELECT ((-1::int2<<15)+1)::text;
89+
SELECT ((-1::int2<<15)+1::int2)::text;

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