Coverage Report

Created: 2026-03-08 06:40

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
130M
#define MAX_PREALLOC 12
15
16
/* 5 splits gives 6 elements */
17
#define PREALLOC_SIZE(maxsplit) \
18
28.8M
    (maxsplit >= MAX_PREALLOC ? MAX_PREALLOC : maxsplit+1)
19
20
#define SPLIT_APPEND(data, left, right)         \
21
32.9M
    sub = STRINGLIB_NEW((data) + (left),        \
22
32.9M
                        (right) - (left));      \
23
32.9M
    if (sub == NULL)                            \
24
32.9M
        goto onError;                           \
25
32.9M
    if (PyList_Append(list, sub)) {             \
26
0
        Py_DECREF(sub);                         \
27
0
        goto onError;                           \
28
0
    }                                           \
29
32.9M
    else                                        \
30
32.9M
        Py_DECREF(sub);
31
32
68.9M
#define SPLIT_ADD(data, left, right) {          \
33
68.9M
    sub = STRINGLIB_NEW((data) + (left),        \
34
68.9M
                        (right) - (left));      \
35
68.9M
    if (sub == NULL)                            \
36
68.9M
        goto onError;                           \
37
68.9M
    if (count < MAX_PREALLOC) {                 \
38
46.6M
        PyList_SET_ITEM(list, count, sub);      \
39
46.6M
    } else {                                    \
40
22.2M
        if (PyList_Append(list, sub)) {         \
41
0
            Py_DECREF(sub);                     \
42
0
            goto onError;                       \
43
0
        }                                       \
44
22.2M
        else                                    \
45
22.2M
            Py_DECREF(sub);                     \
46
22.2M
    }                                           \
47
68.9M
    count++; }
48
49
50
/* Always force the list to the expected size. */
51
28.8M
#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
168k
{
58
168k
    Py_ssize_t i, j, count=0;
59
168k
    PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
60
168k
    PyObject *sub;
61
62
168k
    if (list == NULL)
63
0
        return NULL;
64
65
168k
    i = j = 0;
66
1.89M
    while (maxcount-- > 0) {
67
3.68M
        while (i < str_len && STRINGLIB_ISSPACE(str[i]))
68
1.81M
            i++;
69
1.86M
        if (i == str_len) break;
70
1.77M
        j = i; i++;
71
127M
        while (i < str_len && !STRINGLIB_ISSPACE(str[i]))
72
125M
            i++;
73
#if !STRINGLIB_MUTABLE
74
1.77M
        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
46.8k
            Py_INCREF(str_obj);
77
46.8k
            PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
78
46.8k
            count++;
79
46.8k
            break;
80
46.8k
        }
81
1.72M
#endif
82
5.18M
        SPLIT_ADD(str, j, i);
83
5.18M
    }
84
85
168k
    if (i < str_len) {
86
        /* Only occurs when maxcount was reached */
87
        /* Skip any remaining whitespace and copy to end of string */
88
57.4k
        while (i < str_len && STRINGLIB_ISSPACE(str[i]))
89
31.5k
            i++;
90
25.8k
        if (i != str_len)
91
25.8k
            SPLIT_ADD(str, i, str_len);
92
25.8k
    }
93
168k
    FIX_PREALLOC_SIZE(list);
94
168k
    return list;
95
96
0
  onError:
97
0
    Py_DECREF(list);
98
0
    return NULL;
99
168k
}
Unexecuted instantiation: bytesobject.c:stringlib_split_whitespace
unicodeobject.c:asciilib_split_whitespace
Line
Count
Source
57
79.6k
{
58
79.6k
    Py_ssize_t i, j, count=0;
59
79.6k
    PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
60
79.6k
    PyObject *sub;
61
62
79.6k
    if (list == NULL)
63
0
        return NULL;
64
65
79.6k
    i = j = 0;
66
598k
    while (maxcount-- > 0) {
67
1.07M
        while (i < str_len && STRINGLIB_ISSPACE(str[i]))
68
493k
            i++;
69
582k
        if (i == str_len) break;
70
547k
        j = i; i++;
71
47.0M
        while (i < str_len && !STRINGLIB_ISSPACE(str[i]))
72
46.5M
            i++;
73
547k
#if !STRINGLIB_MUTABLE
74
547k
        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
27.7k
            Py_INCREF(str_obj);
77
27.7k
            PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
78
27.7k
            count++;
79
27.7k
            break;
80
27.7k
        }
81
519k
#endif
82
1.55M
        SPLIT_ADD(str, j, i);
83
1.55M
    }
84
85
79.6k
    if (i < str_len) {
86
        /* Only occurs when maxcount was reached */
87
        /* Skip any remaining whitespace and copy to end of string */
88
29.4k
        while (i < str_len && STRINGLIB_ISSPACE(str[i]))
89
15.7k
            i++;
90
13.7k
        if (i != str_len)
91
13.7k
            SPLIT_ADD(str, i, str_len);
92
13.7k
    }
