Coverage Report

Created: 2026-06-09 06:53

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
111M
#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.8M
#define SPLIT_ADD(data, left, right) {          \
33
61.8M
    sub = STRINGLIB_NEW((data) + (left),        \
34
61.8M
                        (right) - (left));      \
35
61.8M
    if (sub == NULL)                            \
36
61.8M
        goto onError;                           \
37
61.8M
    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.8M
    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
188k
{
58
188k
    Py_ssize_t i, j, count=0;
59
188k
    PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
60
188k
    PyObject *sub;
61
62
188k
    if (list == NULL)
63
0
        return NULL;
64
65
188k
    i = j = 0;
66
1.97M
    while (maxcount-- > 0) {
67
3.77M
        while (i < str_len && STRINGLIB_ISSPACE(str[i]))
68
1.82M
            i++;
69
1.94M
        if (i == str_len) break;
70
1.83M
        j = i; i++;
71
144M
        while (i < str_len && !STRINGLIB_ISSPACE(str[i]))
72
142M
            i++;
73
#if !STRINGLIB_MUTABLE
74
1.83M
        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
56.2k
            Py_INCREF(str_obj);
77
56.2k
            PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
78
56.2k
            count++;
79
56.2k
            break;
80
56.2k
        }
81
1.78M
#endif
82
5.34M
        SPLIT_ADD(str, j, i);
83
5.34M
    }
84
85
188k
    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.7k
        if (i != str_len)
91
24.7k
            SPLIT_ADD(str, i, str_len);
92
24.7k
    }
93
188k
    FIX_PREALLOC_SIZE(list);
94
188k
    return list;
95
96
0
  onError:
97
0
    Py_DECREF(list);
98
0
    return NULL;
99
188k
}
Unexecuted instantiation: bytesobject.c:stringlib_split_whitespace
unicodeobject.c:asciilib_split_whitespace
Line
Count
Source
57
93.5k
{
58
93.5k
    Py_ssize_t i, j, count=0;
59
93.5k
    PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
60
93.5k
    PyObject *sub;
61
62
93.5k
    if (list == NULL)
63
0
        return NULL;
64
65
93.5k
    i = j = 0;
66
587k
    while (maxcount-- > 0) {
67
1.03M
        while (i < str_len && STRINGLIB_ISSPACE(str[i]))
68
458k
            i++;
69
571k
        if (i == str_len) break;
70
531k
        j = i; i++;
71
49.5M
        while (i < str_len && !STRINGLIB_ISSPACE(str[i]))
72
49.0M
            i++;
73
531k
#if !STRINGLIB_MUTABLE
74
531k
        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
37.6k
            Py_INCREF(str_obj);
77
37.6k
            PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
78
37.6k
            count++;
79
37.6k
            break;
80
37.6k
        }
81
493k
#endif
82
1.48M
        SPLIT_ADD(str, j, i);
83
1.48M
    }
84
85
93.5k
    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
93.5k
    FIX_PREALLOC_SIZE(list);
94
93.5k
    return list;
95
96
0
  onError:
97
0
    Py_DECREF(list);
98
    return NULL;
99
93.5k
}
unicodeobject.c:ucs1lib_split_whitespace
Line
Count
Source
57
26.8k
{
58
26.8k
    Py_ssize_t i, j, count=0;
59
26.8k
    PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
60
26.8k
    PyObject *sub;
61
62
26.8k
    if (list == NULL)
63
0
        return NULL;
64
65
26.8k
    i = j = 0;
66
653k
    while (maxcount-- > 0) {
67
1.25M
        while (i < str_len && STRINGLIB_ISSPACE(str[i]))
68
610k
            i++;
69
641k
        if (i == str_len) break;
70
629k
        j = i; i++;
71
41.5M
        while (i < str_len && !STRINGLIB_ISSPACE(str[i]))
72
40.9M
            i++;
73
629k
#if !STRINGLIB_MUTABLE
74
629k
        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.59k
            Py_INCREF(str_obj);
77
2.59k
            PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
78
2.59k
            count++;
79
2.59k
            break;
80
2.59k
        }
81
626k
#endif
82
1.87M
        SPLIT_ADD(str, j, i);
83
1.87M
    }
