Coverage Report

Created: 2025-12-14 07:06

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
127M
#define MAX_PREALLOC 12
15
16
/* 5 splits gives 6 elements */
17
#define PREALLOC_SIZE(maxsplit) \
18
24.4M
    (maxsplit >= MAX_PREALLOC ? MAX_PREALLOC : maxsplit+1)
19
20
#define SPLIT_APPEND(data, left, right)         \
21
32.6M
    sub = STRINGLIB_NEW((data) + (left),        \
22
32.6M
                        (right) - (left));      \
23
32.6M
    if (sub == NULL)                            \
24
32.6M
        goto onError;                           \
25
32.6M
    if (PyList_Append(list, sub)) {             \
26
0
        Py_DECREF(sub);                         \
27
0
        goto onError;                           \
28
0
    }                                           \
29
32.6M
    else                                        \
30
32.6M
        Py_DECREF(sub);
31
32
74.5M
#define SPLIT_ADD(data, left, right) {          \
33
74.5M
    sub = STRINGLIB_NEW((data) + (left),        \
34
74.5M
                        (right) - (left));      \
35
74.5M
    if (sub == NULL)                            \
36
74.5M
        goto onError;                           \
37
74.5M
    if (count < MAX_PREALLOC) {                 \
38
38.3M
        PyList_SET_ITEM(list, count, sub);      \
39
38.3M
    } else {                                    \
40
36.2M
        if (PyList_Append(list, sub)) {         \
41
0
            Py_DECREF(sub);                     \
42
0
            goto onError;                       \
43
0
        }                                       \
44
36.2M
        else                                    \
45
36.2M
            Py_DECREF(sub);                     \
46
36.2M
    }                                           \
47
74.5M
    count++; }
48
49
50
/* Always force the list to the expected size. */
51
24.4M
#define FIX_PREALLOC_SIZE(list) Py_SET_SIZE(list, count)
52
53
Py_LOCAL_INLINE(PyObject *)
54
STRINGLIB(split_whitespace)(PyObject* str_obj,
55
                           const STRINGLIB_CHAR* str, Py_ssize_t str_len,
56
                           Py_ssize_t maxcount)
57
194k
{
58
194k
    Py_ssize_t i, j, count=0;
59
194k
    PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
60
194k
    PyObject *sub;
61
62
194k
    if (list == NULL)
63
0
        return NULL;
64
65
194k
    i = j = 0;
66
2.98M
    while (maxcount-- > 0) {
67
5.78M
        while (i < str_len && STRINGLIB_ISSPACE(str[i]))
68
2.82M
            i++;
69
2.95M
        if (i == str_len) break;
70
2.83M
        j = i; i++;
71
143M
        while (i < str_len && !STRINGLIB_ISSPACE(str[i]))
72
140M
            i++;
73
#if !STRINGLIB_MUTABLE
74
2.83M
        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
44.6k
            Py_INCREF(str_obj);
77
44.6k
            PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
78
44.6k
            count++;
79
44.6k
            break;
80
44.6k
        }
81
2.79M
#endif
82
8.37M
        SPLIT_ADD(str, j, i);
83
8.37M
    }
84
85
194k
    if (i < str_len) {
86
        /* Only occurs when maxcount was reached */
87
        /* Skip any remaining whitespace and copy to end of string */
88
85.5k
        while (i < str_len && STRINGLIB_ISSPACE(str[i]))
89
59.3k
            i++;
90
26.1k
        if (i != str_len)
91
26.1k
            SPLIT_ADD(str, i, str_len);
92
26.1k
    }
93
194k
    FIX_PREALLOC_SIZE(list);
94
194k
    return list;
95
96
0
  onError:
97
0
    Py_DECREF(list);
98
0
    return NULL;
99
194k
}
Unexecuted instantiation: bytesobject.c:stringlib_split_whitespace
unicodeobject.c:asciilib_split_whitespace
Line
Count
Source
57
90.3k
{
58
90.3k
    Py_ssize_t i, j, count=0;
59
90.3k
    PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
60
90.3k
    PyObject *sub;
61
62
90.3k
    if (list == NULL)
63
0
        return NULL;
64
65
90.3k
    i = j = 0;
66
819k
    while (maxcount-- > 0) {
67
1.50M
        while (i < str_len && STRINGLIB_ISSPACE(str[i]))
68
702k
            i++;
69
803k
        if (i == str_len) break;
70
753k
        j = i; i++;
71
46.2M
        while (i < str_len && !STRINGLIB_ISSPACE(str[i]))
72
45.4M
            i++;
73
753k
#if !STRINGLIB_MUTABLE
74
753k
        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
24.1k
            Py_INCREF(str_obj);
77
24.1k
            PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
78
24.1k
            count++;
79
24.1k
            break;
80
24.1k
        }
81
729k
#endif
82
2.18M
        SPLIT_ADD(str, j, i);
83
2.18M
    }
84
85
90.3k
    if (i < str_len) {
86
        /* Only occurs when maxcount was reached */
87
        /* Skip any remaining whitespace and copy to end of string */
88
56.7k
        while (i < str_len && STRINGLIB_ISSPACE(str[i]))
89
43.1k
            i++;
90
13.5k
        if (i != str_len)
91
13.5k
            SPLIT_ADD(str, i, str_len);
92
13.5k
    }
93
90.3k
    FIX_PREALLOC_SIZE(list);
94
90.3k
    return list;
95
96
0
  onError:
97
0
    Py_DECREF(list);
98
    return NULL;
