Coverage Report

Created: 2025-07-18 06:09

/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
95.3M
#define MAX_PREALLOC 12
15
16
/* 5 splits gives 6 elements */
17
#define PREALLOC_SIZE(maxsplit) \
18
20.3M
    (maxsplit >= MAX_PREALLOC ? MAX_PREALLOC : maxsplit+1)
19
20
#define SPLIT_APPEND(data, left, right)         \
21
29.9M
    sub = STRINGLIB_NEW((data) + (left),        \
22
29.9M
                        (right) - (left));      \
23
29.9M
    if (sub == NULL)                            \
24
29.9M
        goto onError;                           \
25
29.9M
    if (PyList_Append(list, sub)) {             \
26
0
        Py_DECREF(sub);                         \
27
0
        goto onError;                           \
28
0
    }                                           \
29
29.9M
    else                                        \
30
29.9M
        Py_DECREF(sub);
31
32
51.7M
#define SPLIT_ADD(data, left, right) {          \
33
51.7M
    sub = STRINGLIB_NEW((data) + (left),        \
34
51.7M
                        (right) - (left));      \
35
51.7M
    if (sub == NULL)                            \
36
51.7M
        goto onError;                           \
37
51.7M
    if (count < MAX_PREALLOC) {                 \
38
30.4M
        PyList_SET_ITEM(list, count, sub);      \
39
30.4M
    } else {                                    \
40
21.3M
        if (PyList_Append(list, sub)) {         \
41
0
            Py_DECREF(sub);                     \
42
0
            goto onError;                       \
43
0
        }                                       \
44
21.3M
        else                                    \
45
21.3M
            Py_DECREF(sub);                     \
46
21.3M
    }                                           \
47
51.7M
    count++; }
48
49
50
/* Always force the list to the expected size. */
51
20.3M
#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
165k
{
58
165k
    Py_ssize_t i, j, count=0;
59
165k
    PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
60
165k
    PyObject *sub;
61
62
165k
    if (list == NULL)
63
0
        return NULL;
64
65
165k
    i = j = 0;
66
1.98M
    while (maxcount-- > 0) {
67
3.80M
        while (i < str_len && STRINGLIB_ISSPACE(str[i]))
68
1.85M
            i++;
69
1.95M
        if (i == str_len) break;
70
1.85M
        j = i; i++;
71
110M
        while (i < str_len && !STRINGLIB_ISSPACE(str[i]))
72
108M
            i++;
73
#if !STRINGLIB_MUTABLE
74
1.85M
        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
39.9k
            Py_INCREF(str_obj);
77
39.9k
            PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
78
39.9k
            count++;
79
39.9k
            break;
80
39.9k
        }
81
1.81M
#endif
82
5.45M
        SPLIT_ADD(str, j, i);
83
5.45M
    }
84
85
165k
    if (i < str_len) {
86
        /* Only occurs when maxcount was reached */
87
        /* Skip any remaining whitespace and copy to end of string */
88
56.4k
        while (i < str_len && STRINGLIB_ISSPACE(str[i]))
89
30.5k
            i++;
90
25.9k
        if (i != str_len)
91
25.9k
            SPLIT_ADD(str, i, str_len);
92
25.9k
    }
93
165k
    FIX_PREALLOC_SIZE(list);
94
165k
    return list;
95
96
0
  onError:
97
0
    Py_DECREF(list);
98
0
    return NULL;
99
165k
}
Unexecuted instantiation: bytesobject.c:stringlib_split_whitespace
unicodeobject.c:asciilib_split_whitespace
Line
Count
Source
57
76.7k
{
58
76.7k
    Py_ssize_t i, j, count=0;
59
76.7k
    PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
60
76.7k
    PyObject *sub;
61
62
76.7k
    if (list == NULL)
63
0
        return NULL;
64
65
76.7k
    i = j = 0;
66
575k
    while (maxcount-- > 0) {
67
1.01M
        while (i < str_len && STRINGLIB_ISSPACE(str[i]))
68
453k
            i++;
69
558k
        if (i == str_len) break;
70
520k
        j = i; i++;
71
38.4M
        while (i < str_len && !STRINGLIB_ISSPACE(str[i]))
72
37.8M
            i++;
73
520k
#if !STRINGLIB_MUTABLE
74
520k
        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
20.9k
            Py_INCREF(str_obj);
77
20.9k
            PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
78
20.9k
            count++;
79
20.9k
            break;
80
20.9k
        }
81
499k
#endif
82
1.49M
        SPLIT_ADD(str, j, i);
83
1.49M
    }
