Coverage Report

Created: 2026-05-16 06:46

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/cpython/Objects/stringlib/split.h
Line
Count
Source
1
/* stringlib: split implementation */
2
3
#ifndef STRINGLIB_FASTSEARCH_H
4
#error must include "stringlib/fastsearch.h" before including this module
5
#endif
6
7
/* Overallocate the initial list to reduce the number of reallocs for small
8
   split sizes.  Eg, "A A A A A A A A A A".split() (10 elements) has three
9
   resizes, to sizes 4, 8, then 16.  Most observed string splits are for human
10
   text (roughly 11 words per line) and field delimited data (usually 1-10
11
   fields).  For large strings the split algorithms are bandwidth limited
12
   so increasing the preallocation likely will not improve things.*/
13
14
106M
#define MAX_PREALLOC 12
15
16
/* 5 splits gives 6 elements */
17
#define PREALLOC_SIZE(maxsplit) \
18
22.4M
    (maxsplit >= MAX_PREALLOC ? MAX_PREALLOC : maxsplit+1)
19
20
#define SPLIT_APPEND(data, left, right)         \
21
34.3M
    sub = STRINGLIB_NEW((data) + (left),        \
22
34.3M
                        (right) - (left));      \
23
34.3M
    if (sub == NULL)                            \
24
34.3M
        goto onError;                           \
25
34.3M
    if (PyList_Append(list, sub)) {             \
26
0
        Py_DECREF(sub);                         \
27
0
        goto onError;                           \
28
0
    }                                           \
29
34.3M
    else                                        \
30
34.3M
        Py_DECREF(sub);
31
32
58.3M
#define SPLIT_ADD(data, left, right) {          \
33
58.3M
    sub = STRINGLIB_NEW((data) + (left),        \
34
58.3M
                        (right) - (left));      \
35
58.3M
    if (sub == NULL)                            \
36
58.3M
        goto onError;                           \
37
58.3M
    if (count < MAX_PREALLOC) {                 \
38
34.8M
        PyList_SET_ITEM(list, count, sub);      \
39
34.8M
    } else {                                    \
40
23.5M
        if (PyList_Append(list, sub)) {         \
41
0
            Py_DECREF(sub);                     \
42
0
            goto onError;                       \
43
0
        }                                       \
44
23.5M
        else                                    \
45
23.5M
            Py_DECREF(sub);                     \
46
23.5M
    }                                           \
47
58.3M
    count++; }
48
49
50
/* Always force the list to the expected size. */
51
22.4M
#define FIX_PREALLOC_SIZE(list) Py_SET_SIZE(list, count)
52
53
Py_LOCAL_INLINE(PyObject *)
54
STRINGLIB(split_whitespace)(PyObject* str_obj,
55
                           const STRINGLIB_CHAR* str, Py_ssize_t str_len,
56
                           Py_ssize_t maxcount)
