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