Coverage Report

Created: 2025-07-11 06:24

/src/cpython/Objects/stringlib/split.h
Line
Count
Source (jump to first uncovered line)
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
86.5M
#define MAX_PREALLOC 12
15
16
/* 5 splits gives 6 elements */
17
#define PREALLOC_SIZE(maxsplit) \
18
16.8M
    (maxsplit >= MAX_PREALLOC ? MAX_PREALLOC : maxsplit+1)
19
20
#define SPLIT_APPEND(data, left, right)         \
21
31.3M
    sub = STRINGLIB_NEW((data) + (left),        \
22
31.3M
                        (right) - (left));      \
23
31.3M
    if (sub == NULL)                            \
24
31.3M
        goto onError;                           \
25
31.3M
    if (PyList_Append(list, sub)) {             \
26
0
        Py_DECREF(sub);                         \
27
0
        goto onError;                           \
28
0
    }                                           \
29
31.3M
    else                                        \
30
31.3M
        Py_DECREF(sub);
31
32
50.7M
#define SPLIT_ADD(data, left, right) {          \
33
50.7M
    sub = STRINGLIB_NEW((data) + (left),        \
34
50.7M
                        (right) - (left));      \
35
50.7M
    if (sub == NULL)                            \
36
50.7M
        goto onError;                           \
37
50.7M
    if (count < MAX_PREALLOC) {                 \
38
26.1M
        PyList_SET_ITEM(list, count, sub);      \
39
26.1M
    } else {                                    \
40
24.6M
        if (PyList_Append(list, sub)) {         \
41
0
            Py_DECREF(sub);                     \
42
0
            goto onError;                       \
43
0
        }                                       \
44
24.6M
        else                                    \
45
24.6M
            Py_DECREF(sub);                     \
46
24.6M
    }                                           \
47
50.7M
    count++; }
48
49
50
/* Always force the list to the expected size. */
51
16.8M
#define FIX_PREALLOC_SIZE(list) Py_SET_SIZE(list, count)
52
53
Py_LOCAL_INLINE(PyObject *)
54
STRINGLIB(split_whitespace)(PyObject* str_obj,
55
                           const STRINGLIB_CHAR* str, Py_ssize_t str_len,
56
                           Py_ssize_t maxcount)
57
160k
{
58
160k
    Py_ssize_t i, j, count=0;
59
160k
    PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
60
160k
    PyObject *sub;
61
62
160k
    if (list == NULL)
63
0
        return NULL;
64
65
160k
    i = j = 0;
66
2.17M
    while (maxcount-- > 0) {
67
4.21M
        while (i < str_len && STRINGLIB_ISSPACE(str[i]))
68
2.06M
            i++;
69
2.14M
        if (i == str_len) break;
70
2.06M
        j = i; i++;
71
121M
        while (i < str_len && !STRINGLIB_ISSPACE(str[i]))
72
119M
            i++;
73
#if !STRINGLIB_MUTABLE
74
2.06M
        if (j == 0 && i == str_len && STRINGLIB_CHECK_EXACT(str_obj)) {
75
            /* No whitespace in str_obj, so just use it as list[0] */
76
46.5k
            Py_INCREF(str_obj);
77
46.5k
            PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
78
46.5k
            count++;
79
46.5k
            break;
80
46.5k
        }
81
2.01M
#endif
82
6.04M
        SPLIT_ADD(str, j, i);
83
6.04M
    }
84
85
160k
    if (i < str_len) {
86
        /* Only occurs when maxcount was reached */
87
        /* Skip any remaining whitespace and copy to end of string */
88
52.0k
        while (i < str_len && STRINGLIB_ISSPACE(str[i]))
89
28.1k
            i++;
90
23.8k
        if (i != str_len)
91
23.8k
            SPLIT_ADD(str, i, str_len);
92
23.8k
    }
93
160k
    FIX_PREALLOC_SIZE(list);
94
160k
    return list;
95
96
0
  onError:
97
0
    Py_DECREF(list);
98
0
    return NULL;
99
160k
}
Unexecuted instantiation: bytesobject.c:stringlib_split_whitespace
unicodeobject.c:asciilib_split_whitespace
Line
Count
Source
57
80.4k
{
58
80.4k
    Py_ssize_t i, j, count=0;
59
80.4k
    PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
60
80.4k
    PyObject *sub;
61
62
80.4k
    if (list == NULL)
63
0
        return NULL;
64
65
80.4k
    i = j = 0;
66
655k
    while (maxcount-- > 0) {
67
1.17M
        while (i < str_len && STRINGLIB_ISSPACE(str[i]))
68
537k
            i++;
69
640k
        if (i == str_len) break;
70
602k
        j = i; i++;
71
43.4M
        while (i < str_len && !STRINGLIB_ISSPACE(str[i]))
72
42.8M
            i++;
73
602k
#if !STRINGLIB_MUTABLE
74
602k
        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.9k
            Py_INCREF(str_obj);
77
27.9k
            PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
78
27.9k
            count++;
79
27.9k
            break;
80
27.9k
        }
81
574k
#endif
82
1.72M
        SPLIT_ADD(str, j, i);
83
1.72M
    }
