Coverage Report

Created: 2026-04-12 06:54

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
107M
#define MAX_PREALLOC 12
15
16
/* 5 splits gives 6 elements */
17
#define PREALLOC_SIZE(maxsplit) \
18
23.9M
    (maxsplit >= MAX_PREALLOC ? MAX_PREALLOC : maxsplit+1)
19
20
#define SPLIT_APPEND(data, left, right)         \
21
34.0M
    sub = STRINGLIB_NEW((data) + (left),        \
22
34.0M
                        (right) - (left));      \
23
34.0M
    if (sub == NULL)                            \
24
34.0M
        goto onError;                           \
25
34.0M
    if (PyList_Append(list, sub)) {             \
26
0
        Py_DECREF(sub);                         \
27
0
        goto onError;                           \
28
0
    }                                           \
29
34.0M
    else                                        \
30
34.0M
        Py_DECREF(sub);
31
32
56.4M
#define SPLIT_ADD(data, left, right) {          \
33
56.4M
    sub = STRINGLIB_NEW((data) + (left),        \
34
56.4M
                        (right) - (left));      \
35
56.4M
    if (sub == NULL)                            \
36
56.4M
        goto onError;                           \
37
56.4M
    if (count < MAX_PREALLOC) {                 \
38
37.4M
        PyList_SET_ITEM(list, count, sub);      \
39
37.4M
    } else {                                    \
40
18.9M
        if (PyList_Append(list, sub)) {         \
41
0
            Py_DECREF(sub);                     \
42
0
            goto onError;                       \
43
0
        }                                       \
44
18.9M
        else                                    \
45
18.9M
            Py_DECREF(sub);                     \
46
18.9M
    }                                           \
47
56.4M
    count++; }
48
49
50
/* Always force the list to the expected size. */
51
23.9M
#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
162k
{
58
162k
    Py_ssize_t i, j, count=0;
59
162k
    PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
60
162k
    PyObject *sub;
61
62
162k
    if (list == NULL)
63
0
        return NULL;
64
65
162k
    i = j = 0;
66
1.74M
    while (maxcount-- > 0) {
67
3.37M
        while (i < str_len && STRINGLIB_ISSPACE(str[i]))
68
1.65M
            i++;
69
1.71M
        if (i == str_len) break;
70
1.62M
        j = i; i++;
71
127M
        while (i < str_len && !STRINGLIB_ISSPACE(str[i]))
72
125M
            i++;
73
#if !STRINGLIB_MUTABLE
74
1.62M
        if (j == 0 && i == str_len && STRINGLIB_CHECK_EXACT(str_obj)) {
75
            /* No whitespace in str_obj, so just use it as list[0] */
76
45.8k
            Py_INCREF(str_obj);
77
45.8k
            PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
78
45.8k
            count++;
79
45.8k
            break;
80
45.8k
        }
81
1.58M
#endif
82
4.74M
        SPLIT_ADD(str, j, i);
83
4.74M
    }
84
85
162k
    if (i < str_len) {
86
        /* Only occurs when maxcount was reached */
87
        /* Skip any remaining whitespace and copy to end of string */
88
61.6k
        while (i < str_len && STRINGLIB_ISSPACE(str[i]))
89
35.1k
            i++;
90
26.5k
        if (i != str_len)
91
26.5k
            SPLIT_ADD(str, i, str_len);
92
26.5k
    }
93
162k
    FIX_PREALLOC_SIZE(list);
94
162k
    return list;
95
96
0
  onError:
97
0
    Py_DECREF(list);
98
0
    return NULL;
99
162k
}
Unexecuted instantiation: bytesobject.c:stringlib_split_whitespace
unicodeobject.c:asciilib_split_whitespace
Line
Count
Source
57
78.3k
{
58
78.3k
    Py_ssize_t i, j, count=0;
59
78.3k
    PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
60
78.3k
    PyObject *sub;
61
62
78.3k
    if (list == NULL)
63
0
        return NULL;
64
65
78.3k
    i = j = 0;
66
547k
    while (maxcount-- > 0) {
67
976k
        while (i < str_len && STRINGLIB_ISSPACE(str[i]))
68
444k
            i++;
69
531k
        if (i == str_len) break;
70
496k
        j = i; i++;
71
49.6M
        while (i < str_len && !STRINGLIB_ISSPACE(str[i]))
72
49.1M
            i++;
73
496k
#if !STRINGLIB_MUTABLE
74
496k
        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.6k
            Py_INCREF(str_obj);
77
27.6k
            PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
78
27.6k
            count++;
79
27.6k
            break;
80
27.6k
        }
81
469k
#endif
82
1.40M
        SPLIT_ADD(str, j, i);
83
1.40M
    }
84
85
78.3k
    if (i < str_len) {
86
        /* Only occurs when maxcount was reached */
87
        /* Skip any remaining whitespace and copy to end of string */
88
32.0k
        while (i < str_len && STRINGLIB_ISSPACE(str[i]))
89
18.1k
            i++;
90
13.9k
        if (i != str_len)
91
13.9k
            SPLIT_ADD(str, i, str_len);
92
13.9k
    }
93
78.3k
    FIX_PREALLOC_SIZE(list);
94
78.3k
    return list;
95
96
0
  onError:
97
0
    Py_DECREF(list);
98
    return NULL;
99
78.3k
}
unicodeobject.c:ucs1lib_split_whitespace
Line
Count
Source
57
26.9k
{
58
26.9k
    Py_ssize_t i, j, count=0;
59
26.9k
    PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
60
26.9k
    PyObject *sub;
61
62
26.9k
    if (list == NULL)
63
0
        return NULL;
64
65
26.9k
    i = j = 0;
66
604k
    while (maxcount-- > 0) {
67
1.15M
        while (i < str_len && STRINGLIB_ISSPACE(str[i]))
68
561k
            i++;
69
591k
        if (i == str_len) break;
70
580k
        j = i; i++;
71
39.1M
        while (i < str_len && !STRINGLIB_ISSPACE(str[i]))
72
38.5M
            i++;
73
580k
#if !STRINGLIB_MUTABLE
74
580k
        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.60k
            Py_INCREF(str_obj);
77
2.60k
            PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
78
2.60k
            count++;
79
2.60k
            break;
80
2.60k
        }
81
577k
#endif
82
1.73M
        SPLIT_ADD(str, j, i);
83
1.73M
    }