84
85
26.8k
    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.8k
    FIX_PREALLOC_SIZE(list);
94
26.8k
    return list;
95
96
0
  onError:
97
0
    Py_DECREF(list);
98
    return NULL;
99
26.8k
}
unicodeobject.c:ucs2lib_split_whitespace
Line
Count
Source
57
57.7k
{
58
57.7k
    Py_ssize_t i, j, count=0;
59
57.7k
    PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
60
57.7k
    PyObject *sub;
61
62
57.7k
    if (list == NULL)
63
0
        return NULL;
64
65
57.7k
    i = j = 0;
66
662k
    while (maxcount-- > 0) {
67
1.35M
        while (i < str_len && STRINGLIB_ISSPACE(str[i]))
68
694k
            i++;
69
661k
        if (i == str_len) break;
70
617k
        j = i; i++;
71
41.2M
        while (i < str_len && !STRINGLIB_ISSPACE(str[i]))
72
40.6M
            i++;
73
617k
#if !STRINGLIB_MUTABLE
74
617k
        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.6k
            Py_INCREF(str_obj);
77
12.6k
            PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
78
12.6k
            count++;
79
12.6k
            break;
80
12.6k
        }
81
604k
#endif
82
1.81M
        SPLIT_ADD(str, j, i);
83
1.81M
    }
84
85
57.7k
    if (i < str_len) {
86
        /* Only occurs when maxcount was reached */
87
        /* Skip any remaining whitespace and copy to end of string */
88
1.62k
        while (i < str_len && STRINGLIB_ISSPACE(str[i]))
89
810
            i++;
90
810
        if (i != str_len)
91
810
            SPLIT_ADD(str, i, str_len);
92
810
    }
93
57.7k
    FIX_PREALLOC_SIZE(list);
94
57.7k
    return list;
95
96
0
  onError:
97
0
    Py_DECREF(list);
98
    return NULL;
99
57.7k
}
unicodeobject.c:ucs4lib_split_whitespace
Line
Count
Source
57
10.3k
{
58
10.3k
    Py_ssize_t i, j, count=0;
59
10.3k
    PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
60
10.3k
    PyObject *sub;
61
62
10.3k
    if (list == NULL)
63
0
        return NULL;
64
65
10.3k
    i = j = 0;
66
67.8k
    while (maxcount-- > 0) {
67
131k
        while (i < str_len && STRINGLIB_ISSPACE(str[i]))
68
64.4k
            i++;
69
67.3k
        if (i == str_len) break;
70
60.9k
        j = i; i++;
71
12.2M
        while (i < str_len && !STRINGLIB_ISSPACE(str[i]))
72
12.1M
            i++;
73
60.9k
#if !STRINGLIB_MUTABLE
74
60.9k
        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.39k
            Py_INCREF(str_obj);
77
3.39k
            PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
78
3.39k
            count++;
79
3.39k
            break;
80
3.39k
        }
81
57.5k
#endif
82
172k
        SPLIT_ADD(str, j, i);
83
172k
    }
84
85
10.3k
    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.3k
    FIX_PREALLOC_SIZE(list);
94
10.3k
    return list;
95
96
0
  onError:
97
0
    Py_DECREF(list);
98
    return NULL;