57
164k
{
58
164k
    Py_ssize_t i, j, count=0;
59
164k
    PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
60
164k
    PyObject *sub;
61
62
164k
    if (list == NULL)
63
0
        return NULL;
64
65
164k
    i = j = 0;
66
1.81M
    while (maxcount-- > 0) {
67
3.48M
        while (i < str_len && STRINGLIB_ISSPACE(str[i]))
68
1.69M
            i++;
69
1.78M
        if (i == str_len) break;
70
1.69M
        j = i; i++;
71
129M
        while (i < str_len && !STRINGLIB_ISSPACE(str[i]))
72
128M
            i++;
73
#if !STRINGLIB_MUTABLE
74
1.69M
        if (j == 0 && i == str_len && STRINGLIB_CHECK_EXACT(str_obj)) {
75
            /* No whitespace in str_obj, so just use it as list[0] */
76
42.9k
            Py_INCREF(str_obj);
77
42.9k
            PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
78
42.9k
            count++;
79
42.9k
            break;
80
42.9k
        }
81
1.64M
#endif
82
4.94M
        SPLIT_ADD(str, j, i);
83
4.94M
    }
84
85
164k
    if (i < str_len) {
86
        /* Only occurs when maxcount was reached */
87
        /* Skip any remaining whitespace and copy to end of string */
88
52.2k
        while (i < str_len && STRINGLIB_ISSPACE(str[i]))
89
28.3k
            i++;
90
23.9k
        if (i != str_len)
91
23.9k
            SPLIT_ADD(str, i, str_len);
92
23.9k
    }
93
164k
    FIX_PREALLOC_SIZE(list);
94
164k
    return list;
95
96
0
  onError:
97
0
    Py_DECREF(list);
98
0
    return NULL;
99
164k
}
Unexecuted instantiation: bytesobject.c:stringlib_split_whitespace
unicodeobject.c:asciilib_split_whitespace
Line
Count
Source
57
77.4k
{
58
77.4k
    Py_ssize_t i, j, count=0;
59
77.4k
    PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
60
77.4k
    PyObject *sub;
61
62
77.4k
    if (list == NULL)
63
0
        return NULL;
64
65
77.4k
    i = j = 0;
66
560k
    while (maxcount-- > 0) {
67
998k
        while (i < str_len && STRINGLIB_ISSPACE(str[i]))
68
452k
            i++;
69
545k
        if (i == str_len) break;
70
508k
        j = i; i++;
71
47.0M
        while (i < str_len && !STRINGLIB_ISSPACE(str[i]))
72
46.5M
            i++;
73
508k
#if !STRINGLIB_MUTABLE
74
508k
        if (j == 0 && i == str_len && STRINGLIB_CHECK_EXACT(str_obj)) {
75
            /* No whitespace in str_obj, so just use it as list[0] */
76
25.4k
            Py_INCREF(str_obj);
77
25.4k
            PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
78
25.4k
            count++;
79
25.4k
            break;
80
25.4k
        }
81
483k
#endif
82
1.44M
        SPLIT_ADD(str, j, i);
83
1.44M
    }
84
85
77.4k
    if (i < str_len) {
86
        /* Only occurs when maxcount was reached */
87
        /* Skip any remaining whitespace and copy to end of string */
88
25.9k
        while (i < str_len && STRINGLIB_ISSPACE(str[i]))
89
13.4k
            i++;
90
12.4k
        if (i != str_len)
91
12.4k
            SPLIT_ADD(str, i, str_len);
92
12.4k
    }
93
77.4k
    FIX_PREALLOC_SIZE(list);
94
77.4k
    return list;
95
96
0
  onError:
97
0
    Py_DECREF(list);
98
    return NULL;
99
77.4k
}
unicodeobject.c:ucs1lib_split_whitespace
Line
Count
Source
57
26.2k
{
58
26.2k
    Py_ssize_t i, j, count=0;
59
26.2k
    PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
60
26.2k
    PyObject *sub;
61
62
26.2k
    if (list == NULL)
63
0
        return NULL;
64
65
26.2k
    i = j = 0;
66
648k
    while (maxcount-- > 0) {
67
1.24M
        while (i < str_len && STRINGLIB_ISSPACE(str[i]))
68
606k
            i++;
69
637k
        if (i == str_len) break;
70
625k
        j = i; i++;
71
41.0M
        while (i < str_len && !STRINGLIB_ISSPACE(str[i]))
72
40.4M
            i++;
73
625k
#if !STRINGLIB_MUTABLE
74
625k
        if (j == 0 && i == str_len && STRINGLIB_CHECK_EXACT(str_obj)) {
75
            /* No whitespace in str_obj, so just use it as list[0] */
76
2.60k
            Py_INCREF(str_obj);
77
2.60k
            PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
78
2.60k
            count++;
79
2.60k
            break;
80
2.60k
        }
81
622k
#endif
82
1.86M
        SPLIT_ADD(str, j, i);
83
1.86M
    }
84
85
26.2k
    if (i < str_len) {
86
        /* Only occurs when maxcount was reached */
87
        /* Skip any remaining whitespace and copy to end of string */
88
24.8k
        while (i < str_len && STRINGLIB_ISSPACE(str[i]))
89
14.1k
            i++;
90
10.6k
        if (i != str_len)
91
10.6k
            SPLIT_ADD(str, i, str_len);
92
10.6k
    }
93
26.2k
    FIX_PREALLOC_SIZE(list);
94
26.2k
    return list;
95
96
0
  onError:
97
0
    Py_DECREF(list);
98
    return NULL;
99
26.2k
}
unicodeobject.c:ucs2lib_split_whitespace
Line
Count
Source
57
49.5k
{
58
49.5k
    Py_ssize_t i, j, count=0;
59
49.5k
    PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
60
49.5k
    PyObject *sub;
61
62
49.5k
    if (list == NULL)
63
0
        return NULL;
64
65
49.5k
    i = j = 0;
66
529k
    while (maxcount-- > 0) {
67
1.10M
        while (i < str_len && STRINGLIB_ISSPACE(str[i]))
68
575k
            i++;
69
528k
        if (i == str_len) break;
70
491k
        j = i; i++;
71
30.9M
        while (i < str_len && !STRINGLIB_ISSPACE(str[i]))
72
30.4M
            i++;
73
491k
#if !STRINGLIB_MUTABLE
74
491k
        if (j == 0 && i == str_len && STRINGLIB_CHECK_EXACT(str_obj)) {
75
            /* No whitespace in str_obj, so just use it as list[0] */
76
11.9k
            Py_INCREF(str_obj);
77
11.9k
            PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
78
11.9k
            count++;
79
11.9k
            break;
80
11.9k
        }
81
479k
#endif
82
1.43M
        SPLIT_ADD(str, j, i);
83
1.43M
    }
84
85
49.5k
    if (i < str_len) {
86
        /* Only occurs when maxcount was reached */
87
        /* Skip any remaining whitespace and copy to end of string */
88
1.50k
        while (i < str_len && STRINGLIB_ISSPACE(str[i]))
89
753
            i++;
90
753
        if (i != str_len)
91
753
            SPLIT_ADD(str, i, str_len);
92
753
    }
93
49.5k
    FIX_PREALLOC_SIZE(list);
94
49.5k
    return list;
95
96
0
  onError:
97
0
    Py_DECREF(list);
98
    return NULL;
99
49.5k
}
unicodeobject.c:ucs4lib_split_whitespace
Line
Count
Source
57
10.9k
{
58
10.9k
    Py_ssize_t i, j, count=0;
59
10.9k
    PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
60
10.9k
    PyObject *sub;
61
62
10.9k
    if (list == NULL)
63
0
        return NULL;
64
65
10.9k
    i = j = 0;
66
74.1k
    while (maxcount-- > 0) {
67
136k
        while (i < str_len && STRINGLIB_ISSPACE(str[i]))
68
62.6k
            i++;
69
73.9k
        if (i == str_len) break;
70
66.2k
        j = i; i++;
71
10.5M
        while (i < str_len && !STRINGLIB_ISSPACE(str[i]))
72
10.5M
            i++;
73
66.2k
#if !STRINGLIB_MUTABLE
74
66.2k
        if (j == 0 && i == str_len && STRINGLIB_CHECK_EXACT(str_obj)) {
75
            /* No whitespace in str_obj, so just use it as list[0] */
76
2.97k
            Py_INCREF(str_obj);
77
2.97k
            PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
78
2.97k
            count++;
79
2.97k
            break;
80
2.97k
        }
81
63.2k
#endif
82
189k
        SPLIT_ADD(str, j, i);
83
189k
    }
84
85
10.9k
    if (i < str_len) {
86
        /* Only occurs when maxcount was reached */
87
        /* Skip any remaining whitespace and copy to end of string */
88
0
        while (i < str_len && STRINGLIB_ISSPACE(str[i]))
89
0
            i++;
90
0
        if (i != str_len)
91
0
            SPLIT_ADD(str, i, str_len);
92
0
    }
93
10.9k
    FIX_PREALLOC_SIZE(list);
94
10.9k
    return list;
95
96
0
  onError:
97
0
    Py_DECREF(list);
98
    return NULL;
99
10.9k
}
Unexecuted instantiation: bytearrayobject.c:stringlib_split_whitespace
100
101
Py_LOCAL_INLINE(PyObject *)
102
STRINGLIB(split_char)(PyObject* str_obj,
103
                     const STRINGLIB_CHAR* str, Py_ssize_t str_len,
104
                     const STRINGLIB_CHAR ch,
105
                     Py_ssize_t maxcount)
