Coverage Report

Created: 2026-05-30 06:18

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
112M
#define MAX_PREALLOC 12
15
16
/* 5 splits gives 6 elements */
17
#define PREALLOC_SIZE(maxsplit) \
18
23.3M
    (maxsplit >= MAX_PREALLOC ? MAX_PREALLOC : maxsplit+1)
19
20
#define SPLIT_APPEND(data, left, right)         \
21
29.8M
    sub = STRINGLIB_NEW((data) + (left),        \
22
29.8M
                        (right) - (left));      \
23
29.8M
    if (sub == NULL)                            \
24
29.8M
        goto onError;                           \
25
29.8M
    if (PyList_Append(list, sub)) {             \
26
0
        Py_DECREF(sub);                         \
27
0
        goto onError;                           \
28
0
    }                                           \
29
29.8M
    else                                        \
30
29.8M
        Py_DECREF(sub);
31
32
61.9M
#define SPLIT_ADD(data, left, right) {          \
33
61.9M
    sub = STRINGLIB_NEW((data) + (left),        \
34
61.9M
                        (right) - (left));      \
35
61.9M
    if (sub == NULL)                            \
36
61.9M
        goto onError;                           \
37
61.9M
    if (count < MAX_PREALLOC) {                 \
38
36.8M
        PyList_SET_ITEM(list, count, sub);      \
39
36.8M
    } else {                                    \
40
25.0M
        if (PyList_Append(list, sub)) {         \
41
0
            Py_DECREF(sub);                     \
42
0
            goto onError;                       \
43
0
        }                                       \
44
25.0M
        else                                    \
45
25.0M
            Py_DECREF(sub);                     \
46
25.0M
    }                                           \
47
61.9M
    count++; }
48
49
50
/* Always force the list to the expected size. */
51
23.3M
#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
185k
{
58
185k
    Py_ssize_t i, j, count=0;
59
185k
    PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
60
185k
    PyObject *sub;
61
62
185k
    if (list == NULL)
63
0
        return NULL;
64
65
185k
    i = j = 0;
66
1.98M
    while (maxcount-- > 0) {
67
3.79M
        while (i < str_len && STRINGLIB_ISSPACE(str[i]))
68
1.84M
            i++;
69
1.95M
        if (i == str_len) break;
70
1.84M
        j = i; i++;
71
144M
        while (i < str_len && !STRINGLIB_ISSPACE(str[i]))
72
142M
            i++;
73
#if !STRINGLIB_MUTABLE
74
1.84M
        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
52.8k
            Py_INCREF(str_obj);
77
52.8k
            PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
78
52.8k
            count++;
79
52.8k
            break;
80
52.8k
        }
81
1.79M
#endif
82
5.38M
        SPLIT_ADD(str, j, i);
83
5.38M
    }
84
85
185k
    if (i < str_len) {
86
        /* Only occurs when maxcount was reached */
87
        /* Skip any remaining whitespace and copy to end of string */
88
54.3k
        while (i < str_len && STRINGLIB_ISSPACE(str[i]))
89
29.5k
            i++;
90
24.8k
        if (i != str_len)
91
24.8k
            SPLIT_ADD(str, i, str_len);
92
24.8k
    }
93
185k
    FIX_PREALLOC_SIZE(list);
94
185k
    return list;
95
96
0
  onError:
97
0
    Py_DECREF(list);
98
0
    return NULL;
99
185k
}
Unexecuted instantiation: bytesobject.c:stringlib_split_whitespace
unicodeobject.c:asciilib_split_whitespace
Line
Count
Source
57
89.4k
{
58
89.4k
    Py_ssize_t i, j, count=0;
59
89.4k
    PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
60
89.4k
    PyObject *sub;
61
62
89.4k
    if (list == NULL)
63
0
        return NULL;
64
65
89.4k
    i = j = 0;
66
579k
    while (maxcount-- > 0) {
67
1.02M
        while (i < str_len && STRINGLIB_ISSPACE(str[i]))
68
456k
            i++;
69
565k
        if (i == str_len) break;
70
524k
        j = i; i++;
71
49.5M
        while (i < str_len && !STRINGLIB_ISSPACE(str[i]))
72
49.0M
            i++;
73
524k
#if !STRINGLIB_MUTABLE
74
524k
        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
34.6k
            Py_INCREF(str_obj);
77
34.6k
            PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
78
34.6k
            count++;
79
34.6k
            break;
80
34.6k
        }
81
490k
#endif
82
1.47M
        SPLIT_ADD(str, j, i);
83
1.47M
    }
