Coverage Report

Created: 2025-11-02 06:30

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
99.3M
#define MAX_PREALLOC 12
15
16
/* 5 splits gives 6 elements */
17
#define PREALLOC_SIZE(maxsplit) \
18
21.9M
    (maxsplit >= MAX_PREALLOC ? MAX_PREALLOC : maxsplit+1)
19
20
#define SPLIT_APPEND(data, left, right)         \
21
32.2M
    sub = STRINGLIB_NEW((data) + (left),        \
22
32.2M
                        (right) - (left));      \
23
32.2M
    if (sub == NULL)                            \
24
32.2M
        goto onError;                           \
25
32.2M
    if (PyList_Append(list, sub)) {             \
26
0
        Py_DECREF(sub);                         \
27
0
        goto onError;                           \
28
0
    }                                           \
29
32.2M
    else                                        \
30
32.2M
        Py_DECREF(sub);
31
32
51.6M
#define SPLIT_ADD(data, left, right) {          \
33
51.6M
    sub = STRINGLIB_NEW((data) + (left),        \
34
51.6M
                        (right) - (left));      \
35
51.6M
    if (sub == NULL)                            \
36
51.6M
        goto onError;                           \
37
51.6M
    if (count < MAX_PREALLOC) {                 \
38
32.4M
        PyList_SET_ITEM(list, count, sub);      \
39
32.4M
    } else {                                    \
40
19.2M
        if (PyList_Append(list, sub)) {         \
41
0
            Py_DECREF(sub);                     \
42
0
            goto onError;                       \
43
0
        }                                       \
44
19.2M
        else                                    \
45
19.2M
            Py_DECREF(sub);                     \
46
19.2M
    }                                           \
47
51.6M
    count++; }
48
49
50
/* Always force the list to the expected size. */
51
21.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
159k
{
58
159k
    Py_ssize_t i, j, count=0;
59
159k
    PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
60
159k
    PyObject *sub;
61
62
159k
    if (list == NULL)
63
0
        return NULL;
64
65
159k
    i = j = 0;
66
1.86M
    while (maxcount-- > 0) {
67
3.55M
        while (i < str_len && STRINGLIB_ISSPACE(str[i]))
68
1.72M
            i++;
69
1.83M
        if (i == str_len) break;
70
1.73M
        j = i; i++;
71
105M
        while (i < str_len && !STRINGLIB_ISSPACE(str[i]))
72
104M
            i++;
73
#if !STRINGLIB_MUTABLE
74
1.73M
        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
35.9k
            Py_INCREF(str_obj);
77
35.9k
            PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
78
35.9k
            count++;
79
35.9k
            break;
80
35.9k
        }
81
1.70M
#endif
82
5.10M
        SPLIT_ADD(str, j, i);
83
5.10M
    }
84
85
159k
    if (i < str_len) {
86
        /* Only occurs when maxcount was reached */
87
        /* Skip any remaining whitespace and copy to end of string */
88
56.8k
        while (i < str_len && STRINGLIB_ISSPACE(str[i]))
89
30.6k
            i++;
90
26.2k
        if (i != str_len)
91
26.2k
            SPLIT_ADD(str, i, str_len);
92
26.2k
    }
93
159k
    FIX_PREALLOC_SIZE(list);
94
159k
    return list;
95
96
0
  onError:
97
0
    Py_DECREF(list);
98
0
    return NULL;
99
159k
}
Unexecuted instantiation: bytesobject.c:stringlib_split_whitespace
unicodeobject.c:asciilib_split_whitespace
Line
Count
Source
57
72.9k
{
58
72.9k
    Py_ssize_t i, j, count=0;
59
72.9k
    PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
60
72.9k
    PyObject *sub;
61
62
72.9k
    if (list == NULL)
63
0
        return NULL;
64
65
72.9k
    i = j = 0;
66
567k
    while (maxcount-- > 0) {
67
996k
        while (i < str_len && STRINGLIB_ISSPACE(str[i]))
68
445k
            i++;
69
551k
        if (i == str_len) break;
70
510k
        j = i; i++;
71
35.7M
        while (i < str_len && !STRINGLIB_ISSPACE(str[i]))
72
35.2M
            i++;
73
510k
#if !STRINGLIB_MUTABLE
74
510k
        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
494k
#endif
82
1.48M
        SPLIT_ADD(str, j, i);
83
1.48M
    }
84
85
72.9k
    if (i < str_len) {
86
        /* Only occurs when maxcount was reached */
87
        /* Skip any remaining whitespace and copy to end of string */
88
27.3k
        while (i < str_len && STRINGLIB_ISSPACE(str[i]))
89
14.0k
            i++;
90
13.2k
        if (i != str_len)
91
13.2k
            SPLIT_ADD(str, i, str_len);
92
13.2k
    }
93
72.9k
    FIX_PREALLOC_SIZE(list);
94
72.9k
    return list;
95
96
0
  onError:
97
0
    Py_DECREF(list);
98
    return NULL;
99
72.9k
}
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
514k
    while (maxcount-- > 0) {
67
976k
        while (i < str_len && STRINGLIB_ISSPACE(str[i]))
68
474k
            i++;
69
501k
        if (i == str_len) break;
70
492k
        j = i; i++;
71
21.3M
        while (i < str_len && !STRINGLIB_ISSPACE(str[i]))
72
20.8M
            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
3.58k
            Py_INCREF(str_obj);
77
3.58k
            PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
78
3.58k
            count++;
79
3.58k
            break;
80
3.58k
        }
