Coverage Report

Created: 2025-10-10 06:33

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
108M
#define MAX_PREALLOC 12
15
16
/* 5 splits gives 6 elements */
17
#define PREALLOC_SIZE(maxsplit) \
18
20.3M
    (maxsplit >= MAX_PREALLOC ? MAX_PREALLOC : maxsplit+1)
19
20
#define SPLIT_APPEND(data, left, right)         \
21
30.9M
    sub = STRINGLIB_NEW((data) + (left),        \
22
30.9M
                        (right) - (left));      \
23
30.9M
    if (sub == NULL)                            \
24
30.9M
        goto onError;                           \
25
30.9M
    if (PyList_Append(list, sub)) {             \
26
0
        Py_DECREF(sub);                         \
27
0
        goto onError;                           \
28
0
    }                                           \
29
30.9M
    else                                        \
30
30.9M
        Py_DECREF(sub);
31
32
64.6M
#define SPLIT_ADD(data, left, right) {          \
33
64.6M
    sub = STRINGLIB_NEW((data) + (left),        \
34
64.6M
                        (right) - (left));      \
35
64.6M
    if (sub == NULL)                            \
36
64.6M
        goto onError;                           \
37
64.6M
    if (count < MAX_PREALLOC) {                 \
38
29.2M
        PyList_SET_ITEM(list, count, sub);      \
39
35.3M
    } else {                                    \
40
35.3M
        if (PyList_Append(list, sub)) {         \
41
0
            Py_DECREF(sub);                     \
42
0
            goto onError;                       \
43
0
        }                                       \
44
35.3M
        else                                    \
45
35.3M
            Py_DECREF(sub);                     \
46
35.3M
    }                                           \
47
64.6M
    count++; }
48
49
50
/* Always force the list to the expected size. */
51
20.3M
#define FIX_PREALLOC_SIZE(list) Py_SET_SIZE(list, count)
52
53
Py_LOCAL_INLINE(PyObject *)
54
STRINGLIB(split_whitespace)(PyObject* str_obj,
55
                           const STRINGLIB_CHAR* str, Py_ssize_t str_len,
56
                           Py_ssize_t maxcount)
57
180k
{
58
180k
    Py_ssize_t i, j, count=0;
59
180k
    PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
60
180k
    PyObject *sub;
61
62
180k
    if (list == NULL)
63
0
        return NULL;
64
65
180k
    i = j = 0;
66
3.05M
    while (maxcount-- > 0) {
67
5.91M
        while (i < str_len && STRINGLIB_ISSPACE(str[i]))
68
2.89M
            i++;
69
3.02M
        if (i == str_len) break;
70
2.91M
        j = i; i++;
71
155M
        while (i < str_len && !STRINGLIB_ISSPACE(str[i]))
72
152M
            i++;
73
#if !STRINGLIB_MUTABLE
74
2.91M
        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
38.5k
            Py_INCREF(str_obj);
77
38.5k
            PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
78
38.5k
            count++;
79
38.5k
            break;
80
38.5k
        }
81
2.87M
#endif
82
8.63M
        SPLIT_ADD(str, j, i);
83
8.63M
    }
84
85
180k
    if (i < str_len) {
86
        /* Only occurs when maxcount was reached */
87
        /* Skip any remaining whitespace and copy to end of string */
88
59.3k
        while (i < str_len && STRINGLIB_ISSPACE(str[i]))
89
32.1k
            i++;
90
27.2k
        if (i != str_len)
91
27.2k
            SPLIT_ADD(str, i, str_len);
92
27.2k
    }
93
180k
    FIX_PREALLOC_SIZE(list);
94
180k
    return list;
95
96
0
  onError:
97
0
    Py_DECREF(list);
98
0
    return NULL;
99
180k
}
Unexecuted instantiation: bytesobject.c:stringlib_split_whitespace
unicodeobject.c:asciilib_split_whitespace
Line
Count
Source
57
86.3k
{
58
86.3k
    Py_ssize_t i, j, count=0;
59
86.3k
    PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
60
86.3k
    PyObject *sub;
61
62
86.3k
    if (list == NULL)
63
0
        return NULL;
64
65
86.3k
    i = j = 0;
66
921k
    while (maxcount-- > 0) {
67
1.68M
        while (i < str_len && STRINGLIB_ISSPACE(str[i]))
68
779k
            i++;
69
904k
        if (i == str_len) break;
70
853k
        j = i; i++;
71
49.6M
        while (i < str_len && !STRINGLIB_ISSPACE(str[i]))
72
48.7M
            i++;
73
853k
#if !STRINGLIB_MUTABLE
74
853k
        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
18.3k
            Py_INCREF(str_obj);
77
18.3k
            PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
78
18.3k
            count++;
79
18.3k
            break;
80
18.3k
        }
81
835k
#endif
82
2.50M
        SPLIT_ADD(str, j, i);
83
2.50M
    }
84
85
86.3k
    if (i < str_len) {
86
        /* Only occurs when maxcount was reached */
87
        /* Skip any remaining whitespace and copy to end of string */
88
27.5k
        while (i < str_len && STRINGLIB_ISSPACE(str[i]))
89
14.2k
            i++;
90
13.2k
        if (i != str_len)
91
13.2k
            SPLIT_ADD(str, i, str_len);
92
13.2k
    }
