|
3 | 3 | * numutils.c
|
4 | 4 | * utility functions for I/O of built-in numeric types.
|
5 | 5 | *
|
6 |
| - *integer:pg_atoi, pg_itoa, pg_ltoa |
7 |
| - * |
8 | 6 | * Portions Copyright (c) 1996-2010, PostgreSQL Global Development Group
|
9 | 7 | * Portions Copyright (c) 1994, Regents of the University of California
|
10 | 8 | *
|
@@ -109,27 +107,118 @@ pg_atoi(char *s, int size, int c)
|
109 | 107 | }
|
110 | 108 |
|
111 | 109 | /*
|
112 |
| - *pg_itoa- converts a short int to its string represention |
| 110 | + * pg_itoa: converts a signed 16-bit integer to its string representation |
| 111 | + * |
| 112 | + * Caller must ensure that 'a' points to enough memory to hold the result |
| 113 | + * (at least 7 bytes, counting a leading sign and trailing NUL). |
113 | 114 | *
|
114 |
| - *Note: |
115 |
| - *previously based on ~ingres/source/gutil/atoi.c |
116 |
| - *now uses vendor's sprintf conversion |
| 115 | + * It doesn't seem worth implementing this separately. |
117 | 116 | */
|
118 | 117 | void
|
119 | 118 | pg_itoa(int16i,char*a)
|
120 | 119 | {
|
121 |
| -sprintf(a,"%hd", (short)i); |
| 120 | +pg_ltoa((int32)i,a); |
| 121 | +} |
| 122 | + |
| 123 | +/* |
| 124 | + * pg_ltoa: converts a signed 32-bit integer to its string representation |
| 125 | + * |
| 126 | + * Caller must ensure that 'a' points to enough memory to hold the result |
| 127 | + * (at least 12 bytes, counting a leading sign and trailing NUL). |
| 128 | + */ |
| 129 | +void |
| 130 | +pg_ltoa(int32value,char*a) |
| 131 | +{ |
| 132 | +char*start=a; |
| 133 | +boolneg= false; |
| 134 | + |
| 135 | +/* |
| 136 | + * Avoid problems with the most negative integer not being representable |
| 137 | + * as a positive integer. |
| 138 | + */ |
| 139 | +if (value==INT32_MIN) |
| 140 | +{ |
| 141 | +memcpy(a,"-2147483648",12); |
| 142 | +return; |
| 143 | +} |
| 144 | +elseif (value<0) |
| 145 | +{ |
| 146 | +value=-value; |
| 147 | +neg= true; |
| 148 | +} |
| 149 | + |
| 150 | +/* Compute the result backwards. */ |
| 151 | +do |
| 152 | +{ |
| 153 | +int32remainder; |
| 154 | +int32oldval=value; |
| 155 | +value /=10; |
| 156 | +remainder=oldval-value*10; |
| 157 | +*a++='0'+remainder; |
| 158 | +}while (value!=0); |
| 159 | +if (neg) |
| 160 | +*a++='-'; |
| 161 | + |
| 162 | +/* Add trailing NUL byte. */ |
| 163 | +*a--='\0'; |
| 164 | + |
| 165 | +/* reverse string */ |
| 166 | +while (start<a) |
| 167 | +{ |
| 168 | +charswap=*start; |
| 169 | +*start++=*a; |
| 170 | +*a--=swap; |
| 171 | +} |
122 | 172 | }
|
123 | 173 |
|
124 | 174 | /*
|
125 |
| - *pg_ltoa- converts a long intto its stringrepresention |
| 175 | + * pg_lltoa: convert a signed 64bit integerto its stringrepresentation |
126 | 176 | *
|
127 |
| - *Note: |
128 |
| - *previously based on ~ingres/source/gutil/atoi.c |
129 |
| - *now uses vendor's sprintf conversion |
| 177 | + * Caller must ensure that 'a' points to enough memory to hold the result |
| 178 | + * (at least MAXINT8LEN+1 bytes, counting a leading sign and trailing NUL). |
130 | 179 | */
|
131 | 180 | void
|
132 |
| -pg_ltoa(int32l,char*a) |
| 181 | +pg_lltoa(int64value,char*a) |
133 | 182 | {
|
134 |
| -sprintf(a,"%d",l); |
| 183 | +char*start=a; |
| 184 | +boolneg= false; |
| 185 | + |
| 186 | +/* |
| 187 | + * Avoid problems with the most negative integer not being representable |
| 188 | + * as a positive integer. |
| 189 | + */ |
| 190 | +if (value==INT64_MIN) |
| 191 | +{ |
| 192 | +memcpy(a,"-9223372036854775808",21); |
| 193 | +return; |
| 194 | +} |
| 195 | +elseif (value<0) |
| 196 | +{ |
| 197 | +value=-value; |
| 198 | +neg= true; |
| 199 | +} |
| 200 | + |
| 201 | +/* Build the string by computing the wanted string backwards. */ |
| 202 | +do |
| 203 | +{ |
| 204 | +int64remainder; |
| 205 | +int64oldval=value; |
| 206 | +value /=10; |
| 207 | +remainder=oldval-value*10; |
| 208 | +*a++='0'+remainder; |
| 209 | +}while (value!=0); |
| 210 | + |
| 211 | +if (neg) |
| 212 | +*a++='-'; |
| 213 | + |
| 214 | +/* Add trailing NUL byte. */ |
| 215 | +*a--='\0'; |
| 216 | + |
| 217 | +/* Reverse string. */ |
| 218 | +while (start<a) |
| 219 | +{ |
| 220 | +charswap=*start; |
| 221 | +*start++=*a; |
| 222 | +*a--=swap; |
| 223 | +} |
135 | 224 | }
|