Changeset 1113c9e in mainline for uspace/app/sbi/src/bigint.c


Ignore:
Timestamp:
2010-06-09T19:03:24Z (14 years ago)
Author:
Jiri Svoboda <jiri@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
8eec3c8
Parents:
8f80c77 (diff), c5cb943d (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

Merge from lp:~jsvoboda/helenos/sysel.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/app/sbi/src/bigint.c

    r8f80c77 r1113c9e  
    360360}
    361361
    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 */
     367void 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
    365372        bigint_t val, tmp;
    366373        bigint_word_t rem;
    367         size_t ndigits;
    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");
    373380#endif
    374381        assert(BIGINT_BASE >= 10);
    375382
    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
    386389        bigint_clone(bigint, &val);
    387390        while (bigint_is_zero(&val) != b_true) {
     
    390393                bigint_shallow_copy(&tmp, &val);
    391394
    392                 ndigits += 1;
     395                nchars += 1;
    393396        }
    394397        bigint_destroy(&val);
    395398
    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) {
    400403                printf("Memory allocation failed.\n");
    401404                exit(1);
    402405        }
    403406
    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;
    405414        bigint_clone(bigint, &val);
    406415        while (bigint_is_zero(&val) != b_true) {
     
    409418                bigint_shallow_copy(&tmp, &val);
    410419
    411                 digits[idx++] = (int) rem;
    412         }
     420                str[nchars - idx] = digits[(int) rem];
     421                ++idx;
     422        }
     423
    413424        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 */
     433void 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);
    419443}
    420444
Note: See TracChangeset for help on using the changeset viewer.