84
85
76.7k
    if (i < str_len) {
86
        /* Only occurs when maxcount was reached */
87
        /* Skip any remaining whitespace and copy to end of string */
88
24.6k
        while (i < str_len && STRINGLIB_ISSPACE(str[i]))
89
12.7k
            i++;
90
11.9k
        if (i != str_len)
91
11.9k
            SPLIT_ADD(str, i, str_len);
92
11.9k
    }
93
76.7k
    FIX_PREALLOC_SIZE(list);
94
76.7k
    return list;
95
96
0
  onError:
97
0
    Py_DECREF(list);
98
0
    return NULL;
99
76.7k
}
unicodeobject.c:ucs1lib_split_whitespace
Line
Count
Source
57
30.9k
{
58
30.9k
    Py_ssize_t i, j, count=0;
59
30.9k
    PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
60
30.9k
    PyObject *sub;
61
62
30.9k
    if (list == NULL)
63
0
        return NULL;
64
65
30.9k
    i = j = 0;
66
528k
    while (maxcount-- > 0) {
67
994k
        while (i < str_len && STRINGLIB_ISSPACE(str[i]))
68
480k
            i++;
69
514k
        if (i == str_len) break;
70
503k
        j = i; i++;
71
21.7M
        while (i < str_len && !STRINGLIB_ISSPACE(str[i]))
72
21.2M
            i++;
73
503k
#if !STRINGLIB_MUTABLE
74
503k
        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
5.98k
            Py_INCREF(str_obj);
77
5.98k
            PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
78
5.98k
            count++;
79
5.98k
            break;
80
5.98k
        }
81
497k
#endif
82
1.49M
        SPLIT_ADD(str, j, i);
83
1.49M
    }
84
85
30.9k
    if (i < str_len) {
86
        /* Only occurs when maxcount was reached */
87
        /* Skip any remaining whitespace and copy to end of string */
88
30.4k
        while (i < str_len && STRINGLIB_ISSPACE(str[i]))
89
17.1k
            i++;
90
13.3k
        if (i != str_len)
91
13.3k
            SPLIT_ADD(str, i, str_len);
92
13.3k
    }
93
30.9k
    FIX_PREALLOC_SIZE(list);
94
30.9k
    return list;
95
96
0
  onError:
97
0
    Py_DECREF(list);
98
0
    return NULL;
99
30.9k
}
unicodeobject.c:ucs2lib_split_whitespace
Line
Count
Source
57
47.7k
{
58
47.7k
    Py_ssize_t i, j, count=0;
59
47.7k
    PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
60
47.7k
    PyObject *sub;
61
62
47.7k
    if (list == NULL)
63
0
        return NULL;
64
65
47.7k
    i = j = 0;
66
655k
    while (maxcount-- > 0) {
67
1.35M
        while (i < str_len && STRINGLIB_ISSPACE(str[i]))
68
695k
            i++;
69
654k
        if (i == str_len) break;
70
618k
        j = i; i++;
71
35.1M
        while (i < str_len && !STRINGLIB_ISSPACE(str[i]))
72
34.5M
            i++;
73
618k
#if !STRINGLIB_MUTABLE
74
618k
        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
10.9k
            Py_INCREF(str_obj);
77
10.9k
            PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
78
10.9k
            count++;
79
10.9k
            break;
80
10.9k
        }
81
607k
#endif
82
1.82M
        SPLIT_ADD(str, j, i);
83
1.82M
    }
84
85
47.7k
    if (i < str_len) {
86
        /* Only occurs when maxcount was reached */
87
        /* Skip any remaining whitespace and copy to end of string */
88
1.37k
        while (i < str_len && STRINGLIB_ISSPACE(str[i]))
89
689
            i++;
90
689
        if (i != str_len)
91
689
            SPLIT_ADD(str, i, str_len);
92
689
    }
93
47.7k
    FIX_PREALLOC_SIZE(list);
94
47.7k
    return list;
95
96
0
  onError:
97
0
    Py_DECREF(list);
98
0
    return NULL;
99
47.7k
}
unicodeobject.c:ucs4lib_split_whitespace
Line
Count
Source
57
10.4k
{
58
10.4k
    Py_ssize_t i, j, count=0;
59
10.4k
    PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
60
10.4k
    PyObject *sub;
61
62
10.4k
    if (list == NULL)
63
0
        return NULL;
64
65
10.4k
    i = j = 0;
66
224k
    while (maxcount-- > 0) {
67
446k
        while (i < str_len && STRINGLIB_ISSPACE(str[i]))
68
221k
            i++;
69
224k
        if (i == str_len) break;
70
216k
        j = i; i++;
71
15.4M
        while (i < str_len && !STRINGLIB_ISSPACE(str[i]))
72
15.2M
            i++;
73
216k
#if !STRINGLIB_MUTABLE
74
216k
        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
1.94k
            Py_INCREF(str_obj);
77
1.94k
            PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
78
1.94k
            count++;
79
1.94k
            break;
80
1.94k
        }
81
214k
#endif
82
642k
        SPLIT_ADD(str, j, i);
83
642k
    }