99
10.3k
}
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.0M
    while ((j < str_len) && (maxcount-- > 0)) {
116
473M
        for(; j < str_len; j++) {
117
            /* I found that using memchr makes no difference */
118
452M
            if (str[j] == ch) {
119
42.2M
                SPLIT_ADD(str, i, j);
120
42.2M
                i = j = j + 1;
121
42.2M
                break;
122
42.2M
            }
123
452M
        }
124
63.1M
    }
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.00M
{
107
3.00M
    Py_ssize_t i, j, count=0;
108
3.00M
    PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
109
3.00M
    PyObject *sub;
110
111
3.00M
    if (list == NULL)
112
0
        return NULL;
113
114
3.00M
    i = j = 0;
115
12.7M
    while ((j < str_len) && (maxcount-- > 0)) {
116
89.0M
        for(; j < str_len; j++) {
117
            /* I found that using memchr makes no difference */
118
86.4M
            if (str[j] == ch) {
119
7.16M
                SPLIT_ADD(str, i, j);
120
7.16M
                i = j = j + 1;
121
7.16M
                break;
122
7.16M
            }
123
86.4M
        }
124
9.74M
    }
125
3.00M
#if !STRINGLIB_MUTABLE
126
3.00M
    if (count == 0 && STRINGLIB_CHECK_EXACT(str_obj)) {
127
        /* ch not in str_obj, so just use str_obj as list[0] */
128
2.48M
        Py_INCREF(str_obj);
129
2.48M
        PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
130
2.48M
        count++;
131
2.48M
    } else
132
512k
#endif
133
512k
    if (i <= str_len) {
134
1.02M
        SPLIT_ADD(str, i, str_len);
135
1.02M
    }
136
3.00M
    FIX_PREALLOC_SIZE(list);
137
3.00M
    return list;
138
139
0
  onError:
140
0
    Py_DECREF(list);
141
    return NULL;
142
3.00M
}
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.14M
{
107
1.14M
    Py_ssize_t i, j, count=0;
108
1.14M
    PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
109
1.14M
    PyObject *sub;
110
111
1.14M
    if (list == NULL)
112
0
        return NULL;
113
114
1.14M
    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.72M
                SPLIT_ADD(str, i, j);
120
8.72M
                i = j = j + 1;
121
8.72M
                break;
122
8.72M
            }
123
64.2M
        }
124
8.92M
    }
125
1.14M
#if !STRINGLIB_MUTABLE
126
1.14M
    if (count == 0 && STRINGLIB_CHECK_EXACT(str_obj)) {
127
        /* ch not in str_obj, so just use str_obj as list[0] */
128
176k
        Py_INCREF(str_obj);
129
176k
        PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
130
176k
        count++;
131
176k
    } else
132
968k
#endif
133
968k
    if (i <= str_len) {
134
1.93M
        SPLIT_ADD(str, i, str_len);
135
1.93M
    }
136
1.14M
    FIX_PREALLOC_SIZE(list);
137
1.14M
    return list;
138
139
0
  onError:
140
0
    Py_DECREF(list);
141
    return NULL;
142
1.14M
}
unicodeobject.c:ucs2lib_split_char
Line
Count
Source
106
130k
{
107
130k
    Py_ssize_t i, j, count=0;
108
130k
    PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
109
130k
    PyObject *sub;
110
111
130k
    if (list == NULL)
112
0
        return NULL;
113
114
130k
    i = j = 0;
115
5.33M
    while ((j < str_len) && (maxcount-- > 0)) {
116
48.8M
        for(; j < str_len; j++) {
117
            /* I found that using memchr makes no difference */
118
48.7M
            if (str[j] == ch) {
119
5.10M
                SPLIT_ADD(str, i, j);
120
5.10M
                i = j = j + 1;
121
5.10M
                break;
122
5.10M
            }
123
48.7M
        }
124
5.20M
    }
125
130k
#if !STRINGLIB_MUTABLE
126
130k
    if (count == 0 && STRINGLIB_CHECK_EXACT(str_obj)) {
127
        /* ch not in str_obj, so just use str_obj as list[0] */
128
37.2k
        Py_INCREF(str_obj);
129
37.2k
        PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
130
37.2k
        count++;
131
37.2k
    } else
132
93.6k
#endif
133
93.6k
    if (i <= str_len) {
134
187k
        SPLIT_ADD(str, i, str_len);
135
187k
    }
136
130k
    FIX_PREALLOC_SIZE(list);
137
130k
    return list;
138
139
0
  onError:
140
0
    Py_DECREF(list);
141
    return NULL;
142
130k
}
unicodeobject.c:ucs4lib_split_char
Line
Count
Source
106
14.6k
{
107
14.6k
    Py_ssize_t i, j, count=0;
108
14.6k
    PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
109
14.6k
    PyObject *sub;
110
111
14.6k
    if (list == NULL)
112
0
        return NULL;
113
114
14.6k
    i = j = 0;
115
241k
    while ((j < str_len) && (maxcount-- > 0)) {
116
23.2M
        for(; j < str_len; j++) {
117
            /* I found that using memchr makes no difference */
118
23.2M
            if (str[j] == ch) {
119
215k
                SPLIT_ADD(str, i, j);
120
215k
                i = j = j + 1;
121
215k
                break;
122
215k
            }
123
23.2M
        }
124
226k
    }
125
14.6k
#if !STRINGLIB_MUTABLE
126
14.6k
    if (count == 0 && STRINGLIB_CHECK_EXACT(str_obj)) {
127
        /* ch not in str_obj, so just use str_obj as list[0] */
128
936
        Py_INCREF(str_obj);
129
936
        PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
130
936
        count++;
131
936
    } else
132
13.7k
#endif
133
13.7k
    if (i <= str_len) {
134
27.4k
        SPLIT_ADD(str, i, str_len);
135
27.4k
    }
136
14.6k
    FIX_PREALLOC_SIZE(list);
137
14.6k
    return list;
138
139
0
  onError:
140
0
    Py_DECREF(list);
141
    return NULL;
142
14.6k
}
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
244k
    list = PyList_New(PREALLOC_SIZE(maxcount));