81
488k
#endif
82
1.46M
        SPLIT_ADD(str, j, i);
83
1.46M
    }
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
27.4k
        while (i < str_len && STRINGLIB_ISSPACE(str[i]))
89
15.5k
            i++;
90
11.9k
        if (i != str_len)
91
11.9k
            SPLIT_ADD(str, i, str_len);
92
11.9k
    }
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
48.4k
{
58
48.4k
    Py_ssize_t i, j, count=0;
59
48.4k
    PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
60
48.4k
    PyObject *sub;
61
62
48.4k
    if (list == NULL)
63
0
        return NULL;
64
65
48.4k
    i = j = 0;
66
662k
    while (maxcount-- > 0) {
67
1.36M
        while (i < str_len && STRINGLIB_ISSPACE(str[i]))
68
703k
            i++;
69
661k
        if (i == str_len) break;
70
628k
        j = i; i++;
71
37.8M
        while (i < str_len && !STRINGLIB_ISSPACE(str[i]))
72
37.1M
            i++;
73
628k
#if !STRINGLIB_MUTABLE
74
628k
        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
14.0k
            Py_INCREF(str_obj);
77
14.0k
            PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
78
14.0k
            count++;
79
14.0k
            break;
80
14.0k
        }
81
614k
#endif
82
1.84M
        SPLIT_ADD(str, j, i);
83
1.84M
    }
84
85
48.4k
    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
48.4k
    FIX_PREALLOC_SIZE(list);
94
48.4k
    return list;
95
96
0
  onError:
97
0
    Py_DECREF(list);
98
    return NULL;
99
48.4k
}
unicodeobject.c:ucs4lib_split_whitespace
Line
Count
Source
57
13.1k
{
58
13.1k
    Py_ssize_t i, j, count=0;
59
13.1k
    PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
60
13.1k
    PyObject *sub;
61
62
13.1k
    if (list == NULL)
63
0
        return NULL;
64
65
13.1k
    i = j = 0;
66
116k
    while (maxcount-- > 0) {
67
216k
        while (i < str_len && STRINGLIB_ISSPACE(str[i]))
68
101k
            i++;
69
115k
        if (i == str_len) break;
70
106k
        j = i; i++;
71
11.0M
        while (i < str_len && !STRINGLIB_ISSPACE(str[i]))
72
10.9M
            i++;
73
106k
#if !STRINGLIB_MUTABLE
74
106k
        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.04k
            Py_INCREF(str_obj);
77
3.04k
            PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
78
3.04k
            count++;
79
3.04k
            break;
80
3.04k
        }
81
103k
#endif
82
309k
        SPLIT_ADD(str, j, i);
83
309k
    }
84
85
13.1k
    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
13.1k
    FIX_PREALLOC_SIZE(list);
94
13.1k
    return list;
95
96
0
  onError:
97
0
    Py_DECREF(list);
98
    return NULL;