84
85
10.4k
    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
10.4k
    FIX_PREALLOC_SIZE(list);
94
10.4k
    return list;
95
96
0
  onError:
97
0
    Py_DECREF(list);
98
0
    return NULL;
99
10.4k
}
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
19.9M
{
107
19.9M
    Py_ssize_t i, j, count=0;
108
19.9M
    PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
109
19.9M
    PyObject *sub;
110
111
19.9M
    if (list == NULL)
112
0
        return NULL;
113
114
19.9M
    i = j = 0;
115
73.1M
    while ((j < str_len) && (maxcount-- > 0)) {
116
371M
        for(; j < str_len; j++) {
117
            /* I found that using memchr makes no difference */
118
353M
            if (str[j] == ch) {
119
35.2M
                SPLIT_ADD(str, i, j);
120
35.2M
                i = j = j + 1;
121
35.2M
                break;
122
35.2M
            }
123
353M
        }
124
53.1M
    }
125
#if !STRINGLIB_MUTABLE
126
19.9M
    if (count == 0 && STRINGLIB_CHECK_EXACT(str_obj)) {
127
        /* ch not in str_obj, so just use str_obj as list[0] */
128
5.63M
        Py_INCREF(str_obj);
129
5.63M
        PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
130
5.63M
        count++;
131
5.63M
    } else
132
14.3M
#endif
133
14.3M
    if (i <= str_len) {
134
28.6M
        SPLIT_ADD(str, i, str_len);
135
28.6M
    }
136
19.9M
    FIX_PREALLOC_SIZE(list);
137
19.9M
    return list;
138
139
0
  onError:
140
0
    Py_DECREF(list);
141
0
    return NULL;
142
0
}
bytesobject.c:stringlib_split_char
Line
Count
Source
106
2.63M
{
107
2.63M
    Py_ssize_t i, j, count=0;
108
2.63M
    PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
109
2.63M
    PyObject *sub;
110
111
2.63M
    if (list == NULL)
112
0
        return NULL;
113
114
2.63M
    i = j = 0;
115
10.1M
    while ((j < str_len) && (maxcount-- > 0)) {
116
54.8M
        for(; j < str_len; j++) {
117
            /* I found that using memchr makes no difference */
118
52.6M
            if (str[j] == ch) {
119
5.35M
                SPLIT_ADD(str, i, j);
120
5.35M
                i = j = j + 1;
121
5.35M
                break;
122
5.35M
            }
123
52.6M
        }
124
7.55M
    }
125
2.63M
#if !STRINGLIB_MUTABLE
126
2.63M
    if (count == 0 && STRINGLIB_CHECK_EXACT(str_obj)) {
127
        /* ch not in str_obj, so just use str_obj as list[0] */
128
2.12M
        Py_INCREF(str_obj);
129
2.12M
        PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
130
2.12M
        count++;
131
2.12M
    } else
132
514k
#endif
133
514k
    if (i <= str_len) {
134
1.02M
        SPLIT_ADD(str, i, str_len);
135
1.02M
    }
136
2.63M
    FIX_PREALLOC_SIZE(list);
137
2.63M
    return list;
138
139
0
  onError:
140
0
    Py_DECREF(list);
141
0
    return NULL;
142
2.63M
}
unicodeobject.c:asciilib_split_char
Line
Count
Source
106
16.1M
{
107
16.1M
    Py_ssize_t i, j, count=0;
108
16.1M
    PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
109
16.1M
    PyObject *sub;
110
111
16.1M
    if (list == NULL)
112
0
        return NULL;
113
114
16.1M
    i = j = 0;
115
49.8M
    while ((j < str_len) && (maxcount-- > 0)) {
116
201M
        for(; j < str_len; j++) {
117
            /* I found that using memchr makes no difference */
118
185M
            if (str[j] == ch) {
119
18.1M
                SPLIT_ADD(str, i, j);
120
18.1M
                i = j = j + 1;
121
18.1M
                break;
122
18.1M
            }
123
185M
        }
124
33.6M
    }
125
16.1M
#if !STRINGLIB_MUTABLE
126
16.1M
    if (count == 0 && STRINGLIB_CHECK_EXACT(str_obj)) {
127
        /* ch not in str_obj, so just use str_obj as list[0] */
128
3.37M
        Py_INCREF(str_obj);
129
3.37M
        PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
130
3.37M
        count++;
131
3.37M
    } else
132
12.7M
#endif
133
12.7M
    if (i <= str_len) {
134
25.5M
        SPLIT_ADD(str, i, str_len);
135
25.5M
    }
136
16.1M
    FIX_PREALLOC_SIZE(list);
137
16.1M
    return list;
138
139
0
  onError:
140
0
    Py_DECREF(list);
141
0
    return NULL;
142
16.1M
}
unicodeobject.c:ucs1lib_split_char
Line
Count
Source
106
1.06M
{
107
1.06M
    Py_ssize_t i, j, count=0;
108
1.06M
    PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
109
1.06M
    PyObject *sub;
110
111
1.06M
    if (list == NULL)
112
0
        return NULL;
113
114
1.06M
    i = j = 0;
115
8.29M
    while ((j < str_len) && (maxcount-- > 0)) {
116
42.8M
        for(; j < str_len; j++) {
117
            /* I found that using memchr makes no difference */
118
42.6M
            if (str[j] == ch) {
119
7.07M
                SPLIT_ADD(str, i, j);
120
7.07M
                i = j = j + 1;
121
7.07M
                break;
122
7.07M
            }
123
42.6M
        }
124
7.22M
    }
125
1.06M
#if !STRINGLIB_MUTABLE
126
1.06M
    if (count == 0 && STRINGLIB_CHECK_EXACT(str_obj)) {
127
        /* ch not in str_obj, so just use str_obj as list[0] */
128
125k
        Py_INCREF(str_obj);
129
125k
        PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
130
125k
        count++;
131
125k
    } else
132
939k
#endif
133
939k
    if (i <= str_len) {
134
1.87M
        SPLIT_ADD(str, i, str_len);
135
1.87M
    }
136
1.06M
    FIX_PREALLOC_SIZE(list);
137
1.06M
    return list;
138
139
0
  onError:
140
0
    Py_DECREF(list);
141
0
    return NULL;
142
1.06M
}
unicodeobject.c:ucs2lib_split_char
Line
Count
Source
106
88.7k
{
107
88.7k
    Py_ssize_t i, j, count=0;
108
88.7k
    PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
109
88.7k
    PyObject *sub;
110
111
88.7k
    if (list == NULL)
112
0
        return NULL;
113
114
88.7k
    i = j = 0;
115
2.98M
    while ((j < str_len) && (maxcount-- > 0)) {
116
42.8M
        for(; j < str_len; j++) {
117
            /* I found that using memchr makes no difference */
118
42.7M
            if (str[j] == ch) {
119
2.83M
                SPLIT_ADD(str, i, j);
120
2.83M
                i = j = j + 1;
121
2.83M
                break;
122
2.83M
            }
123
42.7M
        }
124
2.89M
    }
125
88.7k
#if !STRINGLIB_MUTABLE
126
88.7k
    if (count == 0 && STRINGLIB_CHECK_EXACT(str_obj)) {
127
        /* ch not in str_obj, so just use str_obj as list[0] */
128
8.03k
        Py_INCREF(str_obj);
129
8.03k
        PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
130
8.03k
        count++;
131
8.03k
    } else
132
80.6k
#endif
133
80.6k
    if (i <= str_len) {
134
161k
        SPLIT_ADD(str, i, str_len);
135
161k
    }
136
88.7k
    FIX_PREALLOC_SIZE(list);
137
88.7k
    return list;
138
139
0
  onError:
140
0
    Py_DECREF(list);
141
0
    return NULL;
142
88.7k
}
unicodeobject.c:ucs4lib_split_char
Line
Count
Source
106
14.6k
{
107
14.6k
    Py_ssize_t i, j, count=0;
108
14.6k
    PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
109
14.6k
    PyObject *sub;
110
111
14.6k
    if (list == NULL)
112
0
        return NULL;
113
114
14.6k
    i = j = 0;
115
1.86M
    while ((j < str_len) && (maxcount-- > 0)) {
116
29.3M
        for(; j < str_len; j++) {
117
            /* I found that using memchr makes no difference */
118
29.3M
            if (str[j] == ch) {
119
1.83M
                SPLIT_ADD(str, i, j);
120
1.83M
                i = j = j + 1;
121
1.83M
                break;
122
1.83M
            }
123
29.3M
        }
124
1.84M
    }
125
14.6k
#if !STRINGLIB_MUTABLE
126
14.6k
    if (count == 0 && STRINGLIB_CHECK_EXACT(str_obj)) {
127
        /* ch not in str_obj, so just use str_obj as list[0] */
128
395
        Py_INCREF(str_obj);
129
395
        PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
130
395
        count++;
131
395
    } else
132
14.2k
#endif
133
14.2k
    if (i <= str_len) {
134
28.5k
        SPLIT_ADD(str, i, str_len);
135
28.5k
    }
136
14.6k
    FIX_PREALLOC_SIZE(list);
137
14.6k
    return list;
138
139
0
  onError:
140
0
    Py_DECREF(list);
141
0
    return NULL;
142
14.6k
}
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
20.1M
{
150
20.1M
    Py_ssize_t i, j, pos, count=0;
151
20.1M
    PyObject *list, *sub;
152
153
20.1M
    if (sep_len == 0) {
154
0
        PyErr_SetString(PyExc_ValueError, "empty separator");
155
0
        return NULL;
156
0
    }
157
20.1M
    else if (sep_len == 1)
158
19.9M
        return STRINGLIB(split_char)(str_obj, str, str_len, sep[0], maxcount);
159
160
202k
    list = PyList_New(PREALLOC_SIZE(maxcount));
161
202k
    if (list == NULL)
162
0
        return NULL;
163
164
202k
    i = j = 0;
165
365k
    while (maxcount-- > 0) {
166
202k
        pos = FASTSEARCH(str+i, str_len-i, sep, sep_len, -1, FAST_SEARCH);
167
202k
        if (pos < 0)
168
38.8k
            break;
169
163k
        j = i + pos;
170
326k
        SPLIT_ADD(str, i, j);
171
326k
        i = j + sep_len;
172
326k
    }
173
#if !STRINGLIB_MUTABLE
174
202k
    if (count == 0 && STRINGLIB_CHECK_EXACT(str_obj)) {
175
        /* No match in str_obj, so just use it as list[0] */
176
38.8k
        Py_INCREF(str_obj);
177
38.8k
        PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
178
38.8k
        count++;
179
38.8k
    } else
180
163k
#endif
181
163k
    {
182
326k
        SPLIT_ADD(str, i, str_len);
183
326k
    }
184
202k
    FIX_PREALLOC_SIZE(list);
185
202k
    return list;
186
187
0
  onError:
188
0
    Py_DECREF(list);
189
0
    return NULL;
190
0
}
bytesobject.c:stringlib_split
Line
Count
Source
149
2.63M
{
150
2.63M
    Py_ssize_t i, j, pos, count=0;
151
2.63M
    PyObject *list, *sub;
152
153
2.63M
    if (sep_len == 0) {
154
0
        PyErr_SetString(PyExc_ValueError, "empty separator");
155
0
        return NULL;
156
0
    }
157
2.63M
    else if (sep_len == 1)
158
2.63M
        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
16.2M
{
150
16.2M
    Py_ssize_t i, j, pos, count=0;
151
16.2M
    PyObject *list, *sub;
152
153
16.2M
    if (sep_len == 0) {
154
0
        PyErr_SetString(PyExc_ValueError, "empty separator");
155
0
        return NULL;
156
0
    }
157
16.2M
    else if (sep_len == 1)
158
16.1M
        return STRINGLIB(split_char)(str_obj, str, str_len, sep[0], maxcount);
159
160
80.8k
    list = PyList_New(PREALLOC_SIZE(maxcount));
161
80.8k
    if (list == NULL)
162
0
        return NULL;
163
164
80.8k
    i = j = 0;
165
132k
    while (maxcount-- > 0) {
166
80.8k
        pos = FASTSEARCH(str+i, str_len-i, sep, sep_len, -1, FAST_SEARCH);
167
80.8k
        if (pos < 0)
168
28.7k
            break;
169
52.1k
        j = i + pos;
170
104k
        SPLIT_ADD(str, i, j);
171
104k
        i = j + sep_len;
172
104k
    }
173
80.8k
#if !STRINGLIB_MUTABLE
174
80.8k
    if (count == 0 && STRINGLIB_CHECK_EXACT(str_obj)) {
175
        /* No match in str_obj, so just use it as list[0] */
176
28.7k
        Py_INCREF(str_obj);
177
28.7k
        PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
178
28.7k
        count++;
179
28.7k
    } else
180
52.1k
#endif
181
52.1k
    {
182
104k
        SPLIT_ADD(str, i, str_len);
183
104k
    }
184
80.8k
    FIX_PREALLOC_SIZE(list);
185
80.8k
    return list;
186
187
0
  onError:
188
0
    Py_DECREF(list);
189
0
    return NULL;
190
80.8k
}
unicodeobject.c:ucs1lib_split
Line
Count
Source
149
1.07M
{
150
1.07M
    Py_ssize_t i, j, pos, count=0;
151
1.07M
    PyObject *list, *sub;
152
153
1.07M
    if (sep_len == 0) {
154
0
        PyErr_SetString(PyExc_ValueError, "empty separator");
155
0
        return NULL;
156
0
    }
157
1.07M
    else if (sep_len == 1)
158
1.06M
        return STRINGLIB(split_char)(str_obj, str, str_len, sep[0], maxcount);
159
160
12.7k
    list = PyList_New(PREALLOC_SIZE(maxcount));
161
12.7k
    if (list == NULL)
162
0
        return NULL;
163
164
12.7k
    i = j = 0;
165
24.0k
    while (maxcount-- > 0) {
166
12.7k
        pos = FASTSEARCH(str+i, str_len-i, sep, sep_len, -1, FAST_SEARCH);
167
12.7k
        if (pos < 0)
168
1.49k
            break;
169
11.2k
        j = i + pos;
170
22.5k
        SPLIT_ADD(str, i, j);
171
22.5k
        i = j + sep_len;
172
22.5k
    }
173
12.7k
#if !STRINGLIB_MUTABLE
174
12.7k
    if (count == 0 && STRINGLIB_CHECK_EXACT(str_obj)) {
175
        /* No match in str_obj, so just use it as list[0] */
176
1.49k
        Py_INCREF(str_obj);
177
1.49k
        PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
178
1.49k
        count++;
179
1.49k
    } else
180
11.2k
#endif
181
11.2k
    {
182
22.5k
        SPLIT_ADD(str, i, str_len);
183
22.5k
    }
184
12.7k
    FIX_PREALLOC_SIZE(list);
185
12.7k
    return list;
186
187
0
  onError:
188
0
    Py_DECREF(list);
189
0
    return NULL;
190
12.7k
}
unicodeobject.c:ucs2lib_split
Line
Count
Source
149
178k
{
150
178k
    Py_ssize_t i, j, pos, count=0;
151
178k
    PyObject *list, *sub;
152
153
178k
    if (sep_len == 0) {
154
0
        PyErr_SetString(PyExc_ValueError, "empty separator");
155
0
        return NULL;
156
0
    }
157
178k
    else if (sep_len == 1)
158
88.7k
        return STRINGLIB(split_char)(str_obj, str, str_len, sep[0], maxcount);
159
160
89.6k
    list = PyList_New(PREALLOC_SIZE(maxcount));
161
89.6k
    if (list == NULL)
162
0
        return NULL;
163
164
89.6k
    i = j = 0;
165
173k
    while (maxcount-- > 0) {
166
89.6k
        pos = FASTSEARCH(str+i, str_len-i, sep, sep_len, -1, FAST_SEARCH);
167
89.6k
        if (pos < 0)
168
5.80k
            break;
169
83.8k
        j = i + pos;
170
167k
        SPLIT_ADD(str, i, j);
171
167k
        i = j + sep_len;
172
167k
    }
173
89.6k
#if !STRINGLIB_MUTABLE
174
89.6k
    if (count == 0 && STRINGLIB_CHECK_EXACT(str_obj)) {
175
        /* No match in str_obj, so just use it as list[0] */
176
5.80k
        Py_INCREF(str_obj);
177
5.80k
        PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
178
5.80k
        count++;
179
5.80k
    } else
180
83.8k
#endif
181
83.8k
    {
182
167k
        SPLIT_ADD(str, i, str_len);
183
167k
    }
184
89.6k
    FIX_PREALLOC_SIZE(list);
185
89.6k
    return list;
186
187
0
  onError:
188
0
    Py_DECREF(list);
189
0
    return NULL;
190
89.6k
}
unicodeobject.c:ucs4lib_split
Line
Count
Source
149
33.5k
{
150
33.5k
    Py_ssize_t i, j, pos, count=0;
151
33.5k
    PyObject *list, *sub;
152
153
33.5k
    if (sep_len == 0) {
154
0
        PyErr_SetString(PyExc_ValueError, "empty separator");
155
0
        return NULL;
156
0
    }
157
33.5k
    else if (sep_len == 1)
158
14.6k
        return STRINGLIB(split_char)(str_obj, str, str_len, sep[0], maxcount);
159
160
18.9k
    list = PyList_New(PREALLOC_SIZE(maxcount));
161
18.9k
    if (list == NULL)
162
0
        return NULL;
163
164
18.9k
    i = j = 0;
165
35.1k
    while (maxcount-- > 0) {
166
18.9k
        pos = FASTSEARCH(str+i, str_len-i, sep, sep_len, -1, FAST_SEARCH);
167
18.9k
        if (pos < 0)
168
2.71k
            break;
169
16.1k
        j = i + pos;
170
32.3k
        SPLIT_ADD(str, i, j);
171
32.3k
        i = j + sep_len;
172
32.3k
    }
173
18.9k
#if !STRINGLIB_MUTABLE
174
18.9k
    if (count == 0 && STRINGLIB_CHECK_EXACT(str_obj)) {
175
        /* No match in str_obj, so just use it as list[0] */
176
2.71k
        Py_INCREF(str_obj);
177
2.71k
        PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
178
2.71k
        count++;
179
2.71k
    } else
180
16.1k
#endif
181
16.1k
    {
182
32.3k
        SPLIT_ADD(str, i, str_len);
183
32.3k
    }
184
18.9k
    FIX_PREALLOC_SIZE(list);
185
18.9k
    return list;
186
187
0
  onError:
188
0
    Py_DECREF(list);
189
0
    return NULL;
190
18.9k
}
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.8k
{
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.8k
    Py_ssize_t i;
349
13.8k
    Py_ssize_t j;
350
13.8k
    PyObject *list = PyList_New(0);
351
13.8k
    PyObject *sub;
352
353
13.8k
    if (list == NULL)
354
0
        return NULL;
355
356
29.9M
    for (i = j = 0; i < str_len; ) {
357
29.9M
        Py_ssize_t eol;
358
359
        /* Find a line and append it */
360
229M
        while (i < str_len && !STRINGLIB_ISLINEBREAK(str[i]))
361
199M
            i++;
362
363
        /* Skip the line break reading CRLF as one line break */
364
29.9M
        eol = i;
365
29.9M
        if (i < str_len) {
366
29.9M
            if (str[i] == '\r' && i + 1 < str_len && str[i+1] == '\n')
367
85.2k
                i += 2;
368
29.8M
            else
369
29.8M
                i++;
370
29.9M
            if (keepends)
371
0
                eol = i;
372
29.9M
        }
373
#if !STRINGLIB_MUTABLE
374
29.9M
        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.35k
            if (PyList_Append(list, str_obj))
377
0
                goto onError;
378
5.35k
            break;
379
5.35k
        }
380
29.9M
#endif
381
59.8M
        SPLIT_APPEND(str, j, eol);
382
29.9M
        j = i;
383
29.9M
    }
384
13.8k
    return list;
385
386
0
  onError:
387
0
    Py_DECREF(list);
388
0
    return NULL;
389
13.8k
}
Unexecuted instantiation: bytesobject.c:stringlib_splitlines
unicodeobject.c:asciilib_splitlines
Line
Count
Source
339
2.70k
{
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.70k
    Py_ssize_t i;
349
2.70k
    Py_ssize_t j;
350
2.70k
    PyObject *list = PyList_New(0);
351
2.70k
    PyObject *sub;
352
353
2.70k
    if (list == NULL)
354
0
        return NULL;
355
356
8.49M
    for (i = j = 0; i < str_len; ) {
357
8.49M
        Py_ssize_t eol;
358
359
        /* Find a line and append it */
360
45.1M
        while (i < str_len && !STRINGLIB_ISLINEBREAK(str[i]))
361
36.6M
            i++;
362
363
        /* Skip the line break reading CRLF as one line break */
364
8.49M
        eol = i;
365
8.49M
        if (i < str_len) {
366
8.49M
            if (str[i] == '\r' && i + 1 < str_len && str[i+1] == '\n')
367
10.4k
                i += 2;
368
8.48M
            else
369
8.48M
                i++;
370
8.49M
            if (keepends)
371
0
                eol = i;
372
8.49M
        }
373
8.49M
#if !STRINGLIB_MUTABLE
374
8.49M
        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
956
            if (PyList_Append(list, str_obj))
377
0
                goto onError;
378
956
            break;
379
956
        }
380
8.49M
#endif
381
16.9M
        SPLIT_APPEND(str, j, eol);
382
8.49M
        j = i;
383
8.49M
    }