84
85
26.9k
    if (i < str_len) {
86
        /* Only occurs when maxcount was reached */
87
        /* Skip any remaining whitespace and copy to end of string */
88
28.2k
        while (i < str_len && STRINGLIB_ISSPACE(str[i]))
89
16.2k
            i++;
90
11.9k
        if (i != str_len)
91
11.9k
            SPLIT_ADD(str, i, str_len);
92
11.9k
    }
93
26.9k
    FIX_PREALLOC_SIZE(list);
94
26.9k
    return list;
95
96
0
  onError:
97
0
    Py_DECREF(list);
98
    return NULL;
99
26.9k
}
unicodeobject.c:ucs2lib_split_whitespace
Line
Count
Source
57
45.6k
{
58
45.6k
    Py_ssize_t i, j, count=0;
59
45.6k
    PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
60
45.6k
    PyObject *sub;
61
62
45.6k
    if (list == NULL)
63
0
        return NULL;
64
65
45.6k
    i = j = 0;
66
525k
    while (maxcount-- > 0) {
67
1.12M
        while (i < str_len && STRINGLIB_ISSPACE(str[i]))
68
595k
            i++;
69
525k
        if (i == str_len) break;
70
492k
        j = i; i++;
71
27.6M
        while (i < str_len && !STRINGLIB_ISSPACE(str[i]))
72
27.2M
            i++;
73
492k
#if !STRINGLIB_MUTABLE
74
492k
        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
12.2k
            Py_INCREF(str_obj);
77
12.2k
            PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
78
12.2k
            count++;
79
12.2k
            break;
80
12.2k
        }
81
480k
#endif
82
1.44M
        SPLIT_ADD(str, j, i);
83
1.44M
    }
84
85
45.6k
    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
45.6k
    FIX_PREALLOC_SIZE(list);
94
45.6k
    return list;
95
96
0
  onError:
97
0
    Py_DECREF(list);
98
    return NULL;
99
45.6k
}
unicodeobject.c:ucs4lib_split_whitespace
Line
Count
Source
57
11.0k
{
58
11.0k
    Py_ssize_t i, j, count=0;
59
11.0k
    PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
60
11.0k
    PyObject *sub;
61
62
11.0k
    if (list == NULL)
63
0
        return NULL;
64
65
11.0k
    i = j = 0;
66
67.0k
    while (maxcount-- > 0) {
67
122k
        while (i < str_len && STRINGLIB_ISSPACE(str[i]))
68
55.9k
            i++;
69
66.7k
        if (i == str_len) break;
70
59.4k
        j = i; i++;
71
11.1M
        while (i < str_len && !STRINGLIB_ISSPACE(str[i]))
72
11.0M
            i++;
73
59.4k
#if !STRINGLIB_MUTABLE
74
59.4k
        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.42k
            Py_INCREF(str_obj);
77
3.42k
            PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
78
3.42k
            count++;
79
3.42k
            break;
80
3.42k
        }
81
56.0k
#endif
82
168k
        SPLIT_ADD(str, j, i);
83
168k
    }
84
85
11.0k
    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.0k
    FIX_PREALLOC_SIZE(list);
94
11.0k
    return list;
95
96
0
  onError:
97
0
    Py_DECREF(list);
98
    return NULL;