84
85
80.4k
    if (i < str_len) {
86
        /* Only occurs when maxcount was reached */
87
        /* Skip any remaining whitespace and copy to end of string */
88
22.0k
        while (i < str_len && STRINGLIB_ISSPACE(str[i]))
89
11.3k
            i++;
90
10.6k
        if (i != str_len)
91
10.6k
            SPLIT_ADD(str, i, str_len);
92
10.6k
    }
93
80.4k
    FIX_PREALLOC_SIZE(list);
94
80.4k
    return list;
95
96
0
  onError:
97
0
    Py_DECREF(list);
98
0
    return NULL;
99
80.4k
}
unicodeobject.c:ucs1lib_split_whitespace
Line
Count
Source
57
28.6k
{
58
28.6k
    Py_ssize_t i, j, count=0;
59
28.6k
    PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
60
28.6k
    PyObject *sub;
61
62
28.6k
    if (list == NULL)
63
0
        return NULL;
64
65
28.6k
    i = j = 0;
66
570k
    while (maxcount-- > 0) {
67
1.08M
        while (i < str_len && STRINGLIB_ISSPACE(str[i]))
68
526k
            i++;
69
557k
        if (i == str_len) break;
70
547k
        j = i; i++;
71
23.4M
        while (i < str_len && !STRINGLIB_ISSPACE(str[i]))
72
22.9M
            i++;
73
547k
#if !STRINGLIB_MUTABLE
74
547k
        if (j == 0 && i == str_len && STRINGLIB_CHECK_EXACT(str_obj)) {
75
            /* No whitespace in str_obj, so just use it as list[0] */
76
4.79k
            Py_INCREF(str_obj);
77
4.79k
            PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
78
4.79k
            count++;
79
4.79k
            break;
80
4.79k
        }
81
542k
#endif
82
1.62M
        SPLIT_ADD(str, j, i);
83
1.62M
    }
84
85
28.6k
    if (i < str_len) {
86
        /* Only occurs when maxcount was reached */
87
        /* Skip any remaining whitespace and copy to end of string */
88
28.9k
        while (i < str_len && STRINGLIB_ISSPACE(str[i]))
89
16.2k
            i++;
90
12.6k
        if (i != str_len)
91
12.6k
            SPLIT_ADD(str, i, str_len);
92
12.6k
    }
93
28.6k
    FIX_PREALLOC_SIZE(list);
94
28.6k
    return list;
95
96
0
  onError:
97
0
    Py_DECREF(list);
98
0
    return NULL;
99
28.6k
}
unicodeobject.c:ucs2lib_split_whitespace
Line
Count
Source
57
40.3k
{
58
40.3k
    Py_ssize_t i, j, count=0;
59
40.3k
    PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
60
40.3k
    PyObject *sub;
61
62
40.3k
    if (list == NULL)
63
0
        return NULL;
64
65
40.3k
    i = j = 0;
66
621k
    while (maxcount-- > 0) {
67
1.29M
        while (i < str_len && STRINGLIB_ISSPACE(str[i]))
68
675k
            i++;
69
620k
        if (i == str_len) break;
70
592k
        j = i; i++;
71
34.8M
        while (i < str_len && !STRINGLIB_ISSPACE(str[i]))
72
34.2M
            i++;
73
592k
#if !STRINGLIB_MUTABLE
74
592k
        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
11.7k
            Py_INCREF(str_obj);
77
11.7k
            PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
78
11.7k
            count++;
79
11.7k
            break;
80
11.7k
        }
81
580k
#endif
82
1.74M
        SPLIT_ADD(str, j, i);
83
1.74M
    }
84
85
40.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.09k
        while (i < str_len && STRINGLIB_ISSPACE(str[i]))
89
548
            i++;
90
548
        if (i != str_len)
91
548
            SPLIT_ADD(str, i, str_len);
92
548
    }
93
40.3k
    FIX_PREALLOC_SIZE(list);
94
40.3k
    return list;
95
96
0
  onError:
97
0
    Py_DECREF(list);
98
0
    return NULL;
99
40.3k
}
unicodeobject.c:ucs4lib_split_whitespace
Line
Count
Source
57
11.3k
{
58
11.3k
    Py_ssize_t i, j, count=0;
59
11.3k
    PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
60
11.3k
    PyObject *sub;
61
62
11.3k
    if (list == NULL)
63
0
        return NULL;
64
65
11.3k
    i = j = 0;
66
329k
    while (maxcount-- > 0) {
67
655k
        while (i < str_len && STRINGLIB_ISSPACE(str[i]))
68
325k
            i++;
69
329k
        if (i == str_len) break;
70
320k
        j = i; i++;
71
19.8M
        while (i < str_len && !STRINGLIB_ISSPACE(str[i]))
72
19.5M
            i++;
73
320k
#if !STRINGLIB_MUTABLE
74
320k
        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.03k
            Py_INCREF(str_obj);
77
2.03k
            PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
78
2.03k
            count++;
79
2.03k
            break;
80
2.03k
        }
81
318k
#endif
82
955k
        SPLIT_ADD(str, j, i);
83
955k
    }
