Coverage Report

Created: 2026-06-21 06:15

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.2M
    (maxsplit >= MAX_PREALLOC ? MAX_PREALLOC : maxsplit+1)
19
20
#define SPLIT_APPEND(data, left, right)         \
21
30.6M
    sub = STRINGLIB_NEW((data) + (left),        \
22
30.6M
                        (right) - (left));      \
23
30.6M
    if (sub == NULL)                            \
24
30.6M
        goto onError;                           \
25
30.6M
    if (PyList_Append(list, sub)) {             \
26
0
        Py_DECREF(sub);                         \
27
0
        goto onError;                           \
28
0
    }                                           \
29
30.6M
    else                                        \
30
30.6M
        Py_DECREF(sub);
31
32
62.5M
#define SPLIT_ADD(data, left, right) {          \
33
62.5M
    sub = STRINGLIB_NEW((data) + (left),        \
34
62.5M
                        (right) - (left));      \
35
62.5M
    if (sub == NULL)                            \
36
62.5M
        goto onError;                           \
37
62.5M
    if (count < MAX_PREALLOC) {                 \
38
36.6M
        PyList_SET_ITEM(list, count, sub);      \
39
36.6M
    } else {                                    \
40
25.9M
        if (PyList_Append(list, sub)) {         \
41
0
            Py_DECREF(sub);                     \
42
0
            goto onError;                       \
43
0
        }                                       \
44
25.9M
        else                                    \
45
25.9M
            Py_DECREF(sub);                     \
46
25.9M
    }                                           \
47
62.5M
    count++; }
48
49
50
/* Always force the list to the expected size. */
51
23.2M
#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
175k
{
58
175k
    Py_ssize_t i, j, count=0;
59
175k
    PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
60
175k
    PyObject *sub;
61
62
175k
    if (list == NULL)
63
0
        return NULL;
64
65
175k
    i = j = 0;
66
1.92M
    while (maxcount-- > 0) {
67
3.68M
        while (i < str_len && STRINGLIB_ISSPACE(str[i]))
68
1.78M
            i++;
69
1.89M
        if (i == str_len) break;
70
1.79M
        j = i; i++;
71
142M
        while (i < str_len && !STRINGLIB_ISSPACE(str[i]))
72
140M
            i++;
73
#if !STRINGLIB_MUTABLE
74
1.79M
        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
48.8k
            Py_INCREF(str_obj);
77
48.8k
            PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
78
48.8k
            count++;
79
48.8k
            break;
80
48.8k
        }
81
1.75M
#endif
82
5.25M
        SPLIT_ADD(str, j, i);
83
5.25M
    }
84
85
175k
    if (i < str_len) {
86
        /* Only occurs when maxcount was reached */
87
        /* Skip any remaining whitespace and copy to end of string */
88
54.2k
        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
175k
    FIX_PREALLOC_SIZE(list);
94
175k
    return list;
95
96
0
  onError:
97
0
    Py_DECREF(list);
98
0
    return NULL;
99
175k
}
Unexecuted instantiation: bytesobject.c:stringlib_split_whitespace
unicodeobject.c:asciilib_split_whitespace
Line
Count
Source
57
84.4k
{
58
84.4k
    Py_ssize_t i, j, count=0;
59
84.4k
    PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
60
84.4k
    PyObject *sub;
61
62
84.4k
    if (list == NULL)
63
0
        return NULL;
64
65
84.4k
    i = j = 0;
66
575k
    while (maxcount-- > 0) {
67
1.02M
        while (i < str_len && STRINGLIB_ISSPACE(str[i]))
68
460k
            i++;
69
561k
        if (i == str_len) break;
70
522k
        j = i; i++;
71
51.0M
        while (i < str_len && !STRINGLIB_ISSPACE(str[i]))
72
50.5M
            i++;
73
522k
#if !STRINGLIB_MUTABLE
74
522k
        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
30.5k
            Py_INCREF(str_obj);
77
30.5k
            PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
78
30.5k
            count++;
79
30.5k
            break;
80
30.5k
        }
81
491k
#endif
82
1.47M
        SPLIT_ADD(str, j, i);
83
1.47M
    }
84
85
84.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
84.4k
    FIX_PREALLOC_SIZE(list);
94
84.4k
    return list;
95
96
0
  onError:
97
0
    Py_DECREF(list);
98
    return NULL;
99
84.4k
}
unicodeobject.c:ucs1lib_split_whitespace
Line
Count
Source
57
25.7k
{
58
25.7k
    Py_ssize_t i, j, count=0;
59
25.7k
    PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
60
25.7k
    PyObject *sub;
61
62
25.7k
    if (list == NULL)
63
0
        return NULL;
64
65
25.7k
    i = j = 0;
66
649k
    while (maxcount-- > 0) {
67
1.24M
        while (i < str_len && STRINGLIB_ISSPACE(str[i]))
68
608k
            i++;
69
637k
        if (i == str_len) break;
70
625k
        j = i; i++;
71
41.5M
        while (i < str_len && !STRINGLIB_ISSPACE(str[i]))
72
40.9M
            i++;
73
625k
#if !STRINGLIB_MUTABLE
74
625k
        if (j == 0 && i == str_len && STRINGLIB_CHECK_EXACT(str_obj)) {
75
            /* No whitespace in str_obj, so just use it as list[0] */
76
1.61k
            Py_INCREF(str_obj);
77
1.61k
            PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
78
1.61k
            count++;
79
1.61k
            break;
80
1.61k
        }
81
623k
#endif
82
1.87M
        SPLIT_ADD(str, j, i);
83
1.87M
    }
84
85
25.7k
    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
25.7k
    FIX_PREALLOC_SIZE(list);
94
25.7k
    return list;
95
96
0
  onError:
97
0
    Py_DECREF(list);
98
    return NULL;
99
25.7k
}
unicodeobject.c:ucs2lib_split_whitespace
Line
Count
Source
57
55.0k
{
58
55.0k
    Py_ssize_t i, j, count=0;
59
55.0k
    PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
60
55.0k
    PyObject *sub;
61
62
55.0k
    if (list == NULL)
63
0
        return NULL;
64
65
55.0k
    i = j = 0;
66
634k
    while (maxcount-- > 0) {
67
1.29M
        while (i < str_len && STRINGLIB_ISSPACE(str[i]))
68
661k
            i++;
69
633k
        if (i == str_len) break;
70
592k
        j = i; i++;
71
38.8M
        while (i < str_len && !STRINGLIB_ISSPACE(str[i]))
72
38.2M
            i++;
73
592k
#if !STRINGLIB_MUTABLE
74
592k
        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.5k
            Py_INCREF(str_obj);
77
13.5k
            PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
78
13.5k
            count++;
79
13.5k
            break;
80
13.5k
        }
81
579k
#endif
82
1.73M
        SPLIT_ADD(str, j, i);
83
1.73M
    }
84
85
55.0k
    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
55.0k
    FIX_PREALLOC_SIZE(list);
94
55.0k
    return list;
95
96
0
  onError:
97
0
    Py_DECREF(list);
98
    return NULL;
99
55.0k
}
unicodeobject.c:ucs4lib_split_whitespace
Line
Count
Source
57
9.82k
{
58
9.82k
    Py_ssize_t i, j, count=0;
59
9.82k
    PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
60
9.82k
    PyObject *sub;
61
62
9.82k
    if (list == NULL)
63
0
        return NULL;
64
65
9.82k
    i = j = 0;
66
65.4k
    while (maxcount-- > 0) {
67
120k
        while (i < str_len && STRINGLIB_ISSPACE(str[i]))
68
55.3k
            i++;
69
64.9k
        if (i == str_len) break;
70
58.7k
        j = i; i++;
71
11.1M
        while (i < str_len && !STRINGLIB_ISSPACE(str[i]))
72
11.0M
            i++;
73
58.7k
#if !STRINGLIB_MUTABLE
74
58.7k
        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.18k
            Py_INCREF(str_obj);
77
3.18k
            PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
78
3.18k
            count++;
79
3.18k
            break;
80
3.18k
        }
81
55.6k
#endif
82
166k
        SPLIT_ADD(str, j, i);
83
166k
    }
84
85
9.82k
    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.82k
    FIX_PREALLOC_SIZE(list);
94
9.82k
    return list;
95
96
0
  onError:
97
0
    Py_DECREF(list);
98
    return NULL;