106
22.0M
{
107
22.0M
    Py_ssize_t i, j, count=0;
108
22.0M
    PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
109
22.0M
    PyObject *sub;
110
111
22.0M
    if (list == NULL)
112
0
        return NULL;
113
114
22.0M
    i = j = 0;
115
82.0M
    while ((j < str_len) && (maxcount-- > 0)) {
116
445M
        for(; j < str_len; j++) {
117
            /* I found that using memchr makes no difference */
118
425M
            if (str[j] == ch) {
119
39.9M
                SPLIT_ADD(str, i, j);
120
39.9M
                i = j = j + 1;
121
39.9M
                break;
122
39.9M
            }
123
425M
        }
124
59.9M
    }
125
#if !STRINGLIB_MUTABLE
126
22.0M
    if (count == 0 && STRINGLIB_CHECK_EXACT(str_obj)) {
127
        /* ch not in str_obj, so just use str_obj as list[0] */
128
5.58M
        Py_INCREF(str_obj);
129
5.58M
        PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
130
5.58M
        count++;
131
5.58M
    } else
132
16.4M
#endif
133
16.4M
    if (i <= str_len) {
134
32.9M
        SPLIT_ADD(str, i, str_len);
135
32.9M
    }
136
22.0M
    FIX_PREALLOC_SIZE(list);
137
22.0M
    return list;
138
139
0
  onError:
140
0
    Py_DECREF(list);
141
0
    return NULL;
142
0
}
bytesobject.c:stringlib_split_char
Line
Count
Source
106
2.93M
{
107
2.93M
    Py_ssize_t i, j, count=0;
108
2.93M
    PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
109
2.93M
    PyObject *sub;
110
111
2.93M
    if (list == NULL)
112
0
        return NULL;
113
114
2.93M
    i = j = 0;
115
12.4M
    while ((j < str_len) && (maxcount-- > 0)) {
116
85.9M
        for(; j < str_len; j++) {
117
            /* I found that using memchr makes no difference */
118
83.4M
            if (str[j] == ch) {
119
6.96M
                SPLIT_ADD(str, i, j);
120
6.96M
                i = j = j + 1;
121
6.96M
                break;
122
6.96M
            }
123
83.4M
        }
124
9.48M
    }
125
2.93M
#if !STRINGLIB_MUTABLE
126
2.93M
    if (count == 0 && STRINGLIB_CHECK_EXACT(str_obj)) {
127
        /* ch not in str_obj, so just use str_obj as list[0] */
128
2.43M
        Py_INCREF(str_obj);
129
2.43M
        PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
130
2.43M
        count++;
131
2.43M
    } else
132
497k
#endif
133
497k
    if (i <= str_len) {
134
995k
        SPLIT_ADD(str, i, str_len);
135
995k
    }
136
2.93M
    FIX_PREALLOC_SIZE(list);
137
2.93M
    return list;
138
139
0
  onError:
140
0
    Py_DECREF(list);
141
    return NULL;
142
2.93M
}
unicodeobject.c:asciilib_split_char
Line
Count
Source
106
17.9M
{
107
17.9M
    Py_ssize_t i, j, count=0;
108
17.9M
    PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
109
17.9M
    PyObject *sub;
110
111
17.9M
    if (list == NULL)
112
0
        return NULL;
113
114
17.9M
    i = j = 0;
115
55.3M
    while ((j < str_len) && (maxcount-- > 0)) {
116
236M
        for(; j < str_len; j++) {
117
            /* I found that using memchr makes no difference */
118
219M
            if (str[j] == ch) {
119
20.1M
                SPLIT_ADD(str, i, j);
120
20.1M
                i = j = j + 1;
121
20.1M
                break;
122
20.1M
            }
123
219M
        }
124
37.4M
    }
125
17.9M
#if !STRINGLIB_MUTABLE
126
17.9M
    if (count == 0 && STRINGLIB_CHECK_EXACT(str_obj)) {
127
        /* ch not in str_obj, so just use str_obj as list[0] */
128
2.93M
        Py_INCREF(str_obj);
129
2.93M
        PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
130
2.93M
        count++;
131
2.93M
    } else
132
14.9M
#endif
133
14.9M
    if (i <= str_len) {
134
29.9M
        SPLIT_ADD(str, i, str_len);
135
29.9M
    }
136
17.9M
    FIX_PREALLOC_SIZE(list);
137
17.9M
    return list;
138
139
0
  onError:
140
0
    Py_DECREF(list);
141
    return NULL;
142
17.9M
}
unicodeobject.c:ucs1lib_split_char
Line
Count
Source
106
1.10M
{
107
1.10M
    Py_ssize_t i, j, count=0;
108
1.10M
    PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
109
1.10M
    PyObject *sub;
110
111
1.10M
    if (list == NULL)
112
0
        return NULL;
113
114
1.10M
    i = j = 0;
115
9.97M
    while ((j < str_len) && (maxcount-- > 0)) {
116
64.0M
        for(; j < str_len; j++) {
117
            /* I found that using memchr makes no difference */
118
63.8M
            if (str[j] == ch) {
119
8.67M
                SPLIT_ADD(str, i, j);
120
8.67M
                i = j = j + 1;
121
8.67M
                break;
122
8.67M
            }
123
63.8M
        }
124
8.87M
    }
125
1.10M
#if !STRINGLIB_MUTABLE
126
1.10M
    if (count == 0 && STRINGLIB_CHECK_EXACT(str_obj)) {
127
        /* ch not in str_obj, so just use str_obj as list[0] */
128
172k
        Py_INCREF(str_obj);
129
172k
        PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
130
172k
        count++;
131
172k
    } else
132
932k
#endif
133
932k
    if (i <= str_len) {
134
1.86M
        SPLIT_ADD(str, i, str_len);
135
1.86M
    }
136
1.10M
    FIX_PREALLOC_SIZE(list);
137
1.10M
    return list;
138
139
0
  onError:
140
0
    Py_DECREF(list);
141
    return NULL;
142
1.10M
}
unicodeobject.c:ucs2lib_split_char
Line
Count
Source
106
125k
{
107
125k
    Py_ssize_t i, j, count=0;
108
125k
    PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
109
125k
    PyObject *sub;
110
111
125k
    if (list == NULL)
112
0
        return NULL;
113
114
125k
    i = j = 0;
115
4.09M
    while ((j < str_len) && (maxcount-- > 0)) {
116
38.6M
        for(; j < str_len; j++) {
117
            /* I found that using memchr makes no difference */
118
38.5M
            if (str[j] == ch) {
119
3.87M
                SPLIT_ADD(str, i, j);
120
3.87M
                i = j = j + 1;
121
3.87M
                break;
122
3.87M
            }
123
38.5M
        }
124
3.96M
    }
125
125k
#if !STRINGLIB_MUTABLE
126
125k
    if (count == 0 && STRINGLIB_CHECK_EXACT(str_obj)) {
127
        /* ch not in str_obj, so just use str_obj as list[0] */
128
38.0k
        Py_INCREF(str_obj);
129
38.0k
        PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
130
38.0k
        count++;
131
38.0k
    } else
132
87.7k
#endif
133
87.7k
    if (i <= str_len) {
134
175k
        SPLIT_ADD(str, i, str_len);
135
175k
    }
136
125k
    FIX_PREALLOC_SIZE(list);
137
125k
    return list;
138
139
0
  onError:
140
0
    Py_DECREF(list);
141
    return NULL;
142
125k
}
unicodeobject.c:ucs4lib_split_char
Line
Count
Source
106
15.3k
{
107
15.3k
    Py_ssize_t i, j, count=0;
108
15.3k
    PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
109
15.3k
    PyObject *sub;
110
111
15.3k
    if (list == NULL)
112
0
        return NULL;
113
114
15.3k
    i = j = 0;
115
249k
    while ((j < str_len) && (maxcount-- > 0)) {
116
20.7M
        for(; j < str_len; j++) {
117
            /* I found that using memchr makes no difference */
118
20.6M
            if (str[j] == ch) {
119
221k
                SPLIT_ADD(str, i, j);
120
221k
                i = j = j + 1;
121
221k
                break;
122
221k
            }
123
20.6M
        }
124
233k
    }
125
15.3k
#if !STRINGLIB_MUTABLE
126
15.3k
    if (count == 0 && STRINGLIB_CHECK_EXACT(str_obj)) {
127
        /* ch not in str_obj, so just use str_obj as list[0] */
128
981
        Py_INCREF(str_obj);
129
981
        PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
130
981
        count++;
131
981
    } else
132
14.4k
#endif
133
14.4k
    if (i <= str_len) {
134
28.8k
        SPLIT_ADD(str, i, str_len);
135
28.8k
    }
136
15.3k
    FIX_PREALLOC_SIZE(list);
137
15.3k
    return list;
138
139
0
  onError:
140
0
    Py_DECREF(list);
141
    return NULL;
142
15.3k
}
Unexecuted instantiation: bytearrayobject.c:stringlib_split_char
143
144
Py_LOCAL_INLINE(PyObject *)
145
STRINGLIB(split)(PyObject* str_obj,
146
                const STRINGLIB_CHAR* str, Py_ssize_t str_len,
147
                const STRINGLIB_CHAR* sep, Py_ssize_t sep_len,
148
                Py_ssize_t maxcount)