93
86.3k
    FIX_PREALLOC_SIZE(list);
94
86.3k
    return list;
95
96
0
  onError:
97
0
    Py_DECREF(list);
98
    return NULL;
99
86.3k
}
unicodeobject.c:ucs1lib_split_whitespace
Line
Count
Source
57
33.2k
{
58
33.2k
    Py_ssize_t i, j, count=0;
59
33.2k
    PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
60
33.2k
    PyObject *sub;
61
62
33.2k
    if (list == NULL)
63
0
        return NULL;
64
65
33.2k
    i = j = 0;
66
1.13M
    while (maxcount-- > 0) {
67
2.20M
        while (i < str_len && STRINGLIB_ISSPACE(str[i]))
68
1.08M
            i++;
69
1.12M
        if (i == str_len) break;
70
1.10M
        j = i; i++;
71
46.6M
        while (i < str_len && !STRINGLIB_ISSPACE(str[i]))
72
45.5M
            i++;
73
1.10M
#if !STRINGLIB_MUTABLE
74
1.10M
        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
4.08k
            Py_INCREF(str_obj);
77
4.08k
            PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
78
4.08k
            count++;
79
4.08k
            break;
80
4.08k
        }
81
1.10M
#endif
82
3.31M
        SPLIT_ADD(str, j, i);
83
3.31M
    }
84
85
33.2k
    if (i < str_len) {
86
        /* Only occurs when maxcount was reached */
87
        /* Skip any remaining whitespace and copy to end of string */
88
30.1k
        while (i < str_len && STRINGLIB_ISSPACE(str[i]))
89
17.0k
            i++;
90
13.1k
        if (i != str_len)
91
13.1k
            SPLIT_ADD(str, i, str_len);
92
13.1k
    }
93
33.2k
    FIX_PREALLOC_SIZE(list);
94
33.2k
    return list;
95
96
0
  onError:
97
0
    Py_DECREF(list);
98
    return NULL;
99
33.2k
}
unicodeobject.c:ucs2lib_split_whitespace
Line
Count
Source
57
50.3k
{
58
50.3k
    Py_ssize_t i, j, count=0;
59
50.3k
    PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
60
50.3k
    PyObject *sub;
61
62
50.3k
    if (list == NULL)
63
0
        return NULL;
64
65
50.3k
    i = j = 0;
66
889k
    while (maxcount-- > 0) {
67
1.81M
        while (i < str_len && STRINGLIB_ISSPACE(str[i]))
68
925k
            i++;
69
888k
        if (i == str_len) break;
70
852k
        j = i; i++;
71
47.9M
        while (i < str_len && !STRINGLIB_ISSPACE(str[i]))
72
47.0M
            i++;
73
852k
#if !STRINGLIB_MUTABLE
74
852k
        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.2k
            Py_INCREF(str_obj);
77
13.2k
            PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
78
13.2k
            count++;
79
13.2k
            break;
80
13.2k
        }
81
839k
#endif
82
2.51M
        SPLIT_ADD(str, j, i);
83
2.51M
    }
84
85
50.3k
    if (i < str_len) {
86
        /* Only occurs when maxcount was reached */
87
        /* Skip any remaining whitespace and copy to end of string */
88
1.68k
        while (i < str_len && STRINGLIB_ISSPACE(str[i]))
89
844
            i++;
90
844
        if (i != str_len)
91
844
            SPLIT_ADD(str, i, str_len);
92
844
    }
93
50.3k
    FIX_PREALLOC_SIZE(list);
94
50.3k
    return list;
95
96
0
  onError:
97
0
    Py_DECREF(list);
98
    return NULL;
99
50.3k
}
unicodeobject.c:ucs4lib_split_whitespace
Line
Count
Source
57
10.5k
{
58
10.5k
    Py_ssize_t i, j, count=0;
59
10.5k
    PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
60
10.5k
    PyObject *sub;
61
62
10.5k
    if (list == NULL)
63
0
        return NULL;
64
65
10.5k
    i = j = 0;
66
110k
    while (maxcount-- > 0) {
67
213k
        while (i < str_len && STRINGLIB_ISSPACE(str[i]))
68
103k
            i++;
69
110k
        if (i == str_len) break;
70
103k
        j = i; i++;
71
11.1M
        while (i < str_len && !STRINGLIB_ISSPACE(str[i]))
72
11.0M
            i++;
73
103k
#if !STRINGLIB_MUTABLE
74
103k
        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.93k
            Py_INCREF(str_obj);
77
2.93k
            PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
78
2.93k
            count++;
79
2.93k
            break;
80
2.93k
        }
81
100k
#endif
82
300k
        SPLIT_ADD(str, j, i);
83
300k
    }
84
85
10.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
10.5k
    FIX_PREALLOC_SIZE(list);
94
10.5k
    return list;
95
96
0
  onError:
97
0
    Py_DECREF(list);
98
    return NULL;
