Coverage Report

Created: 2026-02-26 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
98.7M
#define MAX_PREALLOC 12
15
16
/* 5 splits gives 6 elements */
17
#define PREALLOC_SIZE(maxsplit) \
18
19.7M
    (maxsplit >= MAX_PREALLOC ? MAX_PREALLOC : maxsplit+1)
19
20
#define SPLIT_APPEND(data, left, right)         \
21
34.1M
    sub = STRINGLIB_NEW((data) + (left),        \
22
34.1M
                        (right) - (left));      \
23
34.1M
    if (sub == NULL)                            \
24
34.1M
        goto onError;                           \
25
34.1M
    if (PyList_Append(list, sub)) {             \
26
0
        Py_DECREF(sub);                         \
27
0
        goto onError;                           \
28
0
    }                                           \
29
34.1M
    else                                        \
30
34.1M
        Py_DECREF(sub);
31
32
56.0M
#define SPLIT_ADD(data, left, right) {          \
33
56.0M
    sub = STRINGLIB_NEW((data) + (left),        \
34
56.0M
                        (right) - (left));      \
35
56.0M
    if (sub == NULL)                            \
36
56.0M
        goto onError;                           \
37
56.0M
    if (count < MAX_PREALLOC) {                 \
38
29.0M
        PyList_SET_ITEM(list, count, sub);      \
39
29.0M
    } else {                                    \
40
27.0M
        if (PyList_Append(list, sub)) {         \
41
0
            Py_DECREF(sub);                     \
42
0
            goto onError;                       \
43
0
        }                                       \
44
27.0M
        else                                    \
45
27.0M
            Py_DECREF(sub);                     \
46
27.0M
    }                                           \
47
56.0M
    count++; }
48
49
50
/* Always force the list to the expected size. */
51
19.7M
#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
169k
{
58
169k
    Py_ssize_t i, j, count=0;
59
169k
    PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
60
169k
    PyObject *sub;
61
62
169k
    if (list == NULL)
63
0
        return NULL;
64
65
169k
    i = j = 0;
66
2.26M
    while (maxcount-- > 0) {
67
4.40M
        while (i < str_len && STRINGLIB_ISSPACE(str[i]))
68
2.17M
            i++;
69
2.23M
        if (i == str_len) break;
70
2.13M
        j = i; i++;
71
145M
        while (i < str_len && !STRINGLIB_ISSPACE(str[i]))
72
143M
            i++;
73
#if !STRINGLIB_MUTABLE
74
2.13M
        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
45.1k
            Py_INCREF(str_obj);
77
45.1k
            PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
78
45.1k
            count++;
79
45.1k
            break;
80
45.1k
        }
81
2.09M
#endif
82
6.27M
        SPLIT_ADD(str, j, i);
83
6.27M
    }
84
85
169k
    if (i < str_len) {
86
        /* Only occurs when maxcount was reached */
87
        /* Skip any remaining whitespace and copy to end of string */
88
58.0k
        while (i < str_len && STRINGLIB_ISSPACE(str[i]))
89
31.8k
            i++;
90
26.1k
        if (i != str_len)
91
26.1k
            SPLIT_ADD(str, i, str_len);
92
26.1k
    }
93
169k
    FIX_PREALLOC_SIZE(list);
94
169k
    return list;
95
96
0
  onError:
97
0
    Py_DECREF(list);
98
0
    return NULL;
99
169k
}
Unexecuted instantiation: bytesobject.c:stringlib_split_whitespace
unicodeobject.c:asciilib_split_whitespace
Line
Count
Source
57
80.6k
{
58
80.6k
    Py_ssize_t i, j, count=0;
59
80.6k
    PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
60
80.6k
    PyObject *sub;
61
62
80.6k
    if (list == NULL)
63
0
        return NULL;
64
65
80.6k
    i = j = 0;
66
652k
    while (maxcount-- > 0) {
67
1.18M
        while (i < str_len && STRINGLIB_ISSPACE(str[i]))
68
545k
            i++;
69
636k
        if (i == str_len) break;
70
597k
        j = i; i++;
71
51.0M
        while (i < str_len && !STRINGLIB_ISSPACE(str[i]))
72
50.4M
            i++;
73
597k
#if !STRINGLIB_MUTABLE
74
597k
        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.5k
            Py_INCREF(str_obj);
77
25.5k
            PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
78
25.5k
            count++;
79
25.5k
            break;
80
25.5k
        }
81
571k
#endif
82
1.71M
        SPLIT_ADD(str, j, i);
83
1.71M
    }
84
85
80.6k
    if (i < str_len) {
86
        /* Only occurs when maxcount was reached */
87
        /* Skip any remaining whitespace and copy to end of string */
88
30.1k
        while (i < str_len && STRINGLIB_ISSPACE(str[i]))
89
16.1k
            i++;
90
14.0k
        if (i != str_len)
91
14.0k
            SPLIT_ADD(str, i, str_len);
92
14.0k
    }