84
85
11.3k
    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.3k
    FIX_PREALLOC_SIZE(list);
94
11.3k
    return list;
95
96
0
  onError:
97
0
    Py_DECREF(list);
98
0
    return NULL;
99
11.3k
}
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
16.4M
{
107
16.4M
    Py_ssize_t i, j, count=0;
108
16.4M
    PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
109
16.4M
    PyObject *sub;
110
111
16.4M
    if (list == NULL)
112
0
        return NULL;
113
114
16.4M
    i = j = 0;
115
67.6M
    while ((j < str_len) && (maxcount-- > 0)) {
116
343M
        for(; j < str_len; j++) {
117
            /* I found that using memchr makes no difference */
118
328M
            if (str[j] == ch) {
119
36.2M
                SPLIT_ADD(str, i, j);
120
36.2M
                i = j = j + 1;
121
36.2M
                break;
122
36.2M
            }
123
328M
        }
124
51.1M
    }
125
#if !STRINGLIB_MUTABLE
126
16.4M
    if (count == 0 && STRINGLIB_CHECK_EXACT(str_obj)) {
127
        /* ch not in str_obj, so just use str_obj as list[0] */
128
4.24M
        Py_INCREF(str_obj);
129
4.24M
        PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
130
4.24M
        count++;
131
4.24M
    } else
132
12.2M
#endif
133
12.2M
    if (i <= str_len) {
134
24.4M
        SPLIT_ADD(str, i, str_len);
135
24.4M
    }
136
16.4M
    FIX_PREALLOC_SIZE(list);
137
16.4M
    return list;
138
139
0
  onError:
140
0
    Py_DECREF(list);
141
0
    return NULL;
142
0
}
bytesobject.c:stringlib_split_char
Line
Count
Source
106
1.89M
{
107
1.89M
    Py_ssize_t i, j, count=0;
108
1.89M
    PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
109
1.89M
    PyObject *sub;
110
111
1.89M
    if (list == NULL)
112
0
        return NULL;
113
114
1.89M
    i = j = 0;
115
8.71M
    while ((j < str_len) && (maxcount-- > 0)) {
116
44.7M
        for(; j < str_len; j++) {
117
            /* I found that using memchr makes no difference */
118
43.1M
            if (str[j] == ch) {
119
5.23M
                SPLIT_ADD(str, i, j);
120
5.23M
                i = j = j + 1;
121
5.23M
                break;
122
5.23M
            }
123
43.1M
        }
124
6.82M
    }
125
1.89M
#if !STRINGLIB_MUTABLE
126
1.89M
    if (count == 0 && STRINGLIB_CHECK_EXACT(str_obj)) {
127
        /* ch not in str_obj, so just use str_obj as list[0] */
128
1.50M
        Py_INCREF(str_obj);
129
1.50M
        PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
130
1.50M
        count++;
131
1.50M
    } else
132
383k
#endif
133
383k
    if (i <= str_len) {
134
766k
        SPLIT_ADD(str, i, str_len);
135
766k
    }
136
1.89M
    FIX_PREALLOC_SIZE(list);
137
1.89M
    return list;
138
139
0
  onError:
140
0
    Py_DECREF(list);
141
0
    return NULL;
142
1.89M
}
unicodeobject.c:asciilib_split_char
Line
Count
Source
106
13.6M
{
107
13.6M
    Py_ssize_t i, j, count=0;
108
13.6M
    PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
109
13.6M
    PyObject *sub;
110
111
13.6M
    if (list == NULL)
112
0
        return NULL;
113
114
13.6M
    i = j = 0;
115
44.4M
    while ((j < str_len) && (maxcount-- > 0)) {
116
184M
        for(; j < str_len; j++) {
117
            /* I found that using memchr makes no difference */
118
171M
            if (str[j] == ch) {
119
17.5M
                SPLIT_ADD(str, i, j);
120
17.5M
                i = j = j + 1;
121
17.5M
                break;
122
17.5M
            }
123
171M
        }
124
30.7M
    }
125
13.6M
#if !STRINGLIB_MUTABLE
126
13.6M
    if (count == 0 && STRINGLIB_CHECK_EXACT(str_obj)) {
127
        /* ch not in str_obj, so just use str_obj as list[0] */
128
2.62M
        Py_INCREF(str_obj);
129
2.62M
        PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
130
2.62M
        count++;
131
2.62M
    } else
132
11.0M
#endif
133
11.0M
    if (i <= str_len) {
134
22.1M
        SPLIT_ADD(str, i, str_len);
135
22.1M
    }
136
13.6M
    FIX_PREALLOC_SIZE(list);
137
13.6M
    return list;
138
139
0
  onError:
140
0
    Py_DECREF(list);
141
0
    return NULL;
142
13.6M
}
unicodeobject.c:ucs1lib_split_char
Line
Count
Source
106
773k
{
107
773k
    Py_ssize_t i, j, count=0;
108
773k
    PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
109
773k
    PyObject *sub;
110
111
773k
    if (list == NULL)
112
0
        return NULL;
113
114
773k
    i = j = 0;
115
8.25M
    while ((j < str_len) && (maxcount-- > 0)) {
116
39.1M
        for(; j < str_len; j++) {
117
            /* I found that using memchr makes no difference */
118
39.0M
            if (str[j] == ch) {
119
7.35M
                SPLIT_ADD(str, i, j);
120
7.35M
                i = j = j + 1;
121
7.35M
                break;
122
7.35M
            }
123
39.0M
        }
124
7.48M
    }
125
773k
#if !STRINGLIB_MUTABLE
126
773k
    if (count == 0 && STRINGLIB_CHECK_EXACT(str_obj)) {
127
        /* ch not in str_obj, so just use str_obj as list[0] */
128
105k
        Py_INCREF(str_obj);
129
105k
        PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
130
105k
        count++;
131
105k
    } else
132
667k
#endif
133
667k
    if (i <= str_len) {
134
1.33M
        SPLIT_ADD(str, i, str_len);
135
1.33M
    }
136
773k
    FIX_PREALLOC_SIZE(list);
137
773k
    return list;
138
139
0
  onError:
140
0
    Py_DECREF(list);
141
0
    return NULL;
142
773k
}
unicodeobject.c:ucs2lib_split_char
Line
Count
Source
106
81.6k
{
107
81.6k
    Py_ssize_t i, j, count=0;
108
81.6k
    PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
109
81.6k
    PyObject *sub;
110
111
81.6k
    if (list == NULL)
112
0
        return NULL;
113
114
81.6k
    i = j = 0;
115
2.94M
    while ((j < str_len) && (maxcount-- > 0)) {
116
41.8M
        for(; j < str_len; j++) {
117
            /* I found that using memchr makes no difference */
118
41.8M
            if (str[j] == ch) {
119
2.81M
                SPLIT_ADD(str, i, j);
120
2.81M
                i = j = j + 1;
121
2.81M
                break;
122
2.81M
            }
123
41.8M
        }
124
2.86M
    }
125
81.6k
#if !STRINGLIB_MUTABLE
126
81.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.65k
        Py_INCREF(str_obj);
129
7.65k
        PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
130
7.65k
        count++;
131
7.65k
    } else