99
10.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
19.9M
{
107
19.9M
    Py_ssize_t i, j, count=0;
108
19.9M
    PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
109
19.9M
    PyObject *sub;
110
111
19.9M
    if (list == NULL)
112
0
        return NULL;
113
114
19.9M
    i = j = 0;
115
85.7M
    while ((j < str_len) && (maxcount-- > 0)) {
116
408M
        for(; j < str_len; j++) {
117
            /* I found that using memchr makes no difference */
118
390M
            if (str[j] == ch) {
119
47.8M
                SPLIT_ADD(str, i, j);
120
47.8M
                i = j = j + 1;
121
47.8M
                break;
122
47.8M
            }
123
390M
        }
124
65.7M
    }
125
#if !STRINGLIB_MUTABLE
126
19.9M
    if (count == 0 && STRINGLIB_CHECK_EXACT(str_obj)) {
127
        /* ch not in str_obj, so just use str_obj as list[0] */
128
6.44M
        Py_INCREF(str_obj);
129
6.44M
        PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
130
6.44M
        count++;
131
6.44M
    } else
132
13.5M
#endif
133
13.5M
    if (i <= str_len) {
134
27.0M
        SPLIT_ADD(str, i, str_len);
135
27.0M
    }
136
19.9M
    FIX_PREALLOC_SIZE(list);
137
19.9M
    return list;
138
139
0
  onError:
140
0
    Py_DECREF(list);
141
0
    return NULL;
142
0
}
bytesobject.c:stringlib_split_char
Line
Count
Source
106
3.22M
{
107
3.22M
    Py_ssize_t i, j, count=0;
108
3.22M
    PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
109
3.22M
    PyObject *sub;
110
111
3.22M
    if (list == NULL)
112
0
        return NULL;
113
114
3.22M
    i = j = 0;
115
11.0M
    while ((j < str_len) && (maxcount-- > 0)) {
116
56.5M
        for(; j < str_len; j++) {
117
            /* I found that using memchr makes no difference */
118
53.8M
            if (str[j] == ch) {
119
5.11M
                SPLIT_ADD(str, i, j);
120
5.11M
                i = j = j + 1;
121
5.11M
                break;
122
5.11M
            }
123
53.8M
        }
124
7.80M
    }
125
3.22M
#if !STRINGLIB_MUTABLE
126
3.22M
    if (count == 0 && STRINGLIB_CHECK_EXACT(str_obj)) {
127
        /* ch not in str_obj, so just use str_obj as list[0] */
128
2.60M
        Py_INCREF(str_obj);
129
2.60M
        PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
130
2.60M
        count++;
131
2.60M
    } else
132
624k
#endif
133
624k
    if (i <= str_len) {
134
1.24M
        SPLIT_ADD(str, i, str_len);
135
1.24M
    }
136
3.22M
    FIX_PREALLOC_SIZE(list);
137
3.22M
    return list;
138
139
0
  onError:
140
0
    Py_DECREF(list);
141
    return NULL;
142
3.22M
}
unicodeobject.c:asciilib_split_char
Line
Count
Source
106
15.6M
{
107
15.6M
    Py_ssize_t i, j, count=0;
108
15.6M
    PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
109
15.6M
    PyObject *sub;
110
111
15.6M
    if (list == NULL)
112
0
        return NULL;
113
114
15.6M
    i = j = 0;
115
52.1M
    while ((j < str_len) && (maxcount-- > 0)) {
116
204M
        for(; j < str_len; j++) {
117
            /* I found that using memchr makes no difference */
118
189M
            if (str[j] == ch) {
119
21.5M
                SPLIT_ADD(str, i, j);
120
21.5M
                i = j = j + 1;
121
21.5M
                break;
122
21.5M
            }
123
189M
        }
124
36.5M
    }
125
15.6M
#if !STRINGLIB_MUTABLE
126
15.6M
    if (count == 0 && STRINGLIB_CHECK_EXACT(str_obj)) {
127
        /* ch not in str_obj, so just use str_obj as list[0] */
128
3.69M
        Py_INCREF(str_obj);
129
3.69M
        PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
130
3.69M
        count++;
131
3.69M
    } else
132
11.9M
#endif
133
11.9M
    if (i <= str_len) {
134
23.8M
        SPLIT_ADD(str, i, str_len);
135
23.8M
    }
136
15.6M
    FIX_PREALLOC_SIZE(list);
137
15.6M
    return list;
138
139
0
  onError:
140
0
    Py_DECREF(list);
141
    return NULL;
142
15.6M
}
unicodeobject.c:ucs1lib_split_char
Line
Count
Source
106
1.04M
{
107
1.04M
    Py_ssize_t i, j, count=0;
108
1.04M
    PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
109
1.04M
    PyObject *sub;
110
111
1.04M
    if (list == NULL)
112
0
        return NULL;
113
114
1.04M
    i = j = 0;
115
16.0M
    while ((j < str_len) && (maxcount-- > 0)) {
116
68.9M
        for(; j < str_len; j++) {
117
            /* I found that using memchr makes no difference */
118
68.8M
            if (str[j] == ch) {
119
14.8M
                SPLIT_ADD(str, i, j);
120
14.8M
                i = j = j + 1;
121
14.8M
                break;
122
14.8M
            }
123
68.8M
        }
124
15.0M
    }
125
1.04M
#if !STRINGLIB_MUTABLE
126
1.04M
    if (count == 0 && STRINGLIB_CHECK_EXACT(str_obj)) {
127
        /* ch not in str_obj, so just use str_obj as list[0] */
128
138k
        Py_INCREF(str_obj);
129
138k
        PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
130
138k
        count++;
131
138k
    } else
132
906k
#endif
133
906k
    if (i <= str_len) {
134
1.81M
        SPLIT_ADD(str, i, str_len);
135
1.81M
    }
136
1.04M
    FIX_PREALLOC_SIZE(list);
137
1.04M
    return list;
138
139
0
  onError:
140
0
    Py_DECREF(list);
141
    return NULL;
142
1.04M
}
unicodeobject.c:ucs2lib_split_char
Line
Count
Source
106
90.6k
{
107
90.6k
    Py_ssize_t i, j, count=0;
108
90.6k
    PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
109
90.6k
    PyObject *sub;
110
111
90.6k
    if (list == NULL)
112
0
        return NULL;
113
114
90.6k
    i = j = 0;
115
6.07M
    while ((j < str_len) && (maxcount-- > 0)) {
116
54.5M
        for(; j < str_len; j++) {
117
            /* I found that using memchr makes no difference */
118
54.4M
            if (str[j] == ch) {
119
5.92M
                SPLIT_ADD(str, i, j);
120
5.92M
                i = j = j + 1;
121
5.92M
                break;
122
5.92M
            }
123
54.4M
        }
124
5.98M
    }
125
90.6k
#if !STRINGLIB_MUTABLE
126
90.6k
    if (count == 0 && STRINGLIB_CHECK_EXACT(str_obj)) {
127
        /* ch not in str_obj, so just use str_obj as list[0] */
128
7.00k
        Py_INCREF(str_obj);
129
7.00k
        PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
130
7.00k
        count++;
131
7.00k
    } else
132
83.6k
#endif
133
83.6k
    if (i <= str_len) {
134
167k
        SPLIT_ADD(str, i, str_len);
135
167k
    }
136
90.6k
    FIX_PREALLOC_SIZE(list);
137
90.6k
    return list;
138
139
0
  onError:
140
0
    Py_DECREF(list);
141
    return NULL;
142
90.6k
}
unicodeobject.c:ucs4lib_split_char
Line
Count
Source
106
14.5k
{
107
14.5k
    Py_ssize_t i, j, count=0;
108
14.5k
    PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
109
14.5k
    PyObject *sub;
110
111
14.5k
    if (list == NULL)
112
0
        return NULL;
113
114
14.5k
    i = j = 0;
115
388k
    while ((j < str_len) && (maxcount-- > 0)) {
116
23.5M
        for(; j < str_len; j++) {
117
            /* I found that using memchr makes no difference */
118
23.5M
            if (str[j] == ch) {
119
362k
                SPLIT_ADD(str, i, j);
120
362k
                i = j = j + 1;
121
362k
                break;
122
362k
            }
123
23.5M
        }
124
373k
    }
125
14.5k
#if !STRINGLIB_MUTABLE
126
14.5k
    if (count == 0 && STRINGLIB_CHECK_EXACT(str_obj)) {
127
        /* ch not in str_obj, so just use str_obj as list[0] */
128
363
        Py_INCREF(str_obj);
129
363
        PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
130
363
        count++;
131
363
    } else
132
14.1k
#endif
133
14.1k
    if (i <= str_len) {
134
28.3k
        SPLIT_ADD(str, i, str_len);
135
28.3k
    }
136
14.5k
    FIX_PREALLOC_SIZE(list);
137
14.5k
    return list;
138
139
0
  onError:
140
0
    Py_DECREF(list);
141
    return NULL;
142
14.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
20.2M
{
150
20.2M
    Py_ssize_t i, j, pos, count=0;
151
20.2M
    PyObject *list, *sub;
152
153
20.2M
    if (sep_len == 0) {
154
0
        PyErr_SetString(PyExc_ValueError, "empty separator");
155
0
        return NULL;
156
0
    }
157
20.2M
    else if (sep_len == 1)
158
19.9M
        return STRINGLIB(split_char)(str_obj, str, str_len, sep[0], maxcount);
159
160
218k
    list = PyList_New(PREALLOC_SIZE(maxcount));
161
218k
    if (list == NULL)
162
0
        return NULL;
163
164
218k
    i = j = 0;
165
395k
    while (maxcount-- > 0) {
166
218k
        pos = FASTSEARCH(str+i, str_len-i, sep, sep_len, -1, FAST_SEARCH);
167
218k
        if (pos < 0)
168
40.9k
            break;
169
177k
        j = i + pos;
170
354k
        SPLIT_ADD(str, i, j);
171
354k
        i = j + sep_len;
172
354k
    }
173
#if !STRINGLIB_MUTABLE
174
218k
    if (count == 0 && STRINGLIB_CHECK_EXACT(str_obj)) {
175
        /* No match in str_obj, so just use it as list[0] */
176
40.9k
        Py_INCREF(str_obj);
177
40.9k
        PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
178
40.9k
        count++;
179
40.9k
    } else
180
177k
#endif
181
177k
    {
182
354k
        SPLIT_ADD(str, i, str_len);
183
354k
    }
184
218k
    FIX_PREALLOC_SIZE(list);
185
218k
    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.22M
{
150
3.22M
    Py_ssize_t i, j, pos, count=0;
151
3.22M
    PyObject *list, *sub;
152
153
3.22M
    if (sep_len == 0) {
154
0
        PyErr_SetString(PyExc_ValueError, "empty separator");
155
0
        return NULL;
156
0
    }
157
3.22M
    else if (sep_len == 1)
158
3.22M
        return STRINGLIB(split_char)(str_obj, str, str_len, sep[0], maxcount);
159
160
0
    list = PyList_New(PREALLOC_SIZE(maxcount));
161
0
    if (list == NULL)
162
0
        return NULL;
163
164
0
    i = j = 0;
165
0
    while (maxcount-- > 0) {
166
0
        pos = FASTSEARCH(str+i, str_len-i, sep, sep_len, -1, FAST_SEARCH);
167
0
        if (pos < 0)
168
0
            break;
169
0
        j = i + pos;
170
0
        SPLIT_ADD(str, i, j);
171
0
        i = j + sep_len;
172
0
    }
173
0
#if !STRINGLIB_MUTABLE
174
0
    if (count == 0 && STRINGLIB_CHECK_EXACT(str_obj)) {
175
        /* No match in str_obj, so just use it as list[0] */
176
0
        Py_INCREF(str_obj);
177
0
        PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
178
0
        count++;
179
0
    } else
180
0
#endif
181
0
    {
182
0
        SPLIT_ADD(str, i, str_len);
183
0
    }
184
0
    FIX_PREALLOC_SIZE(list);
185
0
    return list;
186
187
0
  onError:
188
0
    Py_DECREF(list);
189
    return NULL;
190
0
}
unicodeobject.c:asciilib_split
Line
Count
Source
149
15.6M
{
150
15.6M
    Py_ssize_t i, j, pos, count=0;
151
15.6M
    PyObject *list, *sub;
152
153
15.6M
    if (sep_len == 0) {
154
0
        PyErr_SetString(PyExc_ValueError, "empty separator");
155
0
        return NULL;
156
0
    }
157
15.6M
    else if (sep_len == 1)
158
15.6M
        return STRINGLIB(split_char)(str_obj, str, str_len, sep[0], maxcount);
159
160
84.4k
    list = PyList_New(PREALLOC_SIZE(maxcount));
161
84.4k
    if (list == NULL)
162
0
        return NULL;
163
164
84.4k
    i = j = 0;
165
141k
    while (maxcount-- > 0) {
166
84.4k
        pos = FASTSEARCH(str+i, str_len-i, sep, sep_len, -1, FAST_SEARCH);
167
84.4k
        if (pos < 0)
168
27.2k
            break;
169
57.1k
        j = i + pos;
170
114k
        SPLIT_ADD(str, i, j);
171
114k
        i = j + sep_len;
172
114k
    }
173
84.4k
#if !STRINGLIB_MUTABLE
174
84.4k
    if (count == 0 && STRINGLIB_CHECK_EXACT(str_obj)) {
175
        /* No match in str_obj, so just use it as list[0] */
176
27.2k
        Py_INCREF(str_obj);
177
27.2k
        PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
178
27.2k
        count++;
179
27.2k
    } else
180
57.1k
#endif
181
57.1k
    {
182
114k
        SPLIT_ADD(str, i, str_len);
183
114k
    }
184
84.4k
    FIX_PREALLOC_SIZE(list);
185
84.4k
    return list;
186
187
0
  onError:
188
0
    Py_DECREF(list);
189
    return NULL;
190
84.4k
}
unicodeobject.c:ucs1lib_split
Line
Count
Source
149
1.06M
{
150
1.06M
    Py_ssize_t i, j, pos, count=0;
151
1.06M
    PyObject *list, *sub;
152
153
1.06M
    if (sep_len == 0) {
154
0
        PyErr_SetString(PyExc_ValueError, "empty separator");
155
0
        return NULL;
156
0
    }
157
1.06M
    else if (sep_len == 1)
158
1.04M
        return STRINGLIB(split_char)(str_obj, str, str_len, sep[0], maxcount);
159
160
22.2k
    list = PyList_New(PREALLOC_SIZE(maxcount));
161
22.2k
    if (list == NULL)
162
0
        return NULL;
163
164
22.2k
    i = j = 0;
165
43.0k
    while (maxcount-- > 0) {
166
22.2k
        pos = FASTSEARCH(str+i, str_len-i, sep, sep_len, -1, FAST_SEARCH);
167
22.2k
        if (pos < 0)
168
1.48k
            break;
169
20.7k
        j = i + pos;
170
41.5k
        SPLIT_ADD(str, i, j);
171
41.5k
        i = j + sep_len;
172
41.5k
    }
173
22.2k
#if !STRINGLIB_MUTABLE
174
22.2k
    if (count == 0 && STRINGLIB_CHECK_EXACT(str_obj)) {
175
        /* No match in str_obj, so just use it as list[0] */
176
1.48k
        Py_INCREF(str_obj);
177
1.48k
        PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
178
1.48k
        count++;
179
1.48k
    } else
180
20.7k
#endif
181
20.7k
    {
182
41.5k
        SPLIT_ADD(str, i, str_len);
183
41.5k
    }
184
22.2k
    FIX_PREALLOC_SIZE(list);
185
22.2k
    return list;
186
187
0
  onError:
188
0
    Py_DECREF(list);
189
    return NULL;
190
22.2k
}
unicodeobject.c:ucs2lib_split
Line
Count
Source
149
181k
{
150
181k
    Py_ssize_t i, j, pos, count=0;
151
181k
    PyObject *list, *sub;
152
153
181k
    if (sep_len == 0) {
154
0
        PyErr_SetString(PyExc_ValueError, "empty separator");
155
0
        return NULL;
156
0
    }
157
181k
    else if (sep_len == 1)
158
90.6k
        return STRINGLIB(split_char)(str_obj, str, str_len, sep[0], maxcount);
159
160
91.0k
    list = PyList_New(PREALLOC_SIZE(maxcount));
161
91.0k
    if (list == NULL)
162
0
        return NULL;
163
164
91.0k
    i = j = 0;
165
173k
    while (maxcount-- > 0) {
166
91.0k
        pos = FASTSEARCH(str+i, str_len-i, sep, sep_len, -1, FAST_SEARCH);
167
91.0k
        if (pos < 0)
168
8.94k
            break;
169
82.1k
        j = i + pos;
170
164k
        SPLIT_ADD(str, i, j);
171
164k
        i = j + sep_len;
172
164k
    }
173
91.0k
#if !STRINGLIB_MUTABLE
174
91.0k
    if (count == 0 && STRINGLIB_CHECK_EXACT(str_obj)) {
175
        /* No match in str_obj, so just use it as list[0] */
176
8.94k
        Py_INCREF(str_obj);
177
8.94k
        PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
178
8.94k
        count++;
179
8.94k
    } else
180
82.1k
#endif
181
82.1k
    {
182
164k
        SPLIT_ADD(str, i, str_len);
183
164k
    }
184
91.0k
    FIX_PREALLOC_SIZE(list);
185
91.0k
    return list;
186
187
0
  onError:
188
0
    Py_DECREF(list);
189
    return NULL;
190
91.0k
}
unicodeobject.c:ucs4lib_split
Line
Count
Source
149
35.0k
{
150
35.0k
    Py_ssize_t i, j, pos, count=0;
151
35.0k
    PyObject *list, *sub;
152
153
35.0k
    if (sep_len == 0) {
154
0
        PyErr_SetString(PyExc_ValueError, "empty separator");
155
0
        return NULL;
156
0
    }
157
35.0k
    else if (sep_len == 1)
158
14.5k
        return STRINGLIB(split_char)(str_obj, str, str_len, sep[0], maxcount);
159
160
20.5k
    list = PyList_New(PREALLOC_SIZE(maxcount));
161
20.5k
    if (list == NULL)
162
0
        return NULL;
163
164
20.5k
    i = j = 0;
165
37.7k
    while (maxcount-- > 0) {
166
20.5k
        pos = FASTSEARCH(str+i, str_len-i, sep, sep_len, -1, FAST_SEARCH);
167
20.5k
        if (pos < 0)
168
3.26k
            break;
169
17.2k
        j = i + pos;
170
34.5k
        SPLIT_ADD(str, i, j);
171
34.5k
        i = j + sep_len;
172
34.5k
    }
173
20.5k
#if !STRINGLIB_MUTABLE
174
20.5k
    if (count == 0 && STRINGLIB_CHECK_EXACT(str_obj)) {
175
        /* No match in str_obj, so just use it as list[0] */
176
3.26k
        Py_INCREF(str_obj);
177
3.26k
        PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
178
3.26k
        count++;
179
3.26k
    } else
180
17.2k
#endif
181
17.2k
    {
182
34.5k
        SPLIT_ADD(str, i, str_len);
183
34.5k
    }
184
20.5k
    FIX_PREALLOC_SIZE(list);
185
20.5k
    return list;
186
187
0
  onError:
188
0
    Py_DECREF(list);
189
    return NULL;
190
20.5k
}
Unexecuted instantiation: bytearrayobject.c:stringlib_split
191
192
Py_LOCAL_INLINE(PyObject *)
193
STRINGLIB(rsplit_whitespace)(PyObject* str_obj,
194
                            const STRINGLIB_CHAR* str, Py_ssize_t str_len,
195
                            Py_ssize_t maxcount)
196
0
{
197
0
    Py_ssize_t i, j, count=0;
198
0
    PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
199
0
    PyObject *sub;
200
201
0
    if (list == NULL)
202
0
        return NULL;
203
204
0
    i = j = str_len - 1;
205
0
    while (maxcount-- > 0) {
206
0
        while (i >= 0 && STRINGLIB_ISSPACE(str[i]))
207
0
            i--;
208
0
        if (i < 0) break;
209
0
        j = i; i--;
210
0
        while (i >= 0 && !STRINGLIB_ISSPACE(str[i]))
211
0
            i--;
212
#if !STRINGLIB_MUTABLE
213
0
        if (j == str_len - 1 && i < 0 && STRINGLIB_CHECK_EXACT(str_obj)) {
214
            /* No whitespace in str_obj, so just use it as list[0] */
215
0
            Py_INCREF(str_obj);
216
0
            PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
217
0
            count++;
218
0
            break;
219
0
        }
220
0
#endif
221
0
        SPLIT_ADD(str, i + 1, j + 1);
222
0
    }
223
224
0
    if (i >= 0) {
225
        /* Only occurs when maxcount was reached */
226
        /* Skip any remaining whitespace and copy to beginning of string */
227
0
        while (i >= 0 && STRINGLIB_ISSPACE(str[i]))
228
0
            i--;
229
0
        if (i >= 0)
230
0
            SPLIT_ADD(str, 0, i + 1);
231
0
    }
232
0
    FIX_PREALLOC_SIZE(list);
233
0
    if (PyList_Reverse(list) < 0)
234
0
        goto onError;
235
0
    return list;
236
237
0
  onError:
238
0
    Py_DECREF(list);
239
0
    return NULL;
240
0
}
Unexecuted instantiation: bytesobject.c:stringlib_rsplit_whitespace
Unexecuted instantiation: unicodeobject.c:asciilib_rsplit_whitespace
Unexecuted instantiation: unicodeobject.c:ucs1lib_rsplit_whitespace
Unexecuted instantiation: unicodeobject.c:ucs2lib_rsplit_whitespace
Unexecuted instantiation: unicodeobject.c:ucs4lib_rsplit_whitespace
Unexecuted instantiation: bytearrayobject.c:stringlib_rsplit_whitespace
241
242
Py_LOCAL_INLINE(PyObject *)
243
STRINGLIB(rsplit_char)(PyObject* str_obj,
244
                      const STRINGLIB_CHAR* str, Py_ssize_t str_len,
245
                      const STRINGLIB_CHAR ch,
246
                      Py_ssize_t maxcount)
247
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.7k
{
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.7k
    Py_ssize_t i;
349
13.7k
    Py_ssize_t j;
350
13.7k
    PyObject *list = PyList_New(0);
351
13.7k
    PyObject *sub;
352
353
13.7k
    if (list == NULL)
354
0
        return NULL;
355
356
31.0M
    for (i = j = 0; i < str_len; ) {
357
31.0M
        Py_ssize_t eol;
358
359
        /* Find a line and append it */
360
243M
        while (i < str_len && !STRINGLIB_ISLINEBREAK(str[i]))
361
212M
            i++;
362
363
        /* Skip the line break reading CRLF as one line break */
364
31.0M
        eol = i;
365
31.0M
        if (i < str_len) {
366
30.9M
            if (str[i] == '\r' && i + 1 < str_len && str[i+1] == '\n')
367
60.6k
                i += 2;
368
30.9M
            else
369
30.9M
                i++;
370
30.9M
            if (keepends)
371
0
                eol = i;
372
30.9M
        }
373
#if !STRINGLIB_MUTABLE
374
31.0M
        if (j == 0 && eol == str_len && STRINGLIB_CHECK_EXACT(str_obj)) {
375
            /* No linebreak in str_obj, so just use it as list[0] */
376
5.38k
            if (PyList_Append(list, str_obj))
377
0
                goto onError;
378
5.38k
            break;
379
5.38k
        }
380
30.9M
#endif
381
61.9M
        SPLIT_APPEND(str, j, eol);
382
30.9M
        j = i;
383
30.9M
    }
384
13.7k
    return list;
385
386
0
  onError:
387
0
    Py_DECREF(list);
388
    return NULL;
389
13.7k
}
Unexecuted instantiation: bytesobject.c:stringlib_splitlines
unicodeobject.c:asciilib_splitlines
Line
Count
Source
339
2.68k
{
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.68k
    Py_ssize_t i;
349
2.68k
    Py_ssize_t j;
350
2.68k
    PyObject *list = PyList_New(0);
351
2.68k
    PyObject *sub;
352
353
2.68k
    if (list == NULL)
354
0
        return NULL;
355
356
6.33M
    for (i = j = 0; i < str_len; ) {
357
6.33M
        Py_ssize_t eol;
358
359
        /* Find a line and append it */
360
39.9M
        while (i < str_len && !STRINGLIB_ISLINEBREAK(str[i]))
361
33.5M
            i++;
362
363
        /* Skip the line break reading CRLF as one line break */
364
6.33M
        eol = i;
365
6.33M
        if (i < str_len) {
366
6.33M
            if (str[i] == '\r' && i + 1 < str_len && str[i+1] == '\n')
367
1.05k
                i += 2;
368
6.33M
            else
369
6.33M
                i++;
370
6.33M
            if (keepends)
371
0
                eol = i;
372
6.33M
        }
373
6.33M
#if !STRINGLIB_MUTABLE
374
6.33M
        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
963
            if (PyList_Append(list, str_obj))
377
0
                goto onError;
378
963
            break;
379
963
        }
380
6.33M
#endif
381
12.6M
        SPLIT_APPEND(str, j, eol);
382
6.33M
        j = i;
383
6.33M
    }
384
2.68k
    return list;
385
386
0
  onError:
387
0
    Py_DECREF(list);
388
    return NULL;
389
2.68k
}
unicodeobject.c:ucs1lib_splitlines
Line
Count
Source
339
852
{
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
852
    Py_ssize_t i;
349
852
    Py_ssize_t j;
350
852
    PyObject *list = PyList_New(0);
351
852
    PyObject *sub;
352
353
852
    if (list == NULL)
354
0
        return NULL;
355
356
2.23M
    for (i = j = 0; i < str_len; ) {
357
2.23M
        Py_ssize_t eol;
358
359
        /* Find a line and append it */
360
9.92M
        while (i < str_len && !STRINGLIB_ISLINEBREAK(str[i]))
361
7.69M
            i++;
362
363
        /* Skip the line break reading CRLF as one line break */
364
2.23M
        eol = i;
365
2.23M
        if (i < str_len) {
366
2.23M
            if (str[i] == '\r' && i + 1 < str_len && str[i+1] == '\n')
367
1.79k
                i += 2;
368
2.22M
            else
369
2.22M
                i++;
370
2.23M
            if (keepends)
371
0
                eol = i;
372
2.23M
        }
373
2.23M
#if !STRINGLIB_MUTABLE
374
2.23M
        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
214
            if (PyList_Append(list, str_obj))
377
0
                goto onError;
378
214
            break;
379
214
        }
380
2.23M
#endif
381
4.46M
        SPLIT_APPEND(str, j, eol);
382
2.23M
        j = i;
383
2.23M
    }
384
852
    return list;
385
386
0
  onError:
387
0
    Py_DECREF(list);
388
    return NULL;
389
852
}
unicodeobject.c:ucs2lib_splitlines
Line
Count
Source
339
7.22k
{
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.22k
    Py_ssize_t i;
349
7.22k
    Py_ssize_t j;
350
7.22k
    PyObject *list = PyList_New(0);
351
7.22k
    PyObject *sub;
352
353
7.22k
    if (list == NULL)
354
0
        return NULL;
355
356
11.5M
    for (i = j = 0; i < str_len; ) {
357
11.5M
        Py_ssize_t eol;
358
359
        /* Find a line and append it */
360
89.9M
        while (i < str_len && !STRINGLIB_ISLINEBREAK(str[i]))
361
78.3M
            i++;
362
363
        /* Skip the line break reading CRLF as one line break */
364
11.5M
        eol = i;
365
11.5M
        if (i < str_len) {
366
11.5M
            if (str[i] == '\r' && i + 1 < str_len && str[i+1] == '\n')
367
11.7k
                i += 2;
368
11.4M
            else
369
11.4M
                i++;
370
11.5M
            if (keepends)
371
0
                eol = i;
372
11.5M
        }
373
11.5M
#if !STRINGLIB_MUTABLE
374
11.5M
        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.03k
            if (PyList_Append(list, str_obj))
377
0
                goto onError;
378
3.03k
            break;
379
3.03k
        }
380
11.5M
#endif
381
23.0M
        SPLIT_APPEND(str, j, eol);
382
11.5M
        j = i;
383
11.5M
    }