99
90.3k
}
unicodeobject.c:ucs1lib_split_whitespace
Line
Count
Source
57
37.6k
{
58
37.6k
    Py_ssize_t i, j, count=0;
59
37.6k
    PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
60
37.6k
    PyObject *sub;
61
62
37.6k
    if (list == NULL)
63
0
        return NULL;
64
65
37.6k
    i = j = 0;
66
1.16M
    while (maxcount-- > 0) {
67
2.24M
        while (i < str_len && STRINGLIB_ISSPACE(str[i]))
68
1.09M
            i++;
69
1.14M
        if (i == str_len) break;
70
1.12M
        j = i; i++;
71
47.2M
        while (i < str_len && !STRINGLIB_ISSPACE(str[i]))
72
46.1M
            i++;
73
1.12M
#if !STRINGLIB_MUTABLE
74
1.12M
        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.97k
            Py_INCREF(str_obj);
77
2.97k
            PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
78
2.97k
            count++;
79
2.97k
            break;
80
2.97k
        }
81
1.12M
#endif
82
3.37M
        SPLIT_ADD(str, j, i);
83
3.37M
    }
84
85
37.6k
    if (i < str_len) {
86
        /* Only occurs when maxcount was reached */
87
        /* Skip any remaining whitespace and copy to end of string */
88
26.7k
        while (i < str_len && STRINGLIB_ISSPACE(str[i]))
89
15.2k
            i++;
90
11.5k
        if (i != str_len)
91
11.5k
            SPLIT_ADD(str, i, str_len);
92
11.5k
    }
93
37.6k
    FIX_PREALLOC_SIZE(list);
94
37.6k
    return list;
95
96
0
  onError:
97
0
    Py_DECREF(list);
98
    return NULL;
99
37.6k
}
unicodeobject.c:ucs2lib_split_whitespace
Line
Count
Source
57
53.3k
{
58
53.3k
    Py_ssize_t i, j, count=0;
59
53.3k
    PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
60
53.3k
    PyObject *sub;
61
62
53.3k
    if (list == NULL)
63
0
        return NULL;
64
65
53.3k
    i = j = 0;
66
915k
    while (maxcount-- > 0) {
67
1.86M
        while (i < str_len && STRINGLIB_ISSPACE(str[i]))
68
948k
            i++;
69
914k
        if (i == str_len) break;
70
876k
        j = i; i++;
71
39.8M
        while (i < str_len && !STRINGLIB_ISSPACE(str[i]))
72
38.9M
            i++;
73
876k
#if !STRINGLIB_MUTABLE
74
876k
        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.6k
            Py_INCREF(str_obj);
77
14.6k
            PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
78
14.6k
            count++;
79
14.6k
            break;
80
14.6k
        }
81
862k
#endif
82
2.58M
        SPLIT_ADD(str, j, i);
83
2.58M
    }
84
85
53.3k
    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
53.3k
    FIX_PREALLOC_SIZE(list);
94
53.3k
    return list;
95
96
0
  onError:
97
0
    Py_DECREF(list);
98
    return NULL;
99
53.3k
}
unicodeobject.c:ucs4lib_split_whitespace
Line
Count
Source
57
13.0k
{
58
13.0k
    Py_ssize_t i, j, count=0;
59
13.0k
    PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
60
13.0k
    PyObject *sub;
61
62
13.0k
    if (list == NULL)
63
0
        return NULL;
64
65
13.0k
    i = j = 0;
66
90.3k
    while (maxcount-- > 0) {
67
164k
        while (i < str_len && STRINGLIB_ISSPACE(str[i]))
68
75.6k
            i++;
69
88.5k
        if (i == str_len) break;
70
80.1k
        j = i; i++;
71
10.2M
        while (i < str_len && !STRINGLIB_ISSPACE(str[i]))
72
10.1M
            i++;
73
80.1k
#if !STRINGLIB_MUTABLE
74
80.1k
        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.82k
            Py_INCREF(str_obj);
77
2.82k
            PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
78
2.82k
            count++;
79
2.82k
            break;
80
2.82k
        }
81
77.2k
#endif
82
231k
        SPLIT_ADD(str, j, i);
83
231k
    }
84
85
13.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
13.0k
    FIX_PREALLOC_SIZE(list);
94
13.0k
    return list;
95
96
0
  onError:
97
0
    Py_DECREF(list);
98
    return NULL;
