Changes in uspace/app/sbi/src/bigint.c [23de644:c5cb943d] in mainline
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/app/sbi/src/bigint.c
r23de644 rc5cb943d 360 360 } 361 361 362 /** Print bigint to standard output. */ 363 void bigint_print(bigint_t *bigint) 364 { 362 /** Convert bigint to string. 363 * 364 * @param bigint Bigint to convert. 365 * @param dptr Place to store pointer to new string. 366 */ 367 void bigint_get_as_string(bigint_t *bigint, char **dptr) 368 { 369 static const char digits[] = { '0', '1', '2', '3', '4', '5', '6', 370 '7', '8', '9' }; 371 365 372 bigint_t val, tmp; 366 373 bigint_word_t rem; 367 size_t n digits;368 int *digits;369 size_t idx; 370 371 #ifdef DEBUG_BIGINT_TRACE 372 printf(" Print bigint.\n");374 size_t nchars; 375 char *str; 376 size_t idx; 377 378 #ifdef DEBUG_BIGINT_TRACE 379 printf("Convert bigint to string.\n"); 373 380 #endif 374 381 assert(BIGINT_BASE >= 10); 375 382 376 if (bigint_is_zero(bigint)) { 377 putchar('0'); 378 return; 379 } 380 381 if (bigint->negative) 382 putchar('-'); 383 384 /* Compute number of digits. */ 385 ndigits = 0; 383 /* Compute number of characters. */ 384 nchars = 0; 385 386 if (bigint_is_zero(bigint) || bigint->negative) 387 nchars += 1; /* '0' or '-' */ 388 386 389 bigint_clone(bigint, &val); 387 390 while (bigint_is_zero(&val) != b_true) { … … 390 393 bigint_shallow_copy(&tmp, &val); 391 394 392 n digits += 1;395 nchars += 1; 393 396 } 394 397 bigint_destroy(&val); 395 398 396 /* Store digits to array. */397 398 digits = malloc(ndigits * sizeof(int));399 if ( digits== NULL) {399 /* Store characters to array. */ 400 401 str = malloc(nchars * sizeof(char) + 1); 402 if (str == NULL) { 400 403 printf("Memory allocation failed.\n"); 401 404 exit(1); 402 405 } 403 406 404 idx = 0; 407 if (bigint_is_zero(bigint)) { 408 str[0] = '0'; 409 } else if (bigint->negative) { 410 str[0] = '-'; 411 } 412 413 idx = 1; 405 414 bigint_clone(bigint, &val); 406 415 while (bigint_is_zero(&val) != b_true) { … … 409 418 bigint_shallow_copy(&tmp, &val); 410 419 411 digits[idx++] = (int) rem; 412 } 420 str[nchars - idx] = digits[(int) rem]; 421 ++idx; 422 } 423 413 424 bigint_destroy(&val); 414 415 for (idx = 0; idx < ndigits; ++idx) 416 printf("%u", digits[ndigits - 1 - idx]); 417 418 free(digits); 425 str[nchars] = '\0'; 426 *dptr = str; 427 } 428 429 /** Print bigint to standard output. 430 * 431 * @param bigint Bigint to print. 432 */ 433 void bigint_print(bigint_t *bigint) 434 { 435 char *str; 436 437 #ifdef DEBUG_BIGINT_TRACE 438 printf("Print bigint.\n"); 439 #endif 440 bigint_get_as_string(bigint, &str); 441 printf("%s", str); 442 free(str); 419 443 } 420 444
Note:
See TracChangeset
for help on using the changeset viewer.