84
85
89.4k
    if (i < str_len) {
86
        /* Only occurs when maxcount was reached */
87
        /* Skip any remaining whitespace and copy to end of string */
88
26.4k
        while (i < str_len && STRINGLIB_ISSPACE(str[i]))
89
13.7k
            i++;
90
12.6k
        if (i != str_len)
91
12.6k
            SPLIT_ADD(str, i, str_len);
92
12.6k
    }
93
89.4k
    FIX_PREALLOC_SIZE(list);
94
89.4k
    return list;
95
96
0
  onError:
97
0
    Py_DECREF(list);
98
    return NULL;
99
89.4k
}
unicodeobject.c:ucs1lib_split_whitespace
Line
Count
Source
57
26.3k
{
58
26.3k
    Py_ssize_t i, j, count=0;
59
26.3k
    PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
60
26.3k
    PyObject *sub;
61
62
26.3k
    if (list == NULL)
63
0
        return NULL;
64
65
26.3k
    i = j = 0;
66
652k
    while (maxcount-- > 0) {
67
1.25M
        while (i < str_len && STRINGLIB_ISSPACE(str[i]))
68
609k
            i++;
69
640k
        if (i == str_len) break;
70
628k
        j = i; i++;
71
41.5M
        while (i < str_len && !STRINGLIB_ISSPACE(str[i]))
72
40.9M
            i++;
73
628k
#if !STRINGLIB_MUTABLE
74
628k
        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
1.96k
            Py_INCREF(str_obj);
77
1.96k
            PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
78
1.96k
            count++;
79
1.96k
            break;
80
1.96k
        }
81
626k
#endif
82
1.87M
        SPLIT_ADD(str, j, i);
83
1.87M
    }
84
85
26.3k
    if (i < str_len) {
86
        /* Only occurs when maxcount was reached */
87
        /* Skip any remaining whitespace and copy to end of string */
88
26.2k
        while (i < str_len && STRINGLIB_ISSPACE(str[i]))
89
14.9k
            i++;
90
11.2k
        if (i != str_len)
91
11.2k
            SPLIT_ADD(str, i, str_len);
92
11.2k
    }
93
26.3k
    FIX_PREALLOC_SIZE(list);
94
26.3k
    return list;
95
96
0
  onError:
97
0
    Py_DECREF(list);
98
    return NULL;
99
26.3k
}
unicodeobject.c:ucs2lib_split_whitespace
Line
Count
Source
57
56.6k
{
58
56.6k
    Py_ssize_t i, j, count=0;
59
56.6k
    PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
60
56.6k
    PyObject *sub;
61
62
56.6k
    if (list == NULL)
63
0
        return NULL;
64
65
56.6k
    i = j = 0;
66
659k
    while (maxcount-- > 0) {
67
1.35M
        while (i < str_len && STRINGLIB_ISSPACE(str[i]))
68
694k
            i++;
69
659k
        if (i == str_len) break;
70
616k
        j = i; i++;
71
42.7M
        while (i < str_len && !STRINGLIB_ISSPACE(str[i]))
72
42.1M
            i++;
73
616k
#if !STRINGLIB_MUTABLE
74
616k
        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
12.9k
            Py_INCREF(str_obj);
77
12.9k
            PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
78
12.9k
            count++;
79
12.9k
            break;
80
12.9k
        }
81
603k
#endif
82
1.81M
        SPLIT_ADD(str, j, i);
83
1.81M
    }
84
85
56.6k
    if (i < str_len) {
86
        /* Only occurs when maxcount was reached */
87
        /* Skip any remaining whitespace and copy to end of string */
88
1.67k
        while (i < str_len && STRINGLIB_ISSPACE(str[i]))
89
838
            i++;
90
838
        if (i != str_len)
91
838
            SPLIT_ADD(str, i, str_len);
92
838
    }
93
56.6k
    FIX_PREALLOC_SIZE(list);
94
56.6k
    return list;
95
96
0
  onError:
97
0
    Py_DECREF(list);
98
    return NULL;