161
244k
    if (list == NULL)
162
0
        return NULL;
163
164
244k
    i = j = 0;
165
426k
    while (maxcount-- > 0) {
166
244k
        pos = FASTSEARCH(str+i, str_len-i, sep, sep_len, -1, FAST_SEARCH);
167
244k
        if (pos < 0)
168
61.1k
            break;
169
182k
        j = i + pos;
170
365k
        SPLIT_ADD(str, i, j);
171
365k
        i = j + sep_len;
172
365k
    }
173
#if !STRINGLIB_MUTABLE
174
244k
    if (count == 0 && STRINGLIB_CHECK_EXACT(str_obj)) {
175
        /* No match in str_obj, so just use it as list[0] */
176
61.1k
        Py_INCREF(str_obj);
177
61.1k
        PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
178
61.1k
        count++;
179
61.1k
    } else
180
182k
#endif
181
182k
    {
182
365k
        SPLIT_ADD(str, i, str_len);
183
365k
    }
184
244k
    FIX_PREALLOC_SIZE(list);
185
244k
    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.00M
{
150
3.00M
    Py_ssize_t i, j, pos, count=0;
151
3.00M
    PyObject *list, *sub;
152
153
3.00M
    if (sep_len == 0) {
154
0
        PyErr_SetString(PyExc_ValueError, "empty separator");
155
0
        return NULL;
156
0
    }
157
3.00M
    else if (sep_len == 1)
158
3.00M
        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
111k
    list = PyList_New(PREALLOC_SIZE(maxcount));
161
111k
    if (list == NULL)
162
0
        return NULL;
163
164
111k
    i = j = 0;
165
178k
    while (maxcount-- > 0) {
166
111k
        pos = FASTSEARCH(str+i, str_len-i, sep, sep_len, -1, FAST_SEARCH);
167
111k
        if (pos < 0)
168
45.4k
            break;
169
66.3k
        j = i + pos;
170
132k
        SPLIT_ADD(str, i, j);
171
132k
        i = j + sep_len;
172
132k
    }
173
111k
#if !STRINGLIB_MUTABLE
174
111k
    if (count == 0 && STRINGLIB_CHECK_EXACT(str_obj)) {
175
        /* No match in str_obj, so just use it as list[0] */
176
45.4k
        Py_INCREF(str_obj);
177
45.4k
        PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
178
45.4k
        count++;
179
45.4k
    } else
180
66.3k
#endif
181
66.3k
    {
182
132k
        SPLIT_ADD(str, i, str_len);
183
132k
    }
184
111k
    FIX_PREALLOC_SIZE(list);
185
111k
    return list;
186
187
0
  onError:
188
0
    Py_DECREF(list);
189
    return NULL;
190
111k
}
unicodeobject.c:ucs1lib_split
Line
Count
Source
149
1.16M
{
150
1.16M
    Py_ssize_t i, j, pos, count=0;
151
1.16M
    PyObject *list, *sub;
152
153
1.16M
    if (sep_len == 0) {
154
0
        PyErr_SetString(PyExc_ValueError, "empty separator");
155
0
        return NULL;
156
0
    }
157
1.16M
    else if (sep_len == 1)
158
1.14M
        return STRINGLIB(split_char)(str_obj, str, str_len, sep[0], maxcount);
159
160
19.0k
    list = PyList_New(PREALLOC_SIZE(maxcount));
161
19.0k
    if (list == NULL)
162
0
        return NULL;
163
164
19.0k
    i = j = 0;
165
35.3k
    while (maxcount-- > 0) {
166
19.0k
        pos = FASTSEARCH(str+i, str_len-i, sep, sep_len, -1, FAST_SEARCH);
167
19.0k
        if (pos < 0)
168
2.81k
            break;
169
16.2k
        j = i + pos;
170
32.5k
        SPLIT_ADD(str, i, j);
171
32.5k
        i = j + sep_len;
172
32.5k
    }
173
19.0k
#if !STRINGLIB_MUTABLE
174
19.0k
    if (count == 0 && STRINGLIB_CHECK_EXACT(str_obj)) {
175
        /* No match in str_obj, so just use it as list[0] */
176
2.81k
        Py_INCREF(str_obj);
177
2.81k
        PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
178
2.81k
        count++;
179
2.81k
    } else
180
16.2k
#endif
181
16.2k
    {
182
32.5k
        SPLIT_ADD(str, i, str_len);
183
32.5k
    }
184
19.0k
    FIX_PREALLOC_SIZE(list);
185
19.0k
    return list;
186
187
0
  onError:
188
0
    Py_DECREF(list);
189
    return NULL;
190
19.0k
}
unicodeobject.c:ucs2lib_split
Line
Count
Source
149
223k
{
150
223k
    Py_ssize_t i, j, pos, count=0;
151
223k
    PyObject *list, *sub;
152
153
223k
    if (sep_len == 0) {
154
0
        PyErr_SetString(PyExc_ValueError, "empty separator");
155
0
        return NULL;
156
0
    }
157
223k
    else if (sep_len == 1)
158
130k
        return STRINGLIB(split_char)(str_obj, str, str_len, sep[0], maxcount);
159
160
92.6k
    list = PyList_New(PREALLOC_SIZE(maxcount));
161
92.6k
    if (list == NULL)
162
0
        return NULL;
163
164
92.6k
    i = j = 0;
165
176k
    while (maxcount-- > 0) {
166
92.6k
        pos = FASTSEARCH(str+i, str_len-i, sep, sep_len, -1, FAST_SEARCH);
167
92.6k
        if (pos < 0)
168
8.97k
            break;
169
83.6k
        j = i + pos;
170
167k
        SPLIT_ADD(str, i, j);
171
167k
        i = j + sep_len;
172
167k
    }
173
92.6k
#if !STRINGLIB_MUTABLE
174
92.6k
    if (count == 0 && STRINGLIB_CHECK_EXACT(str_obj)) {
175
        /* No match in str_obj, so just use it as list[0] */
176
8.97k
        Py_INCREF(str_obj);
177
8.97k
        PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
178
8.97k
        count++;
179
8.97k
    } else
180
83.6k
#endif
181
83.6k
    {
182
167k
        SPLIT_ADD(str, i, str_len);
183
167k
    }
184
92.6k
    FIX_PREALLOC_SIZE(list);
185
92.6k
    return list;
186
187
0
  onError:
188
0
    Py_DECREF(list);
189
    return NULL;
190
92.6k
}
unicodeobject.c:ucs4lib_split
Line
Count
Source
149
35.1k
{
150
35.1k
    Py_ssize_t i, j, pos, count=0;
151
35.1k
    PyObject *list, *sub;
152
153
35.1k
    if (sep_len == 0) {
154
0
        PyErr_SetString(PyExc_ValueError, "empty separator");
155
0
        return NULL;
156
0
    }
157
35.1k
    else if (sep_len == 1)
158
14.6k
        return STRINGLIB(split_char)(str_obj, str, str_len, sep[0], maxcount);
159
160
20.5k
    list = PyList_New(PREALLOC_SIZE(maxcount));
161
20.5k
    if (list == NULL)
162
0
        return NULL;
163
164
20.5k
    i = j = 0;
165
37.1k
    while (maxcount-- > 0) {
166
20.5k
        pos = FASTSEARCH(str+i, str_len-i, sep, sep_len, -1, FAST_SEARCH);
167
20.5k
        if (pos < 0)
168
3.90k
            break;
169
16.6k
        j = i + pos;
170
33.2k
        SPLIT_ADD(str, i, j);
171
33.2k
        i = j + sep_len;
172
33.2k
    }
173
20.5k
#if !STRINGLIB_MUTABLE
174
20.5k
    if (count == 0 && STRINGLIB_CHECK_EXACT(str_obj)) {
175
        /* No match in str_obj, so just use it as list[0] */
176
3.90k
        Py_INCREF(str_obj);
177
3.90k
        PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
178
3.90k
        count++;
179
3.90k
    } else
180
16.6k
#endif
181
16.6k
    {
182
33.2k
        SPLIT_ADD(str, i, str_len);
183
33.2k
    }
184
20.5k
    FIX_PREALLOC_SIZE(list);
185
20.5k
    return list;
186
187
0
  onError:
188
0
    Py_DECREF(list);
189
    return NULL;
190
20.5k
}
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.4k
{
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.4k
    Py_ssize_t i;
349
17.4k
    Py_ssize_t j;
350
17.4k
    PyObject *list = PyList_New(0);
351
17.4k
    PyObject *sub;
352
353
17.4k
    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.9M
            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
29.3k
                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.28k
            if (PyList_Append(list, str_obj))
377
0
                goto onError;
378
7.28k
            break;
379
7.28k
        }