99
13.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
24.0M
{
107
24.0M
    Py_ssize_t i, j, count=0;
108
24.0M
    PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
109
24.0M
    PyObject *sub;
110
111
24.0M
    if (list == NULL)
112
0
        return NULL;
113
114
24.0M
    i = j = 0;
115
99.1M
    while ((j < str_len) && (maxcount-- > 0)) {
116
443M
        for(; j < str_len; j++) {
117
            /* I found that using memchr makes no difference */
118
421M
            if (str[j] == ch) {
119
53.3M
                SPLIT_ADD(str, i, j);
120
53.3M
                i = j = j + 1;
121
53.3M
                break;
122
53.3M
            }
123
421M
        }
124
75.1M
    }
125
#if !STRINGLIB_MUTABLE
126
24.0M
    if (count == 0 && STRINGLIB_CHECK_EXACT(str_obj)) {
127
        /* ch not in str_obj, so just use str_obj as list[0] */
128
6.00M
        Py_INCREF(str_obj);
129
6.00M
        PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
130
6.00M
        count++;
131
6.00M
    } else
132
18.0M
#endif
133
18.0M
    if (i <= str_len) {
134
36.0M
        SPLIT_ADD(str, i, str_len);
135
36.0M
    }
136
24.0M
    FIX_PREALLOC_SIZE(list);
137
24.0M
    return list;
138
139
0
  onError:
140
0
    Py_DECREF(list);
141
0
    return NULL;
142
0
}
bytesobject.c:stringlib_split_char
Line
Count
Source
106
3.27M
{
107
3.27M
    Py_ssize_t i, j, count=0;
108
3.27M
    PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
109
3.27M
    PyObject *sub;
110
111
3.27M
    if (list == NULL)
112
0
        return NULL;
113
114
3.27M
    i = j = 0;
115
10.3M
    while ((j < str_len) && (maxcount-- > 0)) {
116
53.9M
        for(; j < str_len; j++) {
117
            /* I found that using memchr makes no difference */
118
51.1M
            if (str[j] == ch) {
119
4.26M
                SPLIT_ADD(str, i, j);
120
4.26M
                i = j = j + 1;
121
4.26M
                break;
122
4.26M
            }
123
51.1M
        }
124
7.07M
    }
125
3.27M
#if !STRINGLIB_MUTABLE
126
3.27M
    if (count == 0 && STRINGLIB_CHECK_EXACT(str_obj)) {
127
        /* ch not in str_obj, so just use str_obj as list[0] */
128
2.72M
        Py_INCREF(str_obj);
129
2.72M
        PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
130
2.72M
        count++;
131
2.72M
    } else
132
555k
#endif
133
555k
    if (i <= str_len) {
134
1.11M
        SPLIT_ADD(str, i, str_len);
135
1.11M
    }
136
3.27M
    FIX_PREALLOC_SIZE(list);
137
3.27M
    return list;
138
139
0
  onError:
140
0
    Py_DECREF(list);
141
    return NULL;
142
3.27M
}
unicodeobject.c:asciilib_split_char
Line
Count
Source
106
19.3M
{
107
19.3M
    Py_ssize_t i, j, count=0;
108
19.3M
    PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
109
19.3M
    PyObject *sub;
110
111
19.3M
    if (list == NULL)
112
0
        return NULL;
113
114
19.3M
    i = j = 0;
115
62.5M
    while ((j < str_len) && (maxcount-- > 0)) {
116
248M
        for(; j < str_len; j++) {
117
            /* I found that using memchr makes no difference */
118
229M
            if (str[j] == ch) {
119
24.6M
                SPLIT_ADD(str, i, j);
120
24.6M
                i = j = j + 1;
121
24.6M
                break;
122
24.6M
            }
123
229M
        }
124
43.2M
    }
125
19.3M
#if !STRINGLIB_MUTABLE
126
19.3M
    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.2M
#endif
133
16.2M
    if (i <= str_len) {
134
32.5M
        SPLIT_ADD(str, i, str_len);
135
32.5M
    }
136
19.3M
    FIX_PREALLOC_SIZE(list);
137
19.3M
    return list;
138
139
0
  onError:
140
0
    Py_DECREF(list);
141
    return NULL;
142
19.3M
}
unicodeobject.c:ucs1lib_split_char
Line
Count
Source
106
1.24M
{
107
1.24M
    Py_ssize_t i, j, count=0;
108
1.24M
    PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
109
1.24M
    PyObject *sub;
110
111
1.24M
    if (list == NULL)
112
0
        return NULL;
113
114
1.24M
    i = j = 0;
115
16.7M
    while ((j < str_len) && (maxcount-- > 0)) {
116
73.5M
        for(; j < str_len; j++) {
117
            /* I found that using memchr makes no difference */
118
73.2M
            if (str[j] == ch) {
119
15.2M
                SPLIT_ADD(str, i, j);
120
15.2M
                i = j = j + 1;
121
15.2M
                break;
122
15.2M
            }
123
73.2M
        }
124
15.4M
    }
125
1.24M
#if !STRINGLIB_MUTABLE
126
1.24M
    if (count == 0 && STRINGLIB_CHECK_EXACT(str_obj)) {
127
        /* ch not in str_obj, so just use str_obj as list[0] */
128
187k
        Py_INCREF(str_obj);
129
187k
        PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
130
187k
        count++;
131
187k
    } else
132
1.06M
#endif
133
1.06M
    if (i <= str_len) {
134
2.12M
        SPLIT_ADD(str, i, str_len);
135
2.12M
    }
136
1.24M
    FIX_PREALLOC_SIZE(list);
137
1.24M
    return list;
138
139
0
  onError:
140
0
    Py_DECREF(list);
141
    return NULL;
142
1.24M
}
unicodeobject.c:ucs2lib_split_char
Line
Count
Source
106
119k
{
107
119k
    Py_ssize_t i, j, count=0;
108
119k
    PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
109
119k
    PyObject *sub;
110
111
119k
    if (list == NULL)
112
0
        return NULL;
113
114
119k
    i = j = 0;
115
8.92M
    while ((j < str_len) && (maxcount-- > 0)) {
116
47.6M
        for(; j < str_len; j++) {
117
            /* I found that using memchr makes no difference */
118
47.5M
            if (str[j] == ch) {
119
8.71M
                SPLIT_ADD(str, i, j);
120
8.71M
                i = j = j + 1;
121
8.71M
                break;
122
8.71M
            }
123
47.5M
        }
124
8.80M
    }
125
119k
#if !STRINGLIB_MUTABLE
126
119k
    if (count == 0 && STRINGLIB_CHECK_EXACT(str_obj)) {
127
        /* ch not in str_obj, so just use str_obj as list[0] */
128
32.5k
        Py_INCREF(str_obj);
129
32.5k
        PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
130
32.5k
        count++;
131
32.5k
    } else
132
86.4k
#endif
133
86.4k
    if (i <= str_len) {
134
172k
        SPLIT_ADD(str, i, str_len);
135
172k
    }
136
119k
    FIX_PREALLOC_SIZE(list);
137
119k
    return list;
138
139
0
  onError:
140
0
    Py_DECREF(list);
141
    return NULL;
142
119k
}
unicodeobject.c:ucs4lib_split_char
Line
Count
Source
106
16.8k
{
107
16.8k
    Py_ssize_t i, j, count=0;
108
16.8k
    PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
109
16.8k
    PyObject *sub;
110
111
16.8k
    if (list == NULL)
112
0
        return NULL;
113
114
16.8k
    i = j = 0;
115
584k
    while ((j < str_len) && (maxcount-- > 0)) {
116
19.8M
        for(; j < str_len; j++) {
117
            /* I found that using memchr makes no difference */
118
19.8M
            if (str[j] == ch) {
119
553k
                SPLIT_ADD(str, i, j);
120
553k
                i = j = j + 1;
121
553k
                break;
122
553k
            }
123
19.8M
        }
124
567k
    }
125
16.8k
#if !STRINGLIB_MUTABLE
126
16.8k
    if (count == 0 && STRINGLIB_CHECK_EXACT(str_obj)) {
127
        /* ch not in str_obj, so just use str_obj as list[0] */
128
644
        Py_INCREF(str_obj);
129
644
        PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
130
644
        count++;
131
644
    } else
132
16.2k
#endif
133
16.2k
    if (i <= str_len) {
134
32.4k
        SPLIT_ADD(str, i, str_len);
135
32.4k
    }
136
16.8k
    FIX_PREALLOC_SIZE(list);
137
16.8k
    return list;
138
139
0
  onError:
140
0
    Py_DECREF(list);
141
    return NULL;
142
16.8k
}
Unexecuted instantiation: bytearrayobject.c:stringlib_split_char
143
144
Py_LOCAL_INLINE(PyObject *)
145
STRINGLIB(split)(PyObject* str_obj,
146
                const STRINGLIB_CHAR* str, Py_ssize_t str_len,
147
                const STRINGLIB_CHAR* sep, Py_ssize_t sep_len,
148
                Py_ssize_t maxcount)