99
56.6k
}
unicodeobject.c:ucs4lib_split_whitespace
Line
Count
Source
57
12.5k
{
58
12.5k
    Py_ssize_t i, j, count=0;
59
12.5k
    PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
60
12.5k
    PyObject *sub;
61
62
12.5k
    if (list == NULL)
63
0
        return NULL;
64
65
12.5k
    i = j = 0;
66
88.2k
    while (maxcount-- > 0) {
67
169k
        while (i < str_len && STRINGLIB_ISSPACE(str[i]))
68
81.9k
            i++;
69
88.0k
        if (i == str_len) break;
70
79.0k
        j = i; i++;
71
10.4M
        while (i < str_len && !STRINGLIB_ISSPACE(str[i]))
72
10.3M
            i++;
73
79.0k
#if !STRINGLIB_MUTABLE
74
79.0k
        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
3.31k
            Py_INCREF(str_obj);
77
3.31k
            PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
78
3.31k
            count++;
79
3.31k
            break;
80
3.31k
        }
81
75.7k
#endif
82
227k
        SPLIT_ADD(str, j, i);
83
227k
    }
84
85
12.5k
    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
12.5k
    FIX_PREALLOC_SIZE(list);
94
12.5k
    return list;
95
96
0
  onError:
97
0
    Py_DECREF(list);
98
    return NULL;
99
12.5k
}
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.9M
{
107
22.9M
    Py_ssize_t i, j, count=0;
108
22.9M
    PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
109
22.9M
    PyObject *sub;
110
111
22.9M
    if (list == NULL)
112
0
        return NULL;
113
114
22.9M
    i = j = 0;
115
86.2M
    while ((j < str_len) && (maxcount-- > 0)) {
116
475M
        for(; j < str_len; j++) {
117
            /* I found that using memchr makes no difference */
118
454M
            if (str[j] == ch) {
119
42.4M
                SPLIT_ADD(str, i, j);
120
42.4M
                i = j = j + 1;
121
42.4M
                break;
122
42.4M
            }
123
454M
        }
124
63.2M
    }
125
#if !STRINGLIB_MUTABLE
126
22.9M
    if (count == 0 && STRINGLIB_CHECK_EXACT(str_obj)) {
127
        /* ch not in str_obj, so just use str_obj as list[0] */
128
5.56M
        Py_INCREF(str_obj);
129
5.56M
        PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
130
5.56M
        count++;
131
5.56M
    } else
132
17.3M
#endif
133
17.3M
    if (i <= str_len) {
134
34.7M
        SPLIT_ADD(str, i, str_len);
135
34.7M
    }
136
22.9M
    FIX_PREALLOC_SIZE(list);
137
22.9M
    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
3.01M
{
107
3.01M
    Py_ssize_t i, j, count=0;
108
3.01M
    PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
109
3.01M
    PyObject *sub;
110
111
3.01M
    if (list == NULL)
112
0
        return NULL;
113
114
3.01M
    i = j = 0;
115
12.9M
    while ((j < str_len) && (maxcount-- > 0)) {
116
92.1M
        for(; j < str_len; j++) {
117
            /* I found that using memchr makes no difference */
118
89.5M
            if (str[j] == ch) {
119
7.31M
                SPLIT_ADD(str, i, j);
120
7.31M
                i = j = j + 1;
121
7.31M
                break;
122
7.31M
            }
123
89.5M
        }
124
9.90M
    }
125
3.01M
#if !STRINGLIB_MUTABLE
126
3.01M
    if (count == 0 && STRINGLIB_CHECK_EXACT(str_obj)) {
127
        /* ch not in str_obj, so just use str_obj as list[0] */
128
2.50M
        Py_INCREF(str_obj);
129
2.50M
        PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
130
2.50M
        count++;
131
2.50M
    } else
132
514k
#endif
133
514k
    if (i <= str_len) {
134
1.02M
        SPLIT_ADD(str, i, str_len);
135
1.02M
    }
136
3.01M
    FIX_PREALLOC_SIZE(list);
137
3.01M
    return list;
138
139
0
  onError:
140
0
    Py_DECREF(list);
141
    return NULL;
142
3.01M
}
unicodeobject.c:asciilib_split_char
Line
Count
Source
106
18.6M
{
107
18.6M
    Py_ssize_t i, j, count=0;
108
18.6M
    PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
109
18.6M
    PyObject *sub;
110
111
18.6M
    if (list == NULL)
112
0
        return NULL;
113
114
18.6M
    i = j = 0;
115
57.6M
    while ((j < str_len) && (maxcount-- > 0)) {
116
247M
        for(; j < str_len; j++) {
117
            /* I found that using memchr makes no difference */
118
229M
            if (str[j] == ch) {
119
21.0M
                SPLIT_ADD(str, i, j);
120
21.0M
                i = j = j + 1;
121
21.0M
                break;
122
21.0M
            }
123
229M
        }
124
39.0M
    }
125
18.6M
#if !STRINGLIB_MUTABLE
126
18.6M
    if (count == 0 && STRINGLIB_CHECK_EXACT(str_obj)) {
127
        /* ch not in str_obj, so just use str_obj as list[0] */
128
2.85M
        Py_INCREF(str_obj);
129
2.85M
        PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
130
2.85M
        count++;
131
2.85M
    } else
132
15.7M
#endif
133
15.7M
    if (i <= str_len) {
134
31.5M
        SPLIT_ADD(str, i, str_len);
135
31.5M
    }
136
18.6M
    FIX_PREALLOC_SIZE(list);
137
18.6M
    return list;
138
139
0
  onError:
140
0
    Py_DECREF(list);
141
    return NULL;
142
18.6M
}
unicodeobject.c:ucs1lib_split_char
Line
Count
Source
106
1.13M
{
107
1.13M
    Py_ssize_t i, j, count=0;
108
1.13M
    PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
109
1.13M
    PyObject *sub;
110
111
1.13M
    if (list == NULL)
112
0
        return NULL;
113
114
1.13M
    i = j = 0;
115
10.0M
    while ((j < str_len) && (maxcount-- > 0)) {
116
64.4M
        for(; j < str_len; j++) {
117
            /* I found that using memchr makes no difference */
118
64.2M
            if (str[j] == ch) {
119
8.71M
                SPLIT_ADD(str, i, j);
120
8.71M
                i = j = j + 1;
121
8.71M
                break;
122
8.71M
            }
123
64.2M
        }
124
8.91M
    }
125
1.13M
#if !STRINGLIB_MUTABLE
126
1.13M
    if (count == 0 && STRINGLIB_CHECK_EXACT(str_obj)) {
127
        /* ch not in str_obj, so just use str_obj as list[0] */
128
170k
        Py_INCREF(str_obj);
129
170k
        PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
130
170k
        count++;
131
170k
    } else
132
968k
#endif
133
968k
    if (i <= str_len) {
134
1.93M
        SPLIT_ADD(str, i, str_len);
135
1.93M
    }
136
1.13M
    FIX_PREALLOC_SIZE(list);
137
1.13M
    return list;
138
139
0
  onError:
140
0
    Py_DECREF(list);
141
    return NULL;
142
1.13M
}
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
5.28M
    while ((j < str_len) && (maxcount-- > 0)) {
116
50.3M
        for(; j < str_len; j++) {
117
            /* I found that using memchr makes no difference */
118
50.2M
            if (str[j] == ch) {
119
5.06M
                SPLIT_ADD(str, i, j);
120
5.06M
                i = j = j + 1;
121
5.06M
                break;
122
5.06M
            }
123
50.2M
        }
124
5.15M
    }
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
31.3k
        Py_INCREF(str_obj);
129
31.3k
        PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
130
31.3k
        count++;
131
31.3k
    } else