99
9.82k
}
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.8M
{
107
22.8M
    Py_ssize_t i, j, count=0;
108
22.8M
    PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
109
22.8M
    PyObject *sub;
110
111
22.8M
    if (list == NULL)
112
0
        return NULL;
113
114
22.8M
    i = j = 0;
115
86.7M
    while ((j < str_len) && (maxcount-- > 0)) {
116
471M
        for(; j < str_len; j++) {
117
            /* I found that using memchr makes no difference */
118
450M
            if (str[j] == ch) {
119
43.1M
                SPLIT_ADD(str, i, j);
120
43.1M
                i = j = j + 1;
121
43.1M
                break;
122
43.1M
            }
123
450M
        }
124
63.8M
    }
125
#if !STRINGLIB_MUTABLE
126
22.8M
    if (count == 0 && STRINGLIB_CHECK_EXACT(str_obj)) {
127
        /* ch not in str_obj, so just use str_obj as list[0] */
128
5.55M
        Py_INCREF(str_obj);
129
5.55M
        PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
130
5.55M
        count++;
131
5.55M
    } else
132
17.3M
#endif
133
17.3M
    if (i <= str_len) {
134
34.6M
        SPLIT_ADD(str, i, str_len);
135
34.6M
    }
136
22.8M
    FIX_PREALLOC_SIZE(list);
137
22.8M
    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.98M
{
107
2.98M
    Py_ssize_t i, j, count=0;
108
2.98M
    PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
109
2.98M
    PyObject *sub;
110
111
2.98M
    if (list == NULL)
112
0
        return NULL;
113
114
2.98M
    i = j = 0;
115
13.6M
    while ((j < str_len) && (maxcount-- > 0)) {
116
88.9M
        for(; j < str_len; j++) {
117
            /* I found that using memchr makes no difference */
118
86.4M
            if (str[j] == ch) {
119
8.06M
                SPLIT_ADD(str, i, j);
120
8.06M
                i = j = j + 1;
121
8.06M
                break;
122
8.06M
            }
123
86.4M
        }
124
10.6M
    }
125
2.98M
#if !STRINGLIB_MUTABLE
126
2.98M
    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
506k
#endif
133
506k
    if (i <= str_len) {
134
1.01M
        SPLIT_ADD(str, i, str_len);
135
1.01M
    }
136
2.98M
    FIX_PREALLOC_SIZE(list);
137
2.98M
    return list;
138
139
0
  onError:
140
0
    Py_DECREF(list);
141
    return NULL;
142
2.98M
}
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.5M
    while ((j < str_len) && (maxcount-- > 0)) {
116
248M
        for(; j < str_len; j++) {
117
            /* I found that using memchr makes no difference */
118
230M
            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
230M
        }
124
38.9M
    }
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.4M
        SPLIT_ADD(str, i, str_len);
135
31.4M
    }
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.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.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
967k
#endif
133
967k
    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
127k
{
107
127k
    Py_ssize_t i, j, count=0;
108
127k
    PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
109
127k
    PyObject *sub;
110
111
127k
    if (list == NULL)
112
0
        return NULL;
113
114
127k
    i = j = 0;
115
5.31M
    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
5.09M
                SPLIT_ADD(str, i, j);
120
5.09M
                i = j = j + 1;
121
5.09M
                break;
122
5.09M
            }
123
46.3M
        }
124
5.18M
    }
125
127k
#if !STRINGLIB_MUTABLE
126
127k
    if (count == 0 && STRINGLIB_CHECK_EXACT(str_obj)) {
127
        /* ch not in str_obj, so just use str_obj as list[0] */
128
36.2k
        Py_INCREF(str_obj);
129
36.2k
        PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
130
36.2k
        count++;
131
36.2k
    } else
132
91.0k
#endif
133
91.0k
    if (i <= str_len) {
134
182k
        SPLIT_ADD(str, i, str_len);
135
182k
    }
136
127k
    FIX_PREALLOC_SIZE(list);
137
127k
    return list;
138
139
0
  onError:
140
0
    Py_DECREF(list);
141
    return NULL;
