Coverage Report

Created: 2025-11-11 06:44

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
117M
#define MAX_PREALLOC 12
15
16
/* 5 splits gives 6 elements */
17
#define PREALLOC_SIZE(maxsplit) \
18
24.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
63.6M
#define SPLIT_ADD(data, left, right) {          \
33
63.6M
    sub = STRINGLIB_NEW((data) + (left),        \
34
63.6M
                        (right) - (left));      \
35
63.6M
    if (sub == NULL)                            \
36
63.6M
        goto onError;                           \
37
63.6M
    if (count < MAX_PREALLOC) {                 \
38
37.6M
        PyList_SET_ITEM(list, count, sub);      \
39
37.6M
    } else {                                    \
40
25.9M
        if (PyList_Append(list, sub)) {         \
41
0
            Py_DECREF(sub);                     \
42
0
            goto onError;                       \
43
0
        }                                       \
44
25.9M
        else                                    \
45
25.9M
            Py_DECREF(sub);                     \
46
25.9M
    }                                           \
47
63.6M
    count++; }
48
49
50
/* Always force the list to the expected size. */
51
24.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
174k
{
58
174k
    Py_ssize_t i, j, count=0;
59
174k
    PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
60
174k
    PyObject *sub;
61
62
174k
    if (list == NULL)
63
0
        return NULL;
64
65
174k
    i = j = 0;
66
2.33M
    while (maxcount-- > 0) {
67
4.48M
        while (i < str_len && STRINGLIB_ISSPACE(str[i]))
68
2.18M
            i++;
69
2.30M
        if (i == str_len) break;
70
2.20M
        j = i; i++;
71
125M
        while (i < str_len && !STRINGLIB_ISSPACE(str[i]))
72
123M
            i++;
73
#if !STRINGLIB_MUTABLE
74
2.20M
        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.5k
            Py_INCREF(str_obj);
77
44.5k
            PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
78
44.5k
            count++;
79
44.5k
            break;
80
44.5k
        }
81
2.15M
#endif
82
6.47M
        SPLIT_ADD(str, j, i);
83
6.47M
    }
84
85
174k
    if (i < str_len) {
86
        /* Only occurs when maxcount was reached */
87
        /* Skip any remaining whitespace and copy to end of string */
88
57.1k
        while (i < str_len && STRINGLIB_ISSPACE(str[i]))
89
30.7k
            i++;
90
26.3k
        if (i != str_len)
91
26.3k
            SPLIT_ADD(str, i, str_len);
92
26.3k
    }
93
174k
    FIX_PREALLOC_SIZE(list);
94
174k
    return list;
95
96
0
  onError:
97
0
    Py_DECREF(list);
98
0
    return NULL;
99
174k
}
Unexecuted instantiation: bytesobject.c:stringlib_split_whitespace
unicodeobject.c:asciilib_split_whitespace
Line
Count
Source
57
86.3k
{
58
86.3k
    Py_ssize_t i, j, count=0;
59
86.3k
    PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
60
86.3k
    PyObject *sub;
61
62
86.3k
    if (list == NULL)
63
0
        return NULL;
64
65
86.3k
    i = j = 0;
66
829k
    while (maxcount-- > 0) {
67
1.50M
        while (i < str_len && STRINGLIB_ISSPACE(str[i]))
68
692k
            i++;
69
812k
        if (i == str_len) break;
70
766k
        j = i; i++;
71
46.0M
        while (i < str_len && !STRINGLIB_ISSPACE(str[i]))
72
45.2M
            i++;
73
766k
#if !STRINGLIB_MUTABLE
74
766k
        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
23.7k
            Py_INCREF(str_obj);
77
23.7k
            PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
78
23.7k
            count++;
79
23.7k
            break;
80
23.7k
        }
81
742k
#endif
82
2.22M
        SPLIT_ADD(str, j, i);
83
2.22M
    }
84
85
86.3k
    if (i < str_len) {
86
        /* Only occurs when maxcount was reached */
87
        /* Skip any remaining whitespace and copy to end of string */
88
28.4k
        while (i < str_len && STRINGLIB_ISSPACE(str[i]))
89
14.7k
            i++;
90
13.7k
        if (i != str_len)
91
13.7k
            SPLIT_ADD(str, i, str_len);
92
13.7k
    }
93
86.3k
    FIX_PREALLOC_SIZE(list);