93
79.6k
    FIX_PREALLOC_SIZE(list);
94
79.6k
    return list;
95
96
0
  onError:
97
0
    Py_DECREF(list);
98
    return NULL;
99
79.6k
}
unicodeobject.c:ucs1lib_split_whitespace
Line
Count
Source
57
27.3k
{
58
27.3k
    Py_ssize_t i, j, count=0;
59
27.3k
    PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
60
27.3k
    PyObject *sub;
61
62
27.3k
    if (list == NULL)
63
0
        return NULL;
64
65
27.3k
    i = j = 0;
66
687k
    while (maxcount-- > 0) {
67
1.32M
        while (i < str_len && STRINGLIB_ISSPACE(str[i]))
68
644k
            i++;
69
675k
        if (i == str_len) break;
70
662k
        j = i; i++;
71
42.5M
        while (i < str_len && !STRINGLIB_ISSPACE(str[i]))
72
41.8M
            i++;
73
662k
#if !STRINGLIB_MUTABLE
74
662k
        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.63k
            Py_INCREF(str_obj);
77
2.63k
            PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
78
2.63k
            count++;
79
2.63k
            break;
80
2.63k
        }
81
660k
#endif
82
1.98M
        SPLIT_ADD(str, j, i);
83
1.98M
    }
84
85
27.3k
    if (i < str_len) {
86
        /* Only occurs when maxcount was reached */
87
        /* Skip any remaining whitespace and copy to end of string */
88
26.5k
        while (i < str_len && STRINGLIB_ISSPACE(str[i]))
89
15.1k
            i++;
90
11.4k
        if (i != str_len)
91
11.4k
            SPLIT_ADD(str, i, str_len);
92
11.4k
    }
93
27.3k
    FIX_PREALLOC_SIZE(list);
94
27.3k
    return list;
95
96
0
  onError:
97
0
    Py_DECREF(list);
98
    return NULL;
99
27.3k
}
unicodeobject.c:ucs2lib_split_whitespace
Line
Count
Source
57
50.2k
{
58
50.2k
    Py_ssize_t i, j, count=0;
59
50.2k
    PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
60
50.2k
    PyObject *sub;
61
62
50.2k
    if (list == NULL)
63
0
        return NULL;
64
65
50.2k
    i = j = 0;
66
539k
    while (maxcount-- > 0) {
67
1.14M
        while (i < str_len && STRINGLIB_ISSPACE(str[i]))
68
601k
            i++;
69
539k
        if (i == str_len) break;
70
502k
        j = i; i++;
71
26.6M
        while (i < str_len && !STRINGLIB_ISSPACE(str[i]))
72
26.1M
            i++;
73
502k
#if !STRINGLIB_MUTABLE
74
502k
        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.0k
            Py_INCREF(str_obj);
77
13.0k
            PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
78
13.0k
            count++;
79
13.0k
            break;
80
13.0k
        }
81
489k
#endif
82
1.46M
        SPLIT_ADD(str, j, i);
83
1.46M
    }
84
85
50.2k
    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
708
            i++;
90
708
        if (i != str_len)
91
708
            SPLIT_ADD(str, i, str_len);
92
708
    }
93
50.2k
    FIX_PREALLOC_SIZE(list);
94
50.2k
    return list;
95
96
0
  onError:
97
0
    Py_DECREF(list);
98
    return NULL;
99
50.2k
}
unicodeobject.c:ucs4lib_split_whitespace
Line
Count
Source
57
11.5k
{
58
11.5k
    Py_ssize_t i, j, count=0;
59
11.5k
    PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
60
11.5k
    PyObject *sub;
61
62
11.5k
    if (list == NULL)
63
0
        return NULL;
64
65
11.5k
    i = j = 0;
66
70.7k
    while (maxcount-- > 0) {
67
145k
        while (i < str_len && STRINGLIB_ISSPACE(str[i]))
68
75.2k
            i++;
69
70.4k
        if (i == str_len) break;
70
62.5k
        j = i; i++;
71
11.1M
        while (i < str_len && !STRINGLIB_ISSPACE(str[i]))
72
11.1M
            i++;
73
62.5k
#if !STRINGLIB_MUTABLE
74
62.5k
        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.35k
            Py_INCREF(str_obj);
77
3.35k
            PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
78
3.35k
            count++;
79
3.35k
            break;
80
3.35k
        }
81
59.1k
#endif
82
177k
        SPLIT_ADD(str, j, i);
83
177k
    }
84
85
11.5k
    if (i < str_len) {
86
        /* Only occurs when maxcount was reached */
87
        /* Skip any remaining whitespace and copy to end of string */
88
0
        while (i < str_len && STRINGLIB_ISSPACE(str[i]))
89
0
            i++;
90
0
        if (i != str_len)
91
0
            SPLIT_ADD(str, i, str_len);
92
0
    }