149
24.2M
{
150
24.2M
    Py_ssize_t i, j, pos, count=0;
151
24.2M
    PyObject *list, *sub;
152
153
24.2M
    if (sep_len == 0) {
154
0
        PyErr_SetString(PyExc_ValueError, "empty separator");
155
0
        return NULL;
156
0
    }
157
24.2M
    else if (sep_len == 1)
158
24.0M
        return STRINGLIB(split_char)(str_obj, str, str_len, sep[0], maxcount);
159
160
238k
    list = PyList_New(PREALLOC_SIZE(maxcount));
161
238k
    if (list == NULL)
162
0
        return NULL;
163
164
238k
    i = j = 0;
165
428k
    while (maxcount-- > 0) {
166
238k
        pos = FASTSEARCH(str+i, str_len-i, sep, sep_len, -1, FAST_SEARCH);
167
238k
        if (pos < 0)
168
49.2k
            break;
169
189k
        j = i + pos;
170
379k
        SPLIT_ADD(str, i, j);
171
379k
        i = j + sep_len;
172
379k
    }
173
#if !STRINGLIB_MUTABLE
174
238k
    if (count == 0 && STRINGLIB_CHECK_EXACT(str_obj)) {
175
        /* No match in str_obj, so just use it as list[0] */
176
49.2k
        Py_INCREF(str_obj);
177
49.2k
        PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
178
49.2k
        count++;
179
49.2k
    } else
180
189k
#endif
181
189k
    {
182
379k
        SPLIT_ADD(str, i, str_len);
183
379k
    }
184
238k
    FIX_PREALLOC_SIZE(list);
185
238k
    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.27M
{
150
3.27M
    Py_ssize_t i, j, pos, count=0;
151
3.27M
    PyObject *list, *sub;
152
153
3.27M
    if (sep_len == 0) {
154
0
        PyErr_SetString(PyExc_ValueError, "empty separator");
155
0
        return NULL;
156
0
    }
157
3.27M
    else if (sep_len == 1)
158
3.27M
        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.4M
{
150
19.4M
    Py_ssize_t i, j, pos, count=0;
151
19.4M
    PyObject *list, *sub;
152
153
19.4M
    if (sep_len == 0) {
154
0
        PyErr_SetString(PyExc_ValueError, "empty separator");
155
0
        return NULL;
156
0
    }
157
19.4M
    else if (sep_len == 1)
158
19.3M
        return STRINGLIB(split_char)(str_obj, str, str_len, sep[0], maxcount);
159
160
91.3k
    list = PyList_New(PREALLOC_SIZE(maxcount));
161
91.3k
    if (list == NULL)
162
0
        return NULL;
163
164
91.3k
    i = j = 0;
165
149k
    while (maxcount-- > 0) {
166
91.3k
        pos = FASTSEARCH(str+i, str_len-i, sep, sep_len, -1, FAST_SEARCH);
167
91.3k
        if (pos < 0)
168
32.8k
            break;
169
58.4k
        j = i + pos;
170
116k
        SPLIT_ADD(str, i, j);
171
116k
        i = j + sep_len;
172
116k
    }
173
91.3k
#if !STRINGLIB_MUTABLE
174
91.3k
    if (count == 0 && STRINGLIB_CHECK_EXACT(str_obj)) {
175
        /* No match in str_obj, so just use it as list[0] */
176
32.8k
        Py_INCREF(str_obj);
177
32.8k
        PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
178
32.8k
        count++;
179
32.8k
    } else
180
58.4k
#endif
181
58.4k
    {
182
116k
        SPLIT_ADD(str, i, str_len);
183
116k
    }
184
91.3k
    FIX_PREALLOC_SIZE(list);
185
91.3k
    return list;
186
187
0
  onError:
188
0
    Py_DECREF(list);
189
    return NULL;
190
91.3k
}
unicodeobject.c:ucs1lib_split
Line
Count
Source
149
1.27M
{
150
1.27M
    Py_ssize_t i, j, pos, count=0;
151
1.27M
    PyObject *list, *sub;
152
153
1.27M
    if (sep_len == 0) {
154
0
        PyErr_SetString(PyExc_ValueError, "empty separator");
155
0
        return NULL;
156
0
    }
157
1.27M
    else if (sep_len == 1)
158
1.24M
        return STRINGLIB(split_char)(str_obj, str, str_len, sep[0], maxcount);
159
160
26.1k
    list = PyList_New(PREALLOC_SIZE(maxcount));
161
26.1k
    if (list == NULL)
162
0
        return NULL;
163
164
26.1k
    i = j = 0;
165
50.0k
    while (maxcount-- > 0) {
166
26.1k
        pos = FASTSEARCH(str+i, str_len-i, sep, sep_len, -1, FAST_SEARCH);
167
26.1k
        if (pos < 0)
168
2.29k
            break;
169
23.8k
        j = i + pos;
170
47.7k
        SPLIT_ADD(str, i, j);
171
47.7k
        i = j + sep_len;
172
47.7k
    }
173
26.1k
#if !STRINGLIB_MUTABLE
174
26.1k
    if (count == 0 && STRINGLIB_CHECK_EXACT(str_obj)) {
175
        /* No match in str_obj, so just use it as list[0] */
176
2.29k
        Py_INCREF(str_obj);
177
2.29k
        PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
178
2.29k
        count++;
179
2.29k
    } else
180
23.8k
#endif
181
23.8k
    {
182
47.7k
        SPLIT_ADD(str, i, str_len);
183
47.7k
    }
184
26.1k
    FIX_PREALLOC_SIZE(list);
185
26.1k
    return list;
186
187
0
  onError:
188
0
    Py_DECREF(list);
189
    return NULL;
190
26.1k
}
unicodeobject.c:ucs2lib_split
Line
Count
Source
149
217k
{
150
217k
    Py_ssize_t i, j, pos, count=0;
151
217k
    PyObject *list, *sub;
152
153
217k
    if (sep_len == 0) {
154
0
        PyErr_SetString(PyExc_ValueError, "empty separator");
155
0
        return NULL;
156
0
    }
157
217k
    else if (sep_len == 1)
158
119k
        return STRINGLIB(split_char)(str_obj, str, str_len, sep[0], maxcount);
159
160
98.5k
    list = PyList_New(PREALLOC_SIZE(maxcount));
161
98.5k
    if (list == NULL)
162
0
        return NULL;
163
164
98.5k
    i = j = 0;
165
185k
    while (maxcount-- > 0) {
166
98.5k
        pos = FASTSEARCH(str+i, str_len-i, sep, sep_len, -1, FAST_SEARCH);
167
98.5k
        if (pos < 0)
168
11.4k
            break;
169
87.1k
        j = i + pos;
170
174k
        SPLIT_ADD(str, i, j);
171
174k
        i = j + sep_len;
172
174k
    }
173
98.5k
#if !STRINGLIB_MUTABLE
174
98.5k
    if (count == 0 && STRINGLIB_CHECK_EXACT(str_obj)) {
175
        /* No match in str_obj, so just use it as list[0] */
176
11.4k
        Py_INCREF(str_obj);
177
11.4k
        PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
178
11.4k
        count++;
179
11.4k
    } else
180
87.1k
#endif
181
87.1k
    {
182
174k
        SPLIT_ADD(str, i, str_len);
183
174k
    }
184
98.5k
    FIX_PREALLOC_SIZE(list);
185
98.5k
    return list;
186
187
0
  onError:
188
0
    Py_DECREF(list);
189
    return NULL;
190
98.5k
}
unicodeobject.c:ucs4lib_split
Line
Count
Source
149
39.6k
{
150
39.6k
    Py_ssize_t i, j, pos, count=0;
151
39.6k
    PyObject *list, *sub;
152
153
39.6k
    if (sep_len == 0) {
154
0
        PyErr_SetString(PyExc_ValueError, "empty separator");
155
0
        return NULL;
156
0
    }
157
39.6k
    else if (sep_len == 1)
158
16.8k
        return STRINGLIB(split_char)(str_obj, str, str_len, sep[0], maxcount);
159
160
22.8k
    list = PyList_New(PREALLOC_SIZE(maxcount));
161
22.8k
    if (list == NULL)
162
0
        return NULL;
163
164
22.8k
    i = j = 0;
165
42.9k
    while (maxcount-- > 0) {
166
22.8k
        pos = FASTSEARCH(str+i, str_len-i, sep, sep_len, -1, FAST_SEARCH);
167
22.8k
        if (pos < 0)
168
2.67k
            break;
169
20.1k
        j = i + pos;
170
40.2k
        SPLIT_ADD(str, i, j);
171
40.2k
        i = j + sep_len;
172
40.2k
    }
173
22.8k
#if !STRINGLIB_MUTABLE
174
22.8k
    if (count == 0 && STRINGLIB_CHECK_EXACT(str_obj)) {
175
        /* No match in str_obj, so just use it as list[0] */
176
2.67k
        Py_INCREF(str_obj);
177
2.67k
        PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
178
2.67k
        count++;
179
2.67k
    } else
180
20.1k
#endif
181
20.1k
    {
182
40.2k
        SPLIT_ADD(str, i, str_len);
183
40.2k
    }
184
22.8k
    FIX_PREALLOC_SIZE(list);
185
22.8k
    return list;
186
187
0
  onError:
188
0
    Py_DECREF(list);
189
    return NULL;
190
22.8k
}
Unexecuted instantiation: bytearrayobject.c:stringlib_split
191
192
Py_LOCAL_INLINE(PyObject *)
193
STRINGLIB(rsplit_whitespace)(PyObject* str_obj,
194
                            const STRINGLIB_CHAR* str, Py_ssize_t str_len,
195
                            Py_ssize_t maxcount)
196
0
{
197
0
    Py_ssize_t i, j, count=0;
198
0
    PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
199
0
    PyObject *sub;
200
201
0
    if (list == NULL)
202
0
        return NULL;
203
204
0
    i = j = str_len - 1;
205
0
    while (maxcount-- > 0) {
206
0
        while (i >= 0 && STRINGLIB_ISSPACE(str[i]))
207
0
            i--;
208
0
        if (i < 0) break;
209
0
        j = i; i--;
210
0
        while (i >= 0 && !STRINGLIB_ISSPACE(str[i]))
211
0
            i--;
212
#if !STRINGLIB_MUTABLE
213
0
        if (j == str_len - 1 && i < 0 && STRINGLIB_CHECK_EXACT(str_obj)) {
214
            /* No whitespace in str_obj, so just use it as list[0] */
215
0
            Py_INCREF(str_obj);
216
0
            PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
217
0
            count++;
218
0
            break;
219
0
        }
220
0
#endif
221
0
        SPLIT_ADD(str, i + 1, j + 1);
222
0
    }
223
224
0
    if (i >= 0) {
225
        /* Only occurs when maxcount was reached */
226
        /* Skip any remaining whitespace and copy to beginning of string */
227
0
        while (i >= 0 && STRINGLIB_ISSPACE(str[i]))
228
0
            i--;
229
0
        if (i >= 0)
230
0
            SPLIT_ADD(str, 0, i + 1);
231
0
    }
232
0
    FIX_PREALLOC_SIZE(list);
233
0
    if (PyList_Reverse(list) < 0)
234
0
        goto onError;
235
0
    return list;
236
237
0
  onError:
238
0
    Py_DECREF(list);
239
0
    return NULL;
240
0
}
Unexecuted instantiation: bytesobject.c:stringlib_rsplit_whitespace
Unexecuted instantiation: unicodeobject.c:asciilib_rsplit_whitespace
Unexecuted instantiation: unicodeobject.c:ucs1lib_rsplit_whitespace
Unexecuted instantiation: unicodeobject.c:ucs2lib_rsplit_whitespace
Unexecuted instantiation: unicodeobject.c:ucs4lib_rsplit_whitespace
Unexecuted instantiation: bytearrayobject.c:stringlib_rsplit_whitespace
241
242
Py_LOCAL_INLINE(PyObject *)
243
STRINGLIB(rsplit_char)(PyObject* str_obj,
244
                      const STRINGLIB_CHAR* str, Py_ssize_t str_len,
245
                      const STRINGLIB_CHAR ch,
246
                      Py_ssize_t maxcount)
247
50
{
248
50
    Py_ssize_t i, j, count=0;
249
50
    PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
250
50
    PyObject *sub;
251
252
50
    if (list == NULL)
253
0
        return NULL;
254
255
50
    i = j = str_len - 1;
256
100
    while ((i >= 0) && (maxcount-- > 0)) {
257
50
        for(; i >= 0; i--) {
258
50
            if (str[i] == ch) {
259
50
                SPLIT_ADD(str, i + 1, j + 1);
260
50
                j = i = i - 1;
261
50
                break;
262
50
            }
263
50
        }
264
50
    }
265
#if !STRINGLIB_MUTABLE
266
50
    if (count == 0 && STRINGLIB_CHECK_EXACT(str_obj)) {
267
        /* ch not in str_obj, so just use str_obj as list[0] */
268
0
        Py_INCREF(str_obj);
269
0
        PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
270
0
        count++;
271
0
    } else
272
50
#endif
273
50
    if (j >= -1) {
274
100
        SPLIT_ADD(str, 0, j + 1);
275
100
    }
276
50
    FIX_PREALLOC_SIZE(list);
277
50
    if (PyList_Reverse(list) < 0)
278
0
        goto onError;
279
50
    return list;
280
281
0
  onError:
282
0
    Py_DECREF(list);
283
0
    return NULL;
284
50
}
Unexecuted instantiation: bytesobject.c:stringlib_rsplit_char
unicodeobject.c:asciilib_rsplit_char
Line
Count
Source
247
50
{
248
50
    Py_ssize_t i, j, count=0;
249
50
    PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
250
50
    PyObject *sub;
251
252
50
    if (list == NULL)
253
0
        return NULL;
254
255
50
    i = j = str_len - 1;
256
100
    while ((i >= 0) && (maxcount-- > 0)) {
257
50
        for(; i >= 0; i--) {
258
50
            if (str[i] == ch) {
259
50
                SPLIT_ADD(str, i + 1, j + 1);
260
50
                j = i = i - 1;
261
50
                break;
262
50
            }
263
50
        }
264
50
    }
265
50
#if !STRINGLIB_MUTABLE
266
50
    if (count == 0 && STRINGLIB_CHECK_EXACT(str_obj)) {
267
        /* ch not in str_obj, so just use str_obj as list[0] */
268
0
        Py_INCREF(str_obj);
269
0
        PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
270
0
        count++;
271
0
    } else
272
50
#endif
273
50
    if (j >= -1) {
274
100
        SPLIT_ADD(str, 0, j + 1);
275
100
    }
276
50
    FIX_PREALLOC_SIZE(list);
277
50
    if (PyList_Reverse(list) < 0)
278
0
        goto onError;
279
50
    return list;
280
281
0
  onError:
282
0
    Py_DECREF(list);
283
    return NULL;
284
50
}
Unexecuted instantiation: unicodeobject.c:ucs1lib_rsplit_char
Unexecuted instantiation: unicodeobject.c:ucs2lib_rsplit_char
Unexecuted instantiation: unicodeobject.c:ucs4lib_rsplit_char
Unexecuted instantiation: bytearrayobject.c:stringlib_rsplit_char
285
286
Py_LOCAL_INLINE(PyObject *)
287
STRINGLIB(rsplit)(PyObject* str_obj,
288
                 const STRINGLIB_CHAR* str, Py_ssize_t str_len,
289
                 const STRINGLIB_CHAR* sep, Py_ssize_t sep_len,
290
                 Py_ssize_t maxcount)
291
50
{
292
50
    Py_ssize_t j, pos, count=0;
293
50
    PyObject *list, *sub;
294
295
50
    if (sep_len == 0) {
296
0
        PyErr_SetString(PyExc_ValueError, "empty separator");
297
0
        return NULL;
298
0
    }
299
50
    else if (sep_len == 1)
300
50
        return STRINGLIB(rsplit_char)(str_obj, str, str_len, sep[0], maxcount);
301
302
0
    list = PyList_New(PREALLOC_SIZE(maxcount));
303
0
    if (list == NULL)
304
0
        return NULL;
305
306
0
    j = str_len;
307
0
    while (maxcount-- > 0) {
308
0
        pos = FASTSEARCH(str, j, sep, sep_len, -1, FAST_RSEARCH);
309
0
        if (pos < 0)
310
0
            break;
311
0
        SPLIT_ADD(str, pos + sep_len, j);
312
0
        j = pos;
313
0
    }
314
#if !STRINGLIB_MUTABLE
315
0
    if (count == 0 && STRINGLIB_CHECK_EXACT(str_obj)) {
316
        /* No match in str_obj, so just use it as list[0] */
317
0
        Py_INCREF(str_obj);
318
0
        PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
319
0
        count++;
320
0
    } else
321
0
#endif
322
0
    {
323
0
        SPLIT_ADD(str, 0, j);
324
0
    }
325
0
    FIX_PREALLOC_SIZE(list);
326
0
    if (PyList_Reverse(list) < 0)
327
0
        goto onError;
328
0
    return list;
329
330
0
  onError:
331
0
    Py_DECREF(list);
332
0
    return NULL;
333
0
}
Unexecuted instantiation: bytesobject.c:stringlib_rsplit
unicodeobject.c:asciilib_rsplit
Line
Count
Source
291
50
{
292
50
    Py_ssize_t j, pos, count=0;
293
50
    PyObject *list, *sub;
294
295
50
    if (sep_len == 0) {
296
0
        PyErr_SetString(PyExc_ValueError, "empty separator");
297
0
        return NULL;
298
0
    }
299
50
    else if (sep_len == 1)
300
50
        return STRINGLIB(rsplit_char)(str_obj, str, str_len, sep[0], maxcount);
301
302
0
    list = PyList_New(PREALLOC_SIZE(maxcount));
303
0
    if (list == NULL)
304
0
        return NULL;
305
306
0
    j = str_len;
307
0
    while (maxcount-- > 0) {
308
0
        pos = FASTSEARCH(str, j, sep, sep_len, -1, FAST_RSEARCH);
309
0
        if (pos < 0)
310
0
            break;
311
0
        SPLIT_ADD(str, pos + sep_len, j);
312
0
        j = pos;
313
0
    }
314
0
#if !STRINGLIB_MUTABLE
315
0
    if (count == 0 && STRINGLIB_CHECK_EXACT(str_obj)) {
316
        /* No match in str_obj, so just use it as list[0] */
317
0
        Py_INCREF(str_obj);
318
0
        PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
319
0
        count++;
320
0
    } else
321
0
#endif
322
0
    {
323
0
        SPLIT_ADD(str, 0, j);
324
0
    }
325
0
    FIX_PREALLOC_SIZE(list);
326
0
    if (PyList_Reverse(list) < 0)
327
0
        goto onError;
328
0
    return list;
329
330
0
  onError:
331
0
    Py_DECREF(list);
332
    return NULL;
333
0
}
Unexecuted instantiation: unicodeobject.c:ucs1lib_rsplit
Unexecuted instantiation: unicodeobject.c:ucs2lib_rsplit
Unexecuted instantiation: unicodeobject.c:ucs4lib_rsplit
Unexecuted instantiation: bytearrayobject.c:stringlib_rsplit
334
335
Py_LOCAL_INLINE(PyObject *)
336
STRINGLIB(splitlines)(PyObject* str_obj,
337
                     const STRINGLIB_CHAR* str, Py_ssize_t str_len,
338
                     int keepends)
339
13.4k
{
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.4k
    Py_ssize_t i;
349
13.4k
    Py_ssize_t j;
350
13.4k
    PyObject *list = PyList_New(0);
351
13.4k
    PyObject *sub;
352
353
13.4k
    if (list == NULL)
354
0
        return NULL;
355
356
32.6M
    for (i = j = 0; i < str_len; ) {
357
32.6M
        Py_ssize_t eol;
358
359
        /* Find a line and append it */
360
265M
        while (i < str_len && !STRINGLIB_ISLINEBREAK(str[i]))
361
232M
            i++;
362
363
        /* Skip the line break reading CRLF as one line break */
364
32.6M
        eol = i;
365
32.6M
        if (i < str_len) {
366
32.6M
            if (str[i] == '\r' && i + 1 < str_len && str[i+1] == '\n')
367
84.4k
                i += 2;
368
32.5M
            else
369
32.5M
                i++;
370
32.6M
            if (keepends)
371
0
                eol = i;
372
32.6M
        }
373
#if !STRINGLIB_MUTABLE
374
32.6M
        if (j == 0 && eol == str_len && STRINGLIB_CHECK_EXACT(str_obj)) {
375
            /* No linebreak in str_obj, so just use it as list[0] */
376
5.23k
            if (PyList_Append(list, str_obj))
377
0
                goto onError;
378
5.23k
            break;
379
5.23k
        }
380
32.6M
#endif
381
65.2M
        SPLIT_APPEND(str, j, eol);
382
32.6M
        j = i;
383
32.6M
    }
384
13.4k
    return list;
385
386
0
  onError:
387
0
    Py_DECREF(list);
388
    return NULL;
389
13.4k
}
Unexecuted instantiation: bytesobject.c:stringlib_splitlines
unicodeobject.c:asciilib_splitlines
Line
Count
Source
339
2.65k
{
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.65k
    Py_ssize_t i;
349
2.65k
    Py_ssize_t j;
350
2.65k
    PyObject *list = PyList_New(0);
351
2.65k
    PyObject *sub;
352
353
2.65k
    if (list == NULL)
354
0
        return NULL;
355
356
8.58M
    for (i = j = 0; i < str_len; ) {
357
8.58M
        Py_ssize_t eol;
358
359
        /* Find a line and append it */
360
41.2M
        while (i < str_len && !STRINGLIB_ISLINEBREAK(str[i]))
361
32.6M
            i++;
362
363
        /* Skip the line break reading CRLF as one line break */
364
8.58M
        eol = i;
365
8.58M
        if (i < str_len) {
366
8.58M
            if (str[i] == '\r' && i + 1 < str_len && str[i+1] == '\n')
367
1.05k
                i += 2;
368
8.57M
            else
369
8.57M
                i++;
370
8.58M
            if (keepends)
371
0
                eol = i;
372
8.58M
        }
373
8.58M
#if !STRINGLIB_MUTABLE
374
8.58M
        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
943
            if (PyList_Append(list, str_obj))
377
0
                goto onError;
378
943
            break;
379
943
        }
380
8.58M
#endif
381
17.1M
        SPLIT_APPEND(str, j, eol);
382
8.58M
        j = i;
383
8.58M
    }
384
2.65k
    return list;
385
386
0
  onError:
387
0
    Py_DECREF(list);
388
    return NULL;
389
2.65k
}
unicodeobject.c:ucs1lib_splitlines
Line
Count
Source
339
840
{
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
840
    Py_ssize_t i;
349
840
    Py_ssize_t j;
350
840
    PyObject *list = PyList_New(0);
351
840
    PyObject *sub;
352
353
840
    if (list == NULL)
354
0
        return NULL;
355
356
2.14M
    for (i = j = 0; i < str_len; ) {
357
2.14M
        Py_ssize_t eol;
358
359
        /* Find a line and append it */
360
12.2M
        while (i < str_len && !STRINGLIB_ISLINEBREAK(str[i]))
361
10.0M
            i++;
362
363
        /* Skip the line break reading CRLF as one line break */
364
2.14M
        eol = i;
365
2.14M
        if (i < str_len) {
366
2.14M
            if (str[i] == '\r' && i + 1 < str_len && str[i+1] == '\n')
367
1.84k
                i += 2;
368
2.14M
            else
369
2.14M
                i++;
370
2.14M
            if (keepends)
371
0
                eol = i;
372
2.14M
        }
373
2.14M
#if !STRINGLIB_MUTABLE
374
2.14M
        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
212
            if (PyList_Append(list, str_obj))
377
0
                goto onError;
378
212
            break;
379
212
        }
380
2.14M
#endif
381
4.29M
        SPLIT_APPEND(str, j, eol);
382
2.14M
        j = i;
383
2.14M
    }
384
840
    return list;
385
386
0
  onError:
387
0
    Py_DECREF(list);
388
    return NULL;
389
840
}
unicodeobject.c:ucs2lib_splitlines
Line
Count
Source
339
6.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
6.94k
    Py_ssize_t i;