132
94.0k
#endif
133
94.0k
    if (i <= str_len) {
134
188k
        SPLIT_ADD(str, i, str_len);
135
188k
    }
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
16.8k
{
107
16.8k
    Py_ssize_t i, j, count=0;
108
16.8k
    PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
109
16.8k
    PyObject *sub;
110
111
16.8k
    if (list == NULL)
112
0
        return NULL;
113
114
16.8k
    i = j = 0;
115
260k
    while ((j < str_len) && (maxcount-- > 0)) {
116
21.4M
        for(; j < str_len; j++) {
117
            /* I found that using memchr makes no difference */
118
21.4M
            if (str[j] == ch) {
119
230k
                SPLIT_ADD(str, i, j);
120
230k
                i = j = j + 1;
121
230k
                break;
122
230k
            }
123
21.4M
        }
124
243k
    }
125
16.8k
#if !STRINGLIB_MUTABLE
126
16.8k
    if (count == 0 && STRINGLIB_CHECK_EXACT(str_obj)) {
127
        /* ch not in str_obj, so just use str_obj as list[0] */
128
893
        Py_INCREF(str_obj);
129
893
        PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
130
893
        count++;
131
893
    } else
132
15.9k
#endif
133
15.9k
    if (i <= str_len) {
134
31.9k
        SPLIT_ADD(str, i, str_len);
135
31.9k
    }
136
16.8k
    FIX_PREALLOC_SIZE(list);
137
16.8k
    return list;
138
139
0
  onError:
140
0
    Py_DECREF(list);
141
    return NULL;
142
16.8k
}
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
23.1M
{
150
23.1M
    Py_ssize_t i, j, pos, count=0;
151
23.1M
    PyObject *list, *sub;
152
153
23.1M
    if (sep_len == 0) {
154
0
        PyErr_SetString(PyExc_ValueError, "empty separator");
155
0
        return NULL;
156
0
    }
157
23.1M
    else if (sep_len == 1)
158
22.9M
        return STRINGLIB(split_char)(str_obj, str, str_len, sep[0], maxcount);
159
160
239k
    list = PyList_New(PREALLOC_SIZE(maxcount));
161
239k
    if (list == NULL)
162
0
        return NULL;
163
164
239k
    i = j = 0;
165
417k
    while (maxcount-- > 0) {
166
239k
        pos = FASTSEARCH(str+i, str_len-i, sep, sep_len, -1, FAST_SEARCH);
167
239k
        if (pos < 0)
168
61.8k
            break;
169
177k
        j = i + pos;
170
355k
        SPLIT_ADD(str, i, j);
171
355k
        i = j + sep_len;
172
355k
    }
173
#if !STRINGLIB_MUTABLE
174
239k
    if (count == 0 && STRINGLIB_CHECK_EXACT(str_obj)) {
175
        /* No match in str_obj, so just use it as list[0] */
176
61.8k
        Py_INCREF(str_obj);
177
61.8k
        PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
178
61.8k
        count++;
179
61.8k
    } else
180
177k
#endif
181
177k
    {
182
355k
        SPLIT_ADD(str, i, str_len);
183
355k
    }
184
239k
    FIX_PREALLOC_SIZE(list);
185
239k
    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
3.01M
{
150
3.01M
    Py_ssize_t i, j, pos, count=0;
151
3.01M
    PyObject *list, *sub;
152
153
3.01M
    if (sep_len == 0) {
154
0
        PyErr_SetString(PyExc_ValueError, "empty separator");
155
0
        return NULL;
156
0
    }
157
3.01M
    else if (sep_len == 1)
158
3.01M
        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
18.7M
{
150
18.7M
    Py_ssize_t i, j, pos, count=0;
151
18.7M
    PyObject *list, *sub;
152
153
18.7M
    if (sep_len == 0) {
154
0
        PyErr_SetString(PyExc_ValueError, "empty separator");
155
0
        return NULL;
156
0
    }
157
18.7M
    else if (sep_len == 1)
158
18.6M
        return STRINGLIB(split_char)(str_obj, str, str_len, sep[0], maxcount);
159
160
105k
    list = PyList_New(PREALLOC_SIZE(maxcount));
161
105k
    if (list == NULL)
162
0
        return NULL;
163
164
105k
    i = j = 0;
165
167k
    while (maxcount-- > 0) {
166
105k
        pos = FASTSEARCH(str+i, str_len-i, sep, sep_len, -1, FAST_SEARCH);
167
105k
        if (pos < 0)
168
43.3k
            break;
169
62.3k
        j = i + pos;
170
124k
        SPLIT_ADD(str, i, j);
171
124k
        i = j + sep_len;
172
124k
    }
173
105k
#if !STRINGLIB_MUTABLE
174
105k
    if (count == 0 && STRINGLIB_CHECK_EXACT(str_obj)) {
175
        /* No match in str_obj, so just use it as list[0] */
176
43.3k
        Py_INCREF(str_obj);
177
43.3k
        PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
178
43.3k
        count++;
179
43.3k
    } else
180
62.3k
#endif
181
62.3k
    {
182
124k
        SPLIT_ADD(str, i, str_len);
183
124k
    }
184
105k
    FIX_PREALLOC_SIZE(list);
185
105k
    return list;
186
187
0
  onError:
188
0
    Py_DECREF(list);
189
    return NULL;
190
105k
}
unicodeobject.c:ucs1lib_split
Line
Count
Source
149
1.15M
{
150
1.15M
    Py_ssize_t i, j, pos, count=0;
151
1.15M
    PyObject *list, *sub;
152
153
1.15M
    if (sep_len == 0) {
154
0
        PyErr_SetString(PyExc_ValueError, "empty separator");
155
0
        return NULL;
156
0
    }
157
1.15M
    else if (sep_len == 1)
158
1.13M
        return STRINGLIB(split_char)(str_obj, str, str_len, sep[0], maxcount);
159
160
19.5k
    list = PyList_New(PREALLOC_SIZE(maxcount));
161
19.5k
    if (list == NULL)
162
0
        return NULL;
163
164
19.5k
    i = j = 0;
165
35.8k
    while (maxcount-- > 0) {
166
19.5k
        pos = FASTSEARCH(str+i, str_len-i, sep, sep_len, -1, FAST_SEARCH);
167
19.5k
        if (pos < 0)
168
3.25k
            break;
169
16.3k
        j = i + pos;
170
32.6k
        SPLIT_ADD(str, i, j);
171
32.6k
        i = j + sep_len;
172
32.6k
    }
173
19.5k
#if !STRINGLIB_MUTABLE
174
19.5k
    if (count == 0 && STRINGLIB_CHECK_EXACT(str_obj)) {
175
        /* No match in str_obj, so just use it as list[0] */
176
3.25k
        Py_INCREF(str_obj);
177
3.25k
        PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
178
3.25k
        count++;
179
3.25k
    } else
180
16.3k
#endif
181
16.3k
    {
182
32.6k
        SPLIT_ADD(str, i, str_len);
183
32.6k
    }
184
19.5k
    FIX_PREALLOC_SIZE(list);
185
19.5k
    return list;
186
187
0
  onError:
188
0
    Py_DECREF(list);
189
    return NULL;
190
19.5k
}
unicodeobject.c:ucs2lib_split
Line
Count
Source
149
216k
{
150
216k
    Py_ssize_t i, j, pos, count=0;
151
216k
    PyObject *list, *sub;
152
153
216k
    if (sep_len == 0) {
154
0
        PyErr_SetString(PyExc_ValueError, "empty separator");
155
0
        return NULL;
156
0
    }
157
216k
    else if (sep_len == 1)
158
125k
        return STRINGLIB(split_char)(str_obj, str, str_len, sep[0], maxcount);
159
160
91.0k
    list = PyList_New(PREALLOC_SIZE(maxcount));
161
91.0k
    if (list == NULL)
162
0
        return NULL;
163
164
91.0k
    i = j = 0;
165
170k
    while (maxcount-- > 0) {
166
91.0k
        pos = FASTSEARCH(str+i, str_len-i, sep, sep_len, -1, FAST_SEARCH);
167
91.0k
        if (pos < 0)
168
11.3k
            break;
169
79.7k
        j = i + pos;
170
159k
        SPLIT_ADD(str, i, j);
171
159k
        i = j + sep_len;
172
159k
    }
173
91.0k
#if !STRINGLIB_MUTABLE
174
91.0k
    if (count == 0 && STRINGLIB_CHECK_EXACT(str_obj)) {
175
        /* No match in str_obj, so just use it as list[0] */
176
11.3k
        Py_INCREF(str_obj);
177
11.3k
        PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
178
11.3k
        count++;
179
11.3k
    } else
180
79.7k
#endif
181
79.7k
    {
182
159k
        SPLIT_ADD(str, i, str_len);
183
159k
    }
184
91.0k
    FIX_PREALLOC_SIZE(list);
185
91.0k
    return list;
186
187
0
  onError:
188
0
    Py_DECREF(list);
189
    return NULL;
190
91.0k
}
unicodeobject.c:ucs4lib_split
Line
Count
Source
149
40.3k
{
150
40.3k
    Py_ssize_t i, j, pos, count=0;
151
40.3k
    PyObject *list, *sub;
152
153
40.3k
    if (sep_len == 0) {
154
0
        PyErr_SetString(PyExc_ValueError, "empty separator");
155
0
        return NULL;
156
0
    }
157
40.3k
    else if (sep_len == 1)
158
16.8k
        return STRINGLIB(split_char)(str_obj, str, str_len, sep[0], maxcount);
159
160
23.4k
    list = PyList_New(PREALLOC_SIZE(maxcount));
161
23.4k
    if (list == NULL)
162
0
        return NULL;
163
164
23.4k
    i = j = 0;
165
42.9k
    while (maxcount-- > 0) {
166
23.4k
        pos = FASTSEARCH(str+i, str_len-i, sep, sep_len, -1, FAST_SEARCH);
167
23.4k
        if (pos < 0)
168
3.93k
            break;
169
19.5k
        j = i + pos;
170
39.0k
        SPLIT_ADD(str, i, j);
171
39.0k
        i = j + sep_len;
172
39.0k
    }
173
23.4k
#if !STRINGLIB_MUTABLE
174
23.4k
    if (count == 0 && STRINGLIB_CHECK_EXACT(str_obj)) {
175
        /* No match in str_obj, so just use it as list[0] */
176
3.93k
        Py_INCREF(str_obj);
177
3.93k
        PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
178
3.93k
        count++;
179
3.93k
    } else
180
19.5k
#endif
181
19.5k
    {
182
39.0k
        SPLIT_ADD(str, i, str_len);
183
39.0k
    }
184
23.4k
    FIX_PREALLOC_SIZE(list);
185
23.4k
    return list;
186
187
0
  onError:
188
0
    Py_DECREF(list);
189
    return NULL;
190
23.4k
}
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
17.3k
{
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
17.3k
    Py_ssize_t i;
349
17.3k
    Py_ssize_t j;
350
17.3k
    PyObject *list = PyList_New(0);
351
17.3k
    PyObject *sub;
352
353
17.3k
    if (list == NULL)
354
0
        return NULL;
355
356
29.8M
    for (i = j = 0; i < str_len; ) {
357
29.8M
        Py_ssize_t eol;
358
359
        /* Find a line and append it */
360
124M
        while (i < str_len && !STRINGLIB_ISLINEBREAK(str[i]))
361
94.6M
            i++;
362
363
        /* Skip the line break reading CRLF as one line break */
364
29.8M
        eol = i;
365
29.8M
        if (i < str_len) {
366
29.8M
            if (str[i] == '\r' && i + 1 < str_len && str[i+1] == '\n')
367
28.1k
                i += 2;
368
29.8M
            else
369
29.8M
                i++;
370
29.8M
            if (keepends)
371
0
                eol = i;
372
29.8M
        }
373
#if !STRINGLIB_MUTABLE
374
29.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
7.19k
            if (PyList_Append(list, str_obj))
377
0
                goto onError;
378
7.19k
            break;
379
7.19k
        }
380
29.8M
#endif
381
59.6M
        SPLIT_APPEND(str, j, eol);
382
29.8M
        j = i;
383
29.8M
    }
384
17.3k
    return list;
385
386
0
  onError:
387
0
    Py_DECREF(list);
388
    return NULL;
389
17.3k
}
Unexecuted instantiation: bytesobject.c:stringlib_splitlines
unicodeobject.c:asciilib_splitlines
Line
Count
Source
339
3.92k
{
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.92k
    Py_ssize_t i;
349
3.92k
    Py_ssize_t j;
350
3.92k
    PyObject *list = PyList_New(0);
351
3.92k
    PyObject *sub;
352
353
3.92k
    if (list == NULL)
354
0
        return NULL;
355
356
7.08M
    for (i = j = 0; i < str_len; ) {
357
7.07M
        Py_ssize_t eol;
358
359
        /* Find a line and append it */
360
13.6M
        while (i < str_len && !STRINGLIB_ISLINEBREAK(str[i]))
361
6.54M
            i++;
362
363
        /* Skip the line break reading CRLF as one line break */
364
7.07M
        eol = i;
365
7.07M
        if (i < str_len) {
366
7.07M
            if (str[i] == '\r' && i + 1 < str_len && str[i+1] == '\n')
367
458
                i += 2;
368
7.07M
            else
369
7.07M
                i++;
370
7.07M
            if (keepends)
371
0
                eol = i;
372
7.07M
        }
373
7.07M
#if !STRINGLIB_MUTABLE
374
7.07M
        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.11k
            if (PyList_Append(list, str_obj))
377
0
                goto onError;
378
1.11k
            break;
379
1.11k
        }
380
7.07M
#endif
381
14.1M
        SPLIT_APPEND(str, j, eol);
382
7.07M
        j = i;
383
7.07M
    }
384
3.92k
    return list;
385
386
0
  onError:
387
0
    Py_DECREF(list);
388
    return NULL;
389
3.92k
}
unicodeobject.c:ucs1lib_splitlines
Line
Count
Source
339
984
{
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
984
    Py_ssize_t i;
349
984
    Py_ssize_t j;
350
984
    PyObject *list = PyList_New(0);
351
984
    PyObject *sub;
352
353
984
    if (list == NULL)
354
0
        return NULL;
355
356
1.15M
    for (i = j = 0; i < str_len; ) {
357
1.15M
        Py_ssize_t eol;
358
359
        /* Find a line and append it */
360
10.5M
        while (i < str_len && !STRINGLIB_ISLINEBREAK(str[i]))
361
9.43M
            i++;
362
363
        /* Skip the line break reading CRLF as one line break */
364
1.15M
        eol = i;
365
1.15M
        if (i < str_len) {
366
1.15M
            if (str[i] == '\r' && i + 1 < str_len && str[i+1] == '\n')
367
764
                i += 2;
368
1.15M
            else
369
1.15M
                i++;
370
1.15M
            if (keepends)
371
0
                eol = i;
372
1.15M
        }
373
1.15M
#if !STRINGLIB_MUTABLE
374
1.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
270
            if (PyList_Append(list, str_obj))
377
0
                goto onError;
378
270
            break;
379
270
        }
380
1.15M
#endif
381
2.31M
        SPLIT_APPEND(str, j, eol);
382
1.15M
        j = i;
383
1.15M
    }