93
11.5k
    FIX_PREALLOC_SIZE(list);
94
11.5k
    return list;
95
96
0
  onError:
97
0
    Py_DECREF(list);
98
    return NULL;
99
11.5k
}
Unexecuted instantiation: bytearrayobject.c:stringlib_split_whitespace
100
101
Py_LOCAL_INLINE(PyObject *)
102
STRINGLIB(split_char)(PyObject* str_obj,
103
                     const STRINGLIB_CHAR* str, Py_ssize_t str_len,
104
                     const STRINGLIB_CHAR ch,
105
                     Py_ssize_t maxcount)
106
28.4M
{
107
28.4M
    Py_ssize_t i, j, count=0;
108
28.4M
    PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
109
28.4M
    PyObject *sub;
110
111
28.4M
    if (list == NULL)
112
0
        return NULL;
113
114
28.4M
    i = j = 0;
115
99.1M
    while ((j < str_len) && (maxcount-- > 0)) {
116
491M
        for(; j < str_len; j++) {
117
            /* I found that using memchr makes no difference */
118
465M
            if (str[j] == ch) {
119
44.5M
                SPLIT_ADD(str, i, j);
120
44.5M
                i = j = j + 1;
121
44.5M
                break;
122
44.5M
            }
123
465M
        }
124
70.6M
    }
125
#if !STRINGLIB_MUTABLE
126
28.4M
    if (count == 0 && STRINGLIB_CHECK_EXACT(str_obj)) {
127
        /* ch not in str_obj, so just use str_obj as list[0] */
128
6.08M
        Py_INCREF(str_obj);
129
6.08M
        PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
130
6.08M
        count++;
131
6.08M
    } else
132
22.3M
#endif
133
22.3M
    if (i <= str_len) {
134
44.7M
        SPLIT_ADD(str, i, str_len);
135
44.7M
    }
136
28.4M
    FIX_PREALLOC_SIZE(list);
137
28.4M
    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.14M
{
107
3.14M
    Py_ssize_t i, j, count=0;
108
3.14M
    PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
109
3.14M
    PyObject *sub;
110
111
3.14M
    if (list == NULL)
112
0
        return NULL;
113
114
3.14M
    i = j = 0;
115
10.6M
    while ((j < str_len) && (maxcount-- > 0)) {
116
68.8M
        for(; j < str_len; j++) {
117
            /* I found that using memchr makes no difference */
118
66.1M
            if (str[j] == ch) {
119
4.83M
                SPLIT_ADD(str, i, j);
120
4.83M
                i = j = j + 1;
121
4.83M
                break;
122
4.83M
            }
123
66.1M
        }
124
7.52M
    }
125
3.14M
#if !STRINGLIB_MUTABLE
126
3.14M
    if (count == 0 && STRINGLIB_CHECK_EXACT(str_obj)) {
127
        /* ch not in str_obj, so just use str_obj as list[0] */
128
2.58M
        Py_INCREF(str_obj);
129
2.58M
        PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
130
2.58M
        count++;
131
2.58M
    } else
132
560k
#endif
133
560k
    if (i <= str_len) {
134
1.12M
        SPLIT_ADD(str, i, str_len);
135
1.12M
    }
136
3.14M
    FIX_PREALLOC_SIZE(list);
137
3.14M
    return list;
138
139
0
  onError:
140
0
    Py_DECREF(list);
141
    return NULL;
142
3.14M
}
unicodeobject.c:asciilib_split_char
Line
Count
Source
106
23.9M
{
107
23.9M
    Py_ssize_t i, j, count=0;
108
23.9M
    PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
109
23.9M
    PyObject *sub;
110
111
23.9M
    if (list == NULL)
112
0
        return NULL;
113
114
23.9M
    i = j = 0;
115
73.4M
    while ((j < str_len) && (maxcount-- > 0)) {
116
298M
        for(; j < str_len; j++) {
117
            /* I found that using memchr makes no difference */
118
275M
            if (str[j] == ch) {
119
26.3M
                SPLIT_ADD(str, i, j);
120
26.3M
                i = j = j + 1;
121
26.3M
                break;
122
26.3M
            }
123
275M
        }
124
49.4M
    }
125
23.9M
#if !STRINGLIB_MUTABLE
126
23.9M
    if (count == 0 && STRINGLIB_CHECK_EXACT(str_obj)) {
127
        /* ch not in str_obj, so just use str_obj as list[0] */
128
3.26M
        Py_INCREF(str_obj);
129
3.26M
        PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
130
3.26M
        count++;
131
3.26M
    } else
132
20.6M
#endif
133
20.6M
    if (i <= str_len) {
134
41.2M
        SPLIT_ADD(str, i, str_len);
135
41.2M
    }
136
23.9M
    FIX_PREALLOC_SIZE(list);
137
23.9M
    return list;
138
139
0
  onError:
140
0
    Py_DECREF(list);
141
    return NULL;
142
23.9M
}
unicodeobject.c:ucs1lib_split_char
Line
Count
Source
106
1.24M
{
107
1.24M
    Py_ssize_t i, j, count=0;
108
1.24M
    PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
109
1.24M
    PyObject *sub;
110
111
1.24M
    if (list == NULL)
112
0
        return NULL;
113
114
1.24M
    i = j = 0;
115
10.7M
    while ((j < str_len) && (maxcount-- > 0)) {
116
67.1M
        for(; j < str_len; j++) {
117
            /* I found that using memchr makes no difference */
118
66.9M
            if (str[j] == ch) {
119
9.22M
                SPLIT_ADD(str, i, j);
120
9.22M
                i = j = j + 1;
121
9.22M
                break;
122
9.22M
            }
123
66.9M
        }
124
9.45M
    }
125
1.24M
#if !STRINGLIB_MUTABLE
126
1.24M
    if (count == 0 && STRINGLIB_CHECK_EXACT(str_obj)) {
127
        /* ch not in str_obj, so just use str_obj as list[0] */
128
200k
        Py_INCREF(str_obj);
129
200k
        PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
130
200k
        count++;
131
200k
    } else
132
1.04M
#endif
133
1.04M
    if (i <= str_len) {
134
2.09M
        SPLIT_ADD(str, i, str_len);
135
2.09M
    }
136
1.24M
    FIX_PREALLOC_SIZE(list);
137
1.24M
    return list;
138
139
0
  onError:
140
0
    Py_DECREF(list);
141
    return NULL;
142
1.24M
}
unicodeobject.c:ucs2lib_split_char
Line
Count
Source
106
120k
{
107
120k
    Py_ssize_t i, j, count=0;
108
120k
    PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
109
120k
    PyObject *sub;
110
111
120k
    if (list == NULL)
112
0
        return NULL;
113
114
120k
    i = j = 0;
115
4.08M
    while ((j < str_len) && (maxcount-- > 0)) {
116
33.9M
        for(; j < str_len; j++) {
117
            /* I found that using memchr makes no difference */
118
33.8M
            if (str[j] == ch) {
119
3.87M
                SPLIT_ADD(str, i, j);
120
3.87M
                i = j = j + 1;
121
3.87M
                break;
122
3.87M
            }
123
33.8M
        }
124
3.96M
    }
125
120k
#if !STRINGLIB_MUTABLE
126
120k
    if (count == 0 && STRINGLIB_CHECK_EXACT(str_obj)) {
127
        /* ch not in str_obj, so just use str_obj as list[0] */
128
38.0k
        Py_INCREF(str_obj);
129
38.0k
        PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
130
38.0k
        count++;
131
38.0k
    } else
132
82.5k
#endif
133
82.5k
    if (i <= str_len) {
134
165k
        SPLIT_ADD(str, i, str_len);
135
165k
    }
136
120k
    FIX_PREALLOC_SIZE(list);
137
120k
    return list;
138
139
0
  onError:
140
0
    Py_DECREF(list);
141
    return NULL;
142
120k
}
unicodeobject.c:ucs4lib_split_char
Line
Count
Source
106
15.5k
{
107
15.5k
    Py_ssize_t i, j, count=0;
108
15.5k
    PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
109
15.5k
    PyObject *sub;
110
111
15.5k
    if (list == NULL)
112
0
        return NULL;
113
114
15.5k
    i = j = 0;
115
254k
    while ((j < str_len) && (maxcount-- > 0)) {
116
22.9M
        for(; j < str_len; j++) {
117
            /* I found that using memchr makes no difference */
118
22.9M
            if (str[j] == ch) {
119
226k
                SPLIT_ADD(str, i, j);
120
226k
                i = j = j + 1;
121
226k
                break;
122
226k
            }
123
22.9M
        }
124
239k
    }
125
15.5k
#if !STRINGLIB_MUTABLE
126
15.5k
    if (count == 0 && STRINGLIB_CHECK_EXACT(str_obj)) {
127
        /* ch not in str_obj, so just use str_obj as list[0] */
128
880
        Py_INCREF(str_obj);
129
880
        PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
130
880
        count++;
131
880
    } else
132
14.6k
#endif
133
14.6k
    if (i <= str_len) {
134
29.3k
        SPLIT_ADD(str, i, str_len);
135
29.3k
    }
136
15.5k
    FIX_PREALLOC_SIZE(list);
137
15.5k
    return list;
138
139
0
  onError:
140
0
    Py_DECREF(list);
141
    return NULL;
142
15.5k
}
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
28.6M
{
150
28.6M
    Py_ssize_t i, j, pos, count=0;
151
28.6M
    PyObject *list, *sub;
152
153
28.6M
    if (sep_len == 0) {
154
0
        PyErr_SetString(PyExc_ValueError, "empty separator");
155
0
        return NULL;
156
0
    }
157
28.6M
    else if (sep_len == 1)
158
28.4M
        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.1k
            break;
169
159k
        j = i + pos;
170
318k
        SPLIT_ADD(str, i, j);
171
318k
        i = j + sep_len;
172
318k
    }
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.1k
        Py_INCREF(str_obj);
177
51.1k
        PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
178
51.1k
        count++;
179
51.1k
    } else