149
22.2M
{
150
22.2M
    Py_ssize_t i, j, pos, count=0;
151
22.2M
    PyObject *list, *sub;
152
153
22.2M
    if (sep_len == 0) {
154
0
        PyErr_SetString(PyExc_ValueError, "empty separator");
155
0
        return NULL;
156
0
    }
157
22.2M
    else if (sep_len == 1)
158
22.0M
        return STRINGLIB(split_char)(str_obj, str, str_len, sep[0], maxcount);
159
160
209k
    list = PyList_New(PREALLOC_SIZE(maxcount));
161
209k
    if (list == NULL)
162
0
        return NULL;
163
164
209k
    i = j = 0;
165
367k
    while (maxcount-- > 0) {
166
209k
        pos = FASTSEARCH(str+i, str_len-i, sep, sep_len, -1, FAST_SEARCH);
167
209k
        if (pos < 0)
168
50.7k
            break;
169
158k
        j = i + pos;
170
316k
        SPLIT_ADD(str, i, j);
171
316k
        i = j + sep_len;
172
316k
    }
173
#if !STRINGLIB_MUTABLE
174
209k
    if (count == 0 && STRINGLIB_CHECK_EXACT(str_obj)) {
175
        /* No match in str_obj, so just use it as list[0] */
176
50.7k
        Py_INCREF(str_obj);
177
50.7k
        PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
178
50.7k
        count++;
179
50.7k
    } else
180
158k
#endif
181
158k
    {
182
316k
        SPLIT_ADD(str, i, str_len);
183
316k
    }
184
209k
    FIX_PREALLOC_SIZE(list);
185
209k
    return list;
186
187
0
  onError:
188
0
    Py_DECREF(list);
189
0
    return NULL;
190
0
}
bytesobject.c:stringlib_split
Line
Count
Source
149
2.93M
{
150
2.93M
    Py_ssize_t i, j, pos, count=0;
151
2.93M
    PyObject *list, *sub;
152
153
2.93M
    if (sep_len == 0) {
154
0
        PyErr_SetString(PyExc_ValueError, "empty separator");
155
0
        return NULL;
156
0
    }
157
2.93M
    else if (sep_len == 1)
158
2.93M
        return STRINGLIB(split_char)(str_obj, str, str_len, sep[0], maxcount);
159
160
0
    list = PyList_New(PREALLOC_SIZE(maxcount));
161
0
    if (list == NULL)
162
0
        return NULL;
163
164
0
    i = j = 0;
165
0
    while (maxcount-- > 0) {
166
0
        pos = FASTSEARCH(str+i, str_len-i, sep, sep_len, -1, FAST_SEARCH);
167
0
        if (pos < 0)
168
0
            break;
169
0
        j = i + pos;
170
0
        SPLIT_ADD(str, i, j);
171
0
        i = j + sep_len;
172
0
    }
173
0
#if !STRINGLIB_MUTABLE
174
0
    if (count == 0 && STRINGLIB_CHECK_EXACT(str_obj)) {
175
        /* No match in str_obj, so just use it as list[0] */
176
0
        Py_INCREF(str_obj);
177
0
        PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
178
0
        count++;
179
0
    } else
180
0
#endif
181
0
    {
182
0
        SPLIT_ADD(str, i, str_len);
183
0
    }
184
0
    FIX_PREALLOC_SIZE(list);
185
0
    return list;
186
187
0
  onError:
188
0
    Py_DECREF(list);
189
    return NULL;
190
0
}
unicodeobject.c:asciilib_split
Line
Count
Source
149
17.9M
{
150
17.9M
    Py_ssize_t i, j, pos, count=0;
151
17.9M
    PyObject *list, *sub;
152
153
17.9M
    if (sep_len == 0) {
154
0
        PyErr_SetString(PyExc_ValueError, "empty separator");
155
0
        return NULL;
156
0
    }
157
17.9M
    else if (sep_len == 1)
158
17.9M
        return STRINGLIB(split_char)(str_obj, str, str_len, sep[0], maxcount);
159
160
85.6k
    list = PyList_New(PREALLOC_SIZE(maxcount));
161
85.6k
    if (list == NULL)
162
0
        return NULL;
163
164
85.6k
    i = j = 0;
165
137k
    while (maxcount-- > 0) {
166
85.6k
        pos = FASTSEARCH(str+i, str_len-i, sep, sep_len, -1, FAST_SEARCH);
167
85.6k
        if (pos < 0)
168
34.2k
            break;
169
51.4k
        j = i + pos;
170
102k
        SPLIT_ADD(str, i, j);
171
102k
        i = j + sep_len;
172
102k
    }
173
85.6k
#if !STRINGLIB_MUTABLE
174
85.6k
    if (count == 0 && STRINGLIB_CHECK_EXACT(str_obj)) {
175
        /* No match in str_obj, so just use it as list[0] */
176
34.2k
        Py_INCREF(str_obj);
177
34.2k
        PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
178
34.2k
        count++;
179
34.2k
    } else
180
51.4k
#endif
181
51.4k
    {
182
102k
        SPLIT_ADD(str, i, str_len);
183
102k
    }
184
85.6k
    FIX_PREALLOC_SIZE(list);
185
85.6k
    return list;
186
187
0
  onError:
188
0
    Py_DECREF(list);
189
    return NULL;
190
85.6k
}
unicodeobject.c:ucs1lib_split
Line
Count
Source
149
1.12M
{
150
1.12M
    Py_ssize_t i, j, pos, count=0;
151
1.12M
    PyObject *list, *sub;
152
153
1.12M
    if (sep_len == 0) {
154
0
        PyErr_SetString(PyExc_ValueError, "empty separator");
155
0
        return NULL;
156
0
    }
157
1.12M
    else if (sep_len == 1)
158
1.10M
        return STRINGLIB(split_char)(str_obj, str, str_len, sep[0], maxcount);
159
160
19.3k
    list = PyList_New(PREALLOC_SIZE(maxcount));
161
19.3k
    if (list == NULL)
162
0
        return NULL;
163
164
19.3k
    i = j = 0;
165
35.4k
    while (maxcount-- > 0) {
166
19.3k
        pos = FASTSEARCH(str+i, str_len-i, sep, sep_len, -1, FAST_SEARCH);
167
19.3k
        if (pos < 0)
168
3.34k
            break;
169
16.0k
        j = i + pos;
170
32.0k
        SPLIT_ADD(str, i, j);
171
32.0k
        i = j + sep_len;
172
32.0k
    }
173
19.3k
#if !STRINGLIB_MUTABLE
174
19.3k
    if (count == 0 && STRINGLIB_CHECK_EXACT(str_obj)) {
175
        /* No match in str_obj, so just use it as list[0] */
176
3.34k
        Py_INCREF(str_obj);
177
3.34k
        PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
178
3.34k
        count++;
179
3.34k
    } else
180
16.0k
#endif
181
16.0k
    {
182
32.0k
        SPLIT_ADD(str, i, str_len);
183
32.0k
    }
184
19.3k
    FIX_PREALLOC_SIZE(list);
185
19.3k
    return list;
186
187
0
  onError:
188
0
    Py_DECREF(list);
189
    return NULL;
190
19.3k
}
unicodeobject.c:ucs2lib_split
Line
Count
Source
149
209k
{
150
209k
    Py_ssize_t i, j, pos, count=0;
151
209k
    PyObject *list, *sub;
152
153
209k
    if (sep_len == 0) {
154
0
        PyErr_SetString(PyExc_ValueError, "empty separator");
155
0
        return NULL;
156
0
    }
157
209k
    else if (sep_len == 1)
158
125k
        return STRINGLIB(split_char)(str_obj, str, str_len, sep[0], maxcount);
159
160
83.2k
    list = PyList_New(PREALLOC_SIZE(maxcount));
161
83.2k
    if (list == NULL)
162
0
        return NULL;
163
164
83.2k
    i = j = 0;
165
157k
    while (maxcount-- > 0) {
166
83.2k
        pos = FASTSEARCH(str+i, str_len-i, sep, sep_len, -1, FAST_SEARCH);
167
83.2k
        if (pos < 0)
168
9.34k
            break;
169
73.8k
        j = i + pos;
170
147k
        SPLIT_ADD(str, i, j);
171
147k
        i = j + sep_len;
172
147k
    }
173
83.2k
#if !STRINGLIB_MUTABLE
174
83.2k
    if (count == 0 && STRINGLIB_CHECK_EXACT(str_obj)) {
175
        /* No match in str_obj, so just use it as list[0] */
176
9.34k
        Py_INCREF(str_obj);
177
9.34k
        PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
178
9.34k
        count++;
179
9.34k
    } else
180
73.8k
#endif
181
73.8k
    {
182
147k
        SPLIT_ADD(str, i, str_len);
183
147k
    }
184
83.2k
    FIX_PREALLOC_SIZE(list);
185
83.2k
    return list;
186
187
0
  onError:
188
0
    Py_DECREF(list);
189
    return NULL;
190
83.2k
}
unicodeobject.c:ucs4lib_split
Line
Count
Source
149
36.2k
{
150
36.2k
    Py_ssize_t i, j, pos, count=0;
151
36.2k
    PyObject *list, *sub;
152
153
36.2k
    if (sep_len == 0) {
154
0
        PyErr_SetString(PyExc_ValueError, "empty separator");
155
0
        return NULL;
156
0
    }
157
36.2k
    else if (sep_len == 1)
158
15.3k
        return STRINGLIB(split_char)(str_obj, str, str_len, sep[0], maxcount);
159
160
20.8k
    list = PyList_New(PREALLOC_SIZE(maxcount));
161
20.8k
    if (list == NULL)
162
0
        return NULL;
163
164
20.8k
    i = j = 0;
165
37.9k
    while (maxcount-- > 0) {
166
20.8k
        pos = FASTSEARCH(str+i, str_len-i, sep, sep_len, -1, FAST_SEARCH);
167
20.8k
        if (pos < 0)
168
3.82k
            break;
169
17.0k
        j = i + pos;
170
34.1k
        SPLIT_ADD(str, i, j);
171
34.1k
        i = j + sep_len;
172
34.1k
    }
173
20.8k
#if !STRINGLIB_MUTABLE
174
20.8k
    if (count == 0 && STRINGLIB_CHECK_EXACT(str_obj)) {
175
        /* No match in str_obj, so just use it as list[0] */
176
3.82k
        Py_INCREF(str_obj);
177
3.82k
        PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
178
3.82k
        count++;
179
3.82k
    } else
180
17.0k
#endif
181
17.0k
    {
182
34.1k
        SPLIT_ADD(str, i, str_len);
183
34.1k
    }
184
20.8k
    FIX_PREALLOC_SIZE(list);
185
20.8k
    return list;
186
187
0
  onError:
188
0
    Py_DECREF(list);
189
    return NULL;
190
20.8k
}
Unexecuted instantiation: bytearrayobject.c:stringlib_split
191
192
Py_LOCAL_INLINE(PyObject *)
193
STRINGLIB(rsplit_whitespace)(PyObject* str_obj,
194
                            const STRINGLIB_CHAR* str, Py_ssize_t str_len,
195
                            Py_ssize_t maxcount)