384
984
    return list;
385
386
0
  onError:
387
0
    Py_DECREF(list);
388
    return NULL;
389
984
}
unicodeobject.c:ucs2lib_splitlines
Line
Count
Source
339
8.92k
{
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
8.92k
    Py_ssize_t i;
349
8.92k
    Py_ssize_t j;
350
8.92k
    PyObject *list = PyList_New(0);
351
8.92k
    PyObject *sub;
352
353
8.92k
    if (list == NULL)
354
0
        return NULL;
355
356
10.5M
    for (i = j = 0; i < str_len; ) {
357
10.5M
        Py_ssize_t eol;
358
359
        /* Find a line and append it */
360
43.1M
        while (i < str_len && !STRINGLIB_ISLINEBREAK(str[i]))
361
32.6M
            i++;
362
363
        /* Skip the line break reading CRLF as one line break */
364
10.5M
        eol = i;
365
10.5M
        if (i < str_len) {
366
10.5M
            if (str[i] == '\r' && i + 1 < str_len && str[i+1] == '\n')
367
10.4k
                i += 2;
368
10.4M
            else
369
10.4M
                i++;
370
10.5M
            if (keepends)
371
0
                eol = i;
372
10.5M
        }
373
10.5M
#if !STRINGLIB_MUTABLE
374
10.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.17k
            if (PyList_Append(list, str_obj))
377
0
                goto onError;
378
4.17k
            break;
379
4.17k
        }
380
10.5M
#endif
381
21.0M
        SPLIT_APPEND(str, j, eol);
382
10.5M
        j = i;
383
10.5M
    }