380
29.8M
#endif
381
59.7M
        SPLIT_APPEND(str, j, eol);
382
29.8M
        j = i;
383
29.8M
    }
384
17.4k
    return list;
385
386
0
  onError:
387
0
    Py_DECREF(list);
388
    return NULL;
389
17.4k
}
Unexecuted instantiation: bytesobject.c:stringlib_splitlines
unicodeobject.c:asciilib_splitlines
Line
Count
Source
339
3.94k
{
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.94k
    Py_ssize_t i;
349
3.94k
    Py_ssize_t j;
350
3.94k
    PyObject *list = PyList_New(0);
351
3.94k
    PyObject *sub;
352
353
3.94k
    if (list == NULL)
354
0
        return NULL;
355
356
5.36M
    for (i = j = 0; i < str_len; ) {
357
5.36M
        Py_ssize_t eol;
358
359
        /* Find a line and append it */
360
10.0M
        while (i < str_len && !STRINGLIB_ISLINEBREAK(str[i]))
361
4.70M
            i++;
362
363
        /* Skip the line break reading CRLF as one line break */
364
5.36M
        eol = i;
365
5.36M
        if (i < str_len) {
366
5.36M
            if (str[i] == '\r' && i + 1 < str_len && str[i+1] == '\n')
367
658
                i += 2;
368
5.36M
            else
369
5.36M
                i++;
370
5.36M
            if (keepends)
371
0
                eol = i;
372
5.36M
        }
373
5.36M
#if !STRINGLIB_MUTABLE
374
5.36M
        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
5.36M
#endif
381
10.7M
        SPLIT_APPEND(str, j, eol);
382
5.36M
        j = i;
383
5.36M
    }
384
3.94k
    return list;
385
386
0
  onError:
387
0
    Py_DECREF(list);
388
    return NULL;
389
3.94k
}
unicodeobject.c:ucs1lib_splitlines
Line
Count
Source
339
1.00k
{
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.00k
    Py_ssize_t i;
349
1.00k
    Py_ssize_t j;
350
1.00k
    PyObject *list = PyList_New(0);
351
1.00k
    PyObject *sub;
352
353
1.00k
    if (list == NULL)
354
0
        return NULL;
355
356
963k
    for (i = j = 0; i < str_len; ) {
357
962k
        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.54M
            i++;
362
363
        /* Skip the line break reading CRLF as one line break */
364
962k
        eol = i;
365
962k
        if (i < str_len) {
366
961k
            if (str[i] == '\r' && i + 1 < str_len && str[i+1] == '\n')
367
710
                i += 2;
368
961k
            else
369
961k
                i++;
370
961k
            if (keepends)
371
0
                eol = i;
372
961k
        }
373
962k
#if !STRINGLIB_MUTABLE
374
962k
        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
285
            if (PyList_Append(list, str_obj))
377
0
                goto onError;
378
285
            break;
379
285
        }
380
962k
#endif
381
1.92M
        SPLIT_APPEND(str, j, eol);
382
962k
        j = i;
383
962k
    }