132
73.9k
#endif
133
73.9k
    if (i <= str_len) {
134
147k
        SPLIT_ADD(str, i, str_len);
135
147k
    }
136
81.6k
    FIX_PREALLOC_SIZE(list);
137
81.6k
    return list;
138
139
0
  onError:
140
0
    Py_DECREF(list);
141
0
    return NULL;
142
81.6k
}
unicodeobject.c:ucs4lib_split_char
Line
Count
Source
106
16.0k
{
107
16.0k
    Py_ssize_t i, j, count=0;
108
16.0k
    PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
109
16.0k
    PyObject *sub;
110
111
16.0k
    if (list == NULL)
112
0
        return NULL;
113
114
16.0k
    i = j = 0;
115
3.26M
    while ((j < str_len) && (maxcount-- > 0)) {
116
33.1M
        for(; j < str_len; j++) {
117
            /* I found that using memchr makes no difference */
118
33.1M
            if (str[j] == ch) {
119
3.23M
                SPLIT_ADD(str, i, j);
120
3.23M
                i = j = j + 1;
121
3.23M
                break;
122
3.23M
            }
123
33.1M
        }
124
3.24M
    }
125
16.0k
#if !STRINGLIB_MUTABLE
126
16.0k
    if (count == 0 && STRINGLIB_CHECK_EXACT(str_obj)) {
127
        /* ch not in str_obj, so just use str_obj as list[0] */
128
423
        Py_INCREF(str_obj);
129
423
        PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
130
423
        count++;
131
423
    } else
132
15.5k
#endif
133
15.5k
    if (i <= str_len) {
134
31.1k
        SPLIT_ADD(str, i, str_len);
135
31.1k
    }
136
16.0k
    FIX_PREALLOC_SIZE(list);
137
16.0k
    return list;
138
139
0
  onError:
140
0
    Py_DECREF(list);
141
0
    return NULL;
142
16.0k
}
Unexecuted instantiation: bytearrayobject.c:stringlib_split_char
143
144
Py_LOCAL_INLINE(PyObject *)
145
STRINGLIB(split)(PyObject* str_obj,
146
                const STRINGLIB_CHAR* str, Py_ssize_t str_len,
147
                const STRINGLIB_CHAR* sep, Py_ssize_t sep_len,
148
                Py_ssize_t maxcount)