384
8.92k
    return list;
385
386
0
  onError:
387
0
    Py_DECREF(list);
388
    return NULL;
389
8.92k
}
unicodeobject.c:ucs4lib_splitlines
Line
Count
Source
339
3.50k
{
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.50k
    Py_ssize_t i;
349
3.50k
    Py_ssize_t j;
350
3.50k
    PyObject *list = PyList_New(0);
351
3.50k
    PyObject *sub;
352
353
3.50k
    if (list == NULL)
354
0
        return NULL;
355
356
11.1M
    for (i = j = 0; i < str_len; ) {
357
11.0M
        Py_ssize_t eol;
358
359
        /* Find a line and append it */
360
57.1M
        while (i < str_len && !STRINGLIB_ISLINEBREAK(str[i]))
361
46.0M
            i++;
362
363
        /* Skip the line break reading CRLF as one line break */
364
11.0M
        eol = i;
365
11.0M
        if (i < str_len) {
366
11.0M
            if (str[i] == '\r' && i + 1 < str_len && str[i+1] == '\n')
367
16.3k
                i += 2;
368
11.0M
            else
369
11.0M
                i++;
370
11.0M
            if (keepends)
371
0
                eol = i;
372
11.0M
        }
373
11.0M
#if !STRINGLIB_MUTABLE
374
11.0M
        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.63k
            if (PyList_Append(list, str_obj))
377
0
                goto onError;
378
1.63k
            break;
379
1.63k
        }
380
11.0M
#endif
381
22.1M
        SPLIT_APPEND(str, j, eol);
382
11.0M
        j = i;
383
11.0M
    }
384
3.50k
    return list;
385
386
0
  onError:
387
0
    Py_DECREF(list);
388
    return NULL;
389
3.50k
}
Unexecuted instantiation: bytearrayobject.c:stringlib_splitlines
390