94
86.3k
    return list;
95
96
0
  onError:
97
0
    Py_DECREF(list);
98
    return NULL;
99
86.3k
}
unicodeobject.c:ucs1lib_split_whitespace
Line
Count
Source
57
26.2k
{
58
26.2k
    Py_ssize_t i, j, count=0;
59
26.2k
    PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
60
26.2k
    PyObject *sub;
61
62
26.2k
    if (list == NULL)
63
0
        return NULL;
64
65
26.2k
    i = j = 0;
66
718k
    while (maxcount-- > 0) {
67
1.38M
        while (i < str_len && STRINGLIB_ISSPACE(str[i]))
68
678k
            i++;
69
706k
        if (i == str_len) break;
70
695k
        j = i; i++;
71
29.8M
        while (i < str_len && !STRINGLIB_ISSPACE(str[i]))
72
29.1M
            i++;
73
695k
#if !STRINGLIB_MUTABLE
74
695k
        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.12k
            Py_INCREF(str_obj);
77
3.12k
            PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
78
3.12k
            count++;
79
3.12k
            break;
80
3.12k
        }
81
692k
#endif
82
2.07M
        SPLIT_ADD(str, j, i);
83
2.07M
    }
84
85
26.2k
    if (i < str_len) {
86
        /* Only occurs when maxcount was reached */
87
        /* Skip any remaining whitespace and copy to end of string */
88
26.5k
        while (i < str_len && STRINGLIB_ISSPACE(str[i]))
89
15.0k
            i++;
90
11.5k
        if (i != str_len)
91
11.5k
            SPLIT_ADD(str, i, str_len);
92
11.5k
    }
93
26.2k
    FIX_PREALLOC_SIZE(list);
94
26.2k
    return list;
95
96
0
  onError:
97
0
    Py_DECREF(list);
98
    return NULL;
99
26.2k
}
unicodeobject.c:ucs2lib_split_whitespace
Line
Count
Source
57
49.3k
{
58
49.3k
    Py_ssize_t i, j, count=0;
59
49.3k
    PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
60
49.3k
    PyObject *sub;
61
62
49.3k
    if (list == NULL)
63
0
        return NULL;
64
65
49.3k
    i = j = 0;
66
667k
    while (maxcount-- > 0) {
67
1.37M
        while (i < str_len && STRINGLIB_ISSPACE(str[i]))
68
706k
            i++;
69
666k
        if (i == str_len) break;
70
633k
        j = i; i++;
71
38.1M
        while (i < str_len && !STRINGLIB_ISSPACE(str[i]))
72
37.4M
            i++;
73
633k
#if !STRINGLIB_MUTABLE
74
633k
        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.9k
            Py_INCREF(str_obj);
77
14.9k
            PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
78
14.9k
            count++;
79
14.9k
            break;
80
14.9k
        }
81
618k
#endif
82
1.85M
        SPLIT_ADD(str, j, i);
83
1.85M
    }
84
85
49.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
49.3k
    FIX_PREALLOC_SIZE(list);
94
49.3k
    return list;
95
96
0
  onError:
97
0
    Py_DECREF(list);
98
    return NULL;
99
49.3k
}
unicodeobject.c:ucs4lib_split_whitespace
Line
Count
Source
57
12.0k
{
58
12.0k
    Py_ssize_t i, j, count=0;
59
12.0k
    PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
60
12.0k
    PyObject *sub;
61
62
12.0k
    if (list == NULL)
63
0
        return NULL;
64
65
12.0k
    i = j = 0;
66
118k
    while (maxcount-- > 0) {
67
221k
        while (i < str_len && STRINGLIB_ISSPACE(str[i]))
68
103k
            i++;
69
118k
        if (i == str_len) break;
70
109k
        j = i; i++;
71
11.7M
        while (i < str_len && !STRINGLIB_ISSPACE(str[i]))
72
11.6M
            i++;
73
109k
#if !STRINGLIB_MUTABLE
74
109k
        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.74k
            Py_INCREF(str_obj);
77
2.74k
            PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
78
2.74k
            count++;
79
2.74k
            break;
80
2.74k
        }
81
106k
#endif
82
319k
        SPLIT_ADD(str, j, i);
83
319k
    }
84
85
12.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
12.0k
    FIX_PREALLOC_SIZE(list);
94
12.0k
    return list;
95
96
0
  onError:
