~pythonregexp2.7/python/issue2636-09-01+10

« back to all changes in this revision

Viewing changes to Objects/stringlib/localeutil.h

  • Committer: Jeffrey C. "The TimeHorse" Jacobs
  • Date: 2008-09-22 21:39:45 UTC
  • mfrom: (39055.1.33 Regexp-2.7)
  • Revision ID: darklord@timehorse.com-20080922213945-23717m5eiqpamcyn
Merged in changes from the Single-Loop Engine branch.

Show diffs side-by-side

added added

removed removed

Lines of Context:
8
8
/**
9
9
 * _Py_InsertThousandsGrouping:
10
10
 * @buffer: A pointer to the start of a string.
11
 
 * @len: The length of the string.
12
 
 * @plast: A pointer to the end of of the digits in the string.  This
13
 
 *         may be before the end of the string (if the string contains
14
 
 *         decimals, for example).
 
11
 * @n_buffer: The length of the string.
 
12
 * @n_digits: The number of digits in the string, in which we want
 
13
 *            to put the grouping chars.
15
14
 * @buf_size: The maximum size of the buffer pointed to by buffer.
16
15
 * @count: If non-NULL, points to a variable that will receive the
17
16
 *         number of characters we need to insert (and no formatting
21
20
 *         string.
22
21
 *
23
22
 * Inserts thousand grouping characters (as defined in the current
24
 
 *  locale) into the string between buffer and plast.  If count is
25
 
 *  non-NULL, don't do any formatting, just count the number of
26
 
 *  characters to insert.  This is used by the caller to appropriately
27
 
 *  resize the buffer, if needed.
 
23
 *  locale) into the string between buffer and buffer+n_digits.  If
 
24
 *  count is non-NULL, don't do any formatting, just count the number
 
25
 *  of characters to insert.  This is used by the caller to
 
26
 *  appropriately resize the buffer, if needed.  If count is non-NULL,
 
27
 *  buffer can be NULL (it is not dereferenced at all in that case).
28
28
 *
29
29
 * Return value: 0 on error, else 1.  Note that no error can occur if
30
30
 *  count is non-NULL.
34
34
 **/
35
35
int
36
36
_Py_InsertThousandsGrouping(STRINGLIB_CHAR *buffer,
37
 
                            Py_ssize_t len,
38
 
                            STRINGLIB_CHAR *plast,
 
37
                            Py_ssize_t n_buffer,
 
38
                            Py_ssize_t n_digits,
39
39
                            Py_ssize_t buf_size,
40
40
                            Py_ssize_t *count,
41
41
                            int append_zero_char)
44
44
        const char *grouping = locale_data->grouping;
45
45
        const char *thousands_sep = locale_data->thousands_sep;
46
46
        Py_ssize_t thousands_sep_len = strlen(thousands_sep);
47
 
        STRINGLIB_CHAR *pend = buffer + len; /* current end of buffer */
48
 
        STRINGLIB_CHAR *pmax = buffer + buf_size;       /* max of buffer */
 
47
        STRINGLIB_CHAR *pend = NULL; /* current end of buffer */
 
48
        STRINGLIB_CHAR *pmax = NULL; /* max of buffer */
49
49
        char current_grouping;
 
50
        Py_ssize_t remaining = n_digits; /* Number of chars remaining to
 
51
                                            be looked at */
50
52
 
51
53
        /* Initialize the character count, if we're just counting. */
52
54
        if (count)
53
55
                *count = 0;
 
56
        else {
 
57
                /* We're not just counting, we're modifying buffer */
 
58
                pend = buffer + n_buffer;
 
59
                pmax = buffer + buf_size;
 
60
        }
54
61
 
55
 
        /* Starting at plast and working right-to-left, keep track of
 
62
        /* Starting at the end and working right-to-left, keep track of
56
63
           what grouping needs to be added and insert that. */
57
64
        current_grouping = *grouping++;
58
65
 
60
67
        if (current_grouping == 0)
61
68
                return 1;
62
69
 
63
 
        while (plast - buffer > current_grouping) {
 
70
        while (remaining > current_grouping) {
64
71
                /* Always leave buffer and pend valid at the end of this
65
72
                   loop, since we might leave with a return statement. */
66
73
 
67
 
                plast -= current_grouping;
 
74
                remaining -= current_grouping;
68
75
                if (count) {
69
76
                        /* We're only counting, not touching the memory. */
70
77
                        *count += thousands_sep_len;
72
79
                else {
73
80
                        /* Do the formatting. */
74
81
 
 
82
                        STRINGLIB_CHAR *plast = buffer + remaining;
 
83
 
75
84
                        /* Is there room to insert thousands_sep_len chars? */
76
85
                        if (pmax - pend < thousands_sep_len)
77
86
                                /* No room. */
111
120
        if (append_zero_char) {
112
121
                /* Append a zero character to mark the end of the string,
113
122
                   if there's room. */
114
 
                if (pend - plast < 1)
 
123
                if (pend - (buffer + remaining) < 1)
115
124
                        /* No room, error. */
116
125
                        return 0;
117
126
                *pend = 0;