~pythonregexp2.7/python/issue2636-06

« back to all changes in this revision

Viewing changes to Objects/stringlib/formatter.h

  • Committer: Jeffrey C. "The TimeHorse" Jacobs
  • Date: 2008-05-24 16:10:37 UTC
  • mfrom: (39025.1.6 Regexp-2.6)
  • Revision ID: darklord@timehorse.com-20080524161037-oa3fo9iyz4fj02do
Merged in changes from the core Regexp branch.

Show diffs side-by-side

added added

removed removed

Lines of Context:
453
453
    Py_ssize_t n_digits;       /* count of digits need from the computed
454
454
                                  string */
455
455
    Py_ssize_t n_leading_chars;
 
456
    Py_ssize_t n_grouping_chars = 0; /* Count of additional chars to
 
457
                                        allocate, used for 'n'
 
458
                                        formatting. */
456
459
    NumberFieldWidths spec;
457
460
    long x;
458
461
 
523
526
            break;
524
527
        default:  /* shouldn't be needed, but stops a compiler warning */
525
528
        case 'd':
 
529
        case 'n':
526
530
            base = 10;
527
531
            leading_chars_to_skip = 0;
528
532
            break;
555
559
    /* Calculate the widths of the various leading and trailing parts */
556
560
    calc_number_widths(&spec, sign, n_digits, format);
557
561
 
 
562
    if (format->type == 'n')
 
563
            /* Compute how many additional chars we need to allocate
 
564
               to hold the thousands grouping. */
 
565
            STRINGLIB_GROUPING(pnumeric_chars, n_digits,
 
566
                               pnumeric_chars+n_digits,
 
567
                               0, &n_grouping_chars, 0);
 
568
 
558
569
    /* Allocate a new string to hold the result */
559
 
    result = STRINGLIB_NEW(NULL, spec.n_total);
 
570
    result = STRINGLIB_NEW(NULL, spec.n_total + n_grouping_chars);
560
571
    if (!result)
561
572
        goto done;
562
573
    p = STRINGLIB_STR(result);
567
578
            pnumeric_chars,
568
579
            n_digits * sizeof(STRINGLIB_CHAR));
569
580
 
570
 
    /* if X, convert to uppercase */
 
581
    /* If type is 'X', convert to uppercase */
571
582
    if (format->type == 'X') {
572
583
        Py_ssize_t t;
573
584
        for (t = 0; t < n_digits; ++t)
574
585
            p[t + n_leading_chars] = STRINGLIB_TOUPPER(p[t + n_leading_chars]);
575
586
    }
576
587
 
 
588
    /* Insert the grouping, if any, after the uppercasing of 'X', so we can
 
589
       ensure that grouping chars won't be affeted. */
 
590
    if (n_grouping_chars && format->type == 'n') {
 
591
            /* We know this can't fail, since we've already
 
592
               reserved enough space. */
 
593
            STRINGLIB_CHAR *pstart = p + n_leading_chars;
 
594
            int r = STRINGLIB_GROUPING(pstart, n_digits,
 
595
                                       pstart + n_digits,
 
596
                                       spec.n_total+n_grouping_chars-n_leading_chars,
 
597
                                       NULL, 0);
 
598
            assert(r);
 
599
    }
 
600
 
577
601
    /* Fill in the non-digit parts */
578
602
    fill_number(p, &spec, n_digits,
579
603
                format->fill_char == '\0' ? ' ' : format->fill_char);
841
865
    case 'o':
842
866
    case 'x':
843
867
    case 'X':
 
868
    case 'n':
844
869
        /* no type conversion needed, already an int (or long).  do
845
870
           the formatting */
846
871
            result = format_int_or_long_internal(value, &format, tostring);
852
877
    case 'F':
853
878
    case 'g':
854
879
    case 'G':
855
 
    case 'n':
856
880
    case '%':
857
881
        /* convert to float */
858
882
        tmp = PyNumber_Float(value);