99
13.1k
}
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
21.6M
{
107
21.6M
    Py_ssize_t i, j, count=0;
108
21.6M
    PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
109
21.6M
    PyObject *sub;
110
111
21.6M
    if (list == NULL)
112
0
        return NULL;
113
114
21.6M
    i = j = 0;
115
75.2M
    while ((j < str_len) && (maxcount-- > 0)) {
116
379M
        for(; j < str_len; j++) {
117
            /* I found that using memchr makes no difference */
118
360M
            if (str[j] == ch) {
119
34.3M
                SPLIT_ADD(str, i, j);
120
34.3M
                i = j = j + 1;
121
34.3M
                break;
122
34.3M
            }
123
360M
        }
124
53.5M
    }
125
#if !STRINGLIB_MUTABLE
126
21.6M
    if (count == 0 && STRINGLIB_CHECK_EXACT(str_obj)) {
127
        /* ch not in str_obj, so just use str_obj as list[0] */
128
6.30M
        Py_INCREF(str_obj);
129
6.30M
        PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
130
6.30M
        count++;
131
6.30M
    } else
132
15.3M
#endif
133
15.3M
    if (i <= str_len) {
134
30.6M
        SPLIT_ADD(str, i, str_len);
135
30.6M
    }
136
21.6M
    FIX_PREALLOC_SIZE(list);
137
21.6M
    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.44M
{
107
3.44M
    Py_ssize_t i, j, count=0;
108
3.44M
    PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
109
3.44M
    PyObject *sub;
110
111
3.44M
    if (list == NULL)
112
0
        return NULL;
113
114
3.44M
    i = j = 0;
115
11.1M
    while ((j < str_len) && (maxcount-- > 0)) {
116
58.1M
        for(; j < str_len; j++) {
117
            /* I found that using memchr makes no difference */
118
55.3M
            if (str[j] == ch) {
119
4.84M
                SPLIT_ADD(str, i, j);
120
4.84M
                i = j = j + 1;
121
4.84M
                break;
122
4.84M
            }
123
55.3M
        }
124
7.69M
    }
125
3.44M
#if !STRINGLIB_MUTABLE
126
3.44M
    if (count == 0 && STRINGLIB_CHECK_EXACT(str_obj)) {
127
        /* ch not in str_obj, so just use str_obj as list[0] */
128
2.74M
        Py_INCREF(str_obj);
129
2.74M
        PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
130
2.74M
        count++;
131
2.74M
    } else
132
695k
#endif
133
695k
    if (i <= str_len) {
134
1.39M
        SPLIT_ADD(str, i, str_len);
135
1.39M
    }
136
3.44M
    FIX_PREALLOC_SIZE(list);
137
3.44M
    return list;
138
139
0
  onError:
140
0
    Py_DECREF(list);
141
    return NULL;
142
3.44M
}
unicodeobject.c:asciilib_split_char
Line
Count
Source
106
16.8M
{
107
16.8M
    Py_ssize_t i, j, count=0;
108
16.8M
    PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
109
16.8M
    PyObject *sub;
110
111
16.8M
    if (list == NULL)
112
0
        return NULL;
113
114
16.8M
    i = j = 0;
115
51.7M
    while ((j < str_len) && (maxcount-- > 0)) {
116
206M
        for(; j < str_len; j++) {
117
            /* I found that using memchr makes no difference */
118
190M
            if (str[j] == ch) {
119
18.7M
                SPLIT_ADD(str, i, j);
120
18.7M
                i = j = j + 1;
121
18.7M
                break;
122
18.7M
            }
123
190M
        }
124
34.8M
    }
125
16.8M
#if !STRINGLIB_MUTABLE
126
16.8M
    if (count == 0 && STRINGLIB_CHECK_EXACT(str_obj)) {
127
        /* ch not in str_obj, so just use str_obj as list[0] */
128
3.41M
        Py_INCREF(str_obj);
129
3.41M
        PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
130
3.41M
        count++;
131
3.41M
    } else
132
13.4M
#endif
133
13.4M
    if (i <= str_len) {
134
26.8M
        SPLIT_ADD(str, i, str_len);
135
26.8M
    }
136
16.8M
    FIX_PREALLOC_SIZE(list);
137
16.8M
    return list;
138
139
0
  onError:
140
0
    Py_DECREF(list);
141
    return NULL;
142
16.8M
}
unicodeobject.c:ucs1lib_split_char
Line
Count
Source
106
1.20M
{
107
1.20M
    Py_ssize_t i, j, count=0;
108
1.20M
    PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
109
1.20M
    PyObject *sub;
110
111
1.20M
    if (list == NULL)
112
0
        return NULL;
113
114
1.20M
    i = j = 0;
115
8.52M
    while ((j < str_len) && (maxcount-- > 0)) {
116
47.1M
        for(; j < str_len; j++) {
117
            /* I found that using memchr makes no difference */
118
46.9M
            if (str[j] == ch) {
119
7.15M
                SPLIT_ADD(str, i, j);
120
7.15M
                i = j = j + 1;
121
7.15M
                break;
122
7.15M
            }
123
46.9M
        }
124
7.31M
    }
125
1.20M
#if !STRINGLIB_MUTABLE
126
1.20M
    if (count == 0 && STRINGLIB_CHECK_EXACT(str_obj)) {
127
        /* ch not in str_obj, so just use str_obj as list[0] */
128
136k
        Py_INCREF(str_obj);
129
136k
        PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
130
136k
        count++;
131
136k
    } else
132
1.07M
#endif
133
1.07M
    if (i <= str_len) {
134
2.14M
        SPLIT_ADD(str, i, str_len);
135
2.14M
    }
136
1.20M
    FIX_PREALLOC_SIZE(list);
137
1.20M
    return list;
138
139
0
  onError:
140
0
    Py_DECREF(list);
141
    return NULL;
142
1.20M
}
unicodeobject.c:ucs2lib_split_char
Line
Count
Source
106
89.1k
{
107
89.1k
    Py_ssize_t i, j, count=0;
108
89.1k
    PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
109
89.1k
    PyObject *sub;
110
111
89.1k
    if (list == NULL)
112
0
        return NULL;
113
114
89.1k
    i = j = 0;
115
3.07M
    while ((j < str_len) && (maxcount-- > 0)) {
116
45.0M
        for(; j < str_len; j++) {
117
            /* I found that using memchr makes no difference */
118
44.9M
            if (str[j] == ch) {
119
2.92M
                SPLIT_ADD(str, i, j);
120
2.92M
                i = j = j + 1;
121
2.92M
                break;
122
2.92M
            }
123
44.9M
        }
124
2.98M
    }
125
89.1k
#if !STRINGLIB_MUTABLE
126
89.1k
    if (count == 0 && STRINGLIB_CHECK_EXACT(str_obj)) {
127
        /* ch not in str_obj, so just use str_obj as list[0] */
128
7.19k
        Py_INCREF(str_obj);
129
7.19k
        PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
130
7.19k
        count++;
131
7.19k
    } else
132
81.9k
#endif
133
81.9k
    if (i <= str_len) {
134
163k
        SPLIT_ADD(str, i, str_len);
135
163k
    }
136
89.1k
    FIX_PREALLOC_SIZE(list);
137
89.1k
    return list;
138
139
0
  onError:
140
0
    Py_DECREF(list);
141
    return NULL;
142
89.1k
}
unicodeobject.c:ucs4lib_split_char
Line
Count
Source
106
17.0k
{
107
17.0k
    Py_ssize_t i, j, count=0;
108
17.0k
    PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
109
17.0k
    PyObject *sub;
110
111
17.0k
    if (list == NULL)
112
0
        return NULL;
113
114
17.0k
    i = j = 0;
115
727k
    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
697k
                SPLIT_ADD(str, i, j);
120
697k
                i = j = j + 1;
121
697k
                break;
122
697k
            }
123
22.9M
        }
