Commit d1d8462d authored by Tom Lane's avatar Tom Lane

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.
parent 4343c0e5
...@@ -18,16 +18,6 @@ ...@@ -18,16 +18,6 @@
#include <limits.h> #include <limits.h>
#include <ctype.h> #include <ctype.h>
/*
* Defining INT64_MIN as -9223372036854775808LL may not work; the compiler's
* tokenizer may see - as a separate token and then be unable to view
* 9223372036854775808 as a number. This is the standard workaround for that
* problem.
*/
#ifndef INT64_MIN
#define INT64_MIN (-9223372036854775807LL - 1)
#endif
#include "utils/builtins.h" #include "utils/builtins.h"
/* /*
...@@ -127,7 +117,7 @@ pg_atoi(char *s, int size, int c) ...@@ -127,7 +117,7 @@ pg_atoi(char *s, int size, int c)
void void
pg_itoa(int16 i, char *a) pg_itoa(int16 i, char *a)
{ {
pg_ltoa((int32)i, a); pg_ltoa((int32) i, a);
} }
/* /*
...@@ -139,14 +129,14 @@ pg_itoa(int16 i, char *a) ...@@ -139,14 +129,14 @@ pg_itoa(int16 i, char *a)
void void
pg_ltoa(int32 value, char *a) pg_ltoa(int32 value, char *a)
{ {
char *start = a; char *start = a;
bool neg = false; bool neg = false;
/* /*
* Avoid problems with the most negative integer not being representable * Avoid problems with the most negative integer not being representable
* as a positive integer. * as a positive integer.
*/ */
if (value == INT_MIN) if (value == (-2147483647-1))
{ {
memcpy(a, "-2147483648", 12); memcpy(a, "-2147483648", 12);
return; return;
...@@ -157,32 +147,35 @@ pg_ltoa(int32 value, char *a) ...@@ -157,32 +147,35 @@ pg_ltoa(int32 value, char *a)
neg = true; neg = true;
} }
/* Compute the result backwards. */ /* Compute the result string backwards. */
do do
{ {
int32 remainder; int32 remainder;
int32 oldval = value; int32 oldval = value;
value /= 10; value /= 10;
remainder = oldval - value * 10; remainder = oldval - value * 10;
*a++ = '0' + remainder; *a++ = '0' + remainder;
} while (value != 0); } while (value != 0);
if (neg) if (neg)
*a++ = '-'; *a++ = '-';
/* Add trailing NUL byte. */ /* Add trailing NUL byte, and back up 'a' to the last character. */
*a-- = '\0'; *a-- = '\0';
/* reverse string */ /* Reverse string. */
while (start < a) while (start < a)
{ {
char swap = *start; char swap = *start;
*start++ = *a; *start++ = *a;
*a-- = swap; *a-- = swap;
} }
} }
/* /*
* pg_lltoa: convert a signed 64bit integer to its string representation * pg_lltoa: convert a signed 64-bit integer to its string representation
* *
* Caller must ensure that 'a' points to enough memory to hold the result * Caller must ensure that 'a' points to enough memory to hold the result
* (at least MAXINT8LEN+1 bytes, counting a leading sign and trailing NUL). * (at least MAXINT8LEN+1 bytes, counting a leading sign and trailing NUL).
...@@ -190,14 +183,14 @@ pg_ltoa(int32 value, char *a) ...@@ -190,14 +183,14 @@ pg_ltoa(int32 value, char *a)
void void
pg_lltoa(int64 value, char *a) pg_lltoa(int64 value, char *a)
{ {
char *start = a; char *start = a;
bool neg = false; bool neg = false;
/* /*
* Avoid problems with the most negative integer not being representable * Avoid problems with the most negative integer not being representable
* as a positive integer. * as a positive integer.
*/ */
if (value == INT64_MIN) if (value == (-INT64CONST(0x7FFFFFFFFFFFFFFF)-1))
{ {
memcpy(a, "-9223372036854775808", 21); memcpy(a, "-9223372036854775808", 21);
return; return;
...@@ -208,11 +201,12 @@ pg_lltoa(int64 value, char *a) ...@@ -208,11 +201,12 @@ pg_lltoa(int64 value, char *a)
neg = true; neg = true;
} }
/* Build the string by computing the wanted string backwards. */ /* Compute the result string backwards. */
do do
{ {
int64 remainder; int64 remainder;
int64 oldval = value; int64 oldval = value;
value /= 10; value /= 10;
remainder = oldval - value * 10; remainder = oldval - value * 10;
*a++ = '0' + remainder; *a++ = '0' + remainder;
...@@ -221,13 +215,14 @@ pg_lltoa(int64 value, char *a) ...@@ -221,13 +215,14 @@ pg_lltoa(int64 value, char *a)
if (neg) if (neg)
*a++ = '-'; *a++ = '-';
/* Add trailing NUL byte. */ /* Add trailing NUL byte, and back up 'a' to the last character. */
*a-- = '\0'; *a-- = '\0';
/* Reverse string. */ /* Reverse string. */
while (start < a) while (start < a)
{ {
char swap = *start; char swap = *start;
*start++ = *a; *start++ = *a;
*a-- = swap; *a-- = swap;
} }
......
...@@ -249,7 +249,7 @@ SELECT (-1::int2<<15)::text; ...@@ -249,7 +249,7 @@ SELECT (-1::int2<<15)::text;
-32768 -32768
(1 row) (1 row)
SELECT ((-1::int2<<15)+1)::text; SELECT ((-1::int2<<15)+1::int2)::text;
text text
-------- --------
-32767 -32767
......
...@@ -86,4 +86,4 @@ SELECT '' AS five, i.f1, i.f1 / int4 '2' AS x FROM INT2_TBL i; ...@@ -86,4 +86,4 @@ SELECT '' AS five, i.f1, i.f1 / int4 '2' AS x FROM INT2_TBL i;
-- corner cases -- corner cases
SELECT (-1::int2<<15)::text; SELECT (-1::int2<<15)::text;
SELECT ((-1::int2<<15)+1)::text; SELECT ((-1::int2<<15)+1::int2)::text;
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment