Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitd1d8462

Browse files
committed
Assorted further cleanup for integer-conversion patch.
Avoid depending on LL notation, which is likely to not work in pre-C99compilers; don't pointlessly use INT32_MIN/INT64_MIN in code that hasthe numerical value hard-wired into it anyway; remove some gratuitousstyle inconsistencies between pg_ltoa and pg_lltoa; fix int2 test caseso it actually tests int2.
1 parent4343c0e commitd1d8462

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-
#ifndefINT64_MIN
28-
#defineINT64_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(int16i,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(int32value,char*a)
141131
{
142-
char*start=a;
143-
boolneg= false;
132+
char*start=a;
133+
boolneg= 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 resultstringbackwards. */
161151
do
162152
{
163-
int32remainder;
164-
int32oldval=value;
153+
int32remainder;
154+
int32oldval=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-
charswap=*start;
170+
charswap=*start;
171+
179172
*start++=*a;
180173
*a--=swap;
181174
}
182175
}
183176

184177
/*
185-
* pg_lltoa: convert a signed64bit integer to its string representation
178+
* pg_lltoa: convert a signed64-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(int64value,char*a)
192185
{
193-
char*start=a;
194-
boolneg= false;
186+
char*start=a;
187+
boolneg= 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 thestring by computing the wanted string backwards. */
204+
/*Compute theresult string backwards. */
212205
do
213206
{
214-
int64remainder;
215-
int64oldval=value;
207+
int64remainder;
208+
int64oldval=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-
charswap=*start;
224+
charswap=*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)

[8]ページ先頭

©2009-2025 Movatter.jp