180
159k
#endif
181
159k
    {
182
318k
        SPLIT_ADD(str, i, str_len);
183
318k
    }
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
3.14M
{
150
3.14M
    Py_ssize_t i, j, pos, count=0;
151
3.14M
    PyObject *list, *sub;
152
153
3.14M
    if (sep_len == 0) {
154
0
        PyErr_SetString(PyExc_ValueError, "empty separator");
155
0
        return NULL;
156
0
    }
157
3.14M
    else if (sep_len == 1)
158
3.14M
        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
24.0M
{
150
24.0M
    Py_ssize_t i, j, pos, count=0;
151
24.0M
    PyObject *list, *sub;
152
153
24.0M
    if (sep_len == 0) {
154
0
        PyErr_SetString(PyExc_ValueError, "empty separator");
155
0
        return NULL;
156
0
    }
157
24.0M
    else if (sep_len == 1)
158
23.9M
        return STRINGLIB(split_char)(str_obj, str, str_len, sep[0], maxcount);
159
160
89.0k
    list = PyList_New(PREALLOC_SIZE(maxcount));
161
89.0k
    if (list == NULL)
162
0
        return NULL;
163
164
89.0k
    i = j = 0;
165
141k
    while (maxcount-- > 0) {
166
89.0k
        pos = FASTSEARCH(str+i, str_len-i, sep, sep_len, -1, FAST_SEARCH);
167
89.0k
        if (pos < 0)
168
36.1k
            break;
169
52.8k
        j = i + pos;
170
105k
        SPLIT_ADD(str, i, j);
171
105k
        i = j + sep_len;
172
105k
    }
173
89.0k
#if !STRINGLIB_MUTABLE
174
89.0k
    if (count == 0 && STRINGLIB_CHECK_EXACT(str_obj)) {
175
        /* No match in str_obj, so just use it as list[0] */
176
36.1k
        Py_INCREF(str_obj);
177
36.1k
        PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
178
36.1k
        count++;
179
36.1k
    } else
180
52.8k
#endif
181
52.8k
    {
182
105k
        SPLIT_ADD(str, i, str_len);
183
105k
    }
184
89.0k
    FIX_PREALLOC_SIZE(list);
185
89.0k
    return list;
186
187
0
  onError:
188
0
    Py_DECREF(list);
189
    return NULL;
190
89.0k
}
unicodeobject.c:ucs1lib_split
Line
Count
Source
149
1.26M
{
150
1.26M
    Py_ssize_t i, j, pos, count=0;
151
1.26M
    PyObject *list, *sub;
152
153
1.26M
    if (sep_len == 0) {
154
0
        PyErr_SetString(PyExc_ValueError, "empty separator");
155
0
        return NULL;
156
0
    }
157
1.26M
    else if (sep_len == 1)
158
1.24M
        return STRINGLIB(split_char)(str_obj, str, str_len, sep[0], maxcount);
159
160
18.3k
    list = PyList_New(PREALLOC_SIZE(maxcount));
161
18.3k
    if (list == NULL)
162
0
        return NULL;
163
164
18.3k
    i = j = 0;
165
34.1k
    while (maxcount-- > 0) {
166
18.3k
        pos = FASTSEARCH(str+i, str_len-i, sep, sep_len, -1, FAST_SEARCH);
167
18.3k
        if (pos < 0)
168
2.46k
            break;
169
15.8k
        j = i + pos;
170
31.6k
        SPLIT_ADD(str, i, j);
171
31.6k
        i = j + sep_len;
172
31.6k
    }
173
18.3k
#if !STRINGLIB_MUTABLE
174
18.3k
    if (count == 0 && STRINGLIB_CHECK_EXACT(str_obj)) {
175
        /* No match in str_obj, so just use it as list[0] */
176
2.46k
        Py_INCREF(str_obj);
177
2.46k
        PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
178
2.46k
        count++;
179
2.46k
    } else
180
15.8k
#endif
181
15.8k
    {
182
31.6k
        SPLIT_ADD(str, i, str_len);
183
31.6k
    }
184
18.3k
    FIX_PREALLOC_SIZE(list);
185
18.3k
    return list;
186
187
0
  onError:
188
0
    Py_DECREF(list);
189
    return NULL;
190
18.3k
}
unicodeobject.c:ucs2lib_split
Line
Count
Source
149
202k
{
150
202k
    Py_ssize_t i, j, pos, count=0;
151
202k
    PyObject *list, *sub;
152
153
202k
    if (sep_len == 0) {
154
0
        PyErr_SetString(PyExc_ValueError, "empty separator");
155
0
        return NULL;
156
0
    }
157
202k
    else if (sep_len == 1)
158
120k
        return STRINGLIB(split_char)(str_obj, str, str_len, sep[0], maxcount);
159
160
82.3k
    list = PyList_New(PREALLOC_SIZE(maxcount));
161
82.3k
    if (list == NULL)
162
0
        return NULL;
163
164
82.3k
    i = j = 0;
165
154k
    while (maxcount-- > 0) {
166
82.3k
        pos = FASTSEARCH(str+i, str_len-i, sep, sep_len, -1, FAST_SEARCH);
167
82.3k
        if (pos < 0)
168
9.74k
            break;
169
72.5k
        j = i + pos;
170
145k
        SPLIT_ADD(str, i, j);
171
145k
        i = j + sep_len;
172
145k
    }
173
82.3k
#if !STRINGLIB_MUTABLE
174
82.3k
    if (count == 0 && STRINGLIB_CHECK_EXACT(str_obj)) {
175
        /* No match in str_obj, so just use it as list[0] */
176
9.74k
        Py_INCREF(str_obj);
177
9.74k
        PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
178
9.74k
        count++;
179
9.74k
    } else
180
72.5k
#endif
181
72.5k
    {
182
145k
        SPLIT_ADD(str, i, str_len);
183
145k
    }
184
82.3k
    FIX_PREALLOC_SIZE(list);
185
82.3k
    return list;
186
187
0
  onError:
188
0
    Py_DECREF(list);
189
    return NULL;
190
82.3k
}
unicodeobject.c:ucs4lib_split
Line
Count
Source
149
36.1k
{
150
36.1k
    Py_ssize_t i, j, pos, count=0;
151
36.1k
    PyObject *list, *sub;
152
153
36.1k
    if (sep_len == 0) {
154
0
        PyErr_SetString(PyExc_ValueError, "empty separator");
155
0
        return NULL;
156
0
    }
157
36.1k
    else if (sep_len == 1)
158
15.5k
        return STRINGLIB(split_char)(str_obj, str, str_len, sep[0], maxcount);
159
160
20.6k
    list = PyList_New(PREALLOC_SIZE(maxcount));
161
20.6k
    if (list == NULL)
162
0
        return NULL;
163
164
20.6k
    i = j = 0;
165
38.5k
    while (maxcount-- > 0) {
166
20.6k
        pos = FASTSEARCH(str+i, str_len-i, sep, sep_len, -1, FAST_SEARCH);
167
20.6k
        if (pos < 0)
168
2.71k
            break;
169
17.9k
        j = i + pos;
170
35.8k
        SPLIT_ADD(str, i, j);
171
35.8k
        i = j + sep_len;
172
35.8k
    }
173
20.6k
#if !STRINGLIB_MUTABLE
174
20.6k
    if (count == 0 && STRINGLIB_CHECK_EXACT(str_obj)) {
175
        /* No match in str_obj, so just use it as list[0] */
176
2.71k
        Py_INCREF(str_obj);
177
2.71k
        PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
178
2.71k
        count++;
179
2.71k
    } else
180
17.9k
#endif
181
17.9k
    {
182
35.8k
        SPLIT_ADD(str, i, str_len);
183
35.8k
    }
184
20.6k
    FIX_PREALLOC_SIZE(list);
185
20.6k
    return list;
186
187
0
  onError:
188
0
    Py_DECREF(list);
189
    return NULL;
190
20.6k
}
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
78
{
248
78
    Py_ssize_t i, j, count=0;
249
78
    PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
250
78
    PyObject *sub;
251
252
78
    if (list == NULL)
253
0
        return NULL;
254
255
78
    i = j = str_len - 1;
256
156
    while ((i >= 0) && (maxcount-- > 0)) {
257
150
        for(; i >= 0; i--) {
258
150
            if (str[i] == ch) {
259
78
                SPLIT_ADD(str, i + 1, j + 1);
260
78
                j = i = i - 1;
261
78
                break;
262
78
            }
263
150
        }
264
78
    }
265
#if !STRINGLIB_MUTABLE
266
78
    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
78
#endif
273
78
    if (j >= -1) {
274
156
        SPLIT_ADD(str, 0, j + 1);
275
156
    }
276
78
    FIX_PREALLOC_SIZE(list);
277
78
    if (PyList_Reverse(list) < 0)
278
0
        goto onError;
279
78
    return list;
280
281
0
  onError:
282
0
    Py_DECREF(list);
283
0
    return NULL;
284
78
}
Unexecuted instantiation: bytesobject.c:stringlib_rsplit_char
unicodeobject.c:asciilib_rsplit_char
Line
Count
Source
247
78
{
248
78
    Py_ssize_t i, j, count=0;
249
78
    PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
250
78
    PyObject *sub;
251
252
78
    if (list == NULL)
253
0
        return NULL;
254
255
78
    i = j = str_len - 1;
256
156
    while ((i >= 0) && (maxcount-- > 0)) {
257
150
        for(; i >= 0; i--) {
258
150
            if (str[i] == ch) {
259
78
                SPLIT_ADD(str, i + 1, j + 1);
260
78
                j = i = i - 1;
261
78
                break;
262
78
            }
263
150
        }
264
78
    }
265
78
#if !STRINGLIB_MUTABLE
266
78
    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
78
#endif
273
78
    if (j >= -1) {
274
156
        SPLIT_ADD(str, 0, j + 1);
275
156
    }
276
78
    FIX_PREALLOC_SIZE(list);
277
78
    if (PyList_Reverse(list) < 0)
278
0
        goto onError;
279
78
    return list;
280
281
0
  onError:
282
0
    Py_DECREF(list);
283
    return NULL;
284
78
}
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
78
{
292
78
    Py_ssize_t j, pos, count=0;
293
78
    PyObject *list, *sub;
294
295
78
    if (sep_len == 0) {
296
0
        PyErr_SetString(PyExc_ValueError, "empty separator");
297
0
        return NULL;
298
0
    }
299
78
    else if (sep_len == 1)
300
78
        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
78
{
292
78
    Py_ssize_t j, pos, count=0;
293
78
    PyObject *list, *sub;
294
295
78
    if (sep_len == 0) {
296
0
        PyErr_SetString(PyExc_ValueError, "empty separator");
297
0
        return NULL;
298
0
    }
299
78
    else if (sep_len == 1)
300
78
        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
13.5k
{
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
13.5k
    Py_ssize_t i;
349
13.5k
    Py_ssize_t j;
350
13.5k
    PyObject *list = PyList_New(0);
351
13.5k
    PyObject *sub;
352
353
13.5k
    if (list == NULL)
354
0
        return NULL;
355
356
32.9M
    for (i = j = 0; i < str_len; ) {
357
32.9M
        Py_ssize_t eol;
358
359
        /* Find a line and append it */
360
250M
        while (i < str_len && !STRINGLIB_ISLINEBREAK(str[i]))
361
217M
            i++;
362
363
        /* Skip the line break reading CRLF as one line break */
364
32.9M
        eol = i;
365
32.9M
        if (i < str_len) {
366
32.9M
            if (str[i] == '\r' && i + 1 < str_len && str[i+1] == '\n')
367
192k
                i += 2;
368
32.7M
            else
369
32.7M
                i++;
370
32.9M
            if (keepends)
371
0
                eol = i;
372
32.9M
        }
373
#if !STRINGLIB_MUTABLE
374
32.9M
        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.36k
            if (PyList_Append(list, str_obj))
377
0
                goto onError;
378
5.36k
            break;
379
5.36k
        }
380
32.9M
#endif
381
65.9M
        SPLIT_APPEND(str, j, eol);
382
32.9M
        j = i;
383
32.9M
    }
384
13.5k
    return list;
385
386
0
  onError:
387
0
    Py_DECREF(list);
388
    return NULL;
389
13.5k
}
Unexecuted instantiation: bytesobject.c:stringlib_splitlines
unicodeobject.c:asciilib_splitlines
Line
Count
Source
339
2.69k
{
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.69k
    Py_ssize_t i;
349
2.69k
    Py_ssize_t j;
350
2.69k
    PyObject *list = PyList_New(0);
351
2.69k
    PyObject *sub;
352
353
2.69k
    if (list == NULL)
354
0
        return NULL;
355
356
9.18M
    for (i = j = 0; i < str_len; ) {
357
9.18M
        Py_ssize_t eol;
358
359
        /* Find a line and append it */
360
39.5M
        while (i < str_len && !STRINGLIB_ISLINEBREAK(str[i]))
361
30.3M
            i++;
362
363
        /* Skip the line break reading CRLF as one line break */
364
9.18M
        eol = i;
365
9.18M
        if (i < str_len) {
366
9.18M
            if (str[i] == '\r' && i + 1 < str_len && str[i+1] == '\n')
367
4.48k
                i += 2;
368
9.18M
            else
369
9.18M
                i++;
370
9.18M
            if (keepends)
371
0
                eol = i;
372
9.18M
        }
373
9.18M
#if !STRINGLIB_MUTABLE
374
9.18M
        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
946
            if (PyList_Append(list, str_obj))
377
0
                goto onError;
378
946
            break;
379
946
        }
380
9.18M
#endif
381
18.3M
        SPLIT_APPEND(str, j, eol);
382
9.18M
        j = i;
383
9.18M
    }
384
2.69k
    return list;
385
386
0
  onError:
387
0
    Py_DECREF(list);
388
    return NULL;
389
2.69k
}
unicodeobject.c:ucs1lib_splitlines
Line
Count
Source
339
848
{
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
848
    Py_ssize_t i;
349
848
    Py_ssize_t j;
350
848
    PyObject *list = PyList_New(0);
351
848
    PyObject *sub;
352
353
848
    if (list == NULL)
354
0
        return NULL;
355
356
2.26M
    for (i = j = 0; i < str_len; ) {
357
2.26M
        Py_ssize_t eol;
358
359
        /* Find a line and append it */
360
10.0M
        while (i < str_len && !STRINGLIB_ISLINEBREAK(str[i]))
361
7.76M
            i++;
362
363
        /* Skip the line break reading CRLF as one line break */
364
2.26M
        eol = i;
365
2.26M
        if (i < str_len) {
366
2.26M
            if (str[i] == '\r' && i + 1 < str_len && str[i+1] == '\n')
367
1.24k
                i += 2;
368
2.25M
            else
369
2.25M
                i++;
370
2.26M
            if (keepends)
371
0
                eol = i;
372
2.26M
        }
373
2.26M
#if !STRINGLIB_MUTABLE
374
2.26M
        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
225
            if (PyList_Append(list, str_obj))
377
0
                goto onError;
378
225
            break;
379
225
        }
380
2.26M
#endif
381
4.52M
        SPLIT_APPEND(str, j, eol);
382
2.26M
        j = i;
383
2.26M
    }
384
848
    return list;
385
386
0
  onError:
387
0
    Py_DECREF(list);
388
    return NULL;
389
848
}
unicodeobject.c:ucs2lib_splitlines
Line
Count
Source
339
6.87k
{
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
6.87k
    Py_ssize_t i;
349
6.87k
    Py_ssize_t j;
350
6.87k
    PyObject *list = PyList_New(0);
351
6.87k
    PyObject *sub;
352
353
6.87k
    if (list == NULL)
354
0
        return NULL;
355
356
8.81M
    for (i = j = 0; i < str_len; ) {
357
8.80M
        Py_ssize_t eol;
358
359
        /* Find a line and append it */
360
84.9M
        while (i < str_len && !STRINGLIB_ISLINEBREAK(str[i]))
361
76.1M
            i++;
362
363
        /* Skip the line break reading CRLF as one line break */
364
8.80M
        eol = i;
365
8.80M
        if (i < str_len) {
366
8.80M
            if (str[i] == '\r' && i + 1 < str_len && str[i+1] == '\n')
367
124k
                i += 2;
368
8.67M
            else
369
8.67M
                i++;
370
8.80M
            if (keepends)
371
0
                eol = i;
372
8.80M
        }
373
8.80M
#if !STRINGLIB_MUTABLE
374
8.80M
        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
2.98k
            if (PyList_Append(list, str_obj))
377
0
                goto onError;
378
2.98k
            break;
379
2.98k
        }
380
8.80M
#endif
381
17.6M
        SPLIT_APPEND(str, j, eol);
382
8.80M
        j = i;
383
8.80M
    }