384
1.00k
    return list;
385
386
0
  onError:
387
0
    Py_DECREF(list);
388
    return NULL;
389
1.00k
}
unicodeobject.c:ucs2lib_splitlines
Line
Count
Source
339
9.06k
{
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.06k
    Py_ssize_t i;
349
9.06k
    Py_ssize_t j;
350
9.06k
    PyObject *list = PyList_New(0);
351
9.06k
    PyObject *sub;
352
353
9.06k
    if (list == NULL)
354
0
        return NULL;
355
356
10.4M
    for (i = j = 0; i < str_len; ) {
357
10.4M
        Py_ssize_t eol;
358
359
        /* Find a line and append it */
360
45.0M
        while (i < str_len && !STRINGLIB_ISLINEBREAK(str[i]))
361
34.5M
            i++;
362
363
        /* Skip the line break reading CRLF as one line break */
364
10.4M
        eol = i;
365
10.4M
        if (i < str_len) {
366
10.4M
            if (str[i] == '\r' && i + 1 < str_len && str[i+1] == '\n')
367
10.1k
                i += 2;
368
10.4M
            else
369
10.4M
                i++;
370
10.4M
            if (keepends)
371
0
                eol = i;
372
10.4M
        }
373
10.4M
#if !STRINGLIB_MUTABLE
374
10.4M
        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.19k
            if (PyList_Append(list, str_obj))
377
0
                goto onError;
378
4.19k
            break;
379
4.19k
        }
380
10.4M
#endif
381
20.9M
        SPLIT_APPEND(str, j, eol);
382
10.4M
        j = i;
383
10.4M
    }