99
11.0k
}
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
23.5M
{
107
23.5M
    Py_ssize_t i, j, count=0;
108
23.5M
    PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
109
23.5M
    PyObject *sub;
110
111
23.5M
    if (list == NULL)
112
0
        return NULL;
113
114
23.5M
    i = j = 0;
115
81.6M
    while ((j < str_len) && (maxcount-- > 0)) {
116
433M
        for(; j < str_len; j++) {
117
            /* I found that using memchr makes no difference */
118
412M
            if (str[j] == ch) {
119
36.7M
                SPLIT_ADD(str, i, j);
120
36.7M
                i = j = j + 1;
121
36.7M
                break;
122
36.7M
            }
123
412M
        }
124
58.0M
    }
125
#if !STRINGLIB_MUTABLE
126
23.5M
    if (count == 0 && STRINGLIB_CHECK_EXACT(str_obj)) {
127
        /* ch not in str_obj, so just use str_obj as list[0] */
128
5.77M
        Py_INCREF(str_obj);
129
5.77M
        PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
130
5.77M
        count++;
131
5.77M
    } else
132
17.8M
#endif
133
17.8M
    if (i <= str_len) {
134
35.6M
        SPLIT_ADD(str, i, str_len);
135
35.6M
    }
136
23.5M
    FIX_PREALLOC_SIZE(list);
137
23.5M
    return list;
138
139
0
  onError:
140
0
    Py_DECREF(list);
141
0
    return NULL;
142
0
}
bytesobject.c:stringlib_split_char
Line
Count
Source
106
2.95M
{
107
2.95M
    Py_ssize_t i, j, count=0;
108
2.95M
    PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
109
2.95M
    PyObject *sub;
110
111
2.95M
    if (list == NULL)
112
0
        return NULL;
113
114
2.95M
    i = j = 0;
115
8.64M
    while ((j < str_len) && (maxcount-- > 0)) {
116
60.1M
        for(; j < str_len; j++) {
117
            /* I found that using memchr makes no difference */
118
57.6M
            if (str[j] == ch) {
119
3.18M
                SPLIT_ADD(str, i, j);
120
3.18M
                i = j = j + 1;
121
3.18M
                break;
122
3.18M
            }
123
57.6M
        }
124
5.68M
    }
125
2.95M
#if !STRINGLIB_MUTABLE
126
2.95M
    if (count == 0 && STRINGLIB_CHECK_EXACT(str_obj)) {
127
        /* ch not in str_obj, so just use str_obj as list[0] */
128
2.40M
        Py_INCREF(str_obj);
129
2.40M
        PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
130
2.40M
        count++;
131
2.40M
    } else
132
553k
#endif
133
553k
    if (i <= str_len) {
134
1.10M
        SPLIT_ADD(str, i, str_len);
135
1.10M
    }
136
2.95M
    FIX_PREALLOC_SIZE(list);
137
2.95M
    return list;
138
139
0
  onError:
140
0
    Py_DECREF(list);
141
    return NULL;
142
2.95M
}
unicodeobject.c:asciilib_split_char
Line
Count
Source
106
19.1M
{
107
19.1M
    Py_ssize_t i, j, count=0;
108
19.1M
    PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
109
19.1M
    PyObject *sub;
110
111
19.1M
    if (list == NULL)
112
0
        return NULL;
113
114
19.1M
    i = j = 0;
115
58.8M
    while ((j < str_len) && (maxcount-- > 0)) {
116
251M
        for(; j < str_len; j++) {
117
            /* I found that using memchr makes no difference */
118
233M
            if (str[j] == ch) {
119
21.1M
                SPLIT_ADD(str, i, j);
120
21.1M
                i = j = j + 1;
121
21.1M
                break;
122
21.1M
            }
123
233M
        }
124
39.6M
    }
125
19.1M
#if !STRINGLIB_MUTABLE
126
19.1M
    if (count == 0 && STRINGLIB_CHECK_EXACT(str_obj)) {
127
        /* ch not in str_obj, so just use str_obj as list[0] */
128
3.06M
        Py_INCREF(str_obj);
129
3.06M
        PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
130
3.06M
        count++;
131
3.06M
    } else
132
16.1M
#endif
133
16.1M
    if (i <= str_len) {
134
32.2M
        SPLIT_ADD(str, i, str_len);
135
32.2M
    }
136
19.1M
    FIX_PREALLOC_SIZE(list);
137
19.1M
    return list;
138
139
0
  onError:
140
0
    Py_DECREF(list);
141
    return NULL;
142
19.1M
}
unicodeobject.c:ucs1lib_split_char
Line
Count
Source
106
1.26M
{
107
1.26M
    Py_ssize_t i, j, count=0;
108
1.26M
    PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
109
1.26M
    PyObject *sub;
110
111
1.26M
    if (list == NULL)
112
0
        return NULL;
113
114
1.26M
    i = j = 0;
115
9.67M
    while ((j < str_len) && (maxcount-- > 0)) {
116
63.1M
        for(; j < str_len; j++) {
117
            /* I found that using memchr makes no difference */
118
62.9M
            if (str[j] == ch) {
119
8.14M
                SPLIT_ADD(str, i, j);
120
8.14M
                i = j = j + 1;
121
8.14M
                break;
122
8.14M
            }
123
62.9M
        }
124
8.40M
    }
125
1.26M
#if !STRINGLIB_MUTABLE
126
1.26M
    if (count == 0 && STRINGLIB_CHECK_EXACT(str_obj)) {
127
        /* ch not in str_obj, so just use str_obj as list[0] */
128
232k
        Py_INCREF(str_obj);
129
232k
        PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
130
232k
        count++;
131
232k
    } else
132
1.03M
#endif
133
1.03M
    if (i <= str_len) {
134
2.06M
        SPLIT_ADD(str, i, str_len);
135
2.06M
    }
136
1.26M
    FIX_PREALLOC_SIZE(list);
137
1.26M
    return list;
138
139
0
  onError:
140
0
    Py_DECREF(list);
141
    return NULL;
142
1.26M
}
unicodeobject.c:ucs2lib_split_char
Line
Count
Source
106
156k
{
107
156k
    Py_ssize_t i, j, count=0;
108
156k
    PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
109
156k
    PyObject *sub;
110
111
156k
    if (list == NULL)
112
0
        return NULL;
113
114
156k
    i = j = 0;
115
4.23M
    while ((j < str_len) && (maxcount-- > 0)) {
116
35.3M
        for(; j < str_len; j++) {
117
            /* I found that using memchr makes no difference */
118
35.2M
            if (str[j] == ch) {
119
3.95M
                SPLIT_ADD(str, i, j);
120
3.95M
                i = j = j + 1;
121
3.95M
                break;
122
3.95M
            }
123
35.2M
        }
124
4.07M
    }
125
156k
#if !STRINGLIB_MUTABLE
126
156k
    if (count == 0 && STRINGLIB_CHECK_EXACT(str_obj)) {
127
        /* ch not in str_obj, so just use str_obj as list[0] */
128
75.8k
        Py_INCREF(str_obj);
129
75.8k
        PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
130
75.8k
        count++;
131
75.8k
    } else
132
80.5k
#endif
133
80.5k
    if (i <= str_len) {
134
161k
        SPLIT_ADD(str, i, str_len);
135
161k
    }
136
156k
    FIX_PREALLOC_SIZE(list);
137
156k
    return list;
138
139
0
  onError:
140
0
    Py_DECREF(list);
141
    return NULL;
142
156k
}
unicodeobject.c:ucs4lib_split_char
Line
Count
Source
106
15.1k
{
107
15.1k
    Py_ssize_t i, j, count=0;
108
15.1k
    PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
109
15.1k
    PyObject *sub;
110
111
15.1k
    if (list == NULL)
112
0
        return NULL;
113
114
15.1k
    i = j = 0;
115
251k
    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
223k
                SPLIT_ADD(str, i, j);
120
223k
                i = j = j + 1;
121
223k
                break;
122
223k
            }
123
22.9M
        }
124
236k
    }
125
15.1k
#if !STRINGLIB_MUTABLE
126
15.1k
    if (count == 0 && STRINGLIB_CHECK_EXACT(str_obj)) {
127
        /* ch not in str_obj, so just use str_obj as list[0] */
128
922
        Py_INCREF(str_obj);
129
922
        PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
130
922
        count++;
131
922
    } else