142
127k
}
unicodeobject.c:ucs4lib_split_char
Line
Count
Source
106
14.1k
{
107
14.1k
    Py_ssize_t i, j, count=0;
108
14.1k
    PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
109
14.1k
    PyObject *sub;
110
111
14.1k
    if (list == NULL)
112
0
        return NULL;
113
114
14.1k
    i = j = 0;
115
238k
    while ((j < str_len) && (maxcount-- > 0)) {
116
22.8M
        for(; j < str_len; j++) {
117
            /* I found that using memchr makes no difference */
118
22.8M
            if (str[j] == ch) {
119
212k
                SPLIT_ADD(str, i, j);
120
212k
                i = j = j + 1;
121
212k
                break;
122
212k
            }
123
22.8M
        }
124
224k
    }
125
14.1k
#if !STRINGLIB_MUTABLE
126
14.1k
    if (count == 0 && STRINGLIB_CHECK_EXACT(str_obj)) {
127
        /* ch not in str_obj, so just use str_obj as list[0] */
128
930
        Py_INCREF(str_obj);
129
930
        PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
130
930
        count++;
131
930
    } else
132
13.2k
#endif
133
13.2k
    if (i <= str_len) {
134
26.4k
        SPLIT_ADD(str, i, str_len);
135
26.4k
    }
136
14.1k
    FIX_PREALLOC_SIZE(list);
137
14.1k
    return list;
138
139
0
  onError:
140
0
    Py_DECREF(list);
141
    return NULL;
142
14.1k
}
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.0M
{
150
23.0M
    Py_ssize_t i, j, pos, count=0;
151
23.0M
    PyObject *list, *sub;
152
153
23.0M
    if (sep_len == 0) {
154
0
        PyErr_SetString(PyExc_ValueError, "empty separator");
155
0
        return NULL;
156
0
    }
157
23.0M
    else if (sep_len == 1)
158
22.8M
        return STRINGLIB(split_char)(str_obj, str, str_len, sep[0], maxcount);
159
160
225k
    list = PyList_New(PREALLOC_SIZE(maxcount));
161
225k
    if (list == NULL)
162
0
        return NULL;
163
164
225k
    i = j = 0;
165
394k
    while (maxcount-- > 0) {
166
225k
        pos = FASTSEARCH(str+i, str_len-i, sep, sep_len, -1, FAST_SEARCH);
167
225k
        if (pos < 0)
168
56.3k
            break;
169
168k
        j = i + pos;
170
337k
        SPLIT_ADD(str, i, j);
171
337k
        i = j + sep_len;
172
337k
    }
173
#if !STRINGLIB_MUTABLE
174
225k
    if (count == 0 && STRINGLIB_CHECK_EXACT(str_obj)) {
175
        /* No match in str_obj, so just use it as list[0] */
176
56.3k
        Py_INCREF(str_obj);
177
56.3k
        PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
178
56.3k
        count++;
179
56.3k
    } else
180
168k
#endif
181
168k
    {
182
337k
        SPLIT_ADD(str, i, str_len);
183
337k
    }
184
225k
    FIX_PREALLOC_SIZE(list);
185
225k
    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.98M
{
150
2.98M
    Py_ssize_t i, j, pos, count=0;
151
2.98M
    PyObject *list, *sub;
152
153
2.98M
    if (sep_len == 0) {
154
0
        PyErr_SetString(PyExc_ValueError, "empty separator");
155
0
        return NULL;
156
0
    }
157
2.98M
    else if (sep_len == 1)
158
2.98M
        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.6M
{
150
18.6M
    Py_ssize_t i, j, pos, count=0;
151
18.6M
    PyObject *list, *sub;
152
153
18.6M
    if (sep_len == 0) {
154
0
        PyErr_SetString(PyExc_ValueError, "empty separator");
155
0
        return NULL;
156
0
    }
157
18.6M
    else if (sep_len == 1)
158
18.6M
        return STRINGLIB(split_char)(str_obj, str, str_len, sep[0], maxcount);
159
160
96.3k
    list = PyList_New(PREALLOC_SIZE(maxcount));
161
96.3k
    if (list == NULL)
162
0
        return NULL;
163
164
96.3k
    i = j = 0;
165
153k
    while (maxcount-- > 0) {
166
96.3k
        pos = FASTSEARCH(str+i, str_len-i, sep, sep_len, -1, FAST_SEARCH);
167
96.3k
        if (pos < 0)
168
38.9k
            break;
169
57.4k
        j = i + pos;
170
114k
        SPLIT_ADD(str, i, j);
171
114k
        i = j + sep_len;
172
114k
    }
173
96.3k
#if !STRINGLIB_MUTABLE
174
96.3k
    if (count == 0 && STRINGLIB_CHECK_EXACT(str_obj)) {
175
        /* No match in str_obj, so just use it as list[0] */
176
38.9k
        Py_INCREF(str_obj);
177
38.9k
        PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
178
38.9k
        count++;
179
38.9k
    } else
180
57.4k
#endif
181
57.4k
    {
182
114k
        SPLIT_ADD(str, i, str_len);
183
114k
    }
184
96.3k
    FIX_PREALLOC_SIZE(list);
185
96.3k
    return list;
186
187
0
  onError:
188
0
    Py_DECREF(list);
189
    return NULL;
190
96.3k
}
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.7k
    list = PyList_New(PREALLOC_SIZE(maxcount));
161
19.7k
    if (list == NULL)
162
0
        return NULL;
163
164
19.7k
    i = j = 0;
165
35.8k
    while (maxcount-- > 0) {
166
19.7k
        pos = FASTSEARCH(str+i, str_len-i, sep, sep_len, -1, FAST_SEARCH);
167
19.7k
        if (pos < 0)
168
3.62k
            break;
169
16.0k
        j = i + pos;
170
32.1k
        SPLIT_ADD(str, i, j);
171
32.1k
        i = j + sep_len;
172
32.1k
    }
173
19.7k
#if !STRINGLIB_MUTABLE
174
19.7k
    if (count == 0 && STRINGLIB_CHECK_EXACT(str_obj)) {
175
        /* No match in str_obj, so just use it as list[0] */
176
3.62k
        Py_INCREF(str_obj);
177
3.62k
        PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
178
3.62k
        count++;
179
3.62k
    } else
180
16.0k
#endif
181
16.0k
    {
182
32.1k
        SPLIT_ADD(str, i, str_len);
183
32.1k
    }
184
19.7k
    FIX_PREALLOC_SIZE(list);
185
19.7k
    return list;
186
187
0
  onError:
188
0
    Py_DECREF(list);
189
    return NULL;
190
19.7k
}
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
127k
        return STRINGLIB(split_char)(str_obj, str, str_len, sep[0], maxcount);
159
160
88.7k
    list = PyList_New(PREALLOC_SIZE(maxcount));
161
88.7k
    if (list == NULL)
162
0
        return NULL;
163
164
88.7k
    i = j = 0;
165
167k
    while (maxcount-- > 0) {
166
88.7k
        pos = FASTSEARCH(str+i, str_len-i, sep, sep_len, -1, FAST_SEARCH);
167
88.7k
        if (pos < 0)
168
10.2k
            break;
169
78.5k
        j = i + pos;
170
157k
        SPLIT_ADD(str, i, j);
171
157k
        i = j + sep_len;
172
157k
    }
173
88.7k
#if !STRINGLIB_MUTABLE
174
88.7k
    if (count == 0 && STRINGLIB_CHECK_EXACT(str_obj)) {
175
        /* No match in str_obj, so just use it as list[0] */
176
10.2k
        Py_INCREF(str_obj);
177
10.2k
        PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
178
10.2k
        count++;
179
10.2k
    } else
180
78.5k
#endif
181
78.5k
    {
182
157k
        SPLIT_ADD(str, i, str_len);
183
157k
    }
184
88.7k
    FIX_PREALLOC_SIZE(list);
185
88.7k
    return list;
186
187
0
  onError:
188
0
    Py_DECREF(list);
189
    return NULL;
190
88.7k
}
unicodeobject.c:ucs4lib_split
Line
Count
Source
149
34.5k
{
150
34.5k
    Py_ssize_t i, j, pos, count=0;
151
34.5k
    PyObject *list, *sub;
152
153
34.5k
    if (sep_len == 0) {
154
0
        PyErr_SetString(PyExc_ValueError, "empty separator");
155
0
        return NULL;
156
0
    }
157
34.5k
    else if (sep_len == 1)
158
14.1k
        return STRINGLIB(split_char)(str_obj, str, str_len, sep[0], maxcount);
159
160
20.3k
    list = PyList_New(PREALLOC_SIZE(maxcount));
161
20.3k
    if (list == NULL)
162
0
        return NULL;
163
164
20.3k
    i = j = 0;
165
37.2k
    while (maxcount-- > 0) {
166
20.3k
        pos = FASTSEARCH(str+i, str_len-i, sep, sep_len, -1, FAST_SEARCH);
167
20.3k
        if (pos < 0)
168
3.52k
            break;
169
16.8k
        j = i + pos;
170
33.6k
        SPLIT_ADD(str, i, j);
171
33.6k
        i = j + sep_len;
172
33.6k
    }
173
20.3k
#if !STRINGLIB_MUTABLE
174
20.3k
    if (count == 0 && STRINGLIB_CHECK_EXACT(str_obj)) {
175
        /* No match in str_obj, so just use it as list[0] */
176
3.52k
        Py_INCREF(str_obj);
177
3.52k
        PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
178
3.52k
        count++;
179
3.52k
    } else
180
16.8k
#endif
181
16.8k
    {
182
33.6k
        SPLIT_ADD(str, i, str_len);
183
33.6k
    }
184
20.3k
    FIX_PREALLOC_SIZE(list);
185
20.3k
    return list;
186
187
0
  onError:
188
0
    Py_DECREF(list);
189
    return NULL;
190
20.3k
}
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
30.6M
    for (i = j = 0; i < str_len; ) {
357
30.6M
        Py_ssize_t eol;
358
359
        /* Find a line and append it */
360
137M
        while (i < str_len && !STRINGLIB_ISLINEBREAK(str[i]))
361
106M
            i++;
362
363
        /* Skip the line break reading CRLF as one line break */
364
30.6M
        eol = i;
365
30.6M
        if (i < str_len) {
366
30.6M
            if (str[i] == '\r' && i + 1 < str_len && str[i+1] == '\n')
367
27.1k
                i += 2;
368
30.6M
            else
369
30.6M
                i++;
370
30.6M
            if (keepends)
371
0
                eol = i;
372
30.6M
        }
373
#if !STRINGLIB_MUTABLE
374
30.6M
        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.25k
            if (PyList_Append(list, str_obj))
377
0
                goto onError;
378
7.25k
            break;
379
7.25k
        }