384
2.70k
    return list;
385
386
0
  onError:
387
0
    Py_DECREF(list);
388
0
    return NULL;
389
2.70k
}
unicodeobject.c:ucs1lib_splitlines
Line
Count
Source
339
845
{
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
845
    Py_ssize_t i;
349
845
    Py_ssize_t j;
350
845
    PyObject *list = PyList_New(0);
351
845
    PyObject *sub;
352
353
845
    if (list == NULL)
354
0
        return NULL;
355
356
927k
    for (i = j = 0; i < str_len; ) {
357
926k
        Py_ssize_t eol;
358
359
        /* Find a line and append it */
360
9.19M
        while (i < str_len && !STRINGLIB_ISLINEBREAK(str[i]))
361
8.26M
            i++;
362
363
        /* Skip the line break reading CRLF as one line break */
364
926k
        eol = i;
365
926k
        if (i < str_len) {
366
926k
            if (str[i] == '\r' && i + 1 < str_len && str[i+1] == '\n')
367
1.35k
                i += 2;
368
924k
            else
369
924k
                i++;
370
926k
            if (keepends)
371
0
                eol = i;
372
926k
        }
373
926k
#if !STRINGLIB_MUTABLE
374
926k
        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
218
            if (PyList_Append(list, str_obj))
377
0
                goto onError;
378
218
            break;
379
218
        }
380
926k
#endif
381
1.85M
        SPLIT_APPEND(str, j, eol);
382
926k
        j = i;
383
926k
    }
