Coverage Report

Created: 2025-11-09 06:26

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
116M
#define MAX_PREALLOC 12
15
16
/* 5 splits gives 6 elements */
17
#define PREALLOC_SIZE(maxsplit) \
18
24.4M
    (maxsplit >= MAX_PREALLOC ? MAX_PREALLOC : maxsplit+1)
19
20
#define SPLIT_APPEND(data, left, right)         \
21
32.6M
    sub = STRINGLIB_NEW((data) + (left),        \
22
32.6M
                        (right) - (left));      \
23
32.6M
    if (sub == NULL)                            \
24
32.6M
        goto onError;                           \
25
32.6M
    if (PyList_Append(list, sub)) {             \
26
0
        Py_DECREF(sub);                         \
27
0
        goto onError;                           \
28
0
    }                                           \
29
32.6M
    else                                        \
30
32.6M
        Py_DECREF(sub);
31
32
63.3M
#define SPLIT_ADD(data, left, right) {          \
33
63.3M
    sub = STRINGLIB_NEW((data) + (left),        \
34
63.3M
                        (right) - (left));      \
35
63.3M
    if (sub == NULL)                            \
36
63.3M
        goto onError;                           \
37
63.3M
    if (count < MAX_PREALLOC) {                 \
38
37.2M
        PyList_SET_ITEM(list, count, sub);      \
39
37.2M
    } else {                                    \
40
26.0M
        if (PyList_Append(list, sub)) {         \
41
0
            Py_DECREF(sub);                     \
42
0
            goto onError;                       \
43
0
        }                                       \
44
26.0M
        else                                    \
45
26.0M
            Py_DECREF(sub);                     \
46
26.0M
    }                                           \
47
63.3M
    count++; }
48
49
50
/* Always force the list to the expected size. */
51
24.4M
#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
173k
{
58
173k
    Py_ssize_t i, j, count=0;
59
173k
    PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
60
173k
    PyObject *sub;
61
62
173k
    if (list == NULL)
63
0
        return NULL;
64
65
173k
    i = j = 0;
66
2.33M
    while (maxcount-- > 0) {
67
4.48M
        while (i < str_len && STRINGLIB_ISSPACE(str[i]))
68
2.18M
            i++;
69
2.30M
        if (i == str_len) break;
70
2.20M
        j = i; i++;
71
125M
        while (i < str_len && !STRINGLIB_ISSPACE(str[i]))
72
123M
            i++;
73
#if !STRINGLIB_MUTABLE
74
2.20M
        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
43.5k
            Py_INCREF(str_obj);
77
43.5k
            PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
78
43.5k
            count++;
79
43.5k
            break;
80
43.5k
        }
81
2.16M
#endif
82
6.48M
        SPLIT_ADD(str, j, i);
83
6.48M
    }
84
85
173k
    if (i < str_len) {
86
        /* Only occurs when maxcount was reached */
87
        /* Skip any remaining whitespace and copy to end of string */
88
54.6k
        while (i < str_len && STRINGLIB_ISSPACE(str[i]))
89
29.3k
            i++;
90
25.3k
        if (i != str_len)
91
25.3k
            SPLIT_ADD(str, i, str_len);
92
25.3k
    }
93
173k
    FIX_PREALLOC_SIZE(list);
94
173k
    return list;
95
96
0
  onError:
97
0
    Py_DECREF(list);
98
0
    return NULL;
99
173k
}
Unexecuted instantiation: bytesobject.c:stringlib_split_whitespace
unicodeobject.c:asciilib_split_whitespace
Line
Count
Source
57
86.2k
{
58
86.2k
    Py_ssize_t i, j, count=0;
59
86.2k
    PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
60
86.2k
    PyObject *sub;
61
62
86.2k
    if (list == NULL)
63
0
        return NULL;
64
65
86.2k
    i = j = 0;
66
831k
    while (maxcount-- > 0) {
67
1.50M
        while (i < str_len && STRINGLIB_ISSPACE(str[i]))
68
693k
            i++;
69
814k
        if (i == str_len) break;
70
767k
        j = i; i++;
71
46.2M
        while (i < str_len && !STRINGLIB_ISSPACE(str[i]))
72
45.5M
            i++;
73
767k
#if !STRINGLIB_MUTABLE
74
767k
        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
22.4k
            Py_INCREF(str_obj);
77
22.4k
            PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
78
22.4k
            count++;
79
22.4k
            break;
80
22.4k
        }
81
745k
#endif
82
2.23M
        SPLIT_ADD(str, j, i);
83
2.23M
    }
84
85
86.2k
    if (i < str_len) {
86
        /* Only occurs when maxcount was reached */
87
        /* Skip any remaining whitespace and copy to end of string */
88
28.1k
        while (i < str_len && STRINGLIB_ISSPACE(str[i]))
89
14.5k
            i++;
90
13.6k
        if (i != str_len)
91
13.6k
            SPLIT_ADD(str, i, str_len);
92
13.6k
    }