380
30.6M
#endif
381
61.2M
        SPLIT_APPEND(str, j, eol);
382
30.6M
        j = i;
383
30.6M
    }
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.96k
{
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.96k
    Py_ssize_t i;
349
3.96k
    Py_ssize_t j;
350
3.96k
    PyObject *list = PyList_New(0);
351
3.96k
    PyObject *sub;
352
353
3.96k
    if (list == NULL)
354
0
        return NULL;
355
356
6.01M
    for (i = j = 0; i < str_len; ) {
357
6.01M
        Py_ssize_t eol;
358
359
        /* Find a line and append it */
360
14.1M
        while (i < str_len && !STRINGLIB_ISLINEBREAK(str[i]))
361
8.17M
            i++;
362
363
        /* Skip the line break reading CRLF as one line break */
364
6.01M
        eol = i;
365
6.01M
        if (i < str_len) {
366
6.01M
            if (str[i] == '\r' && i + 1 < str_len && str[i+1] == '\n')
367
547
                i += 2;
368
6.01M
            else
369
6.01M
                i++;
370
6.01M
            if (keepends)
371
0
                eol = i;
372
6.01M
        }
373
6.01M
#if !STRINGLIB_MUTABLE
374
6.01M
        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.12k
            if (PyList_Append(list, str_obj))
377
0
                goto onError;
378
1.12k
            break;
379
1.12k
        }
380
6.01M
#endif
381
12.0M
        SPLIT_APPEND(str, j, eol);
382
6.01M
        j = i;
383
6.01M
    }