132
14.2k
#endif
133
14.2k
    if (i <= str_len) {
134
28.4k
        SPLIT_ADD(str, i, str_len);
135
28.4k
    }
136
15.1k
    FIX_PREALLOC_SIZE(list);
137
15.1k
    return list;
138
139
0
  onError:
140
0
    Py_DECREF(list);
141
    return NULL;
142
15.1k
}
Unexecuted instantiation: bytearrayobject.c:stringlib_split_char
143
144
Py_LOCAL_INLINE(PyObject *)
145
STRINGLIB(split)(PyObject* str_obj,
146
                const STRINGLIB_CHAR* str, Py_ssize_t str_len,
147
                const STRINGLIB_CHAR* sep, Py_ssize_t sep_len,
148
                Py_ssize_t maxcount)
149
23.7M
{
150
23.7M
    Py_ssize_t i, j, pos, count=0;
151
23.7M
    PyObject *list, *sub;
152
153
23.7M
    if (sep_len == 0) {
154
0
        PyErr_SetString(PyExc_ValueError, "empty separator");
155
0
        return NULL;
156
0
    }
157
23.7M
    else if (sep_len == 1)
158
23.5M
        return STRINGLIB(split_char)(str_obj, str, str_len, sep[0], maxcount);
159
160
202k
    list = PyList_New(PREALLOC_SIZE(maxcount));
161
202k
    if (list == NULL)
162
0
        return NULL;
163
164
202k
    i = j = 0;
165
353k
    while (maxcount-- > 0) {
166
202k
        pos = FASTSEARCH(str+i, str_len-i, sep, sep_len, -1, FAST_SEARCH);
167
202k
        if (pos < 0)
168
51.2k
            break;
169
151k
        j = i + pos;
170
302k
        SPLIT_ADD(str, i, j);
171
302k
        i = j + sep_len;
172
302k
    }
173
#if !STRINGLIB_MUTABLE
174
202k
    if (count == 0 && STRINGLIB_CHECK_EXACT(str_obj)) {
175
        /* No match in str_obj, so just use it as list[0] */
176
51.2k
        Py_INCREF(str_obj);
177
51.2k
        PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
178
51.2k
        count++;
179
51.2k
    } else
180
151k
#endif
181
151k
    {
182
302k
        SPLIT_ADD(str, i, str_len);
183
302k
    }
184
202k
    FIX_PREALLOC_SIZE(list);
185
202k
    return list;
186
187
0
  onError:
188
0
    Py_DECREF(list);
189
0
    return NULL;
190
0
}
bytesobject.c:stringlib_split
Line
Count
Source
149
2.95M
{
150
2.95M
    Py_ssize_t i, j, pos, count=0;
151
2.95M
    PyObject *list, *sub;
152
153
2.95M
    if (sep_len == 0) {
154
0
        PyErr_SetString(PyExc_ValueError, "empty separator");
155
0
        return NULL;
156
0
    }
157
2.95M
    else if (sep_len == 1)
158
2.95M
        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.2M
{
150
19.2M
    Py_ssize_t i, j, pos, count=0;
151
19.2M
    PyObject *list, *sub;
152
153
19.2M
    if (sep_len == 0) {
154
0
        PyErr_SetString(PyExc_ValueError, "empty separator");
155
0
        return NULL;
156
0
    }
157
19.2M
    else if (sep_len == 1)
158
19.1M
        return STRINGLIB(split_char)(str_obj, str, str_len, sep[0], maxcount);
159
160
87.3k
    list = PyList_New(PREALLOC_SIZE(maxcount));
161
87.3k
    if (list == NULL)
162
0
        return NULL;
163
164
87.3k
    i = j = 0;
165
139k
    while (maxcount-- > 0) {
166
87.3k
        pos = FASTSEARCH(str+i, str_len-i, sep, sep_len, -1, FAST_SEARCH);
167
87.3k
        if (pos < 0)
168
35.4k
            break;
169
51.9k
        j = i + pos;
170
103k
        SPLIT_ADD(str, i, j);
171
103k
        i = j + sep_len;
172
103k
    }
173
87.3k
#if !STRINGLIB_MUTABLE
174
87.3k
    if (count == 0 && STRINGLIB_CHECK_EXACT(str_obj)) {
175
        /* No match in str_obj, so just use it as list[0] */
176
35.4k
        Py_INCREF(str_obj);
177
35.4k
        PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
178
35.4k
        count++;
179
35.4k
    } else
180
51.9k
#endif
181
51.9k
    {
182
103k
        SPLIT_ADD(str, i, str_len);
183
103k
    }
184
87.3k
    FIX_PREALLOC_SIZE(list);
185
87.3k
    return list;
186
187
0
  onError:
188
0
    Py_DECREF(list);
189
    return NULL;
190
87.3k
}
unicodeobject.c:ucs1lib_split
Line
Count
Source
149
1.28M
{
150
1.28M
    Py_ssize_t i, j, pos, count=0;
151
1.28M
    PyObject *list, *sub;
152
153
1.28M
    if (sep_len == 0) {
154
0
        PyErr_SetString(PyExc_ValueError, "empty separator");
155
0
        return NULL;
156
0
    }
157
1.28M
    else if (sep_len == 1)
158
1.26M
        return STRINGLIB(split_char)(str_obj, str, str_len, sep[0], maxcount);
159
160
17.0k
    list = PyList_New(PREALLOC_SIZE(maxcount));
161
17.0k
    if (list == NULL)
162
0
        return NULL;
163
164
17.0k
    i = j = 0;
165
31.9k
    while (maxcount-- > 0) {
166
17.0k
        pos = FASTSEARCH(str+i, str_len-i, sep, sep_len, -1, FAST_SEARCH);
167
17.0k
        if (pos < 0)
168
2.03k
            break;
169
14.9k
        j = i + pos;
170
29.9k
        SPLIT_ADD(str, i, j);
171
29.9k
        i = j + sep_len;
172
29.9k
    }
173
17.0k
#if !STRINGLIB_MUTABLE
174
17.0k
    if (count == 0 && STRINGLIB_CHECK_EXACT(str_obj)) {
175
        /* No match in str_obj, so just use it as list[0] */
176
2.03k
        Py_INCREF(str_obj);
177
2.03k
        PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
178
2.03k
        count++;
179
2.03k
    } else
180
14.9k
#endif
181
14.9k
    {
182
29.9k
        SPLIT_ADD(str, i, str_len);
183
29.9k
    }
184
17.0k
    FIX_PREALLOC_SIZE(list);
185
17.0k
    return list;
186
187
0
  onError:
188
0
    Py_DECREF(list);
189
    return NULL;
190
17.0k
}
unicodeobject.c:ucs2lib_split
Line
Count
Source
149
235k
{
150
235k
    Py_ssize_t i, j, pos, count=0;
151
235k
    PyObject *list, *sub;
152
153
235k
    if (sep_len == 0) {
154
0
        PyErr_SetString(PyExc_ValueError, "empty separator");
155
0
        return NULL;
156
0
    }
157
235k
    else if (sep_len == 1)
158
156k
        return STRINGLIB(split_char)(str_obj, str, str_len, sep[0], maxcount);
159
160
79.1k
    list = PyList_New(PREALLOC_SIZE(maxcount));
161
79.1k
    if (list == NULL)
162
0
        return NULL;
163
164
79.1k
    i = j = 0;
165
147k
    while (maxcount-- > 0) {
166
79.1k
        pos = FASTSEARCH(str+i, str_len-i, sep, sep_len, -1, FAST_SEARCH);
167
79.1k
        if (pos < 0)
168
11.2k
            break;
169
67.9k
        j = i + pos;
170
135k
        SPLIT_ADD(str, i, j);
171
135k
        i = j + sep_len;
172
135k
    }
173
79.1k
#if !STRINGLIB_MUTABLE
174
79.1k
    if (count == 0 && STRINGLIB_CHECK_EXACT(str_obj)) {
175
        /* No match in str_obj, so just use it as list[0] */
176
11.2k
        Py_INCREF(str_obj);
177
11.2k
        PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
178
11.2k
        count++;
179
11.2k
    } else
180
67.9k
#endif
181
67.9k
    {
182
135k
        SPLIT_ADD(str, i, str_len);
183
135k
    }
184
79.1k
    FIX_PREALLOC_SIZE(list);
185
79.1k
    return list;
186
187
0
  onError:
188
0
    Py_DECREF(list);
189
    return NULL;
190
79.1k
}
unicodeobject.c:ucs4lib_split
Line
Count
Source
149
34.1k
{
150
34.1k
    Py_ssize_t i, j, pos, count=0;
151
34.1k
    PyObject *list, *sub;
152
153
34.1k
    if (sep_len == 0) {
154
0
        PyErr_SetString(PyExc_ValueError, "empty separator");
155
0
        return NULL;
156
0
    }
157
34.1k
    else if (sep_len == 1)
158
15.1k
        return STRINGLIB(split_char)(str_obj, str, str_len, sep[0], maxcount);
159
160
19.0k
    list = PyList_New(PREALLOC_SIZE(maxcount));
161
19.0k
    if (list == NULL)
162
0
        return NULL;
163
164
19.0k
    i = j = 0;
165
35.5k
    while (maxcount-- > 0) {
166
19.0k
        pos = FASTSEARCH(str+i, str_len-i, sep, sep_len, -1, FAST_SEARCH);
167
19.0k
        if (pos < 0)
168
2.59k
            break;
169
16.4k
        j = i + pos;
170
32.9k
        SPLIT_ADD(str, i, j);
171
32.9k
        i = j + sep_len;
172
32.9k
    }
173
19.0k
#if !STRINGLIB_MUTABLE
174
19.0k
    if (count == 0 && STRINGLIB_CHECK_EXACT(str_obj)) {
175
        /* No match in str_obj, so just use it as list[0] */
176
2.59k
        Py_INCREF(str_obj);
177
2.59k
        PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
178
2.59k
        count++;
179
2.59k
    } else
180
16.4k
#endif
181
16.4k
    {
182
32.9k
        SPLIT_ADD(str, i, str_len);
183
32.9k
    }
184
19.0k
    FIX_PREALLOC_SIZE(list);
185
19.0k
    return list;
186
187
0
  onError:
188
0
    Py_DECREF(list);
189
    return NULL;
190
19.0k
}
Unexecuted instantiation: bytearrayobject.c:stringlib_split
191
192
Py_LOCAL_INLINE(PyObject *)
193
STRINGLIB(rsplit_whitespace)(PyObject* str_obj,
194
                            const STRINGLIB_CHAR* str, Py_ssize_t str_len,
195
                            Py_ssize_t maxcount)
196
0
{
197
0
    Py_ssize_t i, j, count=0;
198
0
    PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
199
0
    PyObject *sub;
200
201
0
    if (list == NULL)
202
0
        return NULL;
203
204
0
    i = j = str_len - 1;
205
0
    while (maxcount-- > 0) {
206
0
        while (i >= 0 && STRINGLIB_ISSPACE(str[i]))
207
0
            i--;
208
0
        if (i < 0) break;
209
0
        j = i; i--;
210
0
        while (i >= 0 && !STRINGLIB_ISSPACE(str[i]))
211
0
            i--;
212
#if !STRINGLIB_MUTABLE
213
0
        if (j == str_len - 1 && i < 0 && STRINGLIB_CHECK_EXACT(str_obj)) {
214
            /* No whitespace in str_obj, so just use it as list[0] */
215
0
            Py_INCREF(str_obj);
216
0
            PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
217
0
            count++;
218
0
            break;
219
0
        }
220
0
#endif
221
0
        SPLIT_ADD(str, i + 1, j + 1);
222
0
    }
223
224
0
    if (i >= 0) {
225
        /* Only occurs when maxcount was reached */
226
        /* Skip any remaining whitespace and copy to beginning of string */
227
0
        while (i >= 0 && STRINGLIB_ISSPACE(str[i]))
228
0
            i--;
229
0
        if (i >= 0)
230
0
            SPLIT_ADD(str, 0, i + 1);
231
0
    }
232
0
    FIX_PREALLOC_SIZE(list);
233
0
    if (PyList_Reverse(list) < 0)
234
0
        goto onError;
235
0
    return list;
236
237
0
  onError:
238
0
    Py_DECREF(list);
239
0
    return NULL;
240
0
}
Unexecuted instantiation: bytesobject.c:stringlib_rsplit_whitespace
Unexecuted instantiation: unicodeobject.c:asciilib_rsplit_whitespace
Unexecuted instantiation: unicodeobject.c:ucs1lib_rsplit_whitespace
Unexecuted instantiation: unicodeobject.c:ucs2lib_rsplit_whitespace
Unexecuted instantiation: unicodeobject.c:ucs4lib_rsplit_whitespace
Unexecuted instantiation: bytearrayobject.c:stringlib_rsplit_whitespace
241
242
Py_LOCAL_INLINE(PyObject *)
243
STRINGLIB(rsplit_char)(PyObject* str_obj,
244
                      const STRINGLIB_CHAR* str, Py_ssize_t str_len,
245
                      const STRINGLIB_CHAR ch,
246
                      Py_ssize_t maxcount)
247
66
{
248
66
    Py_ssize_t i, j, count=0;
249
66
    PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
250
66
    PyObject *sub;
251
252
66
    if (list == NULL)
253
0
        return NULL;
254
255
66
    i = j = str_len - 1;
256
132
    while ((i >= 0) && (maxcount-- > 0)) {
257
138
        for(; i >= 0; i--) {
258
138
            if (str[i] == ch) {
259
66
                SPLIT_ADD(str, i + 1, j + 1);
260
66
                j = i = i - 1;
261
66
                break;
262
66
            }
263
138
        }
264
66
    }
265
#if !STRINGLIB_MUTABLE
266
66
    if (count == 0 && STRINGLIB_CHECK_EXACT(str_obj)) {
267
        /* ch not in str_obj, so just use str_obj as list[0] */
268
0
        Py_INCREF(str_obj);
269
0
        PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
270
0
        count++;
271
0
    } else
272
66
#endif
273
66
    if (j >= -1) {
274
132
        SPLIT_ADD(str, 0, j + 1);
275
132
    }
276
66
    FIX_PREALLOC_SIZE(list);
277
66
    if (PyList_Reverse(list) < 0)
278
0
        goto onError;
279
66
    return list;
280
281
0
  onError:
282
0
    Py_DECREF(list);
283
0
    return NULL;
284
66
}
Unexecuted instantiation: bytesobject.c:stringlib_rsplit_char
unicodeobject.c:asciilib_rsplit_char
Line
Count
Source
247
66
{
248
66
    Py_ssize_t i, j, count=0;
249
66
    PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
250
66
    PyObject *sub;
251
252
66
    if (list == NULL)
253
0
        return NULL;
254
255
66
    i = j = str_len - 1;
256
132
    while ((i >= 0) && (maxcount-- > 0)) {
257
138
        for(; i >= 0; i--) {
258
138
            if (str[i] == ch) {
259
66
                SPLIT_ADD(str, i + 1, j + 1);
260
66
                j = i = i - 1;
261
66
                break;
262
66
            }
263
138
        }
264
66
    }
265
66
#if !STRINGLIB_MUTABLE
266
66
    if (count == 0 && STRINGLIB_CHECK_EXACT(str_obj)) {
267
        /* ch not in str_obj, so just use str_obj as list[0] */
268
0
        Py_INCREF(str_obj);
269
0
        PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
270
0
        count++;
271
0
    } else
272
66
#endif
273
66
    if (j >= -1) {
274
132
        SPLIT_ADD(str, 0, j + 1);
275
132
    }
276
66
    FIX_PREALLOC_SIZE(list);
277
66
    if (PyList_Reverse(list) < 0)
278
0
        goto onError;
279
66
    return list;
280
281
0
  onError:
282
0
    Py_DECREF(list);
283
    return NULL;
284
66
}
Unexecuted instantiation: unicodeobject.c:ucs1lib_rsplit_char
Unexecuted instantiation: unicodeobject.c:ucs2lib_rsplit_char
Unexecuted instantiation: unicodeobject.c:ucs4lib_rsplit_char
Unexecuted instantiation: bytearrayobject.c:stringlib_rsplit_char
285
286
Py_LOCAL_INLINE(PyObject *)
287
STRINGLIB(rsplit)(PyObject* str_obj,
288
                 const STRINGLIB_CHAR* str, Py_ssize_t str_len,
289
                 const STRINGLIB_CHAR* sep, Py_ssize_t sep_len,
290
                 Py_ssize_t maxcount)
291
66
{
292
66
    Py_ssize_t j, pos, count=0;
293
66
    PyObject *list, *sub;
294
295
66
    if (sep_len == 0) {
296
0
        PyErr_SetString(PyExc_ValueError, "empty separator");
297
0
        return NULL;
298
0
    }
299
66
    else if (sep_len == 1)
300
66
        return STRINGLIB(rsplit_char)(str_obj, str, str_len, sep[0], maxcount);
301
302
0
    list = PyList_New(PREALLOC_SIZE(maxcount));
303
0
    if (list == NULL)
304
0
        return NULL;
305
306
0
    j = str_len;
307
0
    while (maxcount-- > 0) {
308
0
        pos = FASTSEARCH(str, j, sep, sep_len, -1, FAST_RSEARCH);
309
0
        if (pos < 0)
310
0
            break;
311
0
        SPLIT_ADD(str, pos + sep_len, j);
312
0
        j = pos;
313
0
    }
314
#if !STRINGLIB_MUTABLE
315
0
    if (count == 0 && STRINGLIB_CHECK_EXACT(str_obj)) {
316
        /* No match in str_obj, so just use it as list[0] */
317
0
        Py_INCREF(str_obj);
318
0
        PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
319
0
        count++;
320
0
    } else
321
0
#endif
322
0
    {
323
0
        SPLIT_ADD(str, 0, j);
324
0
    }
325
0
    FIX_PREALLOC_SIZE(list);
326
0
    if (PyList_Reverse(list) < 0)
327
0
        goto onError;
328
0
    return list;
329
330
0
  onError:
331
0
    Py_DECREF(list);
332
0
    return NULL;
333
0
}
Unexecuted instantiation: bytesobject.c:stringlib_rsplit
unicodeobject.c:asciilib_rsplit
Line
Count
Source
291
66
{
292
66
    Py_ssize_t j, pos, count=0;
293
66
    PyObject *list, *sub;
294
295
66
    if (sep_len == 0) {
296
0
        PyErr_SetString(PyExc_ValueError, "empty separator");
297
0
        return NULL;
298
0
    }
299
66
    else if (sep_len == 1)
300
66
        return STRINGLIB(rsplit_char)(str_obj, str, str_len, sep[0], maxcount);
301
302
0
    list = PyList_New(PREALLOC_SIZE(maxcount));
303
0
    if (list == NULL)
304
0
        return NULL;
305
306
0
    j = str_len;
307
0
    while (maxcount-- > 0) {
308
0
        pos = FASTSEARCH(str, j, sep, sep_len, -1, FAST_RSEARCH);
309
0
        if (pos < 0)
310
0
            break;
311
0
        SPLIT_ADD(str, pos + sep_len, j);
312
0
        j = pos;
313
0
    }
314
0
#if !STRINGLIB_MUTABLE
315
0
    if (count == 0 && STRINGLIB_CHECK_EXACT(str_obj)) {
316
        /* No match in str_obj, so just use it as list[0] */
317
0
        Py_INCREF(str_obj);
318
0
        PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
319
0
        count++;
320
0
    } else
321
0
#endif
322
0
    {
323
0
        SPLIT_ADD(str, 0, j);
324
0
    }
325
0
    FIX_PREALLOC_SIZE(list);
326
0
    if (PyList_Reverse(list) < 0)
327
0
        goto onError;
328
0
    return list;
329
330
0
  onError:
331
0
    Py_DECREF(list);
332
    return NULL;
333
0
}
Unexecuted instantiation: unicodeobject.c:ucs1lib_rsplit
Unexecuted instantiation: unicodeobject.c:ucs2lib_rsplit
Unexecuted instantiation: unicodeobject.c:ucs4lib_rsplit
Unexecuted instantiation: bytearrayobject.c:stringlib_rsplit
334
335
Py_LOCAL_INLINE(PyObject *)
336
STRINGLIB(splitlines)(PyObject* str_obj,
337
                     const STRINGLIB_CHAR* str, Py_ssize_t str_len,
338
                     int keepends)
339
19.3k
{
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
19.3k
    Py_ssize_t i;
349
19.3k
    Py_ssize_t j;
350
19.3k
    PyObject *list = PyList_New(0);
351
19.3k
    PyObject *sub;
352
353
19.3k
    if (list == NULL)
354
0
        return NULL;
355
356
34.0M
    for (i = j = 0; i < str_len; ) {
357
34.0M
        Py_ssize_t eol;
358
359
        /* Find a line and append it */
360
131M
        while (i < str_len && !STRINGLIB_ISLINEBREAK(str[i]))
361
97.0M
            i++;
362
363
        /* Skip the line break reading CRLF as one line break */
364
34.0M
        eol = i;
365
34.0M
        if (i < str_len) {
366
34.0M
            if (str[i] == '\r' && i + 1 < str_len && str[i+1] == '\n')
367
30.9k
                i += 2;
368
34.0M
            else
369
34.0M
                i++;
370
34.0M
            if (keepends)
371
0
                eol = i;
372
34.0M
        }
373
#if !STRINGLIB_MUTABLE
374
34.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
8.43k
            if (PyList_Append(list, str_obj))
377
0
                goto onError;
378
8.43k
            break;
379
8.43k
        }
380
34.0M
#endif
381
68.1M
        SPLIT_APPEND(str, j, eol);
382
34.0M
        j = i;
383
34.0M
    }
384
19.3k
    return list;
385
386
0
  onError:
387
0
    Py_DECREF(list);
388
    return NULL;
389
19.3k
}
Unexecuted instantiation: bytesobject.c:stringlib_splitlines
unicodeobject.c:asciilib_splitlines
Line
Count
Source
339
4.37k
{
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
4.37k
    Py_ssize_t i;
349
4.37k
    Py_ssize_t j;
350
4.37k
    PyObject *list = PyList_New(0);
351
4.37k
    PyObject *sub;
352
353
4.37k
    if (list == NULL)
354
0
        return NULL;
355
356
6.92M
    for (i = j = 0; i < str_len; ) {
357
6.91M
        Py_ssize_t eol;
358
359
        /* Find a line and append it */
360
13.3M
        while (i < str_len && !STRINGLIB_ISLINEBREAK(str[i]))
361
6.40M
            i++;
362
363
        /* Skip the line break reading CRLF as one line break */
364
6.91M
        eol = i;
365
6.91M
        if (i < str_len) {
366
6.91M
            if (str[i] == '\r' && i + 1 < str_len && str[i+1] == '\n')
367
749
                i += 2;
368
6.91M
            else
369
6.91M
                i++;
370
6.91M
            if (keepends)
371
0
                eol = i;
372
6.91M
        }
373
6.91M
#if !STRINGLIB_MUTABLE
374
6.91M
        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.51k
            if (PyList_Append(list, str_obj))
377
0
                goto onError;
378
1.51k
            break;
379
1.51k
        }
380
6.91M
#endif
381
13.8M
        SPLIT_APPEND(str, j, eol);
382
6.91M
        j = i;
383
6.91M
    }