196
0
{
197
0
    Py_ssize_t i, j, count=0;
198
0
    PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
199
0
    PyObject *sub;
200
201
0
    if (list == NULL)
202
0
        return NULL;
203
204
0
    i = j = str_len - 1;
205
0
    while (maxcount-- > 0) {
206
0
        while (i >= 0 && STRINGLIB_ISSPACE(str[i]))
207
0
            i--;
208
0
        if (i < 0) break;
209
0
        j = i; i--;
210
0
        while (i >= 0 && !STRINGLIB_ISSPACE(str[i]))
211
0
            i--;
212
#if !STRINGLIB_MUTABLE
213
0
        if (j == str_len - 1 && i < 0 && STRINGLIB_CHECK_EXACT(str_obj)) {
214
            /* No whitespace in str_obj, so just use it as list[0] */
215
0
            Py_INCREF(str_obj);
216
0
            PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
217
0
            count++;
218
0
            break;
219
0
        }
220
0
#endif
221
0
        SPLIT_ADD(str, i + 1, j + 1);
222
0
    }
223
224
0
    if (i >= 0) {
225
        /* Only occurs when maxcount was reached */
226
        /* Skip any remaining whitespace and copy to beginning of string */
227
0
        while (i >= 0 && STRINGLIB_ISSPACE(str[i]))
228
0
            i--;
229
0
        if (i >= 0)
230
0
            SPLIT_ADD(str, 0, i + 1);
231
0
    }
232
0
    FIX_PREALLOC_SIZE(list);
233
0
    if (PyList_Reverse(list) < 0)
234
0
        goto onError;
235
0
    return list;
236
237
0
  onError:
238
0
    Py_DECREF(list);
239
0
    return NULL;
240
0
}
Unexecuted instantiation: bytesobject.c:stringlib_rsplit_whitespace
Unexecuted instantiation: unicodeobject.c:asciilib_rsplit_whitespace
Unexecuted instantiation: unicodeobject.c:ucs1lib_rsplit_whitespace
Unexecuted instantiation: unicodeobject.c:ucs2lib_rsplit_whitespace
Unexecuted instantiation: unicodeobject.c:ucs4lib_rsplit_whitespace
Unexecuted instantiation: bytearrayobject.c:stringlib_rsplit_whitespace
241
242
Py_LOCAL_INLINE(PyObject *)
243
STRINGLIB(rsplit_char)(PyObject* str_obj,
244
                      const STRINGLIB_CHAR* str, Py_ssize_t str_len,
245
                      const STRINGLIB_CHAR ch,
246
                      Py_ssize_t maxcount)