124
710k
    }
125
17.0k
#if !STRINGLIB_MUTABLE
126
17.0k
    if (count == 0 && STRINGLIB_CHECK_EXACT(str_obj)) {
127
        /* ch not in str_obj, so just use str_obj as list[0] */
128
360
        Py_INCREF(str_obj);
129
360
        PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
130
360
        count++;
131
360
    } else
132
16.6k
#endif
133
16.6k
    if (i <= str_len) {
134
33.3k
        SPLIT_ADD(str, i, str_len);
135
33.3k
    }
136
17.0k
    FIX_PREALLOC_SIZE(list);
137
17.0k
    return list;
138
139
0
  onError:
140
0
    Py_DECREF(list);
141
    return NULL;
142
17.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
21.8M
{
150
21.8M
    Py_ssize_t i, j, pos, count=0;
151
21.8M
    PyObject *list, *sub;
152
153
21.8M
    if (sep_len == 0) {
154
0
        PyErr_SetString(PyExc_ValueError, "empty separator");
155
0
        return NULL;
156
0
    }
157
21.8M
    else if (sep_len == 1)
158
21.6M
        return STRINGLIB(split_char)(str_obj, str, str_len, sep[0], maxcount);
159
160
193k
    list = PyList_New(PREALLOC_SIZE(maxcount));
161
193k
    if (list == NULL)
162
0
        return NULL;
163
164
193k
    i = j = 0;
165
347k
    while (maxcount-- > 0) {
166
193k
        pos = FASTSEARCH(str+i, str_len-i, sep, sep_len, -1, FAST_SEARCH);
167
193k
        if (pos < 0)
168
39.6k
            break;
169
154k
        j = i + pos;
170
308k
        SPLIT_ADD(str, i, j);
171
308k
        i = j + sep_len;
172
308k
    }
173
#if !STRINGLIB_MUTABLE
174
193k
    if (count == 0 && STRINGLIB_CHECK_EXACT(str_obj)) {
175
        /* No match in str_obj, so just use it as list[0] */
176
39.6k
        Py_INCREF(str_obj);
177
39.6k
        PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
178
39.6k
        count++;
179
39.6k
    } else
180
154k
#endif
181
154k
    {
182
308k
        SPLIT_ADD(str, i, str_len);
183
308k
    }
184
193k
    FIX_PREALLOC_SIZE(list);
185
193k
    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.44M
{
150
3.44M
    Py_ssize_t i, j, pos, count=0;
151
3.44M
    PyObject *list, *sub;
152
153
3.44M
    if (sep_len == 0) {
154
0
        PyErr_SetString(PyExc_ValueError, "empty separator");
155
0
        return NULL;
156
0
    }
157
3.44M
    else if (sep_len == 1)
158
3.44M
        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
16.9M
{
150
16.9M
    Py_ssize_t i, j, pos, count=0;
151
16.9M
    PyObject *list, *sub;
152
153
16.9M
    if (sep_len == 0) {
154
0
        PyErr_SetString(PyExc_ValueError, "empty separator");
155
0
        return NULL;
156
0
    }
157
16.9M
    else if (sep_len == 1)
158
16.8M
        return STRINGLIB(split_char)(str_obj, str, str_len, sep[0], maxcount);
159
160
71.2k
    list = PyList_New(PREALLOC_SIZE(maxcount));
161
71.2k
    if (list == NULL)
162
0
        return NULL;
163
164
71.2k
    i = j = 0;
165
118k
    while (maxcount-- > 0) {
166
71.2k
        pos = FASTSEARCH(str+i, str_len-i, sep, sep_len, -1, FAST_SEARCH);
167
71.2k
        if (pos < 0)
168
23.8k
            break;
169
47.4k
        j = i + pos;
170
94.9k
        SPLIT_ADD(str, i, j);
171
94.9k
        i = j + sep_len;
172
94.9k
    }
173
71.2k
#if !STRINGLIB_MUTABLE
174
71.2k
    if (count == 0 && STRINGLIB_CHECK_EXACT(str_obj)) {
175
        /* No match in str_obj, so just use it as list[0] */
176
23.8k
        Py_INCREF(str_obj);
177
23.8k
        PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
178
23.8k
        count++;
179
23.8k
    } else
180
47.4k
#endif
181
47.4k
    {
182
94.9k
        SPLIT_ADD(str, i, str_len);
183
94.9k
    }
184
71.2k
    FIX_PREALLOC_SIZE(list);
185
71.2k
    return list;
186
187
0
  onError:
188
0
    Py_DECREF(list);
189
    return NULL;
190
71.2k
}
unicodeobject.c:ucs1lib_split
Line
Count
Source
149
1.22M
{
150
1.22M
    Py_ssize_t i, j, pos, count=0;
151
1.22M
    PyObject *list, *sub;
152
153
1.22M
    if (sep_len == 0) {
154
0
        PyErr_SetString(PyExc_ValueError, "empty separator");
155
0
        return NULL;
156
0
    }
157
1.22M
    else if (sep_len == 1)
158
1.20M
        return STRINGLIB(split_char)(str_obj, str, str_len, sep[0], maxcount);
159
160
12.0k
    list = PyList_New(PREALLOC_SIZE(maxcount));
161
12.0k
    if (list == NULL)
162
0
        return NULL;
163
164
12.0k
    i = j = 0;
165
22.8k
    while (maxcount-- > 0) {
166
12.0k
        pos = FASTSEARCH(str+i, str_len-i, sep, sep_len, -1, FAST_SEARCH);
167
12.0k
        if (pos < 0)
168
1.21k
            break;
169
10.8k
        j = i + pos;
170
21.6k
        SPLIT_ADD(str, i, j);
171
21.6k
        i = j + sep_len;
172
21.6k
    }
173
12.0k
#if !STRINGLIB_MUTABLE
174
12.0k
    if (count == 0 && STRINGLIB_CHECK_EXACT(str_obj)) {
175
        /* No match in str_obj, so just use it as list[0] */
176
1.21k
        Py_INCREF(str_obj);
177
1.21k
        PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
178
1.21k
        count++;
179
1.21k
    } else
180
10.8k
#endif
181
10.8k
    {
182
21.6k
        SPLIT_ADD(str, i, str_len);
183
21.6k
    }
184
12.0k
    FIX_PREALLOC_SIZE(list);
185
12.0k
    return list;
186
187
0
  onError:
188
0
    Py_DECREF(list);
189
    return NULL;
190
12.0k
}
unicodeobject.c:ucs2lib_split
Line
Count
Source
149
175k
{
150
175k
    Py_ssize_t i, j, pos, count=0;
151
175k
    PyObject *list, *sub;
152
153
175k
    if (sep_len == 0) {
154
0
        PyErr_SetString(PyExc_ValueError, "empty separator");
155
0
        return NULL;
156
0
    }
157
175k
    else if (sep_len == 1)
158
89.1k
        return STRINGLIB(split_char)(str_obj, str, str_len, sep[0], maxcount);
159
160
86.0k
    list = PyList_New(PREALLOC_SIZE(maxcount));
161
86.0k
    if (list == NULL)
162
0
        return NULL;
163
164
86.0k
    i = j = 0;
165
161k
    while (maxcount-- > 0) {
166
86.0k
        pos = FASTSEARCH(str+i, str_len-i, sep, sep_len, -1, FAST_SEARCH);
167
86.0k
        if (pos < 0)
168
10.6k
            break;
169
75.4k
        j = i + pos;
170
150k
        SPLIT_ADD(str, i, j);
171
150k
        i = j + sep_len;
172
150k
    }
173
86.0k
#if !STRINGLIB_MUTABLE
174
86.0k
    if (count == 0 && STRINGLIB_CHECK_EXACT(str_obj)) {
175
        /* No match in str_obj, so just use it as list[0] */
176
10.6k
        Py_INCREF(str_obj);
177
10.6k
        PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
178
10.6k
        count++;
179
10.6k
    } else
180
75.4k
#endif
181
75.4k
    {
182
150k
        SPLIT_ADD(str, i, str_len);
183
150k
    }
184
86.0k
    FIX_PREALLOC_SIZE(list);
185
86.0k
    return list;
186
187
0
  onError:
188
0
    Py_DECREF(list);
189
    return NULL;
190
86.0k
}
unicodeobject.c:ucs4lib_split
Line
Count
Source
149
41.4k
{
150
41.4k
    Py_ssize_t i, j, pos, count=0;
151
41.4k
    PyObject *list, *sub;
152
153
41.4k
    if (sep_len == 0) {
154
0
        PyErr_SetString(PyExc_ValueError, "empty separator");
155
0
        return NULL;
156
0
    }
157
41.4k
    else if (sep_len == 1)
158
17.0k
        return STRINGLIB(split_char)(str_obj, str, str_len, sep[0], maxcount);
159
160
24.3k
    list = PyList_New(PREALLOC_SIZE(maxcount));
161
24.3k
    if (list == NULL)
162
0
        return NULL;
163
164
24.3k
    i = j = 0;
165
44.7k
    while (maxcount-- > 0) {
166
24.3k
        pos = FASTSEARCH(str+i, str_len-i, sep, sep_len, -1, FAST_SEARCH);
167
24.3k
        if (pos < 0)
168
3.97k
            break;
169
20.4k
        j = i + pos;
170
40.8k
        SPLIT_ADD(str, i, j);
171
40.8k
        i = j + sep_len;
172
40.8k
    }
173
24.3k
#if !STRINGLIB_MUTABLE
174
24.3k
    if (count == 0 && STRINGLIB_CHECK_EXACT(str_obj)) {
175
        /* No match in str_obj, so just use it as list[0] */
176
3.97k
        Py_INCREF(str_obj);
177
3.97k
        PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
178
3.97k
        count++;
179
3.97k
    } else
180
20.4k
#endif
181
20.4k
    {
182
40.8k
        SPLIT_ADD(str, i, str_len);
183
40.8k
    }
184
24.3k
    FIX_PREALLOC_SIZE(list);
185
24.3k
    return list;
186
187
0
  onError:
188
0
    Py_DECREF(list);
189
    return NULL;
190
24.3k
}
Unexecuted instantiation: bytearrayobject.c:stringlib_split
191
192
Py_LOCAL_INLINE(PyObject *)
193
STRINGLIB(rsplit_whitespace)(PyObject* str_obj,
194
                            const STRINGLIB_CHAR* str, Py_ssize_t str_len,
195
                            Py_ssize_t maxcount)
196
0
{
197
0
    Py_ssize_t i, j, count=0;
198
0
    PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
199
0
    PyObject *sub;
200
201
0
    if (list == NULL)
202
0
        return NULL;
203
204
0
    i = j = str_len - 1;
205
0
    while (maxcount-- > 0) {
206
0
        while (i >= 0 && STRINGLIB_ISSPACE(str[i]))
207
0
            i--;
208
0
        if (i < 0) break;
209
0
        j = i; i--;
210
0
        while (i >= 0 && !STRINGLIB_ISSPACE(str[i]))
211
0
            i--;
212
#if !STRINGLIB_MUTABLE
213
0
        if (j == str_len - 1 && i < 0 && STRINGLIB_CHECK_EXACT(str_obj)) {
214
            /* No whitespace in str_obj, so just use it as list[0] */
215
0
            Py_INCREF(str_obj);
216
0
            PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
217
0
            count++;
218
0
            break;
219
0
        }
220
0
#endif
221
0
        SPLIT_ADD(str, i + 1, j + 1);
222
0
    }
223
224
0
    if (i >= 0) {
225
        /* Only occurs when maxcount was reached */
226
        /* Skip any remaining whitespace and copy to beginning of string */
227
0
        while (i >= 0 && STRINGLIB_ISSPACE(str[i]))
228
0
            i--;
229
0
        if (i >= 0)
230
0
            SPLIT_ADD(str, 0, i + 1);
231
0
    }
232
0
    FIX_PREALLOC_SIZE(list);
233
0
    if (PyList_Reverse(list) < 0)
234
0
        goto onError;
235
0
    return list;
236
237
0
  onError:
238
0
    Py_DECREF(list);
239
0
    return NULL;
240
0
}
Unexecuted instantiation: bytesobject.c:stringlib_rsplit_whitespace
Unexecuted instantiation: unicodeobject.c:asciilib_rsplit_whitespace
Unexecuted instantiation: unicodeobject.c:ucs1lib_rsplit_whitespace
Unexecuted instantiation: unicodeobject.c:ucs2lib_rsplit_whitespace
Unexecuted instantiation: unicodeobject.c:ucs4lib_rsplit_whitespace
Unexecuted instantiation: bytearrayobject.c:stringlib_rsplit_whitespace
241
242
Py_LOCAL_INLINE(PyObject *)
243
STRINGLIB(rsplit_char)(PyObject* str_obj,
244
                      const STRINGLIB_CHAR* str, Py_ssize_t str_len,
245
                      const STRINGLIB_CHAR ch,
246
                      Py_ssize_t maxcount)
247
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
14.0k
{
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
14.0k
    Py_ssize_t i;
349
14.0k
    Py_ssize_t j;
350
14.0k
    PyObject *list = PyList_New(0);
351
14.0k
    PyObject *sub;
352
353
14.0k
    if (list == NULL)
354
0
        return NULL;
355
356
32.3M
    for (i = j = 0; i < str_len; ) {
357
32.2M
        Py_ssize_t eol;
358
359
        /* Find a line and append it */
360
260M
        while (i < str_len && !STRINGLIB_ISLINEBREAK(str[i]))
361
227M
            i++;
362
363
        /* Skip the line break reading CRLF as one line break */
364
32.2M
        eol = i;
365
32.2M
        if (i < str_len) {
366
32.2M
            if (str[i] == '\r' && i + 1 < str_len && str[i+1] == '\n')
367
71.3k
                i += 2;
368
32.2M
            else
369
32.2M
                i++;
370
32.2M
            if (keepends)
371
0
                eol = i;
372
32.2M
        }
373
#if !STRINGLIB_MUTABLE
374
32.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
5.44k
            if (PyList_Append(list, str_obj))
377
0
                goto onError;
378
5.44k
            break;
379
5.44k
        }
380
32.2M
#endif
381
64.5M
        SPLIT_APPEND(str, j, eol);
382
32.2M
        j = i;
383
32.2M
    }
384
14.0k
    return list;
385
386
0
  onError:
387
0
    Py_DECREF(list);
388
    return NULL;
389
14.0k
}
Unexecuted instantiation: bytesobject.c:stringlib_splitlines
unicodeobject.c:asciilib_splitlines
Line
Count
Source
339
2.72k
{
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.72k
    Py_ssize_t i;
349
2.72k
    Py_ssize_t j;
350
2.72k
    PyObject *list = PyList_New(0);
351
2.72k
    PyObject *sub;
352
353
2.72k
    if (list == NULL)
354
0
        return NULL;
355
356
8.40M
    for (i = j = 0; i < str_len; ) {
357
8.40M
        Py_ssize_t eol;
358
359
        /* Find a line and append it */
360
40.0M
        while (i < str_len && !STRINGLIB_ISLINEBREAK(str[i]))
361
31.6M
            i++;
362
363
        /* Skip the line break reading CRLF as one line break */
364
8.40M
        eol = i;
365
8.40M
        if (i < str_len) {
366
8.39M
            if (str[i] == '\r' && i + 1 < str_len && str[i+1] == '\n')
367
11.3k
                i += 2;
368
8.38M
            else
369
8.38M
                i++;
370
8.39M
            if (keepends)
371
0
                eol = i;
372
8.39M
        }
373
8.40M
#if !STRINGLIB_MUTABLE
374
8.40M
        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
974
            if (PyList_Append(list, str_obj))
377
0
                goto onError;
378
974
            break;
379
974
        }
380
8.39M
#endif
381
16.7M
        SPLIT_APPEND(str, j, eol);
382
8.39M
        j = i;
383
8.39M
    }
384
2.72k
    return list;
385
386
0
  onError:
387
0
    Py_DECREF(list);
388
    return NULL;
389
2.72k
}
unicodeobject.c:ucs1lib_splitlines
Line
Count
Source
339
856
{
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
856
    Py_ssize_t i;
349
856
    Py_ssize_t j;
350
856
    PyObject *list = PyList_New(0);
351
856
    PyObject *sub;
352
353
856
    if (list == NULL)
354
0
        return NULL;
355
356
1.79M
    for (i = j = 0; i < str_len; ) {
357
1.79M
        Py_ssize_t eol;
358
359
        /* Find a line and append it */
360
10.7M
        while (i < str_len && !STRINGLIB_ISLINEBREAK(str[i]))
361
8.93M
            i++;
362
363
        /* Skip the line break reading CRLF as one line break */
364
1.79M
        eol = i;
365
1.79M
        if (i < str_len) {
366
1.79M
            if (str[i] == '\r' && i + 1 < str_len && str[i+1] == '\n')
367
1.87k
                i += 2;
368
1.79M
            else
369
1.79M
                i++;
370
1.79M
            if (keepends)
371
0
                eol = i;
372
1.79M
        }
373
1.79M
#if !STRINGLIB_MUTABLE
374
1.79M
        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
205
            if (PyList_Append(list, str_obj))
377
0
                goto onError;
378
205
            break;
379
205
        }
380
1.79M
#endif
381
3.59M
        SPLIT_APPEND(str, j, eol);
382
1.79M
        j = i;
383
1.79M
    }
