|
18 | 18 | #include<limits.h>
|
19 | 19 | #include<ctype.h>
|
20 | 20 |
|
| 21 | +#include"common/int.h" |
21 | 22 | #include"utils/builtins.h"
|
22 | 23 |
|
23 | 24 | /*
|
@@ -108,6 +109,154 @@ pg_atoi(const char *s, int size, int c)
|
108 | 109 | return (int32)l;
|
109 | 110 | }
|
110 | 111 |
|
| 112 | +/* |
| 113 | + * Convert input string to a signed 16 bit integer. |
| 114 | + * |
| 115 | + * Allows any number of leading or trailing whitespace characters. Will throw |
| 116 | + * ereport() upon bad input format or overflow. |
| 117 | + * |
| 118 | + * NB: Accumulate input as a negative number, to deal with two's complement |
| 119 | + * representation of the most negative number, which can't be represented as a |
| 120 | + * positive number. |
| 121 | + */ |
| 122 | +int16 |
| 123 | +pg_strtoint16(constchar*s) |
| 124 | +{ |
| 125 | +constchar*ptr=s; |
| 126 | +int16tmp=0; |
| 127 | +boolneg= false; |
| 128 | + |
| 129 | +/* skip leading spaces */ |
| 130 | +while (likely(*ptr)&&isspace((unsignedchar)*ptr)) |
| 131 | +ptr++; |
| 132 | + |
| 133 | +/* handle sign */ |
| 134 | +if (*ptr=='-') |
| 135 | +{ |
| 136 | +ptr++; |
| 137 | +neg= true; |
| 138 | +} |
| 139 | +elseif (*ptr=='+') |
| 140 | +ptr++; |
| 141 | + |
| 142 | +/* require at least one digit */ |
| 143 | +if (unlikely(!isdigit((unsignedchar)*ptr))) |
| 144 | +gotoinvalid_syntax; |
| 145 | + |
| 146 | +/* process digits */ |
| 147 | +while (*ptr&&isdigit((unsignedchar)*ptr)) |
| 148 | +{ |
| 149 | +int8digit= (*ptr++-'0'); |
| 150 | + |
| 151 | +if (unlikely(pg_mul_s16_overflow(tmp,10,&tmp))|| |
| 152 | +unlikely(pg_sub_s16_overflow(tmp,digit,&tmp))) |
| 153 | +gotoout_of_range; |
| 154 | +} |
| 155 | + |
| 156 | +/* allow trailing whitespace, but not other trailing chars */ |
| 157 | +while (*ptr!='\0'&&isspace((unsignedchar)*ptr)) |
| 158 | +ptr++; |
| 159 | + |
| 160 | +if (unlikely(*ptr!='\0')) |
| 161 | +gotoinvalid_syntax; |
| 162 | + |
| 163 | +if (!neg) |
| 164 | +{ |
| 165 | +/* could fail if input is most negative number */ |
| 166 | +if (unlikely(tmp==PG_INT16_MIN)) |
| 167 | +gotoout_of_range; |
| 168 | +tmp=-tmp; |
| 169 | +} |
| 170 | + |
| 171 | +returntmp; |
| 172 | + |
| 173 | +out_of_range: |
| 174 | +ereport(ERROR, |
| 175 | +(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE), |
| 176 | +errmsg("value \"%s\" is out of range for type %s", |
| 177 | +s,"smallint"))); |
| 178 | + |
| 179 | +invalid_syntax: |
| 180 | +ereport(ERROR, |
| 181 | +(errcode(ERRCODE_INVALID_TEXT_REPRESENTATION), |
| 182 | +errmsg("invalid input syntax for type %s: \"%s\"", |
| 183 | +"smallint",s))); |
| 184 | +} |
| 185 | + |
| 186 | +/* |
| 187 | + * Convert input string to a signed 32 bit integer. |
| 188 | + * |
| 189 | + * Allows any number of leading or trailing whitespace characters. Will throw |
| 190 | + * ereport() upon bad input format or overflow. |
| 191 | + * |
| 192 | + * NB: Accumulate input as a negative number, to deal with two's complement |
| 193 | + * representation of the most negative number, which can't be represented as a |
| 194 | + * positive number. |
| 195 | + */ |
| 196 | +int32 |
| 197 | +pg_strtoint32(constchar*s) |
| 198 | +{ |
| 199 | +constchar*ptr=s; |
| 200 | +int32tmp=0; |
| 201 | +boolneg= false; |
| 202 | + |
| 203 | +/* skip leading spaces */ |
| 204 | +while (likely(*ptr)&&isspace((unsignedchar)*ptr)) |
| 205 | +ptr++; |
| 206 | + |
| 207 | +/* handle sign */ |
| 208 | +if (*ptr=='-') |
| 209 | +{ |
| 210 | +ptr++; |
| 211 | +neg= true; |
| 212 | +} |
| 213 | +elseif (*ptr=='+') |
| 214 | +ptr++; |
| 215 | + |
| 216 | +/* require at least one digit */ |
| 217 | +if (unlikely(!isdigit((unsignedchar)*ptr))) |
| 218 | +gotoinvalid_syntax; |
| 219 | + |
| 220 | +/* process digits */ |
| 221 | +while (*ptr&&isdigit((unsignedchar)*ptr)) |
| 222 | +{ |
| 223 | +int8digit= (*ptr++-'0'); |
| 224 | + |
| 225 | +if (unlikely(pg_mul_s32_overflow(tmp,10,&tmp))|| |
| 226 | +unlikely(pg_sub_s32_overflow(tmp,digit,&tmp))) |
| 227 | +gotoout_of_range; |
| 228 | +} |
| 229 | + |
| 230 | +/* allow trailing whitespace, but not other trailing chars */ |
| 231 | +while (*ptr!='\0'&&isspace((unsignedchar)*ptr)) |
| 232 | +ptr++; |
| 233 | + |
| 234 | +if (unlikely(*ptr!='\0')) |
| 235 | +gotoinvalid_syntax; |
| 236 | + |
| 237 | +if (!neg) |
| 238 | +{ |
| 239 | +/* could fail if input is most negative number */ |
| 240 | +if (unlikely(tmp==PG_INT32_MIN)) |
| 241 | +gotoout_of_range; |
| 242 | +tmp=-tmp; |
| 243 | +} |
| 244 | + |
| 245 | +returntmp; |
| 246 | + |
| 247 | +out_of_range: |
| 248 | +ereport(ERROR, |
| 249 | +(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE), |
| 250 | +errmsg("value \"%s\" is out of range for type %s", |
| 251 | +s,"integer"))); |
| 252 | + |
| 253 | +invalid_syntax: |
| 254 | +ereport(ERROR, |
| 255 | +(errcode(ERRCODE_INVALID_TEXT_REPRESENTATION), |
| 256 | +errmsg("invalid input syntax for type %s: \"%s\"", |
| 257 | +"integer",s))); |
| 258 | +} |
| 259 | + |
111 | 260 | /*
|
112 | 261 | * pg_itoa: converts a signed 16-bit integer to its string representation
|
113 | 262 | *
|
|