384
7.22k
    return list;
385
386
0
  onError:
387
0
    Py_DECREF(list);
388
    return NULL;
389
7.22k
}
unicodeobject.c:ucs4lib_splitlines
Line
Count
Source
339
3.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
3.02k
    Py_ssize_t i;
349
3.02k
    Py_ssize_t j;
350
3.02k
    PyObject *list = PyList_New(0);
351
3.02k
    PyObject *sub;
352
353
3.02k
    if (list == NULL)
354
0
        return NULL;
355
356
10.9M
    for (i = j = 0; i < str_len; ) {
357
10.9M
        Py_ssize_t eol;
358
359
        /* Find a line and append it */
360
104M
        while (i < str_len && !STRINGLIB_ISLINEBREAK(str[i]))
361
93.0M
            i++;
362
363
        /* Skip the line break reading CRLF as one line break */
364
10.9M
        eol = i;
365
10.9M
        if (i < str_len) {
366
10.9M
            if (str[i] == '\r' && i + 1 < str_len && str[i+1] == '\n')
367
45.9k
                i += 2;
368
10.8M
            else
369
10.8M
                i++;
370
10.9M
            if (keepends)
371
0
                eol = i;
372
10.9M
        }
373
10.9M
#if !STRINGLIB_MUTABLE
374
10.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
1.17k
            if (PyList_Append(list, str_obj))
377
0
                goto onError;
378
1.17k
            break;
379
1.17k
        }
380
10.9M
#endif
381
21.8M
        SPLIT_APPEND(str, j, eol);
382
10.9M
        j = i;
383
10.9M
    }
384
3.02k
    return list;
385
386
0
  onError:
387
0
    Py_DECREF(list);
388
    return NULL;
389
3.02k
}
Unexecuted instantiation: bytearrayobject.c:stringlib_splitlines
390