149
16.6M
{
150
16.6M
    Py_ssize_t i, j, pos, count=0;
151
16.6M
    PyObject *list, *sub;
152
153
16.6M
    if (sep_len == 0) {
154
0
        PyErr_SetString(PyExc_ValueError, "empty separator");
155
0
        return NULL;
156
0
    }
157
16.6M
    else if (sep_len == 1)
158
16.4M
        return STRINGLIB(split_char)(str_obj, str, str_len, sep[0], maxcount);
159
160
199k
    list = PyList_New(PREALLOC_SIZE(maxcount));
161
199k
    if (list == NULL)
162
0
        return NULL;
163
164
199k
    i = j = 0;
165
354k
    while (maxcount-- > 0) {
166
199k
        pos = FASTSEARCH(str+i, str_len-i, sep, sep_len, -1, FAST_SEARCH);
167
199k
        if (pos < 0)
168
45.3k
            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
199k
    if (count == 0 && STRINGLIB_CHECK_EXACT(str_obj)) {
175
        /* No match in str_obj, so just use it as list[0] */
176
45.3k
        Py_INCREF(str_obj);
177
45.3k
        PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
178
45.3k
        count++;
179
45.3k
    } else
180
154k
#endif
181
154k
    {
182
308k
        SPLIT_ADD(str, i, str_len);
183
308k
    }
184
199k
    FIX_PREALLOC_SIZE(list);
185
199k
    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
1.89M
{
150
1.89M
    Py_ssize_t i, j, pos, count=0;
151
1.89M
    PyObject *list, *sub;
152
153
1.89M
    if (sep_len == 0) {
154
0
        PyErr_SetString(PyExc_ValueError, "empty separator");
155
0
        return NULL;
156
0
    }
157
1.89M
    else if (sep_len == 1)
158
1.89M
        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
0
    return NULL;
190
0
}
unicodeobject.c:asciilib_split
Line
Count
Source
149
13.7M
{
150
13.7M
    Py_ssize_t i, j, pos, count=0;
151
13.7M
    PyObject *list, *sub;
152
153
13.7M
    if (sep_len == 0) {
154
0
        PyErr_SetString(PyExc_ValueError, "empty separator");
155
0
        return NULL;
156
0
    }
157
13.7M
    else if (sep_len == 1)
158
13.6M
        return STRINGLIB(split_char)(str_obj, str, str_len, sep[0], maxcount);
159
160
92.7k
    list = PyList_New(PREALLOC_SIZE(maxcount));
161
92.7k
    if (list == NULL)
162
0
        return NULL;
163
164
92.7k
    i = j = 0;
165
150k
    while (maxcount-- > 0) {
166
92.7k
        pos = FASTSEARCH(str+i, str_len-i, sep, sep_len, -1, FAST_SEARCH);
167
92.7k
        if (pos < 0)
168
34.8k
            break;
169
57.9k
        j = i + pos;
170
115k
        SPLIT_ADD(str, i, j);
171
115k
        i = j + sep_len;
172
115k
    }
173
92.7k
#if !STRINGLIB_MUTABLE
174
92.7k
    if (count == 0 && STRINGLIB_CHECK_EXACT(str_obj)) {
175
        /* No match in str_obj, so just use it as list[0] */
176
34.8k
        Py_INCREF(str_obj);
177
34.8k
        PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
178
34.8k
        count++;
179
34.8k
    } else
180
57.9k
#endif
181
57.9k
    {
182
115k
        SPLIT_ADD(str, i, str_len);
183
115k
    }
184
92.7k
    FIX_PREALLOC_SIZE(list);
185
92.7k
    return list;
186
187
0
  onError:
188
0
    Py_DECREF(list);
189
0
    return NULL;
190
92.7k
}
unicodeobject.c:ucs1lib_split
Line
Count
Source
149
786k
{
150
786k
    Py_ssize_t i, j, pos, count=0;
151
786k
    PyObject *list, *sub;
152
153
786k
    if (sep_len == 0) {
154
0
        PyErr_SetString(PyExc_ValueError, "empty separator");
155
0
        return NULL;
156
0
    }
157
786k
    else if (sep_len == 1)
158
773k
        return STRINGLIB(split_char)(str_obj, str, str_len, sep[0], maxcount);
159
160
13.2k
    list = PyList_New(PREALLOC_SIZE(maxcount));
161
13.2k
    if (list == NULL)
162
0
        return NULL;
163
164
13.2k
    i = j = 0;
165
25.3k
    while (maxcount-- > 0) {
166
13.2k
        pos = FASTSEARCH(str+i, str_len-i, sep, sep_len, -1, FAST_SEARCH);
167
13.2k
        if (pos < 0)
168
1.26k
            break;
169
12.0k
        j = i + pos;
170
24.0k
        SPLIT_ADD(str, i, j);
171
24.0k
        i = j + sep_len;
172
24.0k
    }
173
13.2k
#if !STRINGLIB_MUTABLE
174
13.2k
    if (count == 0 && STRINGLIB_CHECK_EXACT(str_obj)) {
175
        /* No match in str_obj, so just use it as list[0] */
176
1.26k
        Py_INCREF(str_obj);
177
1.26k
        PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
178
1.26k
        count++;
179
1.26k
    } else
180
12.0k
#endif
181
12.0k
    {
182
24.0k
        SPLIT_ADD(str, i, str_len);
183
24.0k
    }
184
13.2k
    FIX_PREALLOC_SIZE(list);
185
13.2k
    return list;
186
187
0
  onError:
188
0
    Py_DECREF(list);
189
0
    return NULL;
190
13.2k
}
unicodeobject.c:ucs2lib_split
Line
Count
Source
149
156k
{
150
156k
    Py_ssize_t i, j, pos, count=0;
151
156k
    PyObject *list, *sub;
152
153
156k
    if (sep_len == 0) {
154
0
        PyErr_SetString(PyExc_ValueError, "empty separator");
155
0
        return NULL;
156
0
    }
157
156k
    else if (sep_len == 1)
158
81.6k
        return STRINGLIB(split_char)(str_obj, str, str_len, sep[0], maxcount);
159
160
75.1k
    list = PyList_New(PREALLOC_SIZE(maxcount));
161
75.1k
    if (list == NULL)
162
0
        return NULL;
163
164
75.1k
    i = j = 0;
165
143k
    while (maxcount-- > 0) {
166
75.1k
        pos = FASTSEARCH(str+i, str_len-i, sep, sep_len, -1, FAST_SEARCH);
167
75.1k
        if (pos < 0)
168
6.68k
            break;
169
68.4k
        j = i + pos;
170
136k
        SPLIT_ADD(str, i, j);
171
136k
        i = j + sep_len;
172
136k
    }
173
75.1k
#if !STRINGLIB_MUTABLE
174
75.1k
    if (count == 0 && STRINGLIB_CHECK_EXACT(str_obj)) {
175
        /* No match in str_obj, so just use it as list[0] */
176
6.68k
        Py_INCREF(str_obj);
177
6.68k
        PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
178
6.68k
        count++;
179
6.68k
    } else
180
68.4k
#endif
181
68.4k
    {
182
136k
        SPLIT_ADD(str, i, str_len);
183
136k
    }
184
75.1k
    FIX_PREALLOC_SIZE(list);
185
75.1k
    return list;
186
187
0
  onError:
188
0
    Py_DECREF(list);
189
0
    return NULL;
190
75.1k
}
unicodeobject.c:ucs4lib_split
Line
Count
Source
149
34.6k
{
150
34.6k
    Py_ssize_t i, j, pos, count=0;
151
34.6k
    PyObject *list, *sub;
152
153
34.6k
    if (sep_len == 0) {
154
0
        PyErr_SetString(PyExc_ValueError, "empty separator");
155
0
        return NULL;
156
0
    }
157
34.6k
    else if (sep_len == 1)
158
16.0k
        return STRINGLIB(split_char)(str_obj, str, str_len, sep[0], maxcount);
159
160
18.6k
    list = PyList_New(PREALLOC_SIZE(maxcount));
161
18.6k
    if (list == NULL)
162
0
        return NULL;
163
164
18.6k
    i = j = 0;
165
34.6k
    while (maxcount-- > 0) {
166
18.6k
        pos = FASTSEARCH(str+i, str_len-i, sep, sep_len, -1, FAST_SEARCH);
167
18.6k
        if (pos < 0)
168
2.63k
            break;
169
16.0k
        j = i + pos;
170
32.0k
        SPLIT_ADD(str, i, j);
171
32.0k
        i = j + sep_len;
172
32.0k
    }
173
18.6k
#if !STRINGLIB_MUTABLE
174
18.6k
    if (count == 0 && STRINGLIB_CHECK_EXACT(str_obj)) {
175
        /* No match in str_obj, so just use it as list[0] */
176
2.63k
        Py_INCREF(str_obj);
177
2.63k
        PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
178
2.63k
        count++;
179
2.63k
    } else
180
16.0k
#endif
181
16.0k
    {
182
32.0k
        SPLIT_ADD(str, i, str_len);
183
32.0k
    }
184
18.6k
    FIX_PREALLOC_SIZE(list);
185
18.6k
    return list;
186
187
0
  onError:
188
0
    Py_DECREF(list);
189
0
    return NULL;
190
18.6k
}
Unexecuted instantiation: bytearrayobject.c:stringlib_split
191
192
Py_LOCAL_INLINE(PyObject *)
193
STRINGLIB(rsplit_whitespace)(PyObject* str_obj,
194
                            const STRINGLIB_CHAR* str, Py_ssize_t str_len,
195
                            Py_ssize_t maxcount)
196
0
{
197
0
    Py_ssize_t i, j, count=0;
198
0
    PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
199
0
    PyObject *sub;
200
201
0
    if (list == NULL)
202
0
        return NULL;
203
204
0
    i = j = str_len - 1;
205
0
    while (maxcount-- > 0) {
206
0
        while (i >= 0 && STRINGLIB_ISSPACE(str[i]))
207
0
            i--;
208
0
        if (i < 0) break;
209
0
        j = i; i--;
210
0
        while (i >= 0 && !STRINGLIB_ISSPACE(str[i]))
211
0
            i--;
212
#if !STRINGLIB_MUTABLE
213
0
        if (j == str_len - 1 && i < 0 && STRINGLIB_CHECK_EXACT(str_obj)) {
214
            /* No whitespace in str_obj, so just use it as list[0] */
215
0
            Py_INCREF(str_obj);
216
0
            PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
217
0
            count++;
218
0
            break;
219
0
        }
220
0
#endif
221
0
        SPLIT_ADD(str, i + 1, j + 1);
222
0
    }
223
224
0
    if (i >= 0) {
225
        /* Only occurs when maxcount was reached */
226
        /* Skip any remaining whitespace and copy to beginning of string */
227
0
        while (i >= 0 && STRINGLIB_ISSPACE(str[i]))
228
0
            i--;
229
0
        if (i >= 0)
230
0
            SPLIT_ADD(str, 0, i + 1);
231
0
    }
232
0
    FIX_PREALLOC_SIZE(list);
233
0
    if (PyList_Reverse(list) < 0)
234
0
        goto onError;
235
0
    return list;
236
237
0
  onError:
238
0
    Py_DECREF(list);
239
0
    return NULL;
240
0
}
Unexecuted instantiation: bytesobject.c:stringlib_rsplit_whitespace
Unexecuted instantiation: unicodeobject.c:asciilib_rsplit_whitespace
Unexecuted instantiation: unicodeobject.c:ucs1lib_rsplit_whitespace
Unexecuted instantiation: unicodeobject.c:ucs2lib_rsplit_whitespace
Unexecuted instantiation: unicodeobject.c:ucs4lib_rsplit_whitespace
Unexecuted instantiation: bytearrayobject.c:stringlib_rsplit_whitespace
241
242
Py_LOCAL_INLINE(PyObject *)
243
STRINGLIB(rsplit_char)(PyObject* str_obj,
244
                      const STRINGLIB_CHAR* str, Py_ssize_t str_len,
245
                      const STRINGLIB_CHAR ch,
246
                      Py_ssize_t maxcount)
247
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
0
    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
0
    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.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
13.3k
    Py_ssize_t i;
349
13.3k
    Py_ssize_t j;
350
13.3k
    PyObject *list = PyList_New(0);
351
13.3k
    PyObject *sub;
352
353
13.3k
    if (list == NULL)
354
0
        return NULL;
355
356
31.4M
    for (i = j = 0; i < str_len; ) {
357
31.4M
        Py_ssize_t eol;
358
359
        /* Find a line and append it */
360
218M
        while (i < str_len && !STRINGLIB_ISLINEBREAK(str[i]))
361
186M
            i++;
362
363
        /* Skip the line break reading CRLF as one line break */
364
31.4M
        eol = i;
365
31.4M
        if (i < str_len) {
366
31.3M
            if (str[i] == '\r' && i + 1 < str_len && str[i+1] == '\n')
367
83.0k
                i += 2;
368
31.3M
            else
369
31.3M
                i++;
370
31.3M
            if (keepends)
371
0
                eol = i;
372
31.3M
        }
373
#if !STRINGLIB_MUTABLE
374
31.4M
        if (j == 0 && eol == str_len && STRINGLIB_CHECK_EXACT(str_obj)) {
375
            /* No linebreak in str_obj, so just use it as list[0] */
376
5.12k
            if (PyList_Append(list, str_obj))
377
0
                goto onError;
378
5.12k
            break;
379
5.12k
        }
380
31.3M
#endif
381
62.7M
        SPLIT_APPEND(str, j, eol);
382
31.3M
        j = i;
383
31.3M
    }
384
13.3k
    return list;
385
386
0
  onError:
387
0
    Py_DECREF(list);
388
0
    return NULL;
389
13.3k
}
Unexecuted instantiation: bytesobject.c:stringlib_splitlines
unicodeobject.c:asciilib_splitlines
Line
Count
Source
339
2.59k
{
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.59k
    Py_ssize_t i;
349
2.59k
    Py_ssize_t j;
350
2.59k
    PyObject *list = PyList_New(0);
351
2.59k
    PyObject *sub;
352
353
2.59k
    if (list == NULL)
354
0
        return NULL;
355
356
9.53M
    for (i = j = 0; i < str_len; ) {
357
9.52M
        Py_ssize_t eol;
358
359
        /* Find a line and append it */
360
40.9M
        while (i < str_len && !STRINGLIB_ISLINEBREAK(str[i]))
361
31.4M
            i++;
362
363
        /* Skip the line break reading CRLF as one line break */
364
9.52M
        eol = i;
365
9.52M
        if (i < str_len) {
366
9.52M
            if (str[i] == '\r' && i + 1 < str_len && str[i+1] == '\n')
367
2.11k
                i += 2;
368
9.52M
            else
369
9.52M
                i++;
370
9.52M
            if (keepends)
371
0
                eol = i;
372
9.52M
        }
373
9.52M
#if !STRINGLIB_MUTABLE
374
9.52M
        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
910
            if (PyList_Append(list, str_obj))
377
0
                goto onError;
378
910
            break;
379
910
        }
380
9.52M
#endif
381
19.0M
        SPLIT_APPEND(str, j, eol);
382
9.52M
        j = i;
383
9.52M
    }
384
2.59k
    return list;
385
386
0
  onError:
387
0
    Py_DECREF(list);
388
0
    return NULL;
389
2.59k
}
unicodeobject.c:ucs1lib_splitlines
Line
Count
Source
339
815
{
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
815
    Py_ssize_t i;
349
815
    Py_ssize_t j;
350
815
    PyObject *list = PyList_New(0);
351
815
    PyObject *sub;
352
353
815
    if (list == NULL)
354
0
        return NULL;
355
356
1.46M
    for (i = j = 0; i < str_len; ) {
357
1.46M
        Py_ssize_t eol;
358
359
        /* Find a line and append it */
360
8.94M
        while (i < str_len && !STRINGLIB_ISLINEBREAK(str[i]))
361
7.48M
            i++;
362
363
        /* Skip the line break reading CRLF as one line break */
364
1.46M
        eol = i;
365
1.46M
        if (i < str_len) {
366
1.46M
            if (str[i] == '\r' && i + 1 < str_len && str[i+1] == '\n')
367
2.01k
                i += 2;
368
1.46M
            else
369
1.46M
                i++;
370
1.46M
            if (keepends)
371
0
                eol = i;
372
1.46M
        }
373
1.46M
#if !STRINGLIB_MUTABLE
374
1.46M
        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
206
            if (PyList_Append(list, str_obj))
377
0
                goto onError;
378
206
            break;
379
206
        }
380
1.46M
#endif
381
2.93M
        SPLIT_APPEND(str, j, eol);
382
1.46M
        j = i;
383
1.46M
    }