93
80.6k
    FIX_PREALLOC_SIZE(list);
94
80.6k
    return list;
95
96
0
  onError:
97
0
    Py_DECREF(list);
98
    return NULL;
99
80.6k
}
unicodeobject.c:ucs1lib_split_whitespace
Line
Count
Source
57
27.9k
{
58
27.9k
    Py_ssize_t i, j, count=0;
59
27.9k
    PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
60
27.9k
    PyObject *sub;
61
62
27.9k
    if (list == NULL)
63
0
        return NULL;
64
65
27.9k
    i = j = 0;
66
746k
    while (maxcount-- > 0) {
67
1.44M
        while (i < str_len && STRINGLIB_ISSPACE(str[i]))
68
709k
            i++;
69
734k
        if (i == str_len) break;
70
720k
        j = i; i++;
71
44.9M
        while (i < str_len && !STRINGLIB_ISSPACE(str[i]))
72
44.2M
            i++;
73
720k
#if !STRINGLIB_MUTABLE
74
720k
        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.62k
            Py_INCREF(str_obj);
77
2.62k
            PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
78
2.62k
            count++;
79
2.62k
            break;
80
2.62k
        }
81
718k
#endif
82
2.15M
        SPLIT_ADD(str, j, i);
83
2.15M
    }
84
85
27.9k
    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
15.0k
            i++;
90
11.4k
        if (i != str_len)
91
11.4k
            SPLIT_ADD(str, i, str_len);
92
11.4k
    }
93
27.9k
    FIX_PREALLOC_SIZE(list);
94
27.9k
    return list;
95
96
0
  onError:
97
0
    Py_DECREF(list);
98
    return NULL;
99
27.9k
}
unicodeobject.c:ucs2lib_split_whitespace
Line
Count
Source
57
50.9k
{
58
50.9k
    Py_ssize_t i, j, count=0;
59
50.9k
    PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
60
50.9k
    PyObject *sub;
61
62
50.9k
    if (list == NULL)
63
0
        return NULL;
64
65
50.9k
    i = j = 0;
66
802k
    while (maxcount-- > 0) {
67
1.66M
        while (i < str_len && STRINGLIB_ISSPACE(str[i]))
68
862k
            i++;
69
801k
        if (i == str_len) break;
70
765k
        j = i; i++;
71
38.3M
        while (i < str_len && !STRINGLIB_ISSPACE(str[i]))
72
37.5M
            i++;
73
765k
#if !STRINGLIB_MUTABLE
74
765k
        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
13.6k
            Py_INCREF(str_obj);
77
13.6k
            PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
78
13.6k
            count++;
79
13.6k
            break;
80
13.6k
        }
81
751k
#endif
82
2.25M
        SPLIT_ADD(str, j, i);
83
2.25M
    }
84
85
50.9k
    if (i < str_len) {
86
        /* Only occurs when maxcount was reached */
87
        /* Skip any remaining whitespace and copy to end of string */
88
1.41k
        while (i < str_len && STRINGLIB_ISSPACE(str[i]))
89
706
            i++;
90
706
        if (i != str_len)
91
706
            SPLIT_ADD(str, i, str_len);
92
706
    }
93
50.9k
    FIX_PREALLOC_SIZE(list);
94
50.9k
    return list;
95
96
0
  onError:
97
0
    Py_DECREF(list);
98
    return NULL;
99
50.9k
}
unicodeobject.c:ucs4lib_split_whitespace
Line
Count
Source
57
9.60k
{
58
9.60k
    Py_ssize_t i, j, count=0;
59
9.60k
    PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
60
9.60k
    PyObject *sub;
61
62
9.60k
    if (list == NULL)
63
0
        return NULL;
64
65
9.60k
    i = j = 0;
66
59.2k
    while (maxcount-- > 0) {
67
116k
        while (i < str_len && STRINGLIB_ISSPACE(str[i]))
68
57.3k
            i++;
69
58.9k
        if (i == str_len) break;
70
52.9k
        j = i; i++;
71
11.1M
        while (i < str_len && !STRINGLIB_ISSPACE(str[i]))
72
11.1M
            i++;
73
52.9k
#if !STRINGLIB_MUTABLE
74
52.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.30k
            Py_INCREF(str_obj);
77
3.30k
            PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
78
3.30k
            count++;
79
3.30k
            break;
80
3.30k
        }
81
49.6k
#endif
82
148k
        SPLIT_ADD(str, j, i);
83
148k
    }
84
85
9.60k
    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
9.60k
    FIX_PREALLOC_SIZE(list);
94
9.60k
    return list;
95
96
0
  onError:
97
0
    Py_DECREF(list);
98
    return NULL;