384
3.96k
    return list;
385
386
0
  onError:
387
0
    Py_DECREF(list);
388
    return NULL;
389
3.96k
}
unicodeobject.c:ucs1lib_splitlines
Line
Count
Source
339
1.02k
{
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.02k
    Py_ssize_t i;
349
1.02k
    Py_ssize_t j;
350
1.02k
    PyObject *list = PyList_New(0);
351
1.02k
    PyObject *sub;
352
353
1.02k
    if (list == NULL)
354
0
        return NULL;
355
356
1.94M
    for (i = j = 0; i < str_len; ) {
357
1.94M
        Py_ssize_t eol;
358
359
        /* Find a line and append it */
360
10.0M
        while (i < str_len && !STRINGLIB_ISLINEBREAK(str[i]))
361
8.14M
            i++;
362
363
        /* Skip the line break reading CRLF as one line break */
364
1.94M
        eol = i;
365
1.94M
        if (i < str_len) {
366
1.94M
            if (str[i] == '\r' && i + 1 < str_len && str[i+1] == '\n')
367
644
                i += 2;
368
1.94M
            else
369
1.94M
                i++;
370
1.94M
            if (keepends)
371
0
                eol = i;
372
1.94M
        }
373
1.94M
#if !STRINGLIB_MUTABLE
374
1.94M
        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
292
            if (PyList_Append(list, str_obj))
377
0
                goto onError;
378
292
            break;
379
292
        }
380
1.94M
#endif
381
3.89M
        SPLIT_APPEND(str, j, eol);
382
1.94M
        j = i;
383
1.94M
    }