384
815
    return list;
385
386
0
  onError:
387
0
    Py_DECREF(list);
388
0
    return NULL;
389
815
}
unicodeobject.c:ucs2lib_splitlines
Line
Count
Source
339
6.97k
{
340
    /* This does not use the preallocated list because splitlines is
341
       usually run with hundreds of newlines.  The overhead of
342
       switching between PyList_SET_ITEM and append causes about a
343
       2-3% slowdown for that common case.  A smarter implementation
344
       could move the if check out, so the SET_ITEMs are done first
345
       and the appends only done when the prealloc buffer is full.
346
       That's too much work for little gain.*/
347
348
6.97k
    Py_ssize_t i;
349
6.97k
    Py_ssize_t j;
350
6.97k
    PyObject *list = PyList_New(0);
351
6.97k
    PyObject *sub;
352
353
6.97k
    if (list == NULL)
354
0
        return NULL;
355
356
10.3M
    for (i = j = 0; i < str_len; ) {
357
10.3M
        Py_ssize_t eol;
358
359
        /* Find a line and append it */
360
74.3M
        while (i < str_len && !STRINGLIB_ISLINEBREAK(str[i]))
361
64.0M
            i++;
362
363
        /* Skip the line break reading CRLF as one line break */
364
10.3M
        eol = i;
365
10.3M
        if (i < str_len) {
366
10.3M
            if (str[i] == '\r' && i + 1 < str_len && str[i+1] == '\n')
367
29.6k
                i += 2;
368
10.2M
            else
369
10.2M
                i++;
370
10.3M
            if (keepends)
371
0
                eol = i;
372
10.3M
        }
373
10.3M
#if !STRINGLIB_MUTABLE
374
10.3M
        if (j == 0 && eol == str_len && STRINGLIB_CHECK_EXACT(str_obj)) {
375
            /* No linebreak in str_obj, so just use it as list[0] */
376
2.88k
            if (PyList_Append(list, str_obj))
377
0
                goto onError;
378
2.88k
            break;
379
2.88k
        }
380
10.3M
#endif
381
20.6M
        SPLIT_APPEND(str, j, eol);
382
10.3M
        j = i;
383
10.3M
    }
384
6.97k
    return list;
385
386
0
  onError:
387
0
    Py_DECREF(list);
388
0
    return NULL;
389
6.97k
}
unicodeobject.c:ucs4lib_splitlines
Line
Count
Source
339
2.94k
{
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.94k
    Py_ssize_t i;
349
2.94k
    Py_ssize_t j;
350
2.94k
    PyObject *list = PyList_New(0);
351
2.94k
    PyObject *sub;
352
353
2.94k
    if (list == NULL)
354
0
        return NULL;
355
356
10.0M
    for (i = j = 0; i < str_len; ) {
357
10.0M
        Py_ssize_t eol;
358
359
        /* Find a line and append it */
360
94.0M
        while (i < str_len && !STRINGLIB_ISLINEBREAK(str[i]))
361
83.9M
            i++;
362
363
        /* Skip the line break reading CRLF as one line break */
364
10.0M
        eol = i;
365
10.0M
        if (i < str_len) {
366
10.0M
            if (str[i] == '\r' && i + 1 < str_len && str[i+1] == '\n')
367
49.2k
                i += 2;
368
10.0M
            else
369
10.0M
                i++;
370
10.0M
            if (keepends)
371
0
                eol = i;
372
10.0M
        }
373
10.0M
#if !STRINGLIB_MUTABLE
374
10.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.11k
            if (PyList_Append(list, str_obj))
377
0
                goto onError;
378
1.11k
            break;
379
1.11k
        }
380
10.0M
#endif
381
20.1M
        SPLIT_APPEND(str, j, eol);
382
10.0M
        j = i;
383
10.0M
    }
384
2.94k
    return list;
385
386
0
  onError:
387
0
    Py_DECREF(list);
388
0
    return NULL;
389
2.94k
}
Unexecuted instantiation: bytearrayobject.c:stringlib_splitlines
390