384
9.06k
    return list;
385
386
0
  onError:
387
0
    Py_DECREF(list);
388
    return NULL;
389
9.06k
}
unicodeobject.c:ucs4lib_splitlines
Line
Count
Source
339
3.47k
{
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.47k
    Py_ssize_t i;
349
3.47k
    Py_ssize_t j;
350
3.47k
    PyObject *list = PyList_New(0);
351
3.47k
    PyObject *sub;
352
353
3.47k
    if (list == NULL)
354
0
        return NULL;
355
356
13.0M
    for (i = j = 0; i < str_len; ) {
357
13.0M
        Py_ssize_t eol;
358
359
        /* Find a line and append it */
360
59.1M
        while (i < str_len && !STRINGLIB_ISLINEBREAK(str[i]))
361
46.1M
            i++;
362
363
        /* Skip the line break reading CRLF as one line break */
364
13.0M
        eol = i;
365
13.0M
        if (i < str_len) {
366
13.0M
            if (str[i] == '\r' && i + 1 < str_len && str[i+1] == '\n')
367
17.8k
                i += 2;
368
13.0M
            else
369
13.0M
                i++;
370
13.0M
            if (keepends)
371
0
                eol = i;
372
13.0M
        }
373
13.0M
#if !STRINGLIB_MUTABLE
374
13.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.70k
            if (PyList_Append(list, str_obj))
377
0
                goto onError;
378
1.70k
            break;
379
1.70k
        }
380
13.0M
#endif
381
26.1M
        SPLIT_APPEND(str, j, eol);
382
13.0M
        j = i;
383
13.0M
    }
384
3.47k
    return list;
385
386
0
  onError:
387
0
    Py_DECREF(list);
388
    return NULL;
389
3.47k
}
Unexecuted instantiation: bytearrayobject.c:stringlib_splitlines
390