97
0
    Py_DECREF(list);
98
    return NULL;
99
12.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.5M
{
107
24.5M
    Py_ssize_t i, j, count=0;
108
24.5M
    PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
109
24.5M
    PyObject *sub;
110
111
24.5M
    if (list == NULL)
112
0
        return NULL;
113
114
24.5M
    i = j = 0;
115
89.6M
    while ((j < str_len) && (maxcount-- > 0)) {
116
435M
        for(; j < str_len; j++) {
117
            /* I found that using memchr makes no difference */
118
413M
            if (str[j] == ch) {
119
43.2M
                SPLIT_ADD(str, i, j);
120
43.2M
                i = j = j + 1;
121
43.2M
                break;
122
43.2M
            }
123
413M
        }
124
65.0M
    }
125
#if !STRINGLIB_MUTABLE
126
24.5M
    if (count == 0 && STRINGLIB_CHECK_EXACT(str_obj)) {
127
        /* ch not in str_obj, so just use str_obj as list[0] */
128
6.71M
        Py_INCREF(str_obj);
129
6.71M
        PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
130
6.71M
        count++;
131
6.71M
    } else
132
17.8M
#endif
133
17.8M
    if (i <= str_len) {
134
35.6M
        SPLIT_ADD(str, i, str_len);
135
35.6M
    }
136
24.5M
    FIX_PREALLOC_SIZE(list);
137
24.5M
    return list;
138
139
0
  onError:
140
0
    Py_DECREF(list);
141
0
    return NULL;
142
0
}
bytesobject.c:stringlib_split_char
Line
Count
Source
106
3.75M
{
107
3.75M
    Py_ssize_t i, j, count=0;
108
3.75M
    PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
109
3.75M
    PyObject *sub;
110
111
3.75M
    if (list == NULL)
112
0
        return NULL;
113
114
3.75M
    i = j = 0;
115
12.2M
    while ((j < str_len) && (maxcount-- > 0)) {
116
64.9M
        for(; j < str_len; j++) {
117
            /* I found that using memchr makes no difference */
118
61.8M
            if (str[j] == ch) {
119
5.41M
                SPLIT_ADD(str, i, j);
120
5.41M
                i = j = j + 1;
121
5.41M
                break;
122
5.41M
            }
123
61.8M
        }
124
8.53M
    }
125
3.75M
#if !STRINGLIB_MUTABLE
126
3.75M
    if (count == 0 && STRINGLIB_CHECK_EXACT(str_obj)) {
127
        /* ch not in str_obj, so just use str_obj as list[0] */
128
3.00M
        Py_INCREF(str_obj);
129
3.00M
        PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
130
3.00M
        count++;
131
3.00M
    } else
132
749k
#endif
133
749k
    if (i <= str_len) {
134
1.49M
        SPLIT_ADD(str, i, str_len);
135
1.49M
    }
136
3.75M
    FIX_PREALLOC_SIZE(list);
137
3.75M
    return list;
138
139
0
  onError:
140
0
    Py_DECREF(list);
141
    return NULL;
142
3.75M
}
unicodeobject.c:asciilib_split_char
Line
Count
Source
106
19.2M
{
107
19.2M
    Py_ssize_t i, j, count=0;
108
19.2M
    PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
109
19.2M
    PyObject *sub;
110
111
19.2M
    if (list == NULL)
112
0
        return NULL;
113
114
19.2M
    i = j = 0;
115
61.9M
    while ((j < str_len) && (maxcount-- > 0)) {
116
242M
        for(; j < str_len; j++) {
117
            /* I found that using memchr makes no difference */
118
224M
            if (str[j] == ch) {
119
24.2M
                SPLIT_ADD(str, i, j);
120
24.2M
                i = j = j + 1;
121
24.2M
                break;
122
24.2M
            }
123
224M
        }
124
42.6M
    }
125
19.2M
#if !STRINGLIB_MUTABLE
126
19.2M
    if (count == 0 && STRINGLIB_CHECK_EXACT(str_obj)) {
127
        /* ch not in str_obj, so just use str_obj as list[0] */
128
3.53M
        Py_INCREF(str_obj);
129
3.53M
        PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
130
3.53M
        count++;
131
3.53M
    } else
132
15.7M
#endif
133
15.7M
    if (i <= str_len) {
134
31.4M
        SPLIT_ADD(str, i, str_len);
135
31.4M
    }
136
19.2M
    FIX_PREALLOC_SIZE(list);
137
19.2M
    return list;
138
139
0
  onError:
140
0
    Py_DECREF(list);
141
    return NULL;
142
19.2M
}
unicodeobject.c:ucs1lib_split_char
Line
Count
Source
106
1.42M
{
107
1.42M
    Py_ssize_t i, j, count=0;
108
1.42M
    PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
109
1.42M
    PyObject *sub;
110
111
1.42M
    if (list == NULL)
112
0
        return NULL;
113
114
1.42M
    i = j = 0;
115
11.5M
    while ((j < str_len) && (maxcount-- > 0)) {
116
59.1M
        for(; j < str_len; j++) {
117
            /* I found that using memchr makes no difference */
118
58.9M
            if (str[j] == ch) {
119
9.98M
                SPLIT_ADD(str, i, j);
120
9.98M
                i = j = j + 1;
121
9.98M
                break;
122
9.98M
            }
123
58.9M
        }
124
10.1M
    }
125
1.42M
#if !STRINGLIB_MUTABLE
126
1.42M
    if (count == 0 && STRINGLIB_CHECK_EXACT(str_obj)) {
127
        /* ch not in str_obj, so just use str_obj as list[0] */
128
165k
        Py_INCREF(str_obj);
129
165k
        PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
130
165k
        count++;
131
165k
    } else
132
1.25M
#endif
133
1.25M
    if (i <= str_len) {
134
2.51M
        SPLIT_ADD(str, i, str_len);
135
2.51M
    }
136
1.42M
    FIX_PREALLOC_SIZE(list);
137
1.42M
    return list;
138
139
0
  onError:
140
0
    Py_DECREF(list);
141
    return NULL;
142
1.42M
}
unicodeobject.c:ucs2lib_split_char
Line
Count
Source
106
90.6k
{
107
90.6k
    Py_ssize_t i, j, count=0;
108
90.6k
    PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
109
90.6k
    PyObject *sub;
110
111
90.6k
    if (list == NULL)
112
0
        return NULL;
113
114
90.6k
    i = j = 0;
115
3.11M
    while ((j < str_len) && (maxcount-- > 0)) {
116
44.4M
        for(; j < str_len; j++) {
117
            /* I found that using memchr makes no difference */
118
44.4M
            if (str[j] == ch) {
119
2.96M
                SPLIT_ADD(str, i, j);
120
2.96M
                i = j = j + 1;
121
2.96M
                break;
122
2.96M
            }
123
44.4M
        }
124
3.02M
    }
125
90.6k
#if !STRINGLIB_MUTABLE
126
90.6k
    if (count == 0 && STRINGLIB_CHECK_EXACT(str_obj)) {
127
        /* ch not in str_obj, so just use str_obj as list[0] */
128
7.29k
        Py_INCREF(str_obj);
129
7.29k
        PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
130
7.29k
        count++;
131
7.29k
    } else
132
83.3k
#endif
133
83.3k
    if (i <= str_len) {
134
166k
        SPLIT_ADD(str, i, str_len);
135
166k
    }
136
90.6k
    FIX_PREALLOC_SIZE(list);
137
90.6k
    return list;
138
139
0
  onError:
140
0
    Py_DECREF(list);
141
    return NULL;
142
90.6k
}
unicodeobject.c:ucs4lib_split_char
Line
Count
Source
106
15.9k
{
107
15.9k
    Py_ssize_t i, j, count=0;
108
15.9k
    PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
109
15.9k
    PyObject *sub;
110
111
15.9k
    if (list == NULL)
112
0
        return NULL;
113
114
15.9k
    i = j = 0;
115
686k
    while ((j < str_len) && (maxcount-- > 0)) {
116
23.4M
        for(; j < str_len; j++) {
117
            /* I found that using memchr makes no difference */
118
23.4M
            if (str[j] == ch) {
119
658k
                SPLIT_ADD(str, i, j);
120
658k
                i = j = j + 1;
121
658k
                break;
122
658k
            }
123
23.4M
        }
124
670k
    }
125
15.9k
#if !STRINGLIB_MUTABLE
126
15.9k
    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
15.6k
#endif
133
15.6k
    if (i <= str_len) {
134
31.2k
        SPLIT_ADD(str, i, str_len);
135
31.2k
    }
136
15.9k
    FIX_PREALLOC_SIZE(list);
137
15.9k
    return list;
138
139
0
  onError:
140
0
    Py_DECREF(list);
141
    return NULL;
142
15.9k
}
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.7M
{
150
24.7M
    Py_ssize_t i, j, pos, count=0;
151
24.7M
    PyObject *list, *sub;
152
153
24.7M
    if (sep_len == 0) {
154
0
        PyErr_SetString(PyExc_ValueError, "empty separator");
155
0
        return NULL;
156
0
    }
157
24.7M
    else if (sep_len == 1)
158
24.5M
        return STRINGLIB(split_char)(str_obj, str, str_len, sep[0], maxcount);
159
160
216k
    list = PyList_New(PREALLOC_SIZE(maxcount));
161
216k
    if (list == NULL)
162
0
        return NULL;
163
164
216k
    i = j = 0;
165
383k
    while (maxcount-- > 0) {
166
216k
        pos = FASTSEARCH(str+i, str_len-i, sep, sep_len, -1, FAST_SEARCH);
167
216k
        if (pos < 0)
168
48.3k
            break;
169
167k
        j = i + pos;
170
335k
        SPLIT_ADD(str, i, j);
171
335k
        i = j + sep_len;
172
335k
    }
173
#if !STRINGLIB_MUTABLE
174
216k
    if (count == 0 && STRINGLIB_CHECK_EXACT(str_obj)) {
175
        /* No match in str_obj, so just use it as list[0] */
176
48.3k
        Py_INCREF(str_obj);
177
48.3k
        PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
178
48.3k
        count++;
179
48.3k
    } else
180
167k
#endif
181
167k
    {
182
335k
        SPLIT_ADD(str, i, str_len);
183
335k
    }
184
216k
    FIX_PREALLOC_SIZE(list);
185
216k
    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.75M
{
150
3.75M
    Py_ssize_t i, j, pos, count=0;
151
3.75M
    PyObject *list, *sub;
152
153
3.75M
    if (sep_len == 0) {
154
0
        PyErr_SetString(PyExc_ValueError, "empty separator");
155
0
        return NULL;
156
0
    }
157
3.75M
    else if (sep_len == 1)
158
3.75M
        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.3M
{
150
19.3M
    Py_ssize_t i, j, pos, count=0;
151
19.3M
    PyObject *list, *sub;
152
153
19.3M
    if (sep_len == 0) {
154
0
        PyErr_SetString(PyExc_ValueError, "empty separator");
155
0
        return NULL;
156
0
    }
157
19.3M
    else if (sep_len == 1)
158
19.2M
        return STRINGLIB(split_char)(str_obj, str, str_len, sep[0], maxcount);
159
160
93.0k
    list = PyList_New(PREALLOC_SIZE(maxcount));
161
93.0k
    if (list == NULL)
162
0
        return NULL;
163
164
93.0k
    i = j = 0;
165
153k
    while (maxcount-- > 0) {
166
93.0k
        pos = FASTSEARCH(str+i, str_len-i, sep, sep_len, -1, FAST_SEARCH);
167
93.0k
        if (pos < 0)
168
32.6k
            break;
169
60.3k
        j = i + pos;
170
120k
        SPLIT_ADD(str, i, j);
171
120k
        i = j + sep_len;
172
120k
    }
173
93.0k
#if !STRINGLIB_MUTABLE
174
93.0k
    if (count == 0 && STRINGLIB_CHECK_EXACT(str_obj)) {
175
        /* No match in str_obj, so just use it as list[0] */
176
32.6k
        Py_INCREF(str_obj);
177
32.6k
        PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
178
32.6k
        count++;
179
32.6k
    } else
180
60.3k
#endif
181
60.3k
    {
182
120k
        SPLIT_ADD(str, i, str_len);
183
120k
    }
184
93.0k
    FIX_PREALLOC_SIZE(list);
185
93.0k
    return list;
186
187
0
  onError:
188
0
    Py_DECREF(list);
189
    return NULL;
190
93.0k
}
unicodeobject.c:ucs1lib_split
Line
Count
Source
149
1.43M
{
150
1.43M
    Py_ssize_t i, j, pos, count=0;
151
1.43M
    PyObject *list, *sub;
152
153
1.43M
    if (sep_len == 0) {
154
0
        PyErr_SetString(PyExc_ValueError, "empty separator");
155
0
        return NULL;
156
0
    }
157
1.43M
    else if (sep_len == 1)
158
1.42M
        return STRINGLIB(split_char)(str_obj, str, str_len, sep[0], maxcount);
159
160
14.4k
    list = PyList_New(PREALLOC_SIZE(maxcount));
161
14.4k
    if (list == NULL)
162
0
        return NULL;
163
164
14.4k
    i = j = 0;
165
27.6k
    while (maxcount-- > 0) {
166
14.4k
        pos = FASTSEARCH(str+i, str_len-i, sep, sep_len, -1, FAST_SEARCH);
167
14.4k
        if (pos < 0)
168
1.22k
            break;
169
13.2k
        j = i + pos;
170
26.4k
        SPLIT_ADD(str, i, j);
171
26.4k
        i = j + sep_len;
172
26.4k
    }
173
14.4k
#if !STRINGLIB_MUTABLE
174
14.4k
    if (count == 0 && STRINGLIB_CHECK_EXACT(str_obj)) {
175
        /* No match in str_obj, so just use it as list[0] */
176
1.22k
        Py_INCREF(str_obj);
177
1.22k
        PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
178
1.22k
        count++;
179
1.22k
    } else
180
13.2k
#endif
181
13.2k
    {
182
26.4k
        SPLIT_ADD(str, i, str_len);
183
26.4k
    }
184
14.4k
    FIX_PREALLOC_SIZE(list);
185
14.4k
    return list;
186
187
0
  onError:
188
0
    Py_DECREF(list);
189
    return NULL;
190
14.4k
}
unicodeobject.c:ucs2lib_split
Line
Count
Source
149
177k
{
150
177k
    Py_ssize_t i, j, pos, count=0;
151
177k
    PyObject *list, *sub;
152
153
177k
    if (sep_len == 0) {
154
0
        PyErr_SetString(PyExc_ValueError, "empty separator");
155
0
        return NULL;
156
0
    }
157
177k
    else if (sep_len == 1)
158
90.6k
        return STRINGLIB(split_char)(str_obj, str, str_len, sep[0], maxcount);
159
160
86.8k
    list = PyList_New(PREALLOC_SIZE(maxcount));
161
86.8k
    if (list == NULL)
162
0
        return NULL;
163
164
86.8k
    i = j = 0;
165
162k
    while (maxcount-- > 0) {
166
86.8k
        pos = FASTSEARCH(str+i, str_len-i, sep, sep_len, -1, FAST_SEARCH);
167
86.8k
        if (pos < 0)
168
11.0k
            break;
169
75.7k
        j = i + pos;
170
151k
        SPLIT_ADD(str, i, j);
171
151k
        i = j + sep_len;
172
151k
    }
173
86.8k
#if !STRINGLIB_MUTABLE
174
86.8k
    if (count == 0 && STRINGLIB_CHECK_EXACT(str_obj)) {
175
        /* No match in str_obj, so just use it as list[0] */
176
11.0k
        Py_INCREF(str_obj);
177
11.0k
        PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
178
11.0k
        count++;
179
11.0k
    } else
180
75.7k
#endif
181
75.7k
    {
182
151k
        SPLIT_ADD(str, i, str_len);
183
151k
    }
184
86.8k
    FIX_PREALLOC_SIZE(list);
185
86.8k
    return list;
186
187
0
  onError:
188
0
    Py_DECREF(list);
189
    return NULL;
190
86.8k
}
unicodeobject.c:ucs4lib_split
Line
Count
Source
149
37.8k
{
150
37.8k
    Py_ssize_t i, j, pos, count=0;
151
37.8k
    PyObject *list, *sub;
152
153
37.8k
    if (sep_len == 0) {
154
0
        PyErr_SetString(PyExc_ValueError, "empty separator");
155
0
        return NULL;
156
0
    }
157
37.8k
    else if (sep_len == 1)
158
15.9k
        return STRINGLIB(split_char)(str_obj, str, str_len, sep[0], maxcount);
159
160
21.8k
    list = PyList_New(PREALLOC_SIZE(maxcount));
161
21.8k
    if (list == NULL)
162
0
        return NULL;
163
164
21.8k
    i = j = 0;
165
40.2k
    while (maxcount-- > 0) {
166
21.8k
        pos = FASTSEARCH(str+i, str_len-i, sep, sep_len, -1, FAST_SEARCH);
167
21.8k
        if (pos < 0)
168
3.48k
            break;
169
18.3k
        j = i + pos;
170
36.7k
        SPLIT_ADD(str, i, j);
171
36.7k
        i = j + sep_len;
172
36.7k
    }
173
21.8k
#if !STRINGLIB_MUTABLE
174
21.8k
    if (count == 0 && STRINGLIB_CHECK_EXACT(str_obj)) {
175
        /* No match in str_obj, so just use it as list[0] */
176
3.48k
        Py_INCREF(str_obj);
177
3.48k
        PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
178
3.48k
        count++;
179
3.48k
    } else
180
18.3k
#endif
181
18.3k
    {
182
36.7k
        SPLIT_ADD(str, i, str_len);
183
36.7k
    }
184
21.8k
    FIX_PREALLOC_SIZE(list);
185
21.8k
    return list;
186
187
0
  onError:
188
0
    Py_DECREF(list);
189
    return NULL;
190
21.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.3M
    for (i = j = 0; i < str_len; ) {
357
32.2M
        Py_ssize_t eol;
358
359
        /* Find a line and append it */
360
263M
        while (i < str_len && !STRINGLIB_ISLINEBREAK(str[i]))
361
231M
            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
102k
                i += 2;
368
32.1M
            else
369
32.1M
                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.29k
            if (PyList_Append(list, str_obj))
377
0
                goto onError;
378
5.29k
            break;
379
5.29k
        }
380
32.2M
#endif
381
64.5M
        SPLIT_APPEND(str, j, eol);
382
32.2M
        j = i;
383
32.2M
    }
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.71k
{
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.71k
    Py_ssize_t i;
349
2.71k
    Py_ssize_t j;
350
2.71k
    PyObject *list = PyList_New(0);
351
2.71k
    PyObject *sub;
352
353
2.71k
    if (list == NULL)
354
0
        return NULL;
355
356
7.65M
    for (i = j = 0; i < str_len; ) {
357
7.65M
        Py_ssize_t eol;
358
359
        /* Find a line and append it */
360
42.7M
        while (i < str_len && !STRINGLIB_ISLINEBREAK(str[i]))
361
35.0M
            i++;
362
363
        /* Skip the line break reading CRLF as one line break */
364
7.65M
        eol = i;
365
7.65M
        if (i < str_len) {
366
7.65M
            if (str[i] == '\r' && i + 1 < str_len && str[i+1] == '\n')
367
18.0k
                i += 2;
368
7.63M
            else
369
7.63M
                i++;
370
7.65M
            if (keepends)
371
0
                eol = i;
372
7.65M
        }
373
7.65M
#if !STRINGLIB_MUTABLE
374
7.65M
        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
985
            if (PyList_Append(list, str_obj))
377
0
                goto onError;
378
985
            break;
379
985
        }
380
7.65M
#endif
381
15.3M
        SPLIT_APPEND(str, j, eol);
382
7.65M
        j = i;
383
7.65M
    }
384
2.71k
    return list;
385
386
0
  onError:
387
0
    Py_DECREF(list);
388
    return NULL;
389
2.71k
}
unicodeobject.c:ucs1lib_splitlines
Line
Count
Source
339
830
{
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
830
    Py_ssize_t i;
349
830
    Py_ssize_t j;
350
830
    PyObject *list = PyList_New(0);
351
830
    PyObject *sub;
352
353
830
    if (list == NULL)
354
0
        return NULL;
355
356
1.58M
    for (i = j = 0; i < str_len; ) {
357
1.58M
        Py_ssize_t eol;
358
359
        /* Find a line and append it */
360
10.5M
        while (i < str_len && !STRINGLIB_ISLINEBREAK(str[i]))
361
8.96M
            i++;
362
363
        /* Skip the line break reading CRLF as one line break */
364
1.58M
        eol = i;
365
1.58M
        if (i < str_len) {
366
1.58M
            if (str[i] == '\r' && i + 1 < str_len && str[i+1] == '\n')
367
1.83k
                i += 2;
368
1.58M
            else
369
1.58M
                i++;
370
1.58M
            if (keepends)
371
0
                eol = i;
372
1.58M
        }
373
1.58M
#if !STRINGLIB_MUTABLE
374
1.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
208
            if (PyList_Append(list, str_obj))
377
0
                goto onError;
378
208
            break;
379
208
        }
380
1.58M
#endif
381
3.17M
        SPLIT_APPEND(str, j, eol);
382
1.58M
        j = i;
383
1.58M
    }
384
830
    return list;
385
386
0
  onError:
387
0
    Py_DECREF(list);
388
    return NULL;
389
830
}
unicodeobject.c:ucs2lib_splitlines
Line
Count
Source
339
7.02k
{
340
    /* This does not use the preallocated list because splitlines is
341
       usually run with hundreds of newlines.  The overhead of
342
       switching between PyList_SET_ITEM and append causes about a
343
       2-3% slowdown for that common case.  A smarter implementation
344
       could move the if check out, so the SET_ITEMs are done first
345
       and the appends only done when the prealloc buffer is full.
346
       That's too much work for little gain.*/
347
348
7.02k
    Py_ssize_t i;
349
7.02k
    Py_ssize_t j;
350
7.02k
    PyObject *list = PyList_New(0);
351
7.02k
    PyObject *sub;
352
353
7.02k
    if (list == NULL)
354
0
        return NULL;
355
356
11.3M
    for (i = j = 0; i < str_len; ) {
357
11.3M
        Py_ssize_t eol;
358
359
        /* Find a line and append it */
360
96.3M
        while (i < str_len && !STRINGLIB_ISLINEBREAK(str[i]))
361
85.0M
            i++;
362
363
        /* Skip the line break reading CRLF as one line break */
364
11.3M
        eol = i;
365
11.3M
        if (i < str_len) {
366
11.3M
            if (str[i] == '\r' && i + 1 < str_len && str[i+1] == '\n')
367
19.9k
                i += 2;
368
11.3M
            else
369
11.3M
                i++;
370
11.3M
            if (keepends)
371
0
                eol = i;
372
11.3M
        }
373
11.3M
#if !STRINGLIB_MUTABLE
374
11.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.95k
            if (PyList_Append(list, str_obj))
377
0
                goto onError;
378
2.95k
            break;
379
2.95k
        }
380
11.3M
#endif
381
22.6M
        SPLIT_APPEND(str, j, eol);
382
11.3M
        j = i;
383
11.3M
    }
