Coverage Report

Created: 2025-07-04 06:49

/src/cpython/Objects/stringlib/localeutil.h
Line
Count
Source (jump to first uncovered line)
1
/* _PyUnicode_InsertThousandsGrouping() helper functions */
2
3
typedef struct {
4
    const char *grouping;
5
    char previous;
6
    Py_ssize_t i; /* Where we're currently pointing in grouping. */
7
} GroupGenerator;
8
9
10
static void
11
GroupGenerator_init(GroupGenerator *self, const char *grouping)
12
128
{
13
128
    self->grouping = grouping;
14
128
    self->i = 0;
15
128
    self->previous = 0;
16
128
}
17
18
19
/* Returns the next grouping, or 0 to signify end. */
20
static Py_ssize_t
21
GroupGenerator_next(GroupGenerator *self)
22
128
{
23
    /* Note that we don't really do much error checking here. If a
24
       grouping string contains just CHAR_MAX, for example, then just
25
       terminate the generator. That shouldn't happen, but at least we
26
       fail gracefully. */
27
128
    switch (self->grouping[self->i]) {
28
0
    case 0:
29
0
        return self->previous;
30
128
    case CHAR_MAX:
31
        /* Stop the generator. */
32
128
        return 0;
33
0
    default: {
34
0
        char ch = self->grouping[self->i];
35
0
        self->previous = ch;
36
0
        self->i++;
37
0
        return (Py_ssize_t)ch;
38
0
    }
39
128
    }
40
128
}
41
42
43
/* Fill in some digits, leading zeros, and thousands separator. All
44
   are optional, depending on when we're called. */
45
static void
46
InsertThousandsGrouping_fill(_PyUnicodeWriter *writer, Py_ssize_t *buffer_pos,
47
                             PyObject *digits, Py_ssize_t *digits_pos,
48
                             Py_ssize_t n_chars, Py_ssize_t n_zeros,
49
                             PyObject *thousands_sep, Py_ssize_t thousands_sep_len,
50
                             Py_UCS4 *maxchar, int forward)
51
128
{
52
128
    if (!writer) {
53
        /* if maxchar > 127, maxchar is already set */
54
64
        if (*maxchar == 127 && thousands_sep) {
55
0
            Py_UCS4 maxchar2 = PyUnicode_MAX_CHAR_VALUE(thousands_sep);
56
0
            *maxchar = Py_MAX(*maxchar, maxchar2);
57
0
        }
58
64
        return;
59
64
    }
60
61
64
    if (thousands_sep) {
62
0
        if (!forward) {
63
0
            *buffer_pos -= thousands_sep_len;
64
0
        }
65
        /* Copy the thousands_sep chars into the buffer. */
66
0
        _PyUnicode_FastCopyCharacters(writer->buffer, *buffer_pos,
67
0
                                      thousands_sep, 0,
68
0
                                      thousands_sep_len);
69
0
        if (forward) {
70
0
            *buffer_pos += thousands_sep_len;
71
0
        }
72
0
    }
73
74
64
    if (!forward) {
75
64
        *buffer_pos -= n_chars;
76
64
        *digits_pos -= n_chars;
77
64
    }
78
64
    _PyUnicode_FastCopyCharacters(writer->buffer, *buffer_pos,
79
64
                                  digits, *digits_pos,
80
64
                                  n_chars);
81
64
    if (forward) {
82
0
        *buffer_pos += n_chars;
83
0
        *digits_pos += n_chars;
84
0
    }
85
86
64
    if (n_zeros) {
87
64
        if (!forward) {
88
64
            *buffer_pos -= n_zeros;
89
64
        }
90
64
        int kind = PyUnicode_KIND(writer->buffer);
91
64
        void *data = PyUnicode_DATA(writer->buffer);
92
64
        unicode_fill(kind, data, '0', *buffer_pos, n_zeros);
93
64
        if (forward) {
94
0
            *buffer_pos += n_zeros;
95
0
        }
96
64
    }
97
64
}