384
856
    return list;
385
386
0
  onError:
387
0
    Py_DECREF(list);
388
    return NULL;
389
856
}
unicodeobject.c:ucs2lib_splitlines
Line
Count
Source
339
7.36k
{
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.36k
    Py_ssize_t i;
349
7.36k
    Py_ssize_t j;
350
7.36k
    PyObject *list = PyList_New(0);
351
7.36k
    PyObject *sub;
352
353
7.36k
    if (list == NULL)
354
0
        return NULL;
355
356
11.0M
    for (i = j = 0; i < str_len; ) {
357
11.0M
        Py_ssize_t eol;
358
359
        /* Find a line and append it */
360
98.3M
        while (i < str_len && !STRINGLIB_ISLINEBREAK(str[i]))
361
87.3M
            i++;
362
363
        /* Skip the line break reading CRLF as one line break */
364
11.0M
        eol = i;
365
11.0M
        if (i < str_len) {
366
11.0M
            if (str[i] == '\r' && i + 1 < str_len && str[i+1] == '\n')
367
11.2k
                i += 2;
368
10.9M
            else
369
10.9M
                i++;
370
11.0M
            if (keepends)
371
0
                eol = i;
372
11.0M
        }
373
11.0M
#if !STRINGLIB_MUTABLE
374
11.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
3.08k
            if (PyList_Append(list, str_obj))
377
0
                goto onError;
378
3.08k
            break;
379
3.08k
        }
380
11.0M
#endif
381
22.0M
        SPLIT_APPEND(str, j, eol);
382
11.0M
        j = i;
383
11.0M
    }