384
7.02k
    return list;
385
386
0
  onError:
387
0
    Py_DECREF(list);
388
    return NULL;
389
7.02k
}
unicodeobject.c:ucs4lib_splitlines
Line
Count
Source
339
2.92k
{
340
    /* This does not use the preallocated list because splitlines is
341
       usually run with hundreds of newlines.  The overhead of
342
       switching between PyList_SET_ITEM and append causes about a
343
       2-3% slowdown for that common case.  A smarter implementation
344
       could move the if check out, so the SET_ITEMs are done first
345
       and the appends only done when the prealloc buffer is full.
346
       That's too much work for little gain.*/
347
348
2.92k
    Py_ssize_t i;
349
2.92k
    Py_ssize_t j;
350
2.92k
    PyObject *list = PyList_New(0);
351
2.92k
    PyObject *sub;
352
353
2.92k
    if (list == NULL)
354
0
        return NULL;
355
356
11.7M
    for (i = j = 0; i < str_len; ) {
357
11.7M
        Py_ssize_t eol;
358
359
        /* Find a line and append it */
360
114M
        while (i < str_len && !STRINGLIB_ISLINEBREAK(str[i]))
361
102M
            i++;
362
363
        /* Skip the line break reading CRLF as one line break */
364
11.7M
        eol = i;
365
11.7M
        if (i < str_len) {
366
11.7M
            if (str[i] == '\r' && i + 1 < str_len && str[i+1] == '\n')
367
62.5k
                i += 2;
368
11.6M
            else
369
11.6M
                i++;
370
11.7M
            if (keepends)
371
0
                eol = i;
372
11.7M
        }
373
11.7M
#if !STRINGLIB_MUTABLE
374
11.7M
        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.14k
            if (PyList_Append(list, str_obj))
377
0
                goto onError;
378
1.14k
            break;
379
1.14k
        }
380
11.7M
#endif
381
23.4M
        SPLIT_APPEND(str, j, eol);
382
11.7M
        j = i;
383
11.7M
    }
384
2.92k
    return list;
385
386
0
  onError:
387
0
    Py_DECREF(list);
388
    return NULL;
389
2.92k
}
Unexecuted instantiation: bytearrayobject.c:stringlib_splitlines
390