247
66
{
248
66
    Py_ssize_t i, j, count=0;
249
66
    PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
250
66
    PyObject *sub;
251
252
66
    if (list == NULL)
253
0
        return NULL;
254
255
66
    i = j = str_len - 1;
256
132
    while ((i >= 0) && (maxcount-- > 0)) {
257
138
        for(; i >= 0; i--) {
258
138
            if (str[i] == ch) {
259
66
                SPLIT_ADD(str, i + 1, j + 1);
260
66
                j = i = i - 1;
261
66
                break;
262
66
            }
263
138
        }
264
66
    }
265
#if !STRINGLIB_MUTABLE
266
66
    if (count == 0 && STRINGLIB_CHECK_EXACT(str_obj)) {
267
        /* ch not in str_obj, so just use str_obj as list[0] */
268
0
        Py_INCREF(str_obj);
269
0
        PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
270
0
        count++;
271
0
    } else
272
66
#endif
273
66
    if (j >= -1) {
274
132
        SPLIT_ADD(str, 0, j + 1);
275
132
    }
276
66
    FIX_PREALLOC_SIZE(list);
277
66
    if (PyList_Reverse(list) < 0)
278
0
        goto onError;
279
66
    return list;
280
281
0
  onError:
282
0
    Py_DECREF(list);
283
0
    return NULL;
284
66
}
Unexecuted instantiation: bytesobject.c:stringlib_rsplit_char
unicodeobject.c:asciilib_rsplit_char
Line
Count
Source
247
66
{
248
66
    Py_ssize_t i, j, count=0;
249
66
    PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
250
66
    PyObject *sub;
251
252
66
    if (list == NULL)
253
0
        return NULL;
254
255
66
    i = j = str_len - 1;
256
132
    while ((i >= 0) && (maxcount-- > 0)) {
257
138
        for(; i >= 0; i--) {
258
138
            if (str[i] == ch) {
259
66
                SPLIT_ADD(str, i + 1, j + 1);
260
66
                j = i = i - 1;
261
66
                break;
262
66
            }
263
138
        }
264
66
    }
265
66
#if !STRINGLIB_MUTABLE
266
66
    if (count == 0 && STRINGLIB_CHECK_EXACT(str_obj)) {
267
        /* ch not in str_obj, so just use str_obj as list[0] */
268
0
        Py_INCREF(str_obj);
269
0
        PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
270
0
        count++;
271
0
    } else
272
66
#endif
273
66
    if (j >= -1) {
274
132
        SPLIT_ADD(str, 0, j + 1);
275
132
    }
276
66
    FIX_PREALLOC_SIZE(list);
277
66
    if (PyList_Reverse(list) < 0)
278
0
        goto onError;
279
66
    return list;
280
281
0
  onError:
282
0
    Py_DECREF(list);
283
    return NULL;
284
66
}
Unexecuted instantiation: unicodeobject.c:ucs1lib_rsplit_char
Unexecuted instantiation: unicodeobject.c:ucs2lib_rsplit_char
Unexecuted instantiation: unicodeobject.c:ucs4lib_rsplit_char
Unexecuted instantiation: bytearrayobject.c:stringlib_rsplit_char
285
286
Py_LOCAL_INLINE(PyObject *)
287
STRINGLIB(rsplit)(PyObject* str_obj,
288
                 const STRINGLIB_CHAR* str, Py_ssize_t str_len,
289
                 const STRINGLIB_CHAR* sep, Py_ssize_t sep_len,
290
                 Py_ssize_t maxcount)