93
86.2k
    FIX_PREALLOC_SIZE(list);
94
86.2k
    return list;
95
96
0
  onError:
97
0
    Py_DECREF(list);
98
    return NULL;
99
86.2k
}
unicodeobject.c:ucs1lib_split_whitespace
Line
Count
Source
57
25.4k
{
58
25.4k
    Py_ssize_t i, j, count=0;
59
25.4k
    PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
60
25.4k
    PyObject *sub;
61
62
25.4k
    if (list == NULL)
63
0
        return NULL;
64
65
25.4k
    i = j = 0;
66
716k
    while (maxcount-- > 0) {
67
1.38M
        while (i < str_len && STRINGLIB_ISSPACE(str[i]))
68
678k
            i++;
69
705k
        if (i == str_len) break;
70
694k
        j = i; i++;
71
29.8M
        while (i < str_len && !STRINGLIB_ISSPACE(str[i]))
72
29.1M
            i++;
73
694k
#if !STRINGLIB_MUTABLE
74
694k
        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.12k
            Py_INCREF(str_obj);
77
3.12k
            PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
78
3.12k
            count++;
79
3.12k
            break;
80
3.12k
        }
81
690k
#endif
82
2.07M
        SPLIT_ADD(str, j, i);
83
2.07M
    }
84
85
25.4k
    if (i < str_len) {
86
        /* Only occurs when maxcount was reached */
87
        /* Skip any remaining whitespace and copy to end of string */
88
24.4k
        while (i < str_len && STRINGLIB_ISSPACE(str[i]))
89
13.7k
            i++;
90
10.6k
        if (i != str_len)
91
10.6k
            SPLIT_ADD(str, i, str_len);
92
10.6k
    }
93
25.4k
    FIX_PREALLOC_SIZE(list);
94
25.4k
    return list;
95
96
0
  onError:
97
0
    Py_DECREF(list);
98
    return NULL;
99
25.4k
}
unicodeobject.c:ucs2lib_split_whitespace
Line
Count
Source
57
49.5k
{
58
49.5k
    Py_ssize_t i, j, count=0;
59
49.5k
    PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
60
49.5k
    PyObject *sub;
61
62
49.5k
    if (list == NULL)
63
0
        return NULL;
64
65
49.5k
    i = j = 0;
66
667k
    while (maxcount-- > 0) {
67
1.37M
        while (i < str_len && STRINGLIB_ISSPACE(str[i]))
68
706k
            i++;
69
666k
        if (i == str_len) break;
70
633k
        j = i; i++;
71
37.9M
        while (i < str_len && !STRINGLIB_ISSPACE(str[i]))
72
37.3M
            i++;
73
633k
#if !STRINGLIB_MUTABLE
74
633k
        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
15.2k
            Py_INCREF(str_obj);
77
15.2k
            PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
78
15.2k
            count++;
79
15.2k
            break;
80
15.2k
        }
81
618k
#endif
82
1.85M
        SPLIT_ADD(str, j, i);
83
1.85M
    }
84
85
49.5k
    if (i < str_len) {
86
        /* Only occurs when maxcount was reached */
87
        /* Skip any remaining whitespace and copy to end of string */
88
2.08k
        while (i < str_len && STRINGLIB_ISSPACE(str[i]))
89
1.04k
            i++;
90
1.04k
        if (i != str_len)
91
1.04k
            SPLIT_ADD(str, i, str_len);
92
1.04k
    }
93
49.5k
    FIX_PREALLOC_SIZE(list);
94
49.5k
    return list;
95
96
0
  onError:
97
0
    Py_DECREF(list);
98
    return NULL;
99
49.5k
}
unicodeobject.c:ucs4lib_split_whitespace
Line
Count
Source
57
12.4k
{
58
12.4k
    Py_ssize_t i, j, count=0;
59
12.4k
    PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
60
12.4k
    PyObject *sub;
61
62
12.4k
    if (list == NULL)
63
0
        return NULL;
64
65
12.4k
    i = j = 0;
66
119k
    while (maxcount-- > 0) {
67
224k
        while (i < str_len && STRINGLIB_ISSPACE(str[i]))
68
105k
            i++;
69
119k
        if (i == str_len) break;
70
110k
        j = i; i++;
71
11.7M
        while (i < str_len && !STRINGLIB_ISSPACE(str[i]))
72
11.6M
            i++;
73
110k
#if !STRINGLIB_MUTABLE
74
110k
        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.75k
            Py_INCREF(str_obj);
77
2.75k
            PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
78
2.75k
            count++;
79
2.75k
            break;
80
2.75k
        }
81
107k
#endif
82
322k
        SPLIT_ADD(str, j, i);
83
322k
    }
84
85
12.4k
    if (i < str_len) {
86
        /* Only occurs when maxcount was reached */
87
        /* Skip any remaining whitespace and copy to end of string */
88
0
        while (i < str_len && STRINGLIB_ISSPACE(str[i]))
89
0
            i++;
90
0
        if (i != str_len)
91
0
            SPLIT_ADD(str, i, str_len);
92
0
    }