349
6.94k
    Py_ssize_t j;
350
6.94k
    PyObject *list = PyList_New(0);
351
6.94k
    PyObject *sub;
352
353
6.94k
    if (list == NULL)
354
0
        return NULL;
355
356
10.4M
    for (i = j = 0; i < str_len; ) {
357
10.4M
        Py_ssize_t eol;
358
359
        /* Find a line and append it */
360
87.7M
        while (i < str_len && !STRINGLIB_ISLINEBREAK(str[i]))
361
77.3M
            i++;
362
363
        /* Skip the line break reading CRLF as one line break */
364
10.4M
        eol = i;
365
10.4M
        if (i < str_len) {
366
10.4M
            if (str[i] == '\r' && i + 1 < str_len && str[i+1] == '\n')
367
20.4k
                i += 2;
368
10.3M
            else
369
10.3M
                i++;
370
10.4M
            if (keepends)
371
0
                eol = i;
372
10.4M
        }
373
10.4M
#if !STRINGLIB_MUTABLE
374
10.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
2.92k
            if (PyList_Append(list, str_obj))
377
0
                goto onError;
378
2.92k
            break;
379
2.92k
        }
380
10.4M
#endif
381
20.8M
        SPLIT_APPEND(str, j, eol);
382
10.4M
        j = i;
