| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
#include "mpdecimal.h" |
| 30 |
#include <stdio.h> |
| 31 |
#include <stdlib.h> |
| 32 |
#include <string.h> |
| 33 |
#include <ctype.h> |
| 34 |
#include <limits.h> |
| 35 |
#include <assert.h> |
| 36 |
#include <errno.h> |
| 37 |
#include <locale.h> |
| 38 |
#include "bits.h" |
| 39 |
#include "constants.h" |
| 40 |
#include "memory.h" |
| 41 |
#include "typearith.h" |
| 42 |
#include "io.h" |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
static inline int |
| 58 |
_mpd_strneq(const char *s, const char *l, const char *u, size_t n) |
| 59 |
{ |
| 60 |
while (--n != SIZE_MAX) { |
| 61 |
if (*s != *l && *s != *u) { |
| 62 |
return 0; |
| 63 |
} |
| 64 |
s++; u++; l++; |
| 65 |
} |
| 66 |
|
| 67 |
return 1; |
| 68 |
} |
| 69 |
|
| 70 |
static mpd_ssize_t |
| 71 |
strtoexp(const char *s) |
| 72 |
{ |
| 73 |
char *end; |
| 74 |
mpd_ssize_t retval; |
| 75 |
|
| 76 |
errno = 0; |
| 77 |
retval = mpd_strtossize(s, &end, 10); |
| 78 |
if (errno == 0 && !(*s != '\0' && *end == '\0')) |
| 79 |
errno = EINVAL; |
| 80 |
|
| 81 |
return retval; |
| 82 |
} |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
static void |
| 90 |
string_to_coeff(mpd_uint_t *data, const char *s, const char *dpoint, int r, |
| 91 |
size_t len) |
| 92 |
{ |
| 93 |
int j; |
| 94 |
|
| 95 |
if (r > 0) { |
| 96 |
data[--len] = 0; |
| 97 |
for (j = 0; j < r; j++, s++) { |
| 98 |
if (s == dpoint) s++; |
| 99 |
data[len] = 10 * data[len] + (*s - '0'); |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
while (--len != SIZE_MAX) { |
| 104 |
data[len] = 0; |
| 105 |
for (j = 0; j < MPD_RDIGITS; j++, s++) { |
| 106 |
if (s == dpoint) s++; |
| 107 |
data[len] = 10 * data[len] + (*s - '0'); |
| 108 |
} |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
static const char * |
| 131 |
scan_dpoint_exp(const char *s, const char **dpoint, const char **exp, |
| 132 |
const char **end) |
| 133 |
{ |
| 134 |
const char *coeff = NULL; |
| 135 |
|
| 136 |
*dpoint = NULL; |
| 137 |
*exp = NULL; |
| 138 |
for (; *s != '\0'; s++) { |
| 139 |
switch (*s) { |
| 140 |
case '.': |
| 141 |
if (*dpoint != NULL || *exp != NULL) |
| 142 |
return NULL; |
| 143 |
*dpoint = s; |
| 144 |
break; |
| 145 |
case 'E': case 'e': |
| 146 |
if (*exp != NULL) |
| 147 |
return NULL; |
| 148 |
*exp = s; |
| 149 |
if (*(s+1) == '+' || *(s+1) == '-') |
| 150 |
s++; |
| 151 |
break; |
| 152 |
default: |
| 153 |
if (!isdigit((uchar)*s)) |
| 154 |
return NULL; |
| 155 |
if (coeff == NULL && *exp == NULL) { |
| 156 |
if (*s == '0') { |
| 157 |
if (!isdigit((uchar)*(s+1))) |
| 158 |
if (!(*(s+1) == '.' && |
| 159 |
isdigit((uchar)*(s+2)))) |
| 160 |
coeff = s; |
| 161 |
} |
| 162 |
else { |
| 163 |
coeff = s; |
| 164 |
} |
| 165 |
} |
| 166 |
break; |
| 167 |
|
| 168 |
} |
| 169 |
} |
| 170 |
|
| 171 |
*end = s; |
| 172 |
return coeff; |
| 173 |
} |
| 174 |
|
| 175 |
|
| 176 |
static const char * |
| 177 |
scan_payload(const char *s, const char **end) |
| 178 |
{ |
| 179 |
const char *coeff; |
| 180 |
|
| 181 |
while (*s == '0') |
| 182 |
s++; |
| 183 |
coeff = s; |
| 184 |
|
| 185 |
while (isdigit((uchar)*s)) |
| 186 |
s++; |
| 187 |
*end = s; |
| 188 |
|
| 189 |
return (*s == '\0') ? coeff : NULL; |
| 190 |
} |
| 191 |
|
| 192 |
|
| 193 |
void |
| 194 |
mpd_qset_string(mpd_t *dec, const char *s, const mpd_context_t *ctx, |
| 195 |
uint32_t *status) |
| 196 |
{ |
| 197 |
mpd_ssize_t q, r, len; |
| 198 |
const char *coeff, *end; |
| 199 |
const char *dpoint = NULL, *exp = NULL; |
| 200 |
size_t digits; |
| 201 |
uint8_t sign = MPD_POS; |
| 202 |
|
| 203 |
mpd_set_flags(dec, 0); |
| 204 |
dec->len = 0; |
| 205 |
dec->exp = 0; |
| 206 |
|
| 207 |
|
| 208 |
if (*s == '+') { |
| 209 |
s++; |
| 210 |
} |
| 211 |
else if (*s == '-') { |
| 212 |
mpd_set_negative(dec); |
| 213 |
sign = MPD_NEG; |
| 214 |
s++; |
| 215 |
} |
| 216 |
|
| 217 |
if (_mpd_strneq(s, "nan", "NAN", 3)) { |
| 218 |
s += 3; |
| 219 |
mpd_setspecial(dec, sign, MPD_NAN); |
| 220 |
if (*s == '\0') |
| 221 |
return; |
| 222 |
|
| 223 |
if ((coeff = scan_payload(s, &end)) == NULL) |
| 224 |
goto conversion_error; |
| 225 |
|
| 226 |
if (*coeff == '\0') |
| 227 |
return; |
| 228 |
digits = end - coeff; |
| 229 |
|
| 230 |
if (digits > (size_t)(ctx->prec-ctx->clamp)) |
| 231 |
goto conversion_error; |
| 232 |
} |
| 233 |
else if (_mpd_strneq(s, "snan", "SNAN", 4)) { |
| 234 |
s += 4; |
| 235 |
mpd_setspecial(dec, sign, MPD_SNAN); |
| 236 |
if (*s == '\0') |
| 237 |
return; |
| 238 |
|
| 239 |
if ((coeff = scan_payload(s, &end)) == NULL) |
| 240 |
goto conversion_error; |
| 241 |
|
| 242 |
if (*coeff == '\0') |
| 243 |
return; |
| 244 |
digits = end - coeff; |
| 245 |
if (digits > (size_t)(ctx->prec-ctx->clamp)) |
| 246 |
goto conversion_error; |
| 247 |
} |
| 248 |
else if (_mpd_strneq(s, "inf", "INF", 3)) { |
| 249 |
s += 3; |
| 250 |
if (*s == '\0' || _mpd_strneq(s, "inity", "INITY", 6)) { |
| 251 |
|
| 252 |
mpd_setspecial(dec, sign, MPD_INF); |
| 253 |
return; |
| 254 |
} |
| 255 |
goto conversion_error; |
| 256 |
} |
| 257 |
else { |
| 258 |
|
| 259 |
if ((coeff = scan_dpoint_exp(s, &dpoint, &exp, &end)) == NULL) |
| 260 |
goto conversion_error; |
| 261 |
|
| 262 |
|
| 263 |
if (exp) { |
| 264 |
|
| 265 |
end = exp; exp++; |
| 266 |
dec->exp = strtoexp(exp); |
| 267 |
if (errno) { |
| 268 |
if (!(errno == ERANGE && |
| 269 |
(dec->exp == MPD_SSIZE_MAX || |
| 270 |
dec->exp == MPD_SSIZE_MIN))) |
| 271 |
goto conversion_error; |
| 272 |
} |
| 273 |
} |
| 274 |
|
| 275 |
digits = end - coeff; |
| 276 |
if (dpoint) { |
| 277 |
size_t fracdigits = end-dpoint-1; |
| 278 |
if (dpoint > coeff) digits--; |
| 279 |
|
| 280 |
if (fracdigits > MPD_MAX_PREC) { |
| 281 |
goto conversion_error; |
| 282 |
} |
| 283 |
if (dec->exp < MPD_SSIZE_MIN+(mpd_ssize_t)fracdigits) { |
| 284 |
dec->exp = MPD_SSIZE_MIN; |
| 285 |
} |
| 286 |
else { |
| 287 |
dec->exp -= (mpd_ssize_t)fracdigits; |
| 288 |
} |
| 289 |
} |
| 290 |
if (digits > MPD_MAX_PREC) { |
| 291 |
goto conversion_error; |
| 292 |
} |
| 293 |
if (dec->exp > MPD_EXP_INF) { |
| 294 |
dec->exp = MPD_EXP_INF; |
| 295 |
} |
| 296 |
if (dec->exp == MPD_SSIZE_MIN) { |
| 297 |
dec->exp = MPD_SSIZE_MIN+1; |
| 298 |
} |
| 299 |
} |
| 300 |
|
| 301 |
_mpd_idiv_word(&q, &r, (mpd_ssize_t)digits, MPD_RDIGITS); |
| 302 |
|
| 303 |
len = (r == 0) ? q : q+1; |
| 304 |
if (len == 0) { |
| 305 |
goto conversion_error; |
| 306 |
} |
| 307 |
if (!mpd_qresize(dec, len, status)) { |
| 308 |
mpd_seterror(dec, MPD_Malloc_error, status); |
| 309 |
return; |
| 310 |
} |
| 311 |
dec->len = len; |
| 312 |
|
| 313 |
string_to_coeff(dec->data, coeff, dpoint, (int)r, len); |
| 314 |
|
| 315 |
mpd_setdigits(dec); |
| 316 |
mpd_qfinalize(dec, ctx, status); |
| 317 |
return; |
| 318 |
|
| 319 |
conversion_error: |
| 320 |
|
| 321 |
mpd_seterror(dec, MPD_Conversion_syntax, status); |
| 322 |
} |
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
#define EXTRACT_DIGIT(s, x, d, dot) \ |
| 327 |
if (s == dot) *s++ = '.'; *s++ = '0' + (char)(x / d); x %= d |
| 328 |
static inline char * |
| 329 |
word_to_string(char *s, mpd_uint_t x, int n, char *dot) |
| 330 |
{ |
| 331 |
switch(n) { |
| 332 |
#ifdef CONFIG_64 |
| 333 |
case 20: EXTRACT_DIGIT(s, x, 10000000000000000000ULL, dot); |
| 334 |
case 19: EXTRACT_DIGIT(s, x, 1000000000000000000ULL, dot); |
| 335 |
case 18: EXTRACT_DIGIT(s, x, 100000000000000000ULL, dot); |
| 336 |
case 17: EXTRACT_DIGIT(s, x, 10000000000000000ULL, dot); |
| 337 |
case 16: EXTRACT_DIGIT(s, x, 1000000000000000ULL, dot); |
| 338 |
case 15: EXTRACT_DIGIT(s, x, 100000000000000ULL, dot); |
| 339 |
case 14: EXTRACT_DIGIT(s, x, 10000000000000ULL, dot); |
| 340 |
case 13: EXTRACT_DIGIT(s, x, 1000000000000ULL, dot); |
| 341 |
case 12: EXTRACT_DIGIT(s, x, 100000000000ULL, dot); |
| 342 |
case 11: EXTRACT_DIGIT(s, x, 10000000000ULL, dot); |
| 343 |
#endif |
| 344 |
case 10: EXTRACT_DIGIT(s, x, 1000000000UL, dot); |
| 345 |
case 9: EXTRACT_DIGIT(s, x, 100000000UL, dot); |
| 346 |
case 8: EXTRACT_DIGIT(s, x, 10000000UL, dot); |
| 347 |
case 7: EXTRACT_DIGIT(s, x, 1000000UL, dot); |
| 348 |
case 6: EXTRACT_DIGIT(s, x, 100000UL, dot); |
| 349 |
case 5: EXTRACT_DIGIT(s, x, 10000UL, dot); |
| 350 |
case 4: EXTRACT_DIGIT(s, x, 1000UL, dot); |
| 351 |
case 3: EXTRACT_DIGIT(s, x, 100UL, dot); |
| 352 |
case 2: EXTRACT_DIGIT(s, x, 10UL, dot); |
| 353 |
default: if (s == dot) *s++ = '.'; *s++ = '0' + (char)x; |
| 354 |
} |
| 355 |
|
| 356 |
*s = '\0'; |
| 357 |
return s; |
| 358 |
} |
| 359 |
|
| 360 |
|
| 361 |
static inline char * |
| 362 |
exp_to_string(char *s, mpd_ssize_t x) |
| 363 |
{ |
| 364 |
char sign = '+'; |
| 365 |
|
| 366 |
if (x < 0) { |
| 367 |
sign = '-'; |
| 368 |
x = -x; |
| 369 |
} |
| 370 |
*s++ = sign; |
| 371 |
|
| 372 |
return word_to_string(s, x, mpd_word_digits(x), NULL); |
| 373 |
} |
| 374 |
|
| 375 |
|
| 376 |
static inline char * |
| 377 |
coeff_to_string(char *s, const mpd_t *dec) |
| 378 |
{ |
| 379 |
mpd_uint_t x; |
| 380 |
mpd_ssize_t i; |
| 381 |
|
| 382 |
|
| 383 |
x = mpd_msword(dec); |
| 384 |
s = word_to_string(s, x, mpd_word_digits(x), NULL); |
| 385 |
|
| 386 |
|
| 387 |
for (i=dec->len-2; i >= 0; --i) { |
| 388 |
x = dec->data[i]; |
| 389 |
s = word_to_string(s, x, MPD_RDIGITS, NULL); |
| 390 |
} |
| 391 |
|
| 392 |
return s; |
| 393 |
} |
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
static inline char * |
| 398 |
coeff_to_string_dot(char *s, char *dot, const mpd_t *dec) |
| 399 |
{ |
| 400 |
mpd_uint_t x; |
| 401 |
mpd_ssize_t i; |
| 402 |
|
| 403 |
|
| 404 |
x = mpd_msword(dec); |
| 405 |
s = word_to_string(s, x, mpd_word_digits(x), dot); |
| 406 |
|
| 407 |
|
| 408 |
for (i=dec->len-2; i >= 0; --i) { |
| 409 |
x = dec->data[i]; |
| 410 |
s = word_to_string(s, x, MPD_RDIGITS, dot); |
| 411 |
} |
| 412 |
|
| 413 |
return s; |
| 414 |
} |
| 415 |
|
| 416 |
|
| 417 |
#define MPD_FMT_LOWER 0x00000000 |
| 418 |
#define MPD_FMT_UPPER 0x00000001 |
| 419 |
#define MPD_FMT_TOSCI 0x00000002 |
| 420 |
#define MPD_FMT_TOENG 0x00000004 |
| 421 |
#define MPD_FMT_EXP 0x00000008 |
| 422 |
#define MPD_FMT_FIXED 0x00000010 |
| 423 |
#define MPD_FMT_PERCENT 0x00000020 |
| 424 |
#define MPD_FMT_SIGN_SPACE 0x00000040 |
| 425 |
#define MPD_FMT_SIGN_PLUS 0x00000080 |
| 426 |
|
| 427 |
|
| 428 |
#define MPD_DEFAULT_DOTPLACE 1 |
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
static mpd_ssize_t |
| 441 |
_mpd_to_string(char **result, const mpd_t *dec, int flags, mpd_ssize_t dplace) |
| 442 |
{ |
| 443 |
char *decstring = NULL, *cp = NULL; |
| 444 |
mpd_ssize_t ldigits; |
| 445 |
mpd_ssize_t mem = 0, k; |
| 446 |
|
| 447 |
if (mpd_isspecial(dec)) { |
| 448 |
|
| 449 |
mem = sizeof "-Infinity%"; |
| 450 |
if (mpd_isnan(dec) && dec->len > 0) { |
| 451 |
|
| 452 |
mem += dec->digits; |
| 453 |
} |
| 454 |
cp = decstring = mpd_alloc(mem, sizeof *decstring); |
| 455 |
if (cp == NULL) { |
| 456 |
*result = NULL; |
| 457 |
return -1; |
| 458 |
} |
| 459 |
|
| 460 |
if (mpd_isnegative(dec)) { |
| 461 |
*cp++ = '-'; |
| 462 |
} |
| 463 |
else if (flags&MPD_FMT_SIGN_SPACE) { |
| 464 |
*cp++ = ' '; |
| 465 |
} |
| 466 |
else if (flags&MPD_FMT_SIGN_PLUS) { |
| 467 |
*cp++ = '+'; |
| 468 |
} |
| 469 |
|
| 470 |
if (mpd_isnan(dec)) { |
| 471 |
if (mpd_isqnan(dec)) { |
| 472 |
strcpy(cp, "NaN"); |
| 473 |
cp += 3; |
| 474 |
} |
| 475 |
else { |
| 476 |
strcpy(cp, "sNaN"); |
| 477 |
cp += 4; |
| 478 |
} |
| 479 |
if (dec->len > 0) { |
| 480 |
cp = coeff_to_string(cp, dec); |
| 481 |
} |
| 482 |
} |
| 483 |
else if (mpd_isinfinite(dec)) { |
| 484 |
strcpy(cp, "Infinity"); |
| 485 |
cp += 8; |
| 486 |
} |
| 487 |
else { |
| 488 |
abort(); |
| 489 |
} |
| 490 |
} |
| 491 |
else { |
| 492 |
assert(dec->len > 0); |
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
|
| 502 |
|
| 503 |
|
| 504 |
|
| 505 |
|
| 506 |
|
| 507 |
|
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
|
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
|
| 517 |
|
| 518 |
ldigits = dec->digits + dec->exp; |
| 519 |
|
| 520 |
if (flags&MPD_FMT_EXP) { |
| 521 |
; |
| 522 |
} |
| 523 |
else if (flags&MPD_FMT_FIXED || (dec->exp <= 0 && ldigits > -6)) { |
| 524 |
|
| 525 |
|
| 526 |
|
| 527 |
dplace = ldigits; |
| 528 |
} |
| 529 |
else if (flags&MPD_FMT_TOENG) { |
| 530 |
if (mpd_iszero(dec)) { |
| 531 |
|
| 532 |
|
| 533 |
|
| 534 |
dplace = -1 + mod_mpd_ssize_t(dec->exp+2, 3); |
| 535 |
} |
| 536 |
else { |
| 537 |
|
| 538 |
|
| 539 |
dplace += mod_mpd_ssize_t(ldigits-1, 3); |
| 540 |
} |
| 541 |
} |
| 542 |
|
| 543 |
|
| 544 |
|
| 545 |
|
| 546 |
|
| 547 |
|
| 548 |
|
| 549 |
|
| 550 |
|
| 551 |
if (dplace <= 0) { |
| 552 |
mem = -dplace + dec->digits + 2; |
| 553 |
} |
| 554 |
else if (dplace >= dec->digits) { |
| 555 |
mem = dplace; |
| 556 |
} |
| 557 |
else { |
| 558 |
mem = dec->digits; |
| 559 |
} |
| 560 |
mem += (MPD_EXPDIGITS+1+6); |
| 561 |
|
| 562 |
cp = decstring = mpd_alloc(mem, sizeof *decstring); |
| 563 |
if (cp == NULL) { |
| 564 |
*result = NULL; |
| 565 |
return -1; |
| 566 |
} |
| 567 |
|
| 568 |
|
| 569 |
if (mpd_isnegative(dec)) { |
| 570 |
*cp++ = '-'; |
| 571 |
} |
| 572 |
else if (flags&MPD_FMT_SIGN_SPACE) { |
| 573 |
*cp++ = ' '; |
| 574 |
} |
| 575 |
else if (flags&MPD_FMT_SIGN_PLUS) { |
| 576 |
*cp++ = '+'; |
| 577 |
} |
| 578 |
|
| 579 |
if (dplace <= 0) { |
| 580 |
|
| 581 |
*cp++ = '0'; |
| 582 |
*cp++ = '.'; |
| 583 |
for (k = 0; k < -dplace; k++) { |
| 584 |
*cp++ = '0'; |
| 585 |
} |
| 586 |
cp = coeff_to_string(cp, dec); |
| 587 |
} |
| 588 |
else if (dplace >= dec->digits) { |
| 589 |
|
| 590 |
cp = coeff_to_string(cp, dec); |
| 591 |
for (k = 0; k < dplace-dec->digits; k++) { |
| 592 |
*cp++ = '0'; |
| 593 |
} |
| 594 |
} |
| 595 |
else { |
| 596 |
|
| 597 |
cp = coeff_to_string_dot(cp, cp+dplace, dec); |
| 598 |
} |
| 599 |
|
| 600 |
|
| 601 |
|
| 602 |
|
| 603 |
|
| 604 |
|
| 605 |
|
| 606 |
|
| 607 |
if (ldigits != dplace || flags&MPD_FMT_EXP) { |
| 608 |
|
| 609 |
*cp++ = (flags&MPD_FMT_UPPER) ? 'E' : 'e'; |
| 610 |
cp = exp_to_string(cp, ldigits-dplace); |
| 611 |
} |
| 612 |
} |
| 613 |
|
| 614 |
if (flags&MPD_FMT_PERCENT) { |
| 615 |
*cp++ = '%'; |
| 616 |
} |
| 617 |
|
| 618 |
assert(cp < decstring+mem); |
| 619 |
assert(cp-decstring < MPD_SSIZE_MAX); |
| 620 |
|
| 621 |
*cp = '\0'; |
| 622 |
*result = decstring; |
| 623 |
return (mpd_ssize_t)(cp-decstring); |
| 624 |
} |
| 625 |
|
| 626 |
char * |
| 627 |
mpd_to_sci(const mpd_t *dec, int fmt) |
| 628 |
{ |
| 629 |
char *res; |
| 630 |
int flags = MPD_FMT_TOSCI; |
| 631 |
|
| 632 |
flags |= fmt ? MPD_FMT_UPPER : MPD_FMT_LOWER; |
| 633 |
(void)_mpd_to_string(&res, dec, flags, MPD_DEFAULT_DOTPLACE); |
| 634 |
return res; |
| 635 |
} |
| 636 |
|
| 637 |
char * |
| 638 |
mpd_to_eng(const mpd_t *dec, int fmt) |
| 639 |
{ |
| 640 |
char *res; |
| 641 |
int flags = MPD_FMT_TOENG; |
| 642 |
|
| 643 |
flags |= fmt ? MPD_FMT_UPPER : MPD_FMT_LOWER; |
| 644 |
(void)_mpd_to_string(&res, dec, flags, MPD_DEFAULT_DOTPLACE); |
| 645 |
return res; |
| 646 |
} |
| 647 |
|
| 648 |
mpd_ssize_t |
| 649 |
mpd_to_sci_size(char **res, const mpd_t *dec, int fmt) |
| 650 |
{ |
| 651 |
int flags = MPD_FMT_TOSCI; |
| 652 |
|
| 653 |
flags |= fmt ? MPD_FMT_UPPER : MPD_FMT_LOWER; |
| 654 |
return _mpd_to_string(res, dec, flags, MPD_DEFAULT_DOTPLACE); |
| 655 |
} |
| 656 |
|
| 657 |
mpd_ssize_t |
| 658 |
mpd_to_eng_size(char **res, const mpd_t *dec, int fmt) |
| 659 |
{ |
| 660 |
int flags = MPD_FMT_TOENG; |
| 661 |
|
| 662 |
flags |= fmt ? MPD_FMT_UPPER : MPD_FMT_LOWER; |
| 663 |
return _mpd_to_string(res, dec, flags, MPD_DEFAULT_DOTPLACE); |
| 664 |
} |
| 665 |
|
| 666 |
|
| 667 |
|
| 668 |
static int |
| 669 |
_mpd_copy_utf8(char dest[5], const char *s) |
| 670 |
{ |
| 671 |
const uchar *cp = (const uchar *)s; |
| 672 |
uchar lb, ub; |
| 673 |
int count, i; |
| 674 |
|
| 675 |
|
| 676 |
if (*cp == 0) { |
| 677 |
|
| 678 |
dest[0] = '\0'; |
| 679 |
return 0; |
| 680 |
} |
| 681 |
else if (*cp <= 0x7f) { |
| 682 |
|
| 683 |
dest[0] = *cp; |
| 684 |
dest[1] = '\0'; |
| 685 |
return 1; |
| 686 |
} |
| 687 |
else if (0xc2 <= *cp && *cp <= 0xdf) { |
| 688 |
lb = 0x80; ub = 0xbf; |
| 689 |
count = 2; |
| 690 |
} |
| 691 |
else if (*cp == 0xe0) { |
| 692 |
lb = 0xa0; ub = 0xbf; |
| 693 |
count = 3; |
| 694 |
} |
| 695 |
else if (*cp <= 0xec) { |
| 696 |
lb = 0x80; ub = 0xbf; |
| 697 |
count = 3; |
| 698 |
} |
| 699 |
else if (*cp == 0xed) { |
| 700 |
lb = 0x80; ub = 0x9f; |
| 701 |
count = 3; |
| 702 |
} |
| 703 |
else if (*cp <= 0xef) { |
| 704 |
lb = 0x80; ub = 0xbf; |
| 705 |
count = 3; |
| 706 |
} |
| 707 |
else if (*cp == 0xf0) { |
| 708 |
lb = 0x90; ub = 0xbf; |
| 709 |
count = 4; |
| 710 |
} |
| 711 |
else if (*cp <= 0xf3) { |
| 712 |
lb = 0x80; ub = 0xbf; |
| 713 |
count = 4; |
| 714 |
} |
| 715 |
else if (*cp == 0xf4) { |
| 716 |
lb = 0x80; ub = 0x8f; |
| 717 |
count = 4; |
| 718 |
} |
| 719 |
else { |
| 720 |
|
| 721 |
goto error; |
| 722 |
} |
| 723 |
|
| 724 |
dest[0] = *cp++; |
| 725 |
if (*cp < lb || ub < *cp) { |
| 726 |
goto error; |
| 727 |
} |
| 728 |
dest[1] = *cp++; |
| 729 |
for (i = 2; i < count; i++) { |
| 730 |
if (*cp < 0x80 || 0xbf < *cp) { |
| 731 |
goto error; |
| 732 |
} |
| 733 |
dest[i] = *cp++; |
| 734 |
} |
| 735 |
dest[i] = '\0'; |
| 736 |
|
| 737 |
return count; |
| 738 |
|
| 739 |
error: |
| 740 |
dest[0] = '\0'; |
| 741 |
return -1; |
| 742 |
} |
| 743 |
|
| 744 |
int |
| 745 |
mpd_validate_lconv(mpd_spec_t *spec) |
| 746 |
{ |
| 747 |
size_t n; |
| 748 |
#if CHAR_MAX == SCHAR_MAX |
| 749 |
const char *cp = spec->grouping; |
| 750 |
while (*cp != '\0') { |
| 751 |
if (*cp++ < 0) { |
| 752 |
return -1; |
| 753 |
} |
| 754 |
} |
| 755 |
#endif |
| 756 |
n = strlen(spec->dot); |
| 757 |
if (n == 0 || n > 4) { |
| 758 |
return -1; |
| 759 |
} |
| 760 |
if (strlen(spec->sep) > 4) { |
| 761 |
return -1; |
| 762 |
} |
| 763 |
|
| 764 |
return 0; |
| 765 |
} |
| 766 |
|
| 767 |
int |
| 768 |
mpd_parse_fmt_str(mpd_spec_t *spec, const char *fmt, int caps) |
| 769 |
{ |
| 770 |
char *cp = (char *)fmt; |
| 771 |
int have_align = 0, n; |
| 772 |
|
| 773 |
|
| 774 |
spec->min_width = 0; |
| 775 |
spec->prec = -1; |
| 776 |
spec->type = caps ? 'G' : 'g'; |
| 777 |
spec->align = '>'; |
| 778 |
spec->sign = '-'; |
| 779 |
spec->dot = ""; |
| 780 |
spec->sep = ""; |
| 781 |
spec->grouping = ""; |
| 782 |
|
| 783 |
|
| 784 |
|
| 785 |
if ((n = _mpd_copy_utf8(spec->fill, cp)) < 0) { |
| 786 |
return 0; |
| 787 |
} |
| 788 |
|
| 789 |
|
| 790 |
if (*cp && (*(cp+n) == '<' || *(cp+n) == '>' || |
| 791 |
*(cp+n) == '=' || *(cp+n) == '^')) { |
| 792 |
cp += n; |
| 793 |
spec->align = *cp++; |
| 794 |
have_align = 1; |
| 795 |
} |
| 796 |
else { |
| 797 |
|
| 798 |
spec->fill[0] = ' '; |
| 799 |
spec->fill[1] = '\0'; |
| 800 |
if (*cp == '<' || *cp == '>' || |
| 801 |
*cp == '=' || *cp == '^') { |
| 802 |
spec->align = *cp++; |
| 803 |
have_align = 1; |
| 804 |
} |
| 805 |
} |
| 806 |
|
| 807 |
|
| 808 |
if (*cp == '+' || *cp == '-' || *cp == ' ') { |
| 809 |
spec->sign = *cp++; |
| 810 |
} |
| 811 |
|
| 812 |
|
| 813 |
if (*cp == '0') { |
| 814 |
|
| 815 |
|
| 816 |
if (have_align) { |
| 817 |
return 0; |
| 818 |
} |
| 819 |
spec->align = 'z'; |
| 820 |
spec->fill[0] = *cp++; |
| 821 |
spec->fill[1] = '\0'; |
| 822 |
} |
| 823 |
|
| 824 |
|
| 825 |
if (isdigit((uchar)*cp)) { |
| 826 |
if (*cp == '0') { |
| 827 |
return 0; |
| 828 |
} |
| 829 |
errno = 0; |
| 830 |
spec->min_width = mpd_strtossize(cp, &cp, 10); |
| 831 |
if (errno == ERANGE || errno == EINVAL) { |
| 832 |
return 0; |
| 833 |
} |
| 834 |
} |
| 835 |
|
| 836 |
|
| 837 |
if (*cp == ',') { |
| 838 |
spec->dot = "."; |
| 839 |
spec->sep = ","; |
| 840 |
spec->grouping = "\003\003"; |
| 841 |
cp++; |
| 842 |
} |
| 843 |
|
| 844 |
|
| 845 |
if (*cp == '.') { |
| 846 |
cp++; |
| 847 |
if (!isdigit((uchar)*cp)) { |
| 848 |
return 0; |
| 849 |
} |
| 850 |
errno = 0; |
| 851 |
spec->prec = mpd_strtossize(cp, &cp, 10); |
| 852 |
if (errno == ERANGE || errno == EINVAL) { |
| 853 |
return 0; |
| 854 |
} |
| 855 |
} |
| 856 |
|
| 857 |
|
| 858 |
if (*cp == 'E' || *cp == 'e' || *cp == 'F' || *cp == 'f' || |
| 859 |
*cp == 'G' || *cp == 'g' || *cp == '%') { |
| 860 |
spec->type = *cp++; |
| 861 |
} |
| 862 |
else if (*cp == 'N' || *cp == 'n') { |
| 863 |
|
| 864 |
struct lconv *lc; |
| 865 |
|
| 866 |
if (*spec->sep) { |
| 867 |
return 0; |
| 868 |
} |
| 869 |
spec->type = *cp++; |
| 870 |
spec->type = (spec->type == 'N') ? 'G' : 'g'; |
| 871 |
lc = localeconv(); |
| 872 |
spec->dot = lc->decimal_point; |
| 873 |
spec->sep = lc->thousands_sep; |
| 874 |
spec->grouping = lc->grouping; |
| 875 |
if (mpd_validate_lconv(spec) < 0) { |
| 876 |
return 0; |
| 877 |
} |
| 878 |
} |
| 879 |
|
| 880 |
|
| 881 |
if (*cp != '\0') { |
| 882 |
return 0; |
| 883 |
} |
| 884 |
|
| 885 |
return 1; |
| 886 |
} |
| 887 |
|
| 888 |
|
| 889 |
|
| 890 |
|
| 891 |
|
| 892 |
|
| 893 |
|
| 894 |
|
| 895 |
|
| 896 |
typedef struct { |
| 897 |
mpd_ssize_t nbytes; |
| 898 |
mpd_ssize_t nchars; |
| 899 |
mpd_ssize_t cur; |
| 900 |
char *data; |
| 901 |
} mpd_mbstr_t; |
| 902 |
|
| 903 |
static inline void |
| 904 |
_mpd_bcopy(char *dest, const char *src, mpd_ssize_t n) |
| 905 |
{ |
| 906 |
while (--n >= 0) { |
| 907 |
dest[n] = src[n]; |
| 908 |
} |
| 909 |
} |
| 910 |
|
| 911 |
static inline void |
| 912 |
_mbstr_copy_char(mpd_mbstr_t *dest, const char *src, mpd_ssize_t n) |
| 913 |
{ |
| 914 |
dest->nbytes += n; |
| 915 |
dest->nchars += (n > 0 ? 1 : 0); |
| 916 |
dest->cur -= n; |
| 917 |
|
| 918 |
if (dest->data != NULL) { |
| 919 |
_mpd_bcopy(dest->data+dest->cur, src, n); |
| 920 |
} |
| 921 |
} |
| 922 |
|
| 923 |
static inline void |
| 924 |
_mbstr_copy_ascii(mpd_mbstr_t *dest, const char *src, mpd_ssize_t n) |
| 925 |
{ |
| 926 |
dest->nbytes += n; |
| 927 |
dest->nchars += n; |
| 928 |
dest->cur -= n; |
| 929 |
|
| 930 |
if (dest->data != NULL) { |
| 931 |
_mpd_bcopy(dest->data+dest->cur, src, n); |
| 932 |
} |
| 933 |
} |
| 934 |
|
| 935 |
static inline void |
| 936 |
_mbstr_copy_pad(mpd_mbstr_t *dest, mpd_ssize_t n) |
| 937 |
{ |
| 938 |
dest->nbytes += n; |
| 939 |
dest->nchars += n; |
| 940 |
dest->cur -= n; |
| 941 |
|
| 942 |
if (dest->data != NULL) { |
| 943 |
char *cp = dest->data + dest->cur; |
| 944 |
while (--n >= 0) { |
| 945 |
cp[n] = '0'; |
| 946 |
} |
| 947 |
} |
| 948 |
} |
| 949 |
|
| 950 |
|
| 951 |
|
| 952 |
|
| 953 |
|
| 954 |
|
| 955 |
|
| 956 |
|
| 957 |
|
| 958 |
|
| 959 |
|
| 960 |
|
| 961 |
|
| 962 |
|
| 963 |
|
| 964 |
|
| 965 |
static void |
| 966 |
_mpd_add_sep_dot(mpd_mbstr_t *dest, |
| 967 |
const char *sign, |
| 968 |
const char *src, mpd_ssize_t n_src, |
| 969 |
const char *dot, |
| 970 |
const char *rest, mpd_ssize_t n_rest, |
| 971 |
const mpd_spec_t *spec) |
| 972 |
{ |
| 973 |
mpd_ssize_t n_sep, n_sign, consume; |
| 974 |
const char *g; |
| 975 |
int pad = 0; |
| 976 |
|
| 977 |
n_sign = sign ? 1 : 0; |
| 978 |
n_sep = (mpd_ssize_t)strlen(spec->sep); |
| 979 |
|
| 980 |
|
| 981 |
dest->cur = dest->nbytes; |
| 982 |
dest->nbytes = dest->nchars = 0; |
| 983 |
|
| 984 |
_mbstr_copy_ascii(dest, rest, n_rest); |
| 985 |
|
| 986 |
if (dot) { |
| 987 |
_mbstr_copy_char(dest, dot, (mpd_ssize_t)strlen(dot)); |
| 988 |
} |
| 989 |
|
| 990 |
g = spec->grouping; |
| 991 |
consume = *g; |
| 992 |
while (1) { |
| 993 |
|
| 994 |
|
| 995 |
if (*g == 0 || *g == CHAR_MAX || consume > n_src) { |
| 996 |
consume = n_src; |
| 997 |
} |
| 998 |
n_src -= consume; |
| 999 |
if (pad) { |
| 1000 |
_mbstr_copy_pad(dest, consume); |
| 1001 |
} |
| 1002 |
else { |
| 1003 |
_mbstr_copy_ascii(dest, src+n_src, consume); |
| 1004 |
} |
| 1005 |
|
| 1006 |
if (n_src == 0) { |
| 1007 |
|
| 1008 |
|
| 1009 |
if (spec->align == 'z' && |
| 1010 |
dest->nchars + n_sign < spec->min_width) { |
| 1011 |
|
| 1012 |
|
| 1013 |
n_src = spec->min_width - (dest->nchars + n_sign); |
| 1014 |
|
| 1015 |
|
| 1016 |
|
| 1017 |
|
| 1018 |
|
| 1019 |
|
| 1020 |
|
| 1021 |
consume = *g - consume; |
| 1022 |
|
| 1023 |
pad = 1; |
| 1024 |
continue; |
| 1025 |
} |
| 1026 |
break; |
| 1027 |
} |
| 1028 |
|
| 1029 |
if (n_sep > 0) { |
| 1030 |
|
| 1031 |
|
| 1032 |
|
| 1033 |
|
| 1034 |
if (pad && n_src > 1) n_src -= 1; |
| 1035 |
_mbstr_copy_char(dest, spec->sep, n_sep); |
| 1036 |
} |
| 1037 |
|
| 1038 |
|
| 1039 |
if (*g && *(g+1)) g++; |
| 1040 |
consume = *g; |
| 1041 |
} |
| 1042 |
|
| 1043 |
if (sign) { |
| 1044 |
_mbstr_copy_ascii(dest, sign, 1); |
| 1045 |
} |
| 1046 |
|
| 1047 |
if (dest->data) { |
| 1048 |
dest->data[dest->nbytes] = '\0'; |
| 1049 |
} |
| 1050 |
} |
| 1051 |
|
| 1052 |
|
| 1053 |
|
| 1054 |
|
| 1055 |
|
| 1056 |
|
| 1057 |
|
| 1058 |
|
| 1059 |
|
| 1060 |
|
| 1061 |
|
| 1062 |
|
| 1063 |
|
| 1064 |
|
| 1065 |
|
| 1066 |
|
| 1067 |
|
| 1068 |
|
| 1069 |
|
| 1070 |
|
| 1071 |
static int |
| 1072 |
_mpd_apply_lconv(mpd_mbstr_t *result, const mpd_spec_t *spec, uint32_t *status) |
| 1073 |
{ |
| 1074 |
const char *sign = NULL, *intpart = NULL, *dot = NULL; |
| 1075 |
const char *rest, *dp; |
| 1076 |
char *decstring; |
| 1077 |
mpd_ssize_t n_int, n_rest; |
| 1078 |
|
| 1079 |
|
| 1080 |
dp = result->data; |
| 1081 |
|
| 1082 |
|
| 1083 |
if (*dp == '+' || *dp == '-' || *dp == ' ') { |
| 1084 |
sign = dp++; |
| 1085 |
} |
| 1086 |
|
| 1087 |
assert(isdigit((uchar)*dp)); |
| 1088 |
intpart = dp++; |
| 1089 |
while (isdigit((uchar)*dp)) { |
| 1090 |
dp++; |
| 1091 |
} |
| 1092 |
n_int = (mpd_ssize_t)(dp-intpart); |
| 1093 |
|
| 1094 |
if (*dp == '.') { |
| 1095 |
dp++; dot = spec->dot; |
| 1096 |
} |
| 1097 |
|
| 1098 |
rest = dp; |
| 1099 |
n_rest = result->nbytes - (mpd_ssize_t)(dp-result->data); |
| 1100 |
|
| 1101 |
if (dot == NULL && (*spec->sep == '\0' || *spec->grouping == '\0')) { |
| 1102 |
|
| 1103 |
return 1; |
| 1104 |
} |
| 1105 |
|
| 1106 |
|
| 1107 |
|
| 1108 |
decstring = result->data; |
| 1109 |
result->data = NULL; |
| 1110 |
_mpd_add_sep_dot(result, sign, intpart, n_int, dot, |
| 1111 |
rest, n_rest, spec); |
| 1112 |
|
| 1113 |
result->data = mpd_alloc(result->nbytes+1, 1); |
| 1114 |
if (result->data == NULL) { |
| 1115 |
*status |= MPD_Malloc_error; |
| 1116 |
mpd_free(decstring); |
| 1117 |
return 0; |
| 1118 |
} |
| 1119 |
|
| 1120 |
|
| 1121 |
_mpd_add_sep_dot(result, sign, intpart, n_int, dot, |
| 1122 |
rest, n_rest, spec); |
| 1123 |
|
| 1124 |
mpd_free(decstring); |
| 1125 |
return 1; |
| 1126 |
} |
| 1127 |
|
| 1128 |
|
| 1129 |
static int |
| 1130 |
_mpd_add_pad(mpd_mbstr_t *result, const mpd_spec_t *spec, uint32_t *status) |
| 1131 |
{ |
| 1132 |
if (result->nchars < spec->min_width) { |
| 1133 |
mpd_ssize_t add_chars, add_bytes; |
| 1134 |
size_t lpad = 0, rpad = 0; |
| 1135 |
size_t n_fill, len, i, j; |
| 1136 |
char align = spec->align; |
| 1137 |
uint8_t err = 0; |
| 1138 |
char *cp; |
| 1139 |
|
| 1140 |
n_fill = strlen(spec->fill); |
| 1141 |
add_chars = (spec->min_width - result->nchars); |
| 1142 |
|
| 1143 |
add_bytes = add_chars * (mpd_ssize_t)n_fill; |
| 1144 |
|
| 1145 |
cp = result->data = mpd_realloc(result->data, |
| 1146 |
result->nbytes+add_bytes+1, |
| 1147 |
sizeof *result->data, &err); |
| 1148 |
if (err) { |
| 1149 |
*status |= MPD_Malloc_error; |
| 1150 |
mpd_free(result->data); |
| 1151 |
return 0; |
| 1152 |
} |
| 1153 |
|
| 1154 |
if (align == 'z') { |
| 1155 |
align = '='; |
| 1156 |
} |
| 1157 |
|
| 1158 |
if (align == '<') { |
| 1159 |
rpad = add_chars; |
| 1160 |
} |
| 1161 |
else if (align == '>' || align == '=') { |
| 1162 |
lpad = add_chars; |
| 1163 |
} |
| 1164 |
else { |
| 1165 |
lpad = add_chars/2; |
| 1166 |
rpad = add_chars-lpad; |
| 1167 |
} |
| 1168 |
|
| 1169 |
len = result->nbytes; |
| 1170 |
if (align == '=' && (*cp == '-' || *cp == '+' || *cp == ' ')) { |
| 1171 |
|
| 1172 |
cp++; len--; |
| 1173 |
} |
| 1174 |
|
| 1175 |
memmove(cp+n_fill*lpad, cp, len); |
| 1176 |
for (i = 0; i < lpad; i++) { |
| 1177 |
for (j = 0; j < n_fill; j++) { |
| 1178 |
cp[i*n_fill+j] = spec->fill[j]; |
| 1179 |
} |
| 1180 |
} |
| 1181 |
cp += (n_fill*lpad + len); |
| 1182 |
for (i = 0; i < rpad; i++) { |
| 1183 |
for (j = 0; j < n_fill; j++) { |
| 1184 |
cp[i*n_fill+j] = spec->fill[j]; |
| 1185 |
} |
| 1186 |
} |
| 1187 |
|
| 1188 |
result->nbytes += add_bytes; |
| 1189 |
result->nchars += add_chars; |
| 1190 |
result->data[result->nbytes] = '\0'; |
| 1191 |
} |
| 1192 |
|
| 1193 |
return 1; |
| 1194 |
} |
| 1195 |
|
| 1196 |
|
| 1197 |
|
| 1198 |
|
| 1199 |
|
| 1200 |
static inline void |
| 1201 |
_mpd_round(mpd_t *result, const mpd_t *a, mpd_ssize_t prec, |
| 1202 |
const mpd_context_t *ctx, uint32_t *status) |
| 1203 |
{ |
| 1204 |
mpd_ssize_t exp = a->exp + a->digits - prec; |
| 1205 |
|
| 1206 |
if (prec <= 0) { |
| 1207 |
mpd_seterror(result, MPD_Invalid_operation, status); |
| 1208 |
return; |
| 1209 |
} |
| 1210 |
if (mpd_isspecial(a) || mpd_iszero(a)) { |
| 1211 |
mpd_qcopy(result, a, status); |
| 1212 |
return; |
| 1213 |
} |
| 1214 |
|
| 1215 |
mpd_qrescale_fmt(result, a, exp, ctx, status); |
| 1216 |
if (result->digits > prec) { |
| 1217 |
mpd_qrescale_fmt(result, result, exp+1, ctx, status); |
| 1218 |
} |
| 1219 |
} |
| 1220 |
|
| 1221 |
|
| 1222 |
|
| 1223 |
|
| 1224 |
|
| 1225 |
|
| 1226 |
char * |
| 1227 |
mpd_qformat_spec(const mpd_t *dec, const mpd_spec_t *spec, |
| 1228 |
const mpd_context_t *ctx, uint32_t *status) |
| 1229 |
{ |
| 1230 |
mpd_uint_t dt[MPD_MINALLOC_MAX]; |
| 1231 |
mpd_t tmp = {MPD_STATIC|MPD_STATIC_DATA,0,0,0,MPD_MINALLOC_MAX,dt}; |
| 1232 |
mpd_ssize_t dplace = MPD_DEFAULT_DOTPLACE; |
| 1233 |
mpd_mbstr_t result; |
| 1234 |
mpd_spec_t stackspec; |
| 1235 |
char type = spec->type; |
| 1236 |
int flags = 0; |
| 1237 |
|
| 1238 |
|
| 1239 |
if (spec->min_width > MPD_MAX_PREC) { |
| 1240 |
*status |= MPD_Invalid_operation; |
| 1241 |
return NULL; |
| 1242 |
} |
| 1243 |
|
| 1244 |
if (isupper((uchar)type)) { |
| 1245 |
type = tolower((uchar)type); |
| 1246 |
flags |= MPD_FMT_UPPER; |
| 1247 |
} |
| 1248 |
if (spec->sign == ' ') { |
| 1249 |
flags |= MPD_FMT_SIGN_SPACE; |
| 1250 |
} |
| 1251 |
else if (spec->sign == '+') { |
| 1252 |
flags |= MPD_FMT_SIGN_PLUS; |
| 1253 |
} |
| 1254 |
|
| 1255 |
if (mpd_isspecial(dec)) { |
| 1256 |
if (spec->align == 'z') { |
| 1257 |
stackspec = *spec; |
| 1258 |
stackspec.fill[0] = ' '; |
| 1259 |
stackspec.fill[1] = '\0'; |
| 1260 |
stackspec.align = '>'; |
| 1261 |
spec = &stackspec; |
| 1262 |
} |
| 1263 |
if (type == '%') { |
| 1264 |
flags |= MPD_FMT_PERCENT; |
| 1265 |
} |
| 1266 |
} |
| 1267 |
else { |
| 1268 |
uint32_t workstatus = 0; |
| 1269 |
mpd_ssize_t prec; |
| 1270 |
|
| 1271 |
switch (type) { |
| 1272 |
case 'g': flags |= MPD_FMT_TOSCI; break; |
| 1273 |
case 'e': flags |= MPD_FMT_EXP; break; |
| 1274 |
case '%': flags |= MPD_FMT_PERCENT; |
| 1275 |
if (!mpd_qcopy(&tmp, dec, status)) { |
| 1276 |
return NULL; |
| 1277 |
} |
| 1278 |
tmp.exp += 2; |
| 1279 |
dec = &tmp; |
| 1280 |
type = 'f'; |
| 1281 |
case 'f': flags |= MPD_FMT_FIXED; break; |
| 1282 |
default: abort(); |
| 1283 |
} |
| 1284 |
|
| 1285 |
if (spec->prec >= 0) { |
| 1286 |
if (spec->prec > MPD_MAX_PREC) { |
| 1287 |
*status |= MPD_Invalid_operation; |
| 1288 |
goto error; |
| 1289 |
} |
| 1290 |
|
| 1291 |
switch (type) { |
| 1292 |
case 'g': |
| 1293 |
prec = (spec->prec == 0) ? 1 : spec->prec; |
| 1294 |
if (dec->digits > prec) { |
| 1295 |
_mpd_round(&tmp, dec, prec, ctx, |
| 1296 |
&workstatus); |
| 1297 |
dec = &tmp; |
| 1298 |
} |
| 1299 |
break; |
| 1300 |
case 'e': |
| 1301 |
if (mpd_iszero(dec)) { |
| 1302 |
dplace = 1-spec->prec; |
| 1303 |
} |
| 1304 |
else { |
| 1305 |
_mpd_round(&tmp, dec, spec->prec+1, ctx, |
| 1306 |
&workstatus); |
| 1307 |
dec = &tmp; |
| 1308 |
} |
| 1309 |
break; |
| 1310 |
case 'f': |
| 1311 |
mpd_qrescale(&tmp, dec, -spec->prec, ctx, |
| 1312 |
&workstatus); |
| 1313 |
dec = &tmp; |
| 1314 |
break; |
| 1315 |
} |
| 1316 |
} |
| 1317 |
|
| 1318 |
if (type == 'f') { |
| 1319 |
if (mpd_iszero(dec) && dec->exp > 0) { |
| 1320 |
mpd_qrescale(&tmp, dec, 0, ctx, &workstatus); |
| 1321 |
dec = &tmp; |
| 1322 |
} |
| 1323 |
} |
| 1324 |
|
| 1325 |
if (workstatus&MPD_Errors) { |
| 1326 |
*status |= (workstatus&MPD_Errors); |
| 1327 |
goto error; |
| 1328 |
} |
| 1329 |
} |
| 1330 |
|
| 1331 |
|
| 1332 |
|
| 1333 |
|
| 1334 |
|
| 1335 |
|
| 1336 |
|
| 1337 |
|
| 1338 |
|
| 1339 |
|
| 1340 |
|
| 1341 |
|
| 1342 |
|
| 1343 |
result.nbytes = _mpd_to_string(&result.data, dec, flags, dplace); |
| 1344 |
result.nchars = result.nbytes; |
| 1345 |
if (result.nbytes < 0) { |
| 1346 |
*status |= MPD_Malloc_error; |
| 1347 |
goto error; |
| 1348 |
} |
| 1349 |
|
| 1350 |
if (*spec->dot != '\0' && !mpd_isspecial(dec)) { |
| 1351 |
if (result.nchars > MPD_MAX_PREC+36) { |
| 1352 |
|
| 1353 |
|
| 1354 |
|
| 1355 |
*status |= MPD_Invalid_operation; |
| 1356 |
mpd_free(result.data); |
| 1357 |
goto error; |
| 1358 |
} |
| 1359 |
if (!_mpd_apply_lconv(&result, spec, status)) { |
| 1360 |
goto error; |
| 1361 |
} |
| 1362 |
} |
| 1363 |
|
| 1364 |
if (spec->min_width) { |
| 1365 |
if (!_mpd_add_pad(&result, spec, status)) { |
| 1366 |
goto error; |
| 1367 |
} |
| 1368 |
} |
| 1369 |
|
| 1370 |
mpd_del(&tmp); |
| 1371 |
return result.data; |
| 1372 |
|
| 1373 |
error: |
| 1374 |
mpd_del(&tmp); |
| 1375 |
return NULL; |
| 1376 |
} |
| 1377 |
|
| 1378 |
char * |
| 1379 |
mpd_qformat(const mpd_t *dec, const char *fmt, const mpd_context_t *ctx, |
| 1380 |
uint32_t *status) |
| 1381 |
{ |
| 1382 |
mpd_spec_t spec; |
| 1383 |
|
| 1384 |
if (!mpd_parse_fmt_str(&spec, fmt, 1)) { |
| 1385 |
*status |= MPD_Invalid_operation; |
| 1386 |
return NULL; |
| 1387 |
} |
| 1388 |
|
| 1389 |
return mpd_qformat_spec(dec, &spec, ctx, status); |
| 1390 |
} |
| 1391 |
|
| 1392 |
|
| 1393 |
|
| 1394 |
|
| 1395 |
|
| 1396 |
|
| 1397 |
|
| 1398 |
|
| 1399 |
|
| 1400 |
|
| 1401 |
|
| 1402 |
|
| 1403 |
|
| 1404 |
|
| 1405 |
|
| 1406 |
static const char *mpd_flag_string[MPD_NUM_FLAGS] = { |
| 1407 |
"Clamped", |
| 1408 |
"Conversion_syntax", |
| 1409 |
"Division_by_zero", |
| 1410 |
"Division_impossible", |
| 1411 |
"Division_undefined", |
| 1412 |
"Fpu_error", |
| 1413 |
"Inexact", |
| 1414 |
"Invalid_context", |
| 1415 |
"Invalid_operation", |
| 1416 |
"Malloc_error", |
| 1417 |
"Not_implemented", |
| 1418 |
"Overflow", |
| 1419 |
"Rounded", |
| 1420 |
"Subnormal", |
| 1421 |
"Underflow", |
| 1422 |
}; |
| 1423 |
|
| 1424 |
static const char *mpd_signal_string[MPD_NUM_FLAGS] = { |
| 1425 |
"Clamped", |
| 1426 |
"IEEE_Invalid_operation", |
| 1427 |
"Division_by_zero", |
| 1428 |
"IEEE_Invalid_operation", |
| 1429 |
"IEEE_Invalid_operation", |
| 1430 |
"IEEE_Invalid_operation", |
| 1431 |
"Inexact", |
| 1432 |
"IEEE_Invalid_operation", |
| 1433 |
"IEEE_Invalid_operation", |
| 1434 |
"IEEE_Invalid_operation", |
| 1435 |
"Not_implemented", |
| 1436 |
"Overflow", |
| 1437 |
"Rounded", |
| 1438 |
"Subnormal", |
| 1439 |
"Underflow", |
| 1440 |
}; |
| 1441 |
|
| 1442 |
|
| 1443 |
int |
| 1444 |
mpd_snprint_flags(char *dest, int nmemb, uint32_t flags) |
| 1445 |
{ |
| 1446 |
char *cp; |
| 1447 |
int n, j; |
| 1448 |
|
| 1449 |
assert(nmemb >= MPD_MAX_FLAG_STRING); |
| 1450 |
|
| 1451 |
*dest = '\0'; cp = dest; |
| 1452 |
for (j = 0; j < MPD_NUM_FLAGS; j++) { |
| 1453 |
if (flags & (1U<<j)) { |
| 1454 |
n = snprintf(cp, nmemb, "%s ", mpd_flag_string[j]); |
| 1455 |
if (n < 0 || n >= nmemb) return -1; |
| 1456 |
cp += n; nmemb -= n; |
| 1457 |
} |
| 1458 |
} |
| 1459 |
|
| 1460 |
if (cp != dest) { |
| 1461 |
*(--cp) = '\0'; |
| 1462 |
} |
| 1463 |
|
| 1464 |
return (int)(cp-dest); |
| 1465 |
} |
| 1466 |
|
| 1467 |
|
| 1468 |
int |
| 1469 |
mpd_lsnprint_flags(char *dest, int nmemb, uint32_t flags, const char *flag_string[]) |
| 1470 |
{ |
| 1471 |
char *cp; |
| 1472 |
int n, j; |
| 1473 |
|
| 1474 |
assert(nmemb >= MPD_MAX_FLAG_LIST); |
| 1475 |
if (flag_string == NULL) { |
| 1476 |
flag_string = mpd_flag_string; |
| 1477 |
} |
| 1478 |
|
| 1479 |
*dest = '['; |
| 1480 |
*(dest+1) = '\0'; |
| 1481 |
cp = dest+1; |
| 1482 |
--nmemb; |
| 1483 |
|
| 1484 |
for (j = 0; j < MPD_NUM_FLAGS; j++) { |
| 1485 |
if (flags & (1U<<j)) { |
| 1486 |
n = snprintf(cp, nmemb, "%s, ", flag_string[j]); |
| 1487 |
if (n < 0 || n >= nmemb) return -1; |
| 1488 |
cp += n; nmemb -= n; |
| 1489 |
} |
| 1490 |
} |
| 1491 |
|
| 1492 |
|
| 1493 |
if (cp != dest+1) { |
| 1494 |
cp -= 2; |
| 1495 |
} |
| 1496 |
|
| 1497 |
*cp++ = ']'; |
| 1498 |
*cp = '\0'; |
| 1499 |
|
| 1500 |
return (int)(cp-dest); |
| 1501 |
} |
| 1502 |
|
| 1503 |
|
| 1504 |
int |
| 1505 |
mpd_lsnprint_signals(char *dest, int nmemb, uint32_t flags, const char *signal_string[]) |
| 1506 |
{ |
| 1507 |
char *cp; |
| 1508 |
int n, j; |
| 1509 |
int ieee_invalid_done = 0; |
| 1510 |
|
| 1511 |
assert(nmemb >= MPD_MAX_SIGNAL_LIST); |
| 1512 |
if (signal_string == NULL) { |
| 1513 |
signal_string = mpd_signal_string; |
| 1514 |
} |
| 1515 |
|
| 1516 |
*dest = '['; |
| 1517 |
*(dest+1) = '\0'; |
| 1518 |
cp = dest+1; |
| 1519 |
--nmemb; |
| 1520 |
|
| 1521 |
for (j = 0; j < MPD_NUM_FLAGS; j++) { |
| 1522 |
uint32_t f = flags & (1U<<j); |
| 1523 |
if (f) { |
| 1524 |
if (f&MPD_IEEE_Invalid_operation) { |
| 1525 |
if (ieee_invalid_done) { |
| 1526 |
continue; |
| 1527 |
} |
| 1528 |
ieee_invalid_done = 1; |
| 1529 |
} |
| 1530 |
n = snprintf(cp, nmemb, "%s, ", signal_string[j]); |
| 1531 |
if (n < 0 || n >= nmemb) return -1; |
| 1532 |
cp += n; nmemb -= n; |
| 1533 |
} |
| 1534 |
} |
| 1535 |
|
| 1536 |
|
| 1537 |
if (cp != dest+1) { |
| 1538 |
cp -= 2; |
| 1539 |
} |
| 1540 |
|
| 1541 |
*cp++ = ']'; |
| 1542 |
*cp = '\0'; |
| 1543 |
|
| 1544 |
return (int)(cp-dest); |
| 1545 |
} |
| 1546 |
|
| 1547 |
|
| 1548 |
void |
| 1549 |
mpd_fprint(FILE *file, const mpd_t *dec) |
| 1550 |
{ |
| 1551 |
char *decstring; |
| 1552 |
|
| 1553 |
decstring = mpd_to_sci(dec, 1); |
| 1554 |
if (decstring != NULL) { |
| 1555 |
fprintf(file, "%s\n", decstring); |
| 1556 |
mpd_free(decstring); |
| 1557 |
} |
| 1558 |
else { |
| 1559 |
fputs("mpd_fprint: output error\n", file); |
| 1560 |
} |
| 1561 |
} |
| 1562 |
|
| 1563 |
void |
| 1564 |
mpd_print(const mpd_t *dec) |
| 1565 |
{ |
| 1566 |
char *decstring; |
| 1567 |
|
| 1568 |
decstring = mpd_to_sci(dec, 1); |
| 1569 |
if (decstring != NULL) { |
| 1570 |
printf("%s\n", decstring); |
| 1571 |
mpd_free(decstring); |
| 1572 |
} |
| 1573 |
else { |
| 1574 |
fputs("mpd_fprint: output error\n", stderr); |
| 1575 |
} |
| 1576 |
} |
| 1577 |
|
| 1578 |
|