93
12.4k
    FIX_PREALLOC_SIZE(list);
94
12.4k
    return list;
95
96
0
  onError:
97
0
    Py_DECREF(list);
98
    return NULL;
99
12.4k
}
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
24.0M
{
107
24.0M
    Py_ssize_t i, j, count=0;
108
24.0M
    PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
109
24.0M
    PyObject *sub;
110
111
24.0M
    if (list == NULL)
112
0
        return NULL;
113
114
24.0M
    i = j = 0;
115
88.8M
    while ((j < str_len) && (maxcount-- > 0)) {
116
431M
        for(; j < str_len; j++) {
117
            /* I found that using memchr makes no difference */
118
409M
            if (str[j] == ch) {
119
43.2M
                SPLIT_ADD(str, i, j);
120
43.2M
                i = j = j + 1;
121
43.2M
                break;
122
43.2M
            }
123
409M
        }
124
64.7M
    }
125
#if !STRINGLIB_MUTABLE
126
24.0M
    if (count == 0 && STRINGLIB_CHECK_EXACT(str_obj)) {
127
        /* ch not in str_obj, so just use str_obj as list[0] */
128
6.45M
        Py_INCREF(str_obj);
129
6.45M
        PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
130
6.45M
        count++;
131
6.45M
    } else
132
17.6M
#endif
133
17.6M
    if (i <= str_len) {
134
35.2M
        SPLIT_ADD(str, i, str_len);
135
35.2M
    }
136
24.0M
    FIX_PREALLOC_SIZE(list);
137
24.0M
    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.59M
{
107
3.59M
    Py_ssize_t i, j, count=0;
108
3.59M
    PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
109
3.59M
    PyObject *sub;
110
111
3.59M
    if (list == NULL)
112
0
        return NULL;
113
114
3.59M
    i = j = 0;
115
12.1M
    while ((j < str_len) && (maxcount-- > 0)) {
116
64.3M
        for(; j < str_len; j++) {
117
            /* I found that using memchr makes no difference */
118
61.3M
            if (str[j] == ch) {
119
5.57M
                SPLIT_ADD(str, i, j);
120
5.57M
                i = j = j + 1;
121
5.57M
                break;
122
5.57M
            }
123
61.3M
        }
124
8.55M
    }
125
3.59M
#if !STRINGLIB_MUTABLE
126
3.59M
    if (count == 0 && STRINGLIB_CHECK_EXACT(str_obj)) {
127
        /* ch not in str_obj, so just use str_obj as list[0] */
128
2.87M
        Py_INCREF(str_obj);
129
2.87M
        PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
130
2.87M
        count++;
131
2.87M
    } else
132
724k
#endif
133
724k
    if (i <= str_len) {
134
1.44M
        SPLIT_ADD(str, i, str_len);
135
1.44M
    }
136
3.59M
    FIX_PREALLOC_SIZE(list);
137
3.59M
    return list;
138
139
0
  onError:
140
0
    Py_DECREF(list);
141
    return NULL;
142
3.59M
}
unicodeobject.c:asciilib_split_char
Line
Count
Source
106
19.0M
{
107
19.0M
    Py_ssize_t i, j, count=0;
108
19.0M
    PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
109
19.0M
    PyObject *sub;
110
111
19.0M
    if (list == NULL)
112
0
        return NULL;
113
114
19.0M
    i = j = 0;
115
61.5M
    while ((j < str_len) && (maxcount-- > 0)) {
116
241M
        for(; j < str_len; j++) {
117
            /* I found that using memchr makes no difference */
118
223M
            if (str[j] == ch) {
119
24.1M
                SPLIT_ADD(str, i, j);
120
24.1M
                i = j = j + 1;
121
24.1M
                break;
122
24.1M
            }
123
223M
        }
124
42.4M
    }
125
19.0M
#if !STRINGLIB_MUTABLE
126
19.0M
    if (count == 0 && STRINGLIB_CHECK_EXACT(str_obj)) {
127
        /* ch not in str_obj, so just use str_obj as list[0] */
128
3.42M
        Py_INCREF(str_obj);
129
3.42M
        PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
130
3.42M
        count++;
131
3.42M
    } else
132
15.6M
#endif
133
15.6M
    if (i <= str_len) {
134
31.2M
        SPLIT_ADD(str, i, str_len);
135
31.2M
    }
136
19.0M
    FIX_PREALLOC_SIZE(list);
137
19.0M
    return list;
138
139
0
  onError:
140
0
    Py_DECREF(list);
141
    return NULL;
142
19.0M
}
unicodeobject.c:ucs1lib_split_char
Line
Count
Source
106
1.30M
{
107
1.30M
    Py_ssize_t i, j, count=0;
108
1.30M
    PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
109
1.30M
    PyObject *sub;
110
111
1.30M
    if (list == NULL)
112
0
        return NULL;
113
114
1.30M
    i = j = 0;
115
11.3M
    while ((j < str_len) && (maxcount-- > 0)) {
116
57.4M
        for(; j < str_len; j++) {
117
            /* I found that using memchr makes no difference */
118
57.2M
            if (str[j] == ch) {
119
9.89M
                SPLIT_ADD(str, i, j);
120
9.89M
                i = j = j + 1;
121
9.89M
                break;
122
9.89M
            }
123
57.2M
        }
124
10.0M
    }
125
1.30M
#if !STRINGLIB_MUTABLE
126
1.30M
    if (count == 0 && STRINGLIB_CHECK_EXACT(str_obj)) {
127
        /* ch not in str_obj, so just use str_obj as list[0] */
128
142k
        Py_INCREF(str_obj);
129
142k
        PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
130
142k
        count++;
131
142k
    } else
132
1.16M
#endif
133
1.16M
    if (i <= str_len) {
134
2.33M
        SPLIT_ADD(str, i, str_len);
135
2.33M
    }
136
1.30M
    FIX_PREALLOC_SIZE(list);
137
1.30M
    return list;
138
139
0
  onError:
140
0
    Py_DECREF(list);
141
    return NULL;
142
1.30M
}
unicodeobject.c:ucs2lib_split_char
Line
Count
Source
106
92.5k
{
107
92.5k
    Py_ssize_t i, j, count=0;
108
92.5k
    PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
109
92.5k
    PyObject *sub;
110
111
92.5k
    if (list == NULL)
112
0
        return NULL;
113
114
92.5k
    i = j = 0;
115
3.11M
    while ((j < str_len) && (maxcount-- > 0)) {
116
44.5M
        for(; j < str_len; j++) {
117
            /* I found that using memchr makes no difference */
118
44.5M
            if (str[j] == ch) {
119
2.96M
                SPLIT_ADD(str, i, j);
120
2.96M
                i = j = j + 1;
121
2.96M
                break;
122
2.96M
            }
123
44.5M
        }
124
3.02M
    }
125
92.5k
#if !STRINGLIB_MUTABLE
126
92.5k
    if (count == 0 && STRINGLIB_CHECK_EXACT(str_obj)) {
127
        /* ch not in str_obj, so just use str_obj as list[0] */
128
7.97k
        Py_INCREF(str_obj);
129
7.97k
        PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
130
7.97k
        count++;
131
7.97k
    } else
132
84.5k
#endif
133
84.5k
    if (i <= str_len) {
134
169k
        SPLIT_ADD(str, i, str_len);
135
169k
    }
136
92.5k
    FIX_PREALLOC_SIZE(list);
137
92.5k
    return list;
138
139
0
  onError:
140
0
    Py_DECREF(list);
141
    return NULL;
142
92.5k
}
unicodeobject.c:ucs4lib_split_char
Line
Count
Source
106
16.0k
{
107
16.0k
    Py_ssize_t i, j, count=0;
108
16.0k
    PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
109
16.0k
    PyObject *sub;
110
111
16.0k
    if (list == NULL)
112
0
        return NULL;
113
114
16.0k
    i = j = 0;
115
687k
    while ((j < str_len) && (maxcount-- > 0)) {
116
23.4M
        for(; j < str_len; j++) {
117
            /* I found that using memchr makes no difference */
118
23.4M
            if (str[j] == ch) {
119
658k
                SPLIT_ADD(str, i, j);
120
658k
                i = j = j + 1;
121
658k
                break;
122
658k
            }
123
23.4M
        }
124
671k
    }
125
16.0k
#if !STRINGLIB_MUTABLE
126
16.0k
    if (count == 0 && STRINGLIB_CHECK_EXACT(str_obj)) {
127
        /* ch not in str_obj, so just use str_obj as list[0] */
128
358
        Py_INCREF(str_obj);
129
358
        PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
130
358
        count++;
131
358
    } else
132
15.7k
#endif
133
15.7k
    if (i <= str_len) {
134
31.4k
        SPLIT_ADD(str, i, str_len);
135
31.4k
    }
136
16.0k
    FIX_PREALLOC_SIZE(list);
137
16.0k
    return list;
138
139
0
  onError:
140
0
    Py_DECREF(list);
141
    return NULL;
142
16.0k
}
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
24.2M
{
150
24.2M
    Py_ssize_t i, j, pos, count=0;
151
24.2M
    PyObject *list, *sub;
152
153
24.2M
    if (sep_len == 0) {
154
0
        PyErr_SetString(PyExc_ValueError, "empty separator");
155
0
        return NULL;
156
0
    }
157
24.2M
    else if (sep_len == 1)
158
24.0M
        return STRINGLIB(split_char)(str_obj, str, str_len, sep[0], maxcount);
159
160
215k
    list = PyList_New(PREALLOC_SIZE(maxcount));
161
215k
    if (list == NULL)
162
0
        return NULL;
163
164
215k
    i = j = 0;
165
384k
    while (maxcount-- > 0) {
166
215k
        pos = FASTSEARCH(str+i, str_len-i, sep, sep_len, -1, FAST_SEARCH);
167
215k
        if (pos < 0)
168
47.0k
            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
215k
    if (count == 0 && STRINGLIB_CHECK_EXACT(str_obj)) {
175
        /* No match in str_obj, so just use it as list[0] */
176
47.0k
        Py_INCREF(str_obj);
177
47.0k
        PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
178
47.0k
        count++;
179
47.0k
    } else
180
168k
#endif
181
168k
    {
182
337k
        SPLIT_ADD(str, i, str_len);
183
337k
    }
184
215k
    FIX_PREALLOC_SIZE(list);
185
215k
    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.59M
{
150
3.59M
    Py_ssize_t i, j, pos, count=0;
151
3.59M
    PyObject *list, *sub;
152
153
3.59M
    if (sep_len == 0) {
154
0
        PyErr_SetString(PyExc_ValueError, "empty separator");
155
0
        return NULL;
156
0
    }
157
3.59M
    else if (sep_len == 1)
158
3.59M
        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
19.1M
{
150
19.1M
    Py_ssize_t i, j, pos, count=0;
151
19.1M
    PyObject *list, *sub;
152
153
19.1M
    if (sep_len == 0) {
154
0
        PyErr_SetString(PyExc_ValueError, "empty separator");
155
0
        return NULL;
156
0
    }
157
19.1M
    else if (sep_len == 1)
158
19.0M
        return STRINGLIB(split_char)(str_obj, str, str_len, sep[0], maxcount);
159
160
90.7k
    list = PyList_New(PREALLOC_SIZE(maxcount));
161
90.7k
    if (list == NULL)
162
0
        return NULL;
163
164
90.7k
    i = j = 0;
165
150k
    while (maxcount-- > 0) {
166
90.7k
        pos = FASTSEARCH(str+i, str_len-i, sep, sep_len, -1, FAST_SEARCH);
167
90.7k
        if (pos < 0)
168
30.9k
            break;
169
59.7k
        j = i + pos;
170
119k
        SPLIT_ADD(str, i, j);
171
119k
        i = j + sep_len;
172
119k
    }
173
90.7k
#if !STRINGLIB_MUTABLE
174
90.7k
    if (count == 0 && STRINGLIB_CHECK_EXACT(str_obj)) {
175
        /* No match in str_obj, so just use it as list[0] */
176
30.9k
        Py_INCREF(str_obj);
177
30.9k
        PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
178
30.9k
        count++;
179
30.9k
    } else
180
59.7k
#endif
181
59.7k
    {
182
119k
        SPLIT_ADD(str, i, str_len);
183
119k
    }
184
90.7k
    FIX_PREALLOC_SIZE(list);
185
90.7k
    return list;
186
187
0
  onError:
188
0
    Py_DECREF(list);
189
    return NULL;
190
90.7k
}
unicodeobject.c:ucs1lib_split
Line
Count
Source
149
1.32M
{
150
1.32M
    Py_ssize_t i, j, pos, count=0;
151
1.32M
    PyObject *list, *sub;
152
153
1.32M
    if (sep_len == 0) {
154
0
        PyErr_SetString(PyExc_ValueError, "empty separator");
155
0
        return NULL;
156
0
    }
157
1.32M
    else if (sep_len == 1)
158
1.30M
        return STRINGLIB(split_char)(str_obj, str, str_len, sep[0], maxcount);
159
160
14.5k
    list = PyList_New(PREALLOC_SIZE(maxcount));
161
14.5k
    if (list == NULL)
162
0
        return NULL;
163
164
14.5k
    i = j = 0;
165
27.7k
    while (maxcount-- > 0) {
166
14.5k
        pos = FASTSEARCH(str+i, str_len-i, sep, sep_len, -1, FAST_SEARCH);
167
14.5k
        if (pos < 0)
168
1.23k
            break;
169
13.2k
        j = i + pos;
170
26.5k
        SPLIT_ADD(str, i, j);
171
26.5k
        i = j + sep_len;
172
26.5k
    }
173
14.5k
#if !STRINGLIB_MUTABLE
174
14.5k
    if (count == 0 && STRINGLIB_CHECK_EXACT(str_obj)) {
175
        /* No match in str_obj, so just use it as list[0] */
176
1.23k
        Py_INCREF(str_obj);
177
1.23k
        PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
178
1.23k
        count++;
179
1.23k
    } else
180
13.2k
#endif
181
13.2k
    {
182
26.5k
        SPLIT_ADD(str, i, str_len);
183
26.5k
    }
184
14.5k
    FIX_PREALLOC_SIZE(list);
185
14.5k
    return list;
186
187
0
  onError:
188
0
    Py_DECREF(list);
189
    return NULL;
190
14.5k
}
unicodeobject.c:ucs2lib_split
Line
Count
Source
149
180k
{
150
180k
    Py_ssize_t i, j, pos, count=0;
151
180k
    PyObject *list, *sub;
152
153
180k
    if (sep_len == 0) {
154
0
        PyErr_SetString(PyExc_ValueError, "empty separator");
155
0
        return NULL;
156
0
    }
157
180k
    else if (sep_len == 1)
158
92.5k
        return STRINGLIB(split_char)(str_obj, str, str_len, sep[0], maxcount);
159
160
88.3k
    list = PyList_New(PREALLOC_SIZE(maxcount));
161
88.3k
    if (list == NULL)
162
0
        return NULL;
163
164
88.3k
    i = j = 0;
165
165k
    while (maxcount-- > 0) {
166
88.3k
        pos = FASTSEARCH(str+i, str_len-i, sep, sep_len, -1, FAST_SEARCH);
167
88.3k
        if (pos < 0)
168
11.3k
            break;
169
77.0k
        j = i + pos;
170
154k
        SPLIT_ADD(str, i, j);
171
154k
        i = j + sep_len;
172
154k
    }
173
88.3k
#if !STRINGLIB_MUTABLE
174
88.3k
    if (count == 0 && STRINGLIB_CHECK_EXACT(str_obj)) {
175
        /* No match in str_obj, so just use it as list[0] */
176
11.3k
        Py_INCREF(str_obj);
177
11.3k
        PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
178
11.3k
        count++;
179
11.3k
    } else
180
77.0k
#endif
181
77.0k
    {
182
154k
        SPLIT_ADD(str, i, str_len);
183
154k
    }
184
88.3k
    FIX_PREALLOC_SIZE(list);
185
88.3k
    return list;
186
187
0
  onError:
188
0
    Py_DECREF(list);
189
    return NULL;
190
88.3k
}
unicodeobject.c:ucs4lib_split
Line
Count
Source
149
38.2k
{
150
38.2k
    Py_ssize_t i, j, pos, count=0;
151
38.2k
    PyObject *list, *sub;
152
153
38.2k
    if (sep_len == 0) {
154
0
        PyErr_SetString(PyExc_ValueError, "empty separator");
155
0
        return NULL;
156
0
    }
157
38.2k
    else if (sep_len == 1)
158
16.0k
        return STRINGLIB(split_char)(str_obj, str, str_len, sep[0], maxcount);
159
160
22.1k
    list = PyList_New(PREALLOC_SIZE(maxcount));
161
22.1k
    if (list == NULL)
162
0
        return NULL;
163
164
22.1k
    i = j = 0;
165
40.8k
    while (maxcount-- > 0) {
166
22.1k
        pos = FASTSEARCH(str+i, str_len-i, sep, sep_len, -1, FAST_SEARCH);
167
22.1k
        if (pos < 0)
168
3.48k
            break;
169
18.6k
        j = i + pos;
170
37.3k
        SPLIT_ADD(str, i, j);
171
37.3k
        i = j + sep_len;
172
37.3k
    }
173
22.1k
#if !STRINGLIB_MUTABLE
174
22.1k
    if (count == 0 && STRINGLIB_CHECK_EXACT(str_obj)) {
175
        /* No match in str_obj, so just use it as list[0] */
176
3.48k
        Py_INCREF(str_obj);
177
3.48k
        PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
178
3.48k
        count++;
179
3.48k
    } else
180
18.6k
#endif
181
18.6k
    {
182
37.3k
        SPLIT_ADD(str, i, str_len);
183
37.3k
    }
184
22.1k
    FIX_PREALLOC_SIZE(list);
185
22.1k
    return list;
186
187
0
  onError:
188
0
    Py_DECREF(list);
189
    return NULL;
190
22.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
50
{
248
50
    Py_ssize_t i, j, count=0;
249
50
    PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
250
50
    PyObject *sub;
251
252
50
    if (list == NULL)
253
0
        return NULL;
254
255
50
    i = j = str_len - 1;
256
100
    while ((i >= 0) && (maxcount-- > 0)) {
257
50
        for(; i >= 0; i--) {
258
50
            if (str[i] == ch) {
259
50
                SPLIT_ADD(str, i + 1, j + 1);
260
50
                j = i = i - 1;
261
50
                break;
262
50
            }
263
50
        }
264
50
    }
265
#if !STRINGLIB_MUTABLE
266
50
    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
50
#endif
273
50
    if (j >= -1) {
274
100
        SPLIT_ADD(str, 0, j + 1);
275
100
    }
276
50
    FIX_PREALLOC_SIZE(list);
277
50
    if (PyList_Reverse(list) < 0)
278
0
        goto onError;
279
50
    return list;
280
281
0
  onError:
282
0
    Py_DECREF(list);
283
0
    return NULL;
284
50
}
Unexecuted instantiation: bytesobject.c:stringlib_rsplit_char
unicodeobject.c:asciilib_rsplit_char
Line
Count
Source
247
50
{
248
50
    Py_ssize_t i, j, count=0;
249
50
    PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
250
50
    PyObject *sub;
251
252
50
    if (list == NULL)
253
0
        return NULL;
254
255
50
    i = j = str_len - 1;
256
100
    while ((i >= 0) && (maxcount-- > 0)) {
257
50
        for(; i >= 0; i--) {
258
50
            if (str[i] == ch) {
259
50
                SPLIT_ADD(str, i + 1, j + 1);
260
50
                j = i = i - 1;
261
50
                break;
262
50
            }
263
50
        }
264
50
    }
265
50
#if !STRINGLIB_MUTABLE
266
50
    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
50
#endif
273
50
    if (j >= -1) {
274
100
        SPLIT_ADD(str, 0, j + 1);
275
100
    }
276
50
    FIX_PREALLOC_SIZE(list);
277
50
    if (PyList_Reverse(list) < 0)
278
0
        goto onError;
279
50
    return list;
280
281
0
  onError:
282
0
    Py_DECREF(list);
283
    return NULL;
284
50
}
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
50
{
292
50
    Py_ssize_t j, pos, count=0;
293
50
    PyObject *list, *sub;
294
295
50
    if (sep_len == 0) {
296
0
        PyErr_SetString(PyExc_ValueError, "empty separator");
297
0
        return NULL;
298
0
    }
299
50
    else if (sep_len == 1)
300
50
        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
50
{
292
50
    Py_ssize_t j, pos, count=0;
293
50
    PyObject *list, *sub;
294
295
50
    if (sep_len == 0) {
296
0
        PyErr_SetString(PyExc_ValueError, "empty separator");
297
0
        return NULL;
298
0
    }
299
50
    else if (sep_len == 1)
300
50
        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.9k
{
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.9k
    Py_ssize_t i;
349
13.9k
    Py_ssize_t j;
350
13.9k
    PyObject *list = PyList_New(0);
351
13.9k
    PyObject *sub;
352
353
13.9k
    if (list == NULL)
354
0
        return NULL;
355
356
32.6M
    for (i = j = 0; i < str_len; ) {
357
32.6M
        Py_ssize_t eol;
358
359
        /* Find a line and append it */
360
276M
        while (i < str_len && !STRINGLIB_ISLINEBREAK(str[i]))
361
244M
            i++;
362
363
        /* Skip the line break reading CRLF as one line break */
364
32.6M
        eol = i;
365
32.6M
        if (i < str_len) {
366
32.6M
            if (str[i] == '\r' && i + 1 < str_len && str[i+1] == '\n')
367
90.7k
                i += 2;
368
32.5M
            else
369
32.5M
                i++;
370
32.6M
            if (keepends)
371
0
                eol = i;
372
32.6M
        }
373
#if !STRINGLIB_MUTABLE
374
32.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
5.47k
            if (PyList_Append(list, str_obj))
377
0
                goto onError;
378
5.47k
            break;
379
5.47k
        }
380
32.6M
#endif
381
65.2M
        SPLIT_APPEND(str, j, eol);
382
32.6M
        j = i;
383
32.6M
    }
384
13.9k
    return list;
385
386
0
  onError:
387
0
    Py_DECREF(list);
388
    return NULL;
389
13.9k
}
Unexecuted instantiation: bytesobject.c:stringlib_splitlines
unicodeobject.c:asciilib_splitlines
Line
Count
Source
339
2.76k
{
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.76k
    Py_ssize_t i;
349
2.76k
    Py_ssize_t j;
350
2.76k
    PyObject *list = PyList_New(0);
351
2.76k
    PyObject *sub;
352
353
2.76k
    if (list == NULL)
354
0
        return NULL;
355
356
7.85M
    for (i = j = 0; i < str_len; ) {
357
7.85M
        Py_ssize_t eol;
358
359
        /* Find a line and append it */
360
49.1M
        while (i < str_len && !STRINGLIB_ISLINEBREAK(str[i]))
361
41.3M
            i++;
362
363
        /* Skip the line break reading CRLF as one line break */
364
7.85M
        eol = i;
365
7.85M
        if (i < str_len) {
366
7.85M
            if (str[i] == '\r' && i + 1 < str_len && str[i+1] == '\n')
367
4.52k
                i += 2;
368
7.84M
            else
369
7.84M
                i++;
370
7.85M
            if (keepends)
371
0
                eol = i;
372
7.85M
        }
373
7.85M
#if !STRINGLIB_MUTABLE
374
7.85M
        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.01k
            if (PyList_Append(list, str_obj))
377
0
                goto onError;
378
1.01k
            break;
379
1.01k
        }
380
7.85M
#endif
381
15.7M
        SPLIT_APPEND(str, j, eol);
382
7.85M
        j = i;
383
7.85M
    }
384
2.76k
    return list;
385
386
0
  onError:
387
0
    Py_DECREF(list);
388
    return NULL;
389
2.76k
}
unicodeobject.c:ucs1lib_splitlines
Line
Count
Source
339
853
{
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
853
    Py_ssize_t i;
349
853
    Py_ssize_t j;
350
853
    PyObject *list = PyList_New(0);
351
853
    PyObject *sub;
352
353
853
    if (list == NULL)
354
0
        return NULL;
355
356
931k
    for (i = j = 0; i < str_len; ) {
357
930k
        Py_ssize_t eol;
358
359
        /* Find a line and append it */
360
10.0M
        while (i < str_len && !STRINGLIB_ISLINEBREAK(str[i]))
361
9.08M
            i++;
362
363
        /* Skip the line break reading CRLF as one line break */
364
930k
        eol = i;
365
930k
        if (i < str_len) {
366
929k
            if (str[i] == '\r' && i + 1 < str_len && str[i+1] == '\n')
367
1.98k
                i += 2;
368
927k
            else
369
927k
                i++;
370
929k
            if (keepends)
371
0
                eol = i;
372
929k
        }
373
930k
#if !STRINGLIB_MUTABLE
374
930k
        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
209
            if (PyList_Append(list, str_obj))
377
0
                goto onError;
378
209
            break;
379
209
        }
380
930k
#endif
381
1.86M
        SPLIT_APPEND(str, j, eol);
382
930k
        j = i;
383
930k
    }
384
853
    return list;
385
386
0
  onError:
387
0
    Py_DECREF(list);
388
    return NULL;
389
853
}
unicodeobject.c:ucs2lib_splitlines
Line
Count
Source
339
7.30k
{
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.30k
    Py_ssize_t i;
349
7.30k
    Py_ssize_t j;
350
7.30k
    PyObject *list = PyList_New(0);
351
7.30k
    PyObject *sub;
352
353
7.30k
    if (list == NULL)
354
0
        return NULL;
355
356
11.3M
    for (i = j = 0; i < str_len; ) {
357
11.3M
        Py_ssize_t eol;
358
359
        /* Find a line and append it */
360
97.0M
        while (i < str_len && !STRINGLIB_ISLINEBREAK(str[i]))
361
85.6M
            i++;
362
363
        /* Skip the line break reading CRLF as one line break */
364
11.3M
        eol = i;
365
11.3M
        if (i < str_len) {
366
11.3M
            if (str[i] == '\r' && i + 1 < str_len && str[i+1] == '\n')
367
20.1k
                i += 2;
368
11.3M
            else
369
11.3M
                i++;
370
11.3M
            if (keepends)
371
0
                eol = i;
372
11.3M
        }
373
11.3M
#if !STRINGLIB_MUTABLE
374
11.3M
        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.07k
            if (PyList_Append(list, str_obj))
377
0
                goto onError;
378
3.07k
            break;
379
3.07k
        }
380
11.3M
#endif
381
22.7M
        SPLIT_APPEND(str, j, eol);
382
11.3M
        j = i;
383
11.3M
    }
384
7.30k
    return list;
385
386
0
  onError:
387
0
    Py_DECREF(list);
388
    return NULL;
389
7.30k
}
unicodeobject.c:ucs4lib_splitlines
Line
Count
Source
339
3.04k
{
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.04k
    Py_ssize_t i;
349
3.04k
    Py_ssize_t j;
350
3.04k
    PyObject *list = PyList_New(0);
351
3.04k
    PyObject *sub;
352
353
3.04k
    if (list == NULL)
354
0
        return NULL;
355
356
12.4M
    for (i = j = 0; i < str_len; ) {
357
12.4M
        Py_ssize_t eol;
358
359
        /* Find a line and append it */
360
120M
        while (i < str_len && !STRINGLIB_ISLINEBREAK(str[i]))
361
108M
            i++;
362
363
        /* Skip the line break reading CRLF as one line break */
364
12.4M
        eol = i;
365
12.4M
        if (i < str_len) {
366
12.4M
            if (str[i] == '\r' && i + 1 < str_len && str[i+1] == '\n')
367
64.0k
                i += 2;
368
12.4M
            else
369
12.4M
                i++;
370
12.4M
            if (keepends)
371
0
                eol = i;
372
12.4M
        }
373
12.4M
#if !STRINGLIB_MUTABLE
374
12.4M
        if (j == 0 && eol == str_len && STRINGLIB_CHECK_EXACT(str_obj)) {
375
            /* No linebreak in str_obj, so just use it as list[0] */
376
1.17k
            if (PyList_Append(list, str_obj))
377
0
                goto onError;
378
1.17k
            break;
379
1.17k
        }
380
12.4M
#endif
381
24.9M
        SPLIT_APPEND(str, j, eol);
382
12.4M
        j = i;
383
12.4M
    }
384
3.04k
    return list;
385
386
0
  onError:
387
0
    Py_DECREF(list);
388
    return NULL;
389
3.04k
}
Unexecuted instantiation: bytearrayobject.c:stringlib_splitlines
390