384
4.37k
    return list;
385
386
0
  onError:
387
0
    Py_DECREF(list);
388
    return NULL;
389
4.37k
}
unicodeobject.c:ucs1lib_splitlines
Line
Count
Source
339
1.10k
{
340
    /* This does not use the preallocated list because splitlines is
341
       usually run with hundreds of newlines.  The overhead of
342
       switching between PyList_SET_ITEM and append causes about a
343
       2-3% slowdown for that common case.  A smarter implementation
344
       could move the if check out, so the SET_ITEMs are done first
345
       and the appends only done when the prealloc buffer is full.
346
       That's too much work for little gain.*/
347
348
1.10k
    Py_ssize_t i;
349
1.10k
    Py_ssize_t j;
350
1.10k
    PyObject *list = PyList_New(0);
351
1.10k
    PyObject *sub;
352
353
1.10k
    if (list == NULL)
354
0
        return NULL;
355
356
1.94M
    for (i = j = 0; i < str_len; ) {
357
1.94M
        Py_ssize_t eol;
358
359
        /* Find a line and append it */
360
11.2M
        while (i < str_len && !STRINGLIB_ISLINEBREAK(str[i]))
361
9.30M
            i++;
362
363
        /* Skip the line break reading CRLF as one line break */
364
1.94M
        eol = i;
365
1.94M
        if (i < str_len) {
366
1.94M
            if (str[i] == '\r' && i + 1 < str_len && str[i+1] == '\n')
367
654
                i += 2;
368
1.94M
            else
369
1.94M
                i++;
370
1.94M
            if (keepends)
371
0
                eol = i;
372
1.94M
        }
373
1.94M
#if !STRINGLIB_MUTABLE
374
1.94M
        if (j == 0 && eol == str_len && STRINGLIB_CHECK_EXACT(str_obj)) {
375
            /* No linebreak in str_obj, so just use it as list[0] */
376
306
            if (PyList_Append(list, str_obj))
377
0
                goto onError;
378
306
            break;
379
306
        }
380
1.94M
#endif
381
3.89M
        SPLIT_APPEND(str, j, eol);
382
1.94M
        j = i;
383
1.94M
    }
384
1.10k
    return list;
385
386
0
  onError:
387
0
    Py_DECREF(list);
388
    return NULL;
389
1.10k
}
unicodeobject.c:ucs2lib_splitlines
Line
Count
Source
339
9.92k
{
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
9.92k
    Py_ssize_t i;
349
9.92k
    Py_ssize_t j;
350
9.92k
    PyObject *list = PyList_New(0);
351
9.92k
    PyObject *sub;
352
353
9.92k
    if (list == NULL)
354
0
        return NULL;
355
356
14.0M
    for (i = j = 0; i < str_len; ) {
357
14.0M
        Py_ssize_t eol;
358
359
        /* Find a line and append it */
360
51.9M
        while (i < str_len && !STRINGLIB_ISLINEBREAK(str[i]))
361
37.9M
            i++;
362
363
        /* Skip the line break reading CRLF as one line break */
364
14.0M
        eol = i;
365
14.0M
        if (i < str_len) {
366
14.0M
            if (str[i] == '\r' && i + 1 < str_len && str[i+1] == '\n')
367
4.96k
                i += 2;
368
13.9M
            else
369
13.9M
                i++;
370
14.0M
            if (keepends)
371
0
                eol = i;
372
14.0M
        }
373
14.0M
#if !STRINGLIB_MUTABLE
374
14.0M
        if (j == 0 && eol == str_len && STRINGLIB_CHECK_EXACT(str_obj)) {
375
            /* No linebreak in str_obj, so just use it as list[0] */
376
4.80k
            if (PyList_Append(list, str_obj))
377
0
                goto onError;
378
4.80k
            break;
379
4.80k
        }
380
14.0M
#endif
381
28.0M
        SPLIT_APPEND(str, j, eol);
382
14.0M
        j = i;
383
14.0M
    }
384
9.92k
    return list;
385
386
0
  onError:
387
0
    Py_DECREF(list);
388
    return NULL;
389
9.92k
}
unicodeobject.c:ucs4lib_splitlines
Line
Count
Source
339
3.90k
{
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.90k
    Py_ssize_t i;
349
3.90k
    Py_ssize_t j;
350
3.90k
    PyObject *list = PyList_New(0);
351
3.90k
    PyObject *sub;
352
353
3.90k
    if (list == NULL)
354
0
        return NULL;
355
356
11.2M
    for (i = j = 0; i < str_len; ) {
357
11.2M
        Py_ssize_t eol;
358
359
        /* Find a line and append it */
360
54.6M
        while (i < str_len && !STRINGLIB_ISLINEBREAK(str[i]))
361
43.4M
            i++;
362
363
        /* Skip the line break reading CRLF as one line break */
364
11.2M
        eol = i;
365
11.2M
        if (i < str_len) {
366
11.2M
            if (str[i] == '\r' && i + 1 < str_len && str[i+1] == '\n')
367
24.6k
                i += 2;
368
11.1M
            else
369
11.1M
                i++;
370
11.2M
            if (keepends)
371
0
                eol = i;
372
11.2M
        }
373
11.2M
#if !STRINGLIB_MUTABLE
374
11.2M
        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.80k
            if (PyList_Append(list, str_obj))
377
0
                goto onError;
378
1.80k
            break;
379
1.80k
        }
380
11.2M
#endif
381
22.4M
        SPLIT_APPEND(str, j, eol);
382
11.2M
        j = i;
383
11.2M
    }
384
3.90k
    return list;
385
386
0
  onError:
387
0
    Py_DECREF(list);
388
    return NULL;
389
3.90k
}
Unexecuted instantiation: bytearrayobject.c:stringlib_splitlines
390