99
9.60k
}
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
19.3M
{
107
19.3M
    Py_ssize_t i, j, count=0;
108
19.3M
    PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
109
19.3M
    PyObject *sub;
110
111
19.3M
    if (list == NULL)
112
0
        return NULL;
113
114
19.3M
    i = j = 0;
115
76.8M
    while ((j < str_len) && (maxcount-- > 0)) {
116
404M
        for(; j < str_len; j++) {
117
            /* I found that using memchr makes no difference */
118
387M
            if (str[j] == ch) {
119
40.1M
                SPLIT_ADD(str, i, j);
120
40.1M
                i = j = j + 1;
121
40.1M
                break;
122
40.1M
            }
123
387M
        }
124
57.5M
    }
125
#if !STRINGLIB_MUTABLE
126
19.3M
    if (count == 0 && STRINGLIB_CHECK_EXACT(str_obj)) {
127
        /* ch not in str_obj, so just use str_obj as list[0] */
128
5.86M
        Py_INCREF(str_obj);
129
5.86M
        PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
130
5.86M
        count++;
131
5.86M
    } else
132
13.4M
#endif
133
13.4M
    if (i <= str_len) {
134
26.9M
        SPLIT_ADD(str, i, str_len);
135
26.9M
    }
136
19.3M
    FIX_PREALLOC_SIZE(list);
137
19.3M
    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.89M
{
107
2.89M
    Py_ssize_t i, j, count=0;
108
2.89M
    PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
109
2.89M
    PyObject *sub;
110
111
2.89M
    if (list == NULL)
112
0
        return NULL;
113
114
2.89M
    i = j = 0;
115
9.82M
    while ((j < str_len) && (maxcount-- > 0)) {
116
58.7M
        for(; j < str_len; j++) {
117
            /* I found that using memchr makes no difference */
118
56.1M
            if (str[j] == ch) {
119
4.39M
                SPLIT_ADD(str, i, j);
120
4.39M
                i = j = j + 1;
121
4.39M
                break;
122
4.39M
            }
123
56.1M
        }
124
6.92M
    }
125
2.89M
#if !STRINGLIB_MUTABLE
126
2.89M
    if (count == 0 && STRINGLIB_CHECK_EXACT(str_obj)) {
127
        /* ch not in str_obj, so just use str_obj as list[0] */
128
2.42M
        Py_INCREF(str_obj);
129
2.42M
        PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
130
2.42M
        count++;
131
2.42M
    } else
132
471k
#endif
133
471k
    if (i <= str_len) {
134
942k
        SPLIT_ADD(str, i, str_len);
135
942k
    }
136
2.89M
    FIX_PREALLOC_SIZE(list);
137
2.89M
    return list;
138
139
0
  onError:
140
0
    Py_DECREF(list);
141
    return NULL;
142
2.89M
}
unicodeobject.c:asciilib_split_char
Line
Count
Source
106
15.2M
{
107
15.2M
    Py_ssize_t i, j, count=0;
108
15.2M
    PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
109
15.2M
    PyObject *sub;
110
111
15.2M
    if (list == NULL)
112
0
        return NULL;
113
114
15.2M
    i = j = 0;
115
48.1M
    while ((j < str_len) && (maxcount-- > 0)) {
116
207M
        for(; j < str_len; j++) {
117
            /* I found that using memchr makes no difference */
118
192M
            if (str[j] == ch) {
119
18.3M
                SPLIT_ADD(str, i, j);
120
18.3M
                i = j = j + 1;
121
18.3M
                break;
122
18.3M
            }
123
192M
        }
124
32.9M
    }
125
15.2M
#if !STRINGLIB_MUTABLE
126
15.2M
    if (count == 0 && STRINGLIB_CHECK_EXACT(str_obj)) {
127
        /* ch not in str_obj, so just use str_obj as list[0] */
128
3.25M
        Py_INCREF(str_obj);
129
3.25M
        PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
130
3.25M
        count++;
131
3.25M
    } else
132
11.9M
#endif
133
11.9M
    if (i <= str_len) {
134
23.9M
        SPLIT_ADD(str, i, str_len);
135
23.9M
    }
136
15.2M
    FIX_PREALLOC_SIZE(list);
137
15.2M
    return list;
138
139
0
  onError:
140
0
    Py_DECREF(list);
141
    return NULL;
142
15.2M
}
unicodeobject.c:ucs1lib_split_char
Line
Count
Source
106
1.05M
{
107
1.05M
    Py_ssize_t i, j, count=0;
108
1.05M
    PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
109
1.05M
    PyObject *sub;
110
111
1.05M
    if (list == NULL)
112
0
        return NULL;
113
114
1.05M
    i = j = 0;
115
11.1M
    while ((j < str_len) && (maxcount-- > 0)) {
116
68.6M
        for(; j < str_len; j++) {
117
            /* I found that using memchr makes no difference */
118
68.4M
            if (str[j] == ch) {
119
9.87M
                SPLIT_ADD(str, i, j);
120
9.87M
                i = j = j + 1;
121
9.87M
                break;
122
9.87M
            }
123
68.4M
        }
124
10.0M
    }
125
1.05M
#if !STRINGLIB_MUTABLE
126
1.05M
    if (count == 0 && STRINGLIB_CHECK_EXACT(str_obj)) {
127
        /* ch not in str_obj, so just use str_obj as list[0] */
128
146k
        Py_INCREF(str_obj);
129
146k
        PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
130
146k
        count++;
131
146k
    } else
132
912k
#endif
133
912k
    if (i <= str_len) {
134
1.82M
        SPLIT_ADD(str, i, str_len);
135
1.82M
    }
136
1.05M
    FIX_PREALLOC_SIZE(list);
137
1.05M
    return list;
138
139
0
  onError:
140
0
    Py_DECREF(list);
141
    return NULL;
142
1.05M
}
unicodeobject.c:ucs2lib_split_char
Line
Count
Source
106
123k
{
107
123k
    Py_ssize_t i, j, count=0;
108
123k
    PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
109
123k
    PyObject *sub;
110
111
123k
    if (list == NULL)
112
0
        return NULL;
113
114
123k
    i = j = 0;
115
7.57M
    while ((j < str_len) && (maxcount-- > 0)) {
116
46.4M
        for(; j < str_len; j++) {
117
            /* I found that using memchr makes no difference */
118
46.3M
            if (str[j] == ch) {
119
7.35M
                SPLIT_ADD(str, i, j);
120
7.35M
                i = j = j + 1;
121
7.35M
                break;
122
7.35M
            }
123
46.3M
        }
124
7.45M
    }
125
123k
#if !STRINGLIB_MUTABLE
126
123k
    if (count == 0 && STRINGLIB_CHECK_EXACT(str_obj)) {
127
        /* ch not in str_obj, so just use str_obj as list[0] */
128
37.4k
        Py_INCREF(str_obj);
129
37.4k
        PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
130
37.4k
        count++;
131
37.4k
    } else
132
86.4k
#endif
133
86.4k
    if (i <= str_len) {
134
172k
        SPLIT_ADD(str, i, str_len);
135
172k
    }
136
123k
    FIX_PREALLOC_SIZE(list);
137
123k
    return list;
138
139
0
  onError:
140
0
    Py_DECREF(list);
141
    return NULL;
142
123k
}
unicodeobject.c:ucs4lib_split_char
Line
Count
Source
106
13.7k
{
107
13.7k
    Py_ssize_t i, j, count=0;
108
13.7k
    PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
109
13.7k
    PyObject *sub;
110
111
13.7k
    if (list == NULL)
112
0
        return NULL;
113
114
13.7k
    i = j = 0;
115
238k
    while ((j < str_len) && (maxcount-- > 0)) {
116
23.6M
        for(; j < str_len; j++) {
117
            /* I found that using memchr makes no difference */
118
23.6M
            if (str[j] == ch) {
119
214k
                SPLIT_ADD(str, i, j);
120
214k
                i = j = j + 1;
121
214k
                break;
122
214k
            }
123
23.6M
        }
124
225k
    }
125
13.7k
#if !STRINGLIB_MUTABLE
126
13.7k
    if (count == 0 && STRINGLIB_CHECK_EXACT(str_obj)) {
127
        /* ch not in str_obj, so just use str_obj as list[0] */
128
894
        Py_INCREF(str_obj);
129
894
        PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
130
894
        count++;
131
894
    } else
132
12.8k
#endif
133
12.8k
    if (i <= str_len) {
134
25.6k
        SPLIT_ADD(str, i, str_len);
135
25.6k
    }
136
13.7k
    FIX_PREALLOC_SIZE(list);
137
13.7k
    return list;
138
139
0
  onError:
140
0
    Py_DECREF(list);
141
    return NULL;
142
13.7k
}
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
19.5M
{
150
19.5M
    Py_ssize_t i, j, pos, count=0;
151
19.5M
    PyObject *list, *sub;
152
153
19.5M
    if (sep_len == 0) {
154
0
        PyErr_SetString(PyExc_ValueError, "empty separator");
155
0
        return NULL;
156
0
    }
157
19.5M
    else if (sep_len == 1)
158
19.3M
        return STRINGLIB(split_char)(str_obj, str, str_len, sep[0], maxcount);
159
160
210k
    list = PyList_New(PREALLOC_SIZE(maxcount));
161
210k
    if (list == NULL)
162
0
        return NULL;
163
164
210k
    i = j = 0;
165
369k
    while (maxcount-- > 0) {
166
210k
        pos = FASTSEARCH(str+i, str_len-i, sep, sep_len, -1, FAST_SEARCH);
167
210k
        if (pos < 0)
168
51.6k
            break;
169
158k
        j = i + pos;
170
317k
        SPLIT_ADD(str, i, j);
171
317k
        i = j + sep_len;
172
317k
    }
173
#if !STRINGLIB_MUTABLE
174
210k
    if (count == 0 && STRINGLIB_CHECK_EXACT(str_obj)) {
175
        /* No match in str_obj, so just use it as list[0] */
176
51.6k
        Py_INCREF(str_obj);
177
51.6k
        PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
178
51.6k
        count++;
179
51.6k
    } else
180
158k
#endif
181
158k
    {
182
317k
        SPLIT_ADD(str, i, str_len);
183
317k
    }
184
210k
    FIX_PREALLOC_SIZE(list);
185
210k
    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.89M
{
150
2.89M
    Py_ssize_t i, j, pos, count=0;
151
2.89M
    PyObject *list, *sub;
152
153
2.89M
    if (sep_len == 0) {
154
0
        PyErr_SetString(PyExc_ValueError, "empty separator");
155
0
        return NULL;
156
0
    }
157
2.89M
    else if (sep_len == 1)
158
2.89M
        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
15.3M
{
150
15.3M
    Py_ssize_t i, j, pos, count=0;
151
15.3M
    PyObject *list, *sub;
152
153
15.3M
    if (sep_len == 0) {
154
0
        PyErr_SetString(PyExc_ValueError, "empty separator");
155
0
        return NULL;
156
0
    }
157
15.3M
    else if (sep_len == 1)
158
15.2M
        return STRINGLIB(split_char)(str_obj, str, str_len, sep[0], maxcount);
159
160
85.7k
    list = PyList_New(PREALLOC_SIZE(maxcount));
161
85.7k
    if (list == NULL)
162
0
        return NULL;
163
164
85.7k
    i = j = 0;
165
136k
    while (maxcount-- > 0) {
166
85.7k
        pos = FASTSEARCH(str+i, str_len-i, sep, sep_len, -1, FAST_SEARCH);
167
85.7k
        if (pos < 0)
168
34.8k
            break;
169
50.8k
        j = i + pos;
170
101k
        SPLIT_ADD(str, i, j);
171
101k
        i = j + sep_len;
172
101k
    }
173
85.7k
#if !STRINGLIB_MUTABLE
174
85.7k
    if (count == 0 && STRINGLIB_CHECK_EXACT(str_obj)) {
175
        /* No match in str_obj, so just use it as list[0] */
176
34.8k
        Py_INCREF(str_obj);
177
34.8k
        PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
178
34.8k
        count++;
179
34.8k
    } else
180
50.8k
#endif
181
50.8k
    {
182
101k
        SPLIT_ADD(str, i, str_len);
183
101k
    }
184
85.7k
    FIX_PREALLOC_SIZE(list);
185
85.7k
    return list;
186
187
0
  onError:
188
0
    Py_DECREF(list);
189
    return NULL;
190
85.7k
}
unicodeobject.c:ucs1lib_split
Line
Count
Source
149
1.08M
{
150
1.08M
    Py_ssize_t i, j, pos, count=0;
151
1.08M
    PyObject *list, *sub;
152
153
1.08M
    if (sep_len == 0) {
154
0
        PyErr_SetString(PyExc_ValueError, "empty separator");
155
0
        return NULL;
156
0
    }
157
1.08M
    else if (sep_len == 1)
158
1.05M
        return STRINGLIB(split_char)(str_obj, str, str_len, sep[0], maxcount);
159
160
21.6k
    list = PyList_New(PREALLOC_SIZE(maxcount));
161
21.6k
    if (list == NULL)
162
0
        return NULL;
163
164
21.6k
    i = j = 0;
165
39.5k
    while (maxcount-- > 0) {
166
21.6k
        pos = FASTSEARCH(str+i, str_len-i, sep, sep_len, -1, FAST_SEARCH);
167
21.6k
        if (pos < 0)
168
3.75k
            break;
169
17.8k
        j = i + pos;
170
35.7k
        SPLIT_ADD(str, i, j);
171
35.7k
        i = j + sep_len;
172
35.7k
    }
173
21.6k
#if !STRINGLIB_MUTABLE
174
21.6k
    if (count == 0 && STRINGLIB_CHECK_EXACT(str_obj)) {
175
        /* No match in str_obj, so just use it as list[0] */
176
3.75k
        Py_INCREF(str_obj);
177
3.75k
        PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
178
3.75k
        count++;
179
3.75k
    } else
180
17.8k
#endif
181
17.8k
    {
182
35.7k
        SPLIT_ADD(str, i, str_len);
183
35.7k
    }
184
21.6k
    FIX_PREALLOC_SIZE(list);
185
21.6k
    return list;
186
187
0
  onError:
188
0
    Py_DECREF(list);
189
    return NULL;
190
21.6k
}
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
123k
        return STRINGLIB(split_char)(str_obj, str, str_len, sep[0], maxcount);
159
160
85.1k
    list = PyList_New(PREALLOC_SIZE(maxcount));
161
85.1k
    if (list == NULL)
162
0
        return NULL;
163
164
85.1k
    i = j = 0;
165
160k
    while (maxcount-- > 0) {
166
85.1k
        pos = FASTSEARCH(str+i, str_len-i, sep, sep_len, -1, FAST_SEARCH);
167
85.1k
        if (pos < 0)
168
10.0k
            break;
169
75.1k
        j = i + pos;
170
150k
        SPLIT_ADD(str, i, j);
171
150k
        i = j + sep_len;
172
150k
    }
173
85.1k
#if !STRINGLIB_MUTABLE
174
85.1k
    if (count == 0 && STRINGLIB_CHECK_EXACT(str_obj)) {
175
        /* No match in str_obj, so just use it as list[0] */
176
10.0k
        Py_INCREF(str_obj);
177
10.0k
        PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
178
10.0k
        count++;
179
10.0k
    } else
180
75.1k
#endif
181
75.1k
    {
182
150k
        SPLIT_ADD(str, i, str_len);
183
150k
    }
184
85.1k
    FIX_PREALLOC_SIZE(list);
185
85.1k
    return list;
186
187
0
  onError:
188
0
    Py_DECREF(list);
189
    return NULL;
190
85.1k
}
unicodeobject.c:ucs4lib_split
Line
Count
Source
149
31.8k
{
150
31.8k
    Py_ssize_t i, j, pos, count=0;
151
31.8k
    PyObject *list, *sub;
152
153
31.8k
    if (sep_len == 0) {
154
0
        PyErr_SetString(PyExc_ValueError, "empty separator");
155
0
        return NULL;
156
0
    }
157
31.8k
    else if (sep_len == 1)
158
13.7k
        return STRINGLIB(split_char)(str_obj, str, str_len, sep[0], maxcount);
159
160
18.1k
    list = PyList_New(PREALLOC_SIZE(maxcount));
161
18.1k
    if (list == NULL)
162
0
        return NULL;
163
164
18.1k
    i = j = 0;
165
33.2k
    while (maxcount-- > 0) {
166
18.1k
        pos = FASTSEARCH(str+i, str_len-i, sep, sep_len, -1, FAST_SEARCH);
167
18.1k
        if (pos < 0)
168
2.96k
            break;
169
15.1k
        j = i + pos;
170
30.3k
        SPLIT_ADD(str, i, j);
171
30.3k
        i = j + sep_len;
172
30.3k
    }
173
18.1k
#if !STRINGLIB_MUTABLE
174
18.1k
    if (count == 0 && STRINGLIB_CHECK_EXACT(str_obj)) {
175
        /* No match in str_obj, so just use it as list[0] */
176
2.96k
        Py_INCREF(str_obj);
177
2.96k
        PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
178
2.96k
        count++;
179
2.96k
    } else
180
15.1k
#endif
181
15.1k
    {
182
30.3k
        SPLIT_ADD(str, i, str_len);
183
30.3k
    }
184
18.1k
    FIX_PREALLOC_SIZE(list);
185
18.1k
    return list;
186
187
0
  onError:
188
0
    Py_DECREF(list);
189
    return NULL;
190
18.1k
}
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
64
{
248
64
    Py_ssize_t i, j, count=0;
249
64
    PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
250
64
    PyObject *sub;
251
252
64
    if (list == NULL)
253
0
        return NULL;
254
255
64
    i = j = str_len - 1;
256
128
    while ((i >= 0) && (maxcount-- > 0)) {
257
100
        for(; i >= 0; i--) {
258
100
            if (str[i] == ch) {
259
64
                SPLIT_ADD(str, i + 1, j + 1);
260
64
                j = i = i - 1;
261
64
                break;
262
64
            }
263
100
        }
264
64
    }
265
#if !STRINGLIB_MUTABLE
266
64
    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
64
#endif
273
64
    if (j >= -1) {
274
128
        SPLIT_ADD(str, 0, j + 1);
275
128
    }
276
64
    FIX_PREALLOC_SIZE(list);
277
64
    if (PyList_Reverse(list) < 0)
278
0
        goto onError;
279
64
    return list;
280
281
0
  onError:
282
0
    Py_DECREF(list);
283
0
    return NULL;
284
64
}
Unexecuted instantiation: bytesobject.c:stringlib_rsplit_char
unicodeobject.c:asciilib_rsplit_char
Line
Count
Source
247
64
{
248
64
    Py_ssize_t i, j, count=0;
249
64
    PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
250
64
    PyObject *sub;
251
252
64
    if (list == NULL)
253
0
        return NULL;
254
255
64
    i = j = str_len - 1;
256
128
    while ((i >= 0) && (maxcount-- > 0)) {
257
100
        for(; i >= 0; i--) {
258
100
            if (str[i] == ch) {
259
64
                SPLIT_ADD(str, i + 1, j + 1);
260
64
                j = i = i - 1;
261
64
                break;
262
64
            }
263
100
        }
264
64
    }
265
64
#if !STRINGLIB_MUTABLE
266
64
    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
64
#endif
273
64
    if (j >= -1) {
274
128
        SPLIT_ADD(str, 0, j + 1);
275
128
    }
276
64
    FIX_PREALLOC_SIZE(list);
277
64
    if (PyList_Reverse(list) < 0)
278
0
        goto onError;
279
64
    return list;
280
281
0
  onError:
282
0
    Py_DECREF(list);
283
    return NULL;
284
64
}
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
64
{
292
64
    Py_ssize_t j, pos, count=0;
293
64
    PyObject *list, *sub;
294
295
64
    if (sep_len == 0) {
296
0
        PyErr_SetString(PyExc_ValueError, "empty separator");
297
0
        return NULL;
298
0
    }
299
64
    else if (sep_len == 1)
300
64
        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
64
{
292
64
    Py_ssize_t j, pos, count=0;
293
64
    PyObject *list, *sub;
294
295
64
    if (sep_len == 0) {
296
0
        PyErr_SetString(PyExc_ValueError, "empty separator");
297
0
        return NULL;
298
0
    }
299
64
    else if (sep_len == 1)
300
64
        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
14.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
14.1k
    Py_ssize_t i;
349
14.1k
    Py_ssize_t j;
350
14.1k
    PyObject *list = PyList_New(0);
351
14.1k
    PyObject *sub;
352
353
14.1k
    if (list == NULL)
354
0
        return NULL;
355
356
34.2M
    for (i = j = 0; i < str_len; ) {
357
34.1M
        Py_ssize_t eol;
358
359
        /* Find a line and append it */
360
277M
        while (i < str_len && !STRINGLIB_ISLINEBREAK(str[i]))
361
243M
            i++;
362
363
        /* Skip the line break reading CRLF as one line break */
364
34.1M
        eol = i;
365
34.1M
        if (i < str_len) {
366
34.1M
            if (str[i] == '\r' && i + 1 < str_len && str[i+1] == '\n')
367
43.1k
                i += 2;
368
34.1M
            else
369
34.1M
                i++;
370
34.1M
            if (keepends)
371
0
                eol = i;
372
34.1M
        }
373
#if !STRINGLIB_MUTABLE
374
34.1M
        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
5.66k
            if (PyList_Append(list, str_obj))
377
0
                goto onError;
378
5.66k
            break;
379
5.66k
        }
380
34.1M
#endif
381
68.3M
        SPLIT_APPEND(str, j, eol);
382
34.1M
        j = i;
383
34.1M
    }
384
14.1k
    return list;
385
386
0
  onError:
387
0
    Py_DECREF(list);
388
    return NULL;
389
14.1k
}
Unexecuted instantiation: bytesobject.c:stringlib_splitlines
unicodeobject.c:asciilib_splitlines
Line
Count
Source
339
2.85k
{
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
2.85k
    Py_ssize_t i;
349
2.85k
    Py_ssize_t j;
350
2.85k
    PyObject *list = PyList_New(0);
351
2.85k
    PyObject *sub;
352
353
2.85k
    if (list == NULL)
354
0
        return NULL;
355
356
8.73M
    for (i = j = 0; i < str_len; ) {
357
8.73M
        Py_ssize_t eol;
358
359
        /* Find a line and append it */
360
42.4M
        while (i < str_len && !STRINGLIB_ISLINEBREAK(str[i]))
361
33.7M
            i++;
362
363
        /* Skip the line break reading CRLF as one line break */
364
8.73M
        eol = i;
365
8.73M
        if (i < str_len) {
366
8.73M
            if (str[i] == '\r' && i + 1 < str_len && str[i+1] == '\n')
367
4.83k
                i += 2;
368
8.72M
            else
369
8.72M
                i++;
370
8.73M
            if (keepends)
371
0
                eol = i;
372
8.73M
        }
373
8.73M
#if !STRINGLIB_MUTABLE
374
8.73M
        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.04k
            if (PyList_Append(list, str_obj))
377
0
                goto onError;
378
1.04k
            break;
379
1.04k
        }
380
8.73M
#endif
381
17.4M
        SPLIT_APPEND(str, j, eol);
382
8.73M
        j = i;
383
8.73M
    }
384
2.85k
    return list;
385
386
0
  onError:
387
0
    Py_DECREF(list);
388
    return NULL;
389
2.85k
}
unicodeobject.c:ucs1lib_splitlines
Line
Count
Source
339
883
{
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
883
    Py_ssize_t i;
349
883
    Py_ssize_t j;
350
883
    PyObject *list = PyList_New(0);
351
883
    PyObject *sub;
352
353
883
    if (list == NULL)
354
0
        return NULL;
355
356
2.04M
    for (i = j = 0; i < str_len; ) {
357
2.04M
        Py_ssize_t eol;
358
359
        /* Find a line and append it */
360
10.4M
        while (i < str_len && !STRINGLIB_ISLINEBREAK(str[i]))
361
8.37M
            i++;
362
363
        /* Skip the line break reading CRLF as one line break */
364
2.04M
        eol = i;
365
2.04M
        if (i < str_len) {
366
2.03M
            if (str[i] == '\r' && i + 1 < str_len && str[i+1] == '\n')
367
2.46k
                i += 2;
368
2.03M
            else
369
2.03M
                i++;
370
2.03M
            if (keepends)
371
0
                eol = i;
372
2.03M
        }
373
2.04M
#if !STRINGLIB_MUTABLE
374
2.04M
        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
228
            if (PyList_Append(list, str_obj))
377
0
                goto onError;
378
228
            break;
379
228
        }
380
2.03M
#endif
381
4.07M
        SPLIT_APPEND(str, j, eol);
382
2.03M
        j = i;
383
2.03M
    }
384
883
    return list;
385
386
0
  onError:
387
0
    Py_DECREF(list);
388
    return NULL;
389
883
}
unicodeobject.c:ucs2lib_splitlines
Line
Count
Source
339
7.18k
{
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
7.18k
    Py_ssize_t i;
349
7.18k
    Py_ssize_t j;
350
7.18k
    PyObject *list = PyList_New(0);
351
7.18k
    PyObject *sub;
352
353
7.18k
    if (list == NULL)
354
0
        return NULL;
355
356
9.32M
    for (i = j = 0; i < str_len; ) {
357
9.32M
        Py_ssize_t eol;
358
359
        /* Find a line and append it */
360
96.0M
        while (i < str_len && !STRINGLIB_ISLINEBREAK(str[i]))
361
86.7M
            i++;
362
363
        /* Skip the line break reading CRLF as one line break */
364
9.32M
        eol = i;
365
9.32M
        if (i < str_len) {
366
9.31M
            if (str[i] == '\r' && i + 1 < str_len && str[i+1] == '\n')
367
20.9k
                i += 2;
368
9.29M
            else
369
9.29M
                i++;
370
9.31M
            if (keepends)
371
0
                eol = i;
372
9.31M
        }
373
9.32M
#if !STRINGLIB_MUTABLE
374
9.32M
        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
3.15k
            if (PyList_Append(list, str_obj))
377
0
                goto onError;
378
3.15k
            break;
379
3.15k
        }
380
9.32M
#endif
381
18.6M
        SPLIT_APPEND(str, j, eol);
382
9.32M
        j = i;
383
9.32M
    }