384
7.36k
    return list;
385
386
0
  onError:
387
0
    Py_DECREF(list);
388
    return NULL;
389
7.36k
}
unicodeobject.c:ucs4lib_splitlines
Line
Count
Source
339
3.09k
{
340
    /* This does not use the preallocated list because splitlines is
341
       usually run with hundreds of newlines.  The overhead of
342
       switching between PyList_SET_ITEM and append causes about a
343
       2-3% slowdown for that common case.  A smarter implementation
344
       could move the if check out, so the SET_ITEMs are done first
345
       and the appends only done when the prealloc buffer is full.
346
       That's too much work for little gain.*/
347
348
3.09k
    Py_ssize_t i;
349
3.09k
    Py_ssize_t j;
350
3.09k
    PyObject *list = PyList_New(0);
351
3.09k
    PyObject *sub;
352
353
3.09k
    if (list == NULL)
354
0
        return NULL;
355
356
11.0M
    for (i = j = 0; i < str_len; ) {
357
11.0M
        Py_ssize_t eol;
358
359
        /* Find a line and append it */
360
110M
        while (i < str_len && !STRINGLIB_ISLINEBREAK(str[i]))
361
99.8M
            i++;
362
363
        /* Skip the line break reading CRLF as one line break */
364
11.0M
        eol = i;
365
11.0M
        if (i < str_len) {
366
11.0M
            if (str[i] == '\r' && i + 1 < str_len && str[i+1] == '\n')
367
46.9k
                i += 2;
368
11.0M
            else
369
11.0M
                i++;
370
11.0M
            if (keepends)
371
0
                eol = i;
372
11.0M
        }
373
11.0M
#if !STRINGLIB_MUTABLE
374
11.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
1.17k
            if (PyList_Append(list, str_obj))
377
0
                goto onError;
378
1.17k
            break;
379
1.17k
        }
380
11.0M
#endif
381
22.1M
        SPLIT_APPEND(str, j, eol);
382
11.0M
        j = i;
383
11.0M
    }
384
3.09k
    return list;
385
386
0
  onError:
387
0
    Py_DECREF(list);
388
    return NULL;
389
3.09k
}
Unexecuted instantiation: bytearrayobject.c:stringlib_splitlines
390