384
845
    return list;
385
386
0
  onError:
387
0
    Py_DECREF(list);
388
0
    return NULL;
389
845
}
unicodeobject.c:ucs2lib_splitlines
Line
Count
Source
339
7.25k
{
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.25k
    Py_ssize_t i;
349
7.25k
    Py_ssize_t j;
350
7.25k
    PyObject *list = PyList_New(0);
351
7.25k
    PyObject *sub;
352
353
7.25k
    if (list == NULL)
354
0
        return NULL;
355
356
11.2M
    for (i = j = 0; i < str_len; ) {
357
11.2M
        Py_ssize_t eol;
358
359
        /* Find a line and append it */
360
77.2M
        while (i < str_len && !STRINGLIB_ISLINEBREAK(str[i]))
361
65.9M
            i++;
362
363
        /* Skip the line break reading CRLF as one line break */
364
11.2M
        eol = i;
365
11.2M
        if (i < str_len) {
366
11.2M
            if (str[i] == '\r' && i + 1 < str_len && str[i+1] == '\n')
367
25.0k
                i += 2;
368
11.2M
            else
369
11.2M
                i++;
370
11.2M
            if (keepends)
371
0
                eol = i;
372
11.2M
        }
373
11.2M
#if !STRINGLIB_MUTABLE
374
11.2M
        if (j == 0 && eol == str_len && STRINGLIB_CHECK_EXACT(str_obj)) {
375
            /* No linebreak in str_obj, so just use it as list[0] */
376
3.00k
            if (PyList_Append(list, str_obj))
377
0
                goto onError;
378
3.00k
            break;
379
3.00k
        }
380
11.2M
#endif
381
22.5M
        SPLIT_APPEND(str, j, eol);
382
11.2M
        j = i;
383
11.2M
    }
384
7.25k
    return list;
385
386
0
  onError:
387
0
    Py_DECREF(list);
388
0
    return NULL;
389
7.25k
}
unicodeobject.c:ucs4lib_splitlines
Line
Count
Source
339
3.08k
{
340
    /* This does not use the preallocated list because splitlines is
341
       usually run with hundreds of newlines.  The overhead of
342
       switching between PyList_SET_ITEM and append causes about a
343
       2-3% slowdown for that common case.  A smarter implementation
344
       could move the if check out, so the SET_ITEMs are done first
345
       and the appends only done when the prealloc buffer is full.
346
       That's too much work for little gain.*/
347
348
3.08k
    Py_ssize_t i;
349
3.08k
    Py_ssize_t j;
350
3.08k
    PyObject *list = PyList_New(0);
351
3.08k
    PyObject *sub;
352
353
3.08k
    if (list == NULL)
354
0
        return NULL;
355
356
9.22M
    for (i = j = 0; i < str_len; ) {
357
9.22M
        Py_ssize_t eol;
358
359
        /* Find a line and append it */
360
98.2M
        while (i < str_len && !STRINGLIB_ISLINEBREAK(str[i]))
361
89.0M
            i++;
362
363
        /* Skip the line break reading CRLF as one line break */
364
9.22M
        eol = i;
365
9.22M
        if (i < str_len) {
366
9.22M
            if (str[i] == '\r' && i + 1 < str_len && str[i+1] == '\n')
367
48.3k
                i += 2;
368
9.17M
            else
369
9.17M
                i++;
370
9.22M
            if (keepends)
371
0
                eol = i;
372
9.22M
        }
373
9.22M
#if !STRINGLIB_MUTABLE
374
9.22M
        if (j == 0 && eol == str_len && STRINGLIB_CHECK_EXACT(str_obj)) {
375
            /* No linebreak in str_obj, so just use it as list[0] */
376
1.17k
            if (PyList_Append(list, str_obj))
377
0
                goto onError;
378
1.17k
            break;
379
1.17k
        }
380
9.22M
#endif
381
18.4M
        SPLIT_APPEND(str, j, eol);
382
9.22M
        j = i;
383
9.22M
    }
384
3.08k
    return list;
385
386
0
  onError:
387
0
    Py_DECREF(list);
388
0
    return NULL;
389
3.08k
}
Unexecuted instantiation: bytearrayobject.c:stringlib_splitlines
390