384
6.87k
    return list;
385
386
0
  onError:
387
0
    Py_DECREF(list);
388
    return NULL;
389
6.87k
}
unicodeobject.c:ucs4lib_splitlines
Line
Count
Source
339
3.09k
{
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.09k
    Py_ssize_t i;
349
3.09k
    Py_ssize_t j;
350
3.09k
    PyObject *list = PyList_New(0);
351
3.09k
    PyObject *sub;
352
353
3.09k
    if (list == NULL)
354
0
        return NULL;
355
356
12.7M
    for (i = j = 0; i < str_len; ) {
357
12.6M
        Py_ssize_t eol;
358
359
        /* Find a line and append it */
360
116M
        while (i < str_len && !STRINGLIB_ISLINEBREAK(str[i]))
361
103M
            i++;
362
363
        /* Skip the line break reading CRLF as one line break */
364
12.6M
        eol = i;
365
12.6M
        if (i < str_len) {
366
12.6M
            if (str[i] == '\r' && i + 1 < str_len && str[i+1] == '\n')
367
61.9k
                i += 2;
368
12.6M
            else
369
12.6M
                i++;
370
12.6M
            if (keepends)
371
0
                eol = i;
372
12.6M
        }
373
12.6M
#if !STRINGLIB_MUTABLE
374
12.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
1.20k
            if (PyList_Append(list, str_obj))
377
0
                goto onError;
378
1.20k
            break;
379
1.20k
        }
380
12.6M
#endif
381
25.3M
        SPLIT_APPEND(str, j, eol);
382
12.6M
        j = i;
383
12.6M
    }
384
3.09k
    return list;
385
386
0
  onError:
387
0
    Py_DECREF(list);
388
    return NULL;
389
3.09k
}
Unexecuted instantiation: bytearrayobject.c:stringlib_splitlines
390