383
10.4M
    }
384
6.94k
    return list;
385
386
0
  onError:
387
0
    Py_DECREF(list);
388
    return NULL;
389
6.94k
}
unicodeobject.c:ucs4lib_splitlines
Line
Count
Source
339
2.99k
{
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.99k
    Py_ssize_t i;
349
2.99k
    Py_ssize_t j;
350
2.99k
    PyObject *list = PyList_New(0);
351
2.99k
    PyObject *sub;
352
353
2.99k
    if (list == NULL)
354
0
        return NULL;
355
356
11.5M
    for (i = j = 0; i < str_len; ) {
357
11.5M
        Py_ssize_t eol;
358
359
        /* Find a line and append it */
360
124M
        while (i < str_len && !STRINGLIB_ISLINEBREAK(str[i]))
361
112M
            i++;
362
363
        /* Skip the line break reading CRLF as one line break */
364
11.5M
        eol = i;
365
11.5M
        if (i < str_len) {
366
11.5M
            if (str[i] == '\r' && i + 1 < str_len && str[i+1] == '\n')
367
61.1k
                i += 2;
368
11.4M
            else
369
11.4M
                i++;
370
11.5M
            if (keepends)
371
0
                eol = i;
372
11.5M
        }
373
11.5M
#if !STRINGLIB_MUTABLE
374
11.5M
        if (j == 0 && eol == str_len && STRINGLIB_CHECK_EXACT(str_obj)) {
375
            /* No linebreak in str_obj, so just use it as list[0] */
376
1.15k
            if (PyList_Append(list, str_obj))
377
0
                goto onError;
378
1.15k
            break;
379
1.15k
        }
380
11.5M
#endif
381
23.0M
        SPLIT_APPEND(str, j, eol);
382
11.5M
        j = i;
383
11.5M
    }
384
2.99k
    return list;
385
386
0
  onError:
387
0
    Py_DECREF(list);
388
    return NULL;
389
2.99k
}
Unexecuted instantiation: bytearrayobject.c:stringlib_splitlines
390