384
1.02k
    return list;
385
386
0
  onError:
387
0
    Py_DECREF(list);
388
    return NULL;
389
1.02k
}
unicodeobject.c:ucs2lib_splitlines
Line
Count
Source
339
8.97k
{
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.97k
    Py_ssize_t i;
349
8.97k
    Py_ssize_t j;
350
8.97k
    PyObject *list = PyList_New(0);
351
8.97k
    PyObject *sub;
352
353
8.97k
    if (list == NULL)
354
0
        return NULL;
355
356
9.95M
    for (i = j = 0; i < str_len; ) {
357
9.95M
        Py_ssize_t eol;
358
359
        /* Find a line and append it */
360
50.1M
        while (i < str_len && !STRINGLIB_ISLINEBREAK(str[i]))
361
40.1M
            i++;
362
363
        /* Skip the line break reading CRLF as one line break */
364
9.95M
        eol = i;
365
9.95M
        if (i < str_len) {
366
9.94M
            if (str[i] == '\r' && i + 1 < str_len && str[i+1] == '\n')
367
11.2k
                i += 2;
368
9.93M
            else
369
9.93M
                i++;
370
9.94M
            if (keepends)
371
0
                eol = i;
372
9.94M
        }
373
9.95M
#if !STRINGLIB_MUTABLE
374
9.95M
        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.12k
            if (PyList_Append(list, str_obj))
377
0
                goto onError;
378
4.12k
            break;
379
4.12k
        }
380
9.94M
#endif
381
19.8M
        SPLIT_APPEND(str, j, eol);
382
9.94M
        j = i;
383
9.94M
    }
384
8.97k
    return list;
385
386
0
  onError:
387
0
    Py_DECREF(list);
388
    return NULL;
389
8.97k
}
unicodeobject.c:ucs4lib_splitlines
Line
Count
Source
339
3.46k
{
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.46k
    Py_ssize_t i;
349
3.46k
    Py_ssize_t j;
350
3.46k
    PyObject *list = PyList_New(0);
351
3.46k
    PyObject *sub;
352
353
3.46k
    if (list == NULL)
354
0
        return NULL;
355
356
12.7M
    for (i = j = 0; i < str_len; ) {
357
12.7M
        Py_ssize_t eol;
358
359
        /* Find a line and append it */
360
62.8M
        while (i < str_len && !STRINGLIB_ISLINEBREAK(str[i]))
361
50.0M
            i++;
362
363
        /* Skip the line break reading CRLF as one line break */
364
12.7M
        eol = i;
365
12.7M
        if (i < str_len) {
366
12.7M
            if (str[i] == '\r' && i + 1 < str_len && str[i+1] == '\n')
367
14.7k
                i += 2;
368
12.7M
            else
369
12.7M
                i++;
370
12.7M
            if (keepends)
371
0
                eol = i;
372
12.7M
        }
373
12.7M
#if !STRINGLIB_MUTABLE
374
12.7M
        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.71k
            if (PyList_Append(list, str_obj))
377
0
                goto onError;
378
1.71k
            break;
379
1.71k
        }
380
12.7M
#endif
381
25.4M
        SPLIT_APPEND(str, j, eol);
382
12.7M
        j = i;
383
12.7M
    }
384
3.46k
    return list;
385
386
0
  onError:
387
0
    Py_DECREF(list);
388
    return NULL;
389
3.46k
}
Unexecuted instantiation: bytearrayobject.c:stringlib_splitlines
390