291
66
{
292
66
    Py_ssize_t j, pos, count=0;
293
66
    PyObject *list, *sub;
294
295
66
    if (sep_len == 0) {
296
0
        PyErr_SetString(PyExc_ValueError, "empty separator");
297
0
        return NULL;
298
0
    }
299
66
    else if (sep_len == 1)
300
66
        return STRINGLIB(rsplit_char)(str_obj, str, str_len, sep[0], maxcount);
301
302
0
    list = PyList_New(PREALLOC_SIZE(maxcount));
303
0
    if (list == NULL)
304
0
        return NULL;
305
306
0
    j = str_len;
307
0
    while (maxcount-- > 0) {
308
0
        pos = FASTSEARCH(str, j, sep, sep_len, -1, FAST_RSEARCH);
309
0
        if (pos < 0)
310
0
            break;
311
0
        SPLIT_ADD(str, pos + sep_len, j);
312
0
        j = pos;
313
0
    }
314
#if !STRINGLIB_MUTABLE
315
0
    if (count == 0 && STRINGLIB_CHECK_EXACT(str_obj)) {
316
        /* No match in str_obj, so just use it as list[0] */
317
0
        Py_INCREF(str_obj);
318
0
        PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
319
0
        count++;
320
0
    } else
321
0
#endif
322
0
    {
323
0
        SPLIT_ADD(str, 0, j);
324
0
    }
325
0
    FIX_PREALLOC_SIZE(list);
326
0
    if (PyList_Reverse(list) < 0)
327
0
        goto onError;
328
0
    return list;
329
330
0
  onError:
331
0
    Py_DECREF(list);
332
0
    return NULL;
333
0
}
Unexecuted instantiation: bytesobject.c:stringlib_rsplit
unicodeobject.c:asciilib_rsplit
Line
Count
Source
291
66
{
292
66
    Py_ssize_t j, pos, count=0;
293
66
    PyObject *list, *sub;
294
295
66
    if (sep_len == 0) {
296
0
        PyErr_SetString(PyExc_ValueError, "empty separator");
297
0
        return NULL;
298
0
    }
299
66
    else if (sep_len == 1)
300
66
        return STRINGLIB(rsplit_char)(str_obj, str, str_len, sep[0], maxcount);
301
302
0
    list = PyList_New(PREALLOC_SIZE(maxcount));
303
0
    if (list == NULL)
304
0
        return NULL;
305
306
0
    j = str_len;
307
0
    while (maxcount-- > 0) {
308
0
        pos = FASTSEARCH(str, j, sep, sep_len, -1, FAST_RSEARCH);
309
0
        if (pos < 0)
310
0
            break;
311
0
        SPLIT_ADD(str, pos + sep_len, j);
312
0
        j = pos;
313
0
    }
314
0
#if !STRINGLIB_MUTABLE
315
0
    if (count == 0 && STRINGLIB_CHECK_EXACT(str_obj)) {
316
        /* No match in str_obj, so just use it as list[0] */
317
0
        Py_INCREF(str_obj);
318
0
        PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
319
0
        count++;
320
0
    } else
321
0
#endif
322
0
    {
323
0
        SPLIT_ADD(str, 0, j);
324
0
    }
325
0
    FIX_PREALLOC_SIZE(list);
326
0
    if (PyList_Reverse(list) < 0)
327
0
        goto onError;
328
0
    return list;
329
330
0
  onError:
331
0
    Py_DECREF(list);
332
    return NULL;
333
0
}
Unexecuted instantiation: unicodeobject.c:ucs1lib_rsplit
Unexecuted instantiation: unicodeobject.c:ucs2lib_rsplit
Unexecuted instantiation: unicodeobject.c:ucs4lib_rsplit
Unexecuted instantiation: bytearrayobject.c:stringlib_rsplit
334
335
Py_LOCAL_INLINE(PyObject *)
336
STRINGLIB(splitlines)(PyObject* str_obj,
337
                     const STRINGLIB_CHAR* str, Py_ssize_t str_len,
338
                     int keepends)