384
7.18k
    return list;
385
386
0
  onError:
387
0
    Py_DECREF(list);
388
    return NULL;
389
7.18k
}
unicodeobject.c:ucs4lib_splitlines
Line
Count
Source
339
3.21k
{
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.21k
    Py_ssize_t i;
349
3.21k
    Py_ssize_t j;
350
3.21k
    PyObject *list = PyList_New(0);
351
3.21k
    PyObject *sub;
352
353
3.21k
    if (list == NULL)
354
0
        return NULL;
355
356
14.0M
    for (i = j = 0; i < str_len; ) {
357
14.0M
        Py_ssize_t eol;
358
359
        /* Find a line and append it */
360
128M
        while (i < str_len && !STRINGLIB_ISLINEBREAK(str[i]))
361
114M
            i++;
362
363
        /* Skip the line break reading CRLF as one line break */
364
14.0M
        eol = i;
365
14.0M
        if (i < str_len) {
366
14.0M
            if (str[i] == '\r' && i + 1 < str_len && str[i+1] == '\n')
367
14.9k
                i += 2;
368
14.0M
            else
369
14.0M
                i++;
370
14.0M
            if (keepends)
371
0
                eol = i;
372
14.0M
        }
373
14.0M
#if !STRINGLIB_MUTABLE
374
14.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.24k
            if (PyList_Append(list, str_obj))
377
0
                goto onError;
378
1.24k
            break;
379
1.24k
        }
380
14.0M
#endif
381
28.1M
        SPLIT_APPEND(str, j, eol);
382
14.0M
        j = i;
383
14.0M
    }
384
3.21k
    return list;
385
386
0
  onError:
387
0
    Py_DECREF(list);
388
    return NULL;
389
3.21k
}
Unexecuted instantiation: bytearrayobject.c:stringlib_splitlines
390