339
18.1k
{
340
    /* This does not use the preallocated list because splitlines is
341
       usually run with hundreds of newlines.  The overhead of
342
       switching between PyList_SET_ITEM and append causes about a
343
       2-3% slowdown for that common case.  A smarter implementation
344
       could move the if check out, so the SET_ITEMs are done first
345
       and the appends only done when the prealloc buffer is full.
346
       That's too much work for little gain.*/
347
348
18.1k
    Py_ssize_t i;
349
18.1k
    Py_ssize_t j;
350
18.1k
    PyObject *list = PyList_New(0);
351
18.1k
    PyObject *sub;
352
353
18.1k
    if (list == NULL)
354
0
        return NULL;
355
356
34.3M
    for (i = j = 0; i < str_len; ) {
357
34.3M
        Py_ssize_t eol;
358
359
        /* Find a line and append it */
360
137M
        while (i < str_len && !STRINGLIB_ISLINEBREAK(str[i]))
361
102M
            i++;
362
363
        /* Skip the line break reading CRLF as one line break */
364
34.3M
        eol = i;
365
34.3M
        if (i < str_len) {
366
34.3M
            if (str[i] == '\r' && i + 1 < str_len && str[i+1] == '\n')
367
29.5k
                i += 2;
368
34.2M
            else
369
34.2M
                i++;
370
34.3M
            if (keepends)
371
0
                eol = i;
372
34.3M
        }
373
#if !STRINGLIB_MUTABLE
374
34.3M
        if (j == 0 && eol == str_len && STRINGLIB_CHECK_EXACT(str_obj)) {
375
            /* No linebreak in str_obj, so just use it as list[0] */
376
7.59k
            if (PyList_Append(list, str_obj))
377
0
                goto onError;
378
7.59k
            break;
379
7.59k
        }
380
34.3M
#endif
381
68.6M
        SPLIT_APPEND(str, j, eol);
382
34.3M
        j = i;
383
34.3M
    }
384
18.1k
    return list;
385
386
0
  onError:
387
0
    Py_DECREF(list);
388
    return NULL;
389
18.1k
}
Unexecuted instantiation: bytesobject.c:stringlib_splitlines
unicodeobject.c:asciilib_splitlines
Line
Count
Source
339
4.20k
{
340
    /* This does not use the preallocated list because splitlines is
341
       usually run with hundreds of newlines.  The overhead of
342
       switching between PyList_SET_ITEM and append causes about a
343
       2-3% slowdown for that common case.  A smarter implementation
344
       could move the if check out, so the SET_ITEMs are done first
345
       and the appends only done when the prealloc buffer is full.
346
       That's too much work for little gain.*/
347
348
4.20k
    Py_ssize_t i;
349
4.20k
    Py_ssize_t j;
350
4.20k
    PyObject *list = PyList_New(0);
351
4.20k
    PyObject *sub;
352
353
4.20k
    if (list == NULL)
354
0
        return NULL;
355
356
7.78M
    for (i = j = 0; i < str_len; ) {
357
7.78M
        Py_ssize_t eol;
358
359
        /* Find a line and append it */
360
14.6M
        while (i < str_len && !STRINGLIB_ISLINEBREAK(str[i]))
361
6.83M
            i++;
362
363
        /* Skip the line break reading CRLF as one line break */
364
7.78M
        eol = i;
365
7.78M
        if (i < str_len) {
366
7.78M
            if (str[i] == '\r' && i + 1 < str_len && str[i+1] == '\n')
367
630
                i += 2;
368
7.78M
            else
369
7.78M
                i++;
370
7.78M
            if (keepends)
371
0
                eol = i;
372
7.78M
        }
373
7.78M
#if !STRINGLIB_MUTABLE
374
7.78M
        if (j == 0 && eol == str_len && STRINGLIB_CHECK_EXACT(str_obj)) {
375
            /* No linebreak in str_obj, so just use it as list[0] */
376
1.25k
            if (PyList_Append(list, str_obj))
377
0
                goto onError;
378
1.25k
            break;
379
1.25k
        }
380
7.78M
#endif
381
15.5M
        SPLIT_APPEND(str, j, eol);
382
7.78M
        j = i;
383
7.78M
    }
384
4.20k
    return list;
385
386
0
  onError:
387
0
    Py_DECREF(list);
388
    return NULL;
389
4.20k
}
unicodeobject.c:ucs1lib_splitlines
Line
Count
Source
339
1.05k
{
340
    /* This does not use the preallocated list because splitlines is
341
       usually run with hundreds of newlines.  The overhead of
342
       switching between PyList_SET_ITEM and append causes about a
343
       2-3% slowdown for that common case.  A smarter implementation
344
       could move the if check out, so the SET_ITEMs are done first
345
       and the appends only done when the prealloc buffer is full.
346
       That's too much work for little gain.*/
347
348
1.05k
    Py_ssize_t i;
349
1.05k
    Py_ssize_t j;
350
1.05k
    PyObject *list = PyList_New(0);
351
1.05k
    PyObject *sub;
352
353
1.05k
    if (list == NULL)
354
0
        return NULL;
355
356
2.15M
    for (i = j = 0; i < str_len; ) {
357
2.15M
        Py_ssize_t eol;
358
359
        /* Find a line and append it */
360
12.0M
        while (i < str_len && !STRINGLIB_ISLINEBREAK(str[i]))
361
9.84M
            i++;
362
363
        /* Skip the line break reading CRLF as one line break */
364
2.15M
        eol = i;
365
2.15M
        if (i < str_len) {
366
2.15M
            if (str[i] == '\r' && i + 1 < str_len && str[i+1] == '\n')
367
814
                i += 2;
368
2.15M
            else
369
2.15M
                i++;
370
2.15M
            if (keepends)
371
0
                eol = i;
372
2.15M
        }
373
2.15M
#if !STRINGLIB_MUTABLE
374
2.15M
        if (j == 0 && eol == str_len && STRINGLIB_CHECK_EXACT(str_obj)) {
375
            /* No linebreak in str_obj, so just use it as list[0] */
376
295
            if (PyList_Append(list, str_obj))
377
0
                goto onError;
378
295
            break;
379
295
        }
380
2.15M
#endif
381
4.31M
        SPLIT_APPEND(str, j, eol);
382
2.15M
        j = i;
383
2.15M
    }
384
1.05k
    return list;
385
386
0
  onError:
387
0
    Py_DECREF(list);
388
    return NULL;
389
1.05k
}
unicodeobject.c:ucs2lib_splitlines
Line
Count
Source
339
9.42k
{
340
    /* This does not use the preallocated list because splitlines is
341
       usually run with hundreds of newlines.  The overhead of
342
       switching between PyList_SET_ITEM and append causes about a
343
       2-3% slowdown for that common case.  A smarter implementation
344
       could move the if check out, so the SET_ITEMs are done first
345
       and the appends only done when the prealloc buffer is full.
346
       That's too much work for little gain.*/
347
348
9.42k
    Py_ssize_t i;
349
9.42k
    Py_ssize_t j;
350
9.42k
    PyObject *list = PyList_New(0);
351
9.42k
    PyObject *sub;
352
353
9.42k
    if (list == NULL)
354
0
        return NULL;
355
356
13.5M
    for (i = j = 0; i < str_len; ) {
357
13.5M
        Py_ssize_t eol;
358
359
        /* Find a line and append it */
360
51.6M
        while (i < str_len && !STRINGLIB_ISLINEBREAK(str[i]))
361
38.1M
            i++;
362
363
        /* Skip the line break reading CRLF as one line break */
364
13.5M
        eol = i;
365
13.5M
        if (i < str_len) {
366
13.5M
            if (str[i] == '\r' && i + 1 < str_len && str[i+1] == '\n')
367
9.86k
                i += 2;
368
13.5M
            else
369
13.5M
                i++;
370
13.5M
            if (keepends)
371
0
                eol = i;
372
13.5M
        }
373
13.5M
#if !STRINGLIB_MUTABLE
374
13.5M
        if (j == 0 && eol == str_len && STRINGLIB_CHECK_EXACT(str_obj)) {
375
            /* No linebreak in str_obj, so just use it as list[0] */
376
4.39k
            if (PyList_Append(list, str_obj))
377
0
                goto onError;
378
4.39k
            break;
379
4.39k
        }
380
13.5M
#endif
381
27.1M
        SPLIT_APPEND(str, j, eol);
382
13.5M
        j = i;
383
13.5M
    }
384
9.42k
    return list;
385
386
0
  onError:
387
0
    Py_DECREF(list);
388
    return NULL;
389
9.42k
}
unicodeobject.c:ucs4lib_splitlines
Line
Count
Source
339
3.49k
{
340
    /* This does not use the preallocated list because splitlines is
341
       usually run with hundreds of newlines.  The overhead of
342
       switching between PyList_SET_ITEM and append causes about a
343
       2-3% slowdown for that common case.  A smarter implementation
344
       could move the if check out, so the SET_ITEMs are done first
345
       and the appends only done when the prealloc buffer is full.
346
       That's too much work for little gain.*/
347
348
3.49k
    Py_ssize_t i;
349
3.49k
    Py_ssize_t j;
350
3.49k
    PyObject *list = PyList_New(0);
351
3.49k
    PyObject *sub;
352
353
3.49k
    if (list == NULL)
354
0
        return NULL;
355
356
10.8M
    for (i = j = 0; i < str_len; ) {
357
10.8M
        Py_ssize_t eol;
358
359
        /* Find a line and append it */
360
58.8M
        while (i < str_len && !STRINGLIB_ISLINEBREAK(str[i]))
361
48.0M
            i++;
362
363
        /* Skip the line break reading CRLF as one line break */
364
10.8M
        eol = i;
365
10.8M
        if (i < str_len) {
366
10.8M
            if (str[i] == '\r' && i + 1 < str_len && str[i+1] == '\n')
367
18.1k
                i += 2;
368
10.8M
            else
369
10.8M
                i++;
370
10.8M
            if (keepends)
371
0
                eol = i;
372
10.8M
        }
373
10.8M
#if !STRINGLIB_MUTABLE
374
10.8M
        if (j == 0 && eol == str_len && STRINGLIB_CHECK_EXACT(str_obj)) {
375
            /* No linebreak in str_obj, so just use it as list[0] */
376
1.64k
            if (PyList_Append(list, str_obj))
377
0
                goto onError;
378
1.64k
            break;
379
1.64k
        }
380
10.8M
#endif
381
21.6M
        SPLIT_APPEND(str, j, eol);
382
10.8M
        j = i;
383
10.8M
    }
384
3.49k
    return list;
385
386
0
  onError:
387
0
    Py_DECREF(list);
388
    return NULL;
389
3.49k
}
Unexecuted instantiation: bytearrayobject.c:stringlib_splitlines
390