Coverage Report

Created: 2026-02-09 07:07

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
92.5M
#define MAX_PREALLOC 12
15
16
/* 5 splits gives 6 elements */
17
#define PREALLOC_SIZE(maxsplit) \
18
21.1M
    (maxsplit >= MAX_PREALLOC ? MAX_PREALLOC : maxsplit+1)
19
20
#define SPLIT_APPEND(data, left, right)         \
21
31.7M
    sub = STRINGLIB_NEW((data) + (left),        \
22
31.7M
                        (right) - (left));      \
23
31.7M
    if (sub == NULL)                            \
24
31.7M
        goto onError;                           \
25
31.7M
    if (PyList_Append(list, sub)) {             \
26
0
        Py_DECREF(sub);                         \
27
0
        goto onError;                           \
28
0
    }                                           \
29
31.7M
    else                                        \
30
31.7M
        Py_DECREF(sub);
31
32
46.8M
#define SPLIT_ADD(data, left, right) {          \
33
46.8M
    sub = STRINGLIB_NEW((data) + (left),        \
34
46.8M
                        (right) - (left));      \
35
46.8M
    if (sub == NULL)                            \
36
46.8M
        goto onError;                           \
37
46.8M
    if (count < MAX_PREALLOC) {                 \
38
31.0M
        PyList_SET_ITEM(list, count, sub);      \
39
31.0M
    } else {                                    \
40
15.8M
        if (PyList_Append(list, sub)) {         \
41
0
            Py_DECREF(sub);                     \
42
0
            goto onError;                       \
43
0
        }                                       \
44
15.8M
        else                                    \
45
15.8M
            Py_DECREF(sub);                     \
46
15.8M
    }                                           \
47
46.8M
    count++; }
48
49
50
/* Always force the list to the expected size. */
51
21.1M
#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
140k
{
58
140k
    Py_ssize_t i, j, count=0;
59
140k
    PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
60
140k
    PyObject *sub;
61
62
140k
    if (list == NULL)
63
0
        return NULL;
64
65
140k
    i = j = 0;
66
1.45M
    while (maxcount-- > 0) {
67
2.79M
        while (i < str_len && STRINGLIB_ISSPACE(str[i]))
68
1.37M
            i++;
69
1.42M
        if (i == str_len) break;
70
1.34M
        j = i; i++;
71
89.0M
        while (i < str_len && !STRINGLIB_ISSPACE(str[i]))
72
87.7M
            i++;
73
#if !STRINGLIB_MUTABLE
74
1.34M
        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
34.9k
            Py_INCREF(str_obj);
77
34.9k
            PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
78
34.9k
            count++;
79
34.9k
            break;
80
34.9k
        }
81
1.31M
#endif
82
3.93M
        SPLIT_ADD(str, j, i);
83
3.93M
    }
84
85
140k
    if (i < str_len) {
86
        /* Only occurs when maxcount was reached */
87
        /* Skip any remaining whitespace and copy to end of string */
88
58.3k
        while (i < str_len && STRINGLIB_ISSPACE(str[i]))
89
31.4k
            i++;
90
26.8k
        if (i != str_len)
91
26.8k
            SPLIT_ADD(str, i, str_len);
92
26.8k
    }
93
140k
    FIX_PREALLOC_SIZE(list);
94
140k
    return list;
95
96
0
  onError:
97
0
    Py_DECREF(list);
98
0
    return NULL;
99
140k
}
Unexecuted instantiation: bytesobject.c:stringlib_split_whitespace
unicodeobject.c:asciilib_split_whitespace
Line
Count
Source
57
61.7k
{
58
61.7k
    Py_ssize_t i, j, count=0;
59
61.7k
    PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
60
61.7k
    PyObject *sub;
61
62
61.7k
    if (list == NULL)
63
0
        return NULL;
64
65
61.7k
    i = j = 0;
66
513k
    while (maxcount-- > 0) {
67
929k
        while (i < str_len && STRINGLIB_ISSPACE(str[i]))
68
431k
            i++;
69
498k
        if (i == str_len) break;
70
469k
        j = i; i++;
71
40.6M
        while (i < str_len && !STRINGLIB_ISSPACE(str[i]))
72
40.2M
            i++;
73
469k
#if !STRINGLIB_MUTABLE
74
469k
        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
17.1k
            Py_INCREF(str_obj);
77
17.1k
            PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
78
17.1k
            count++;
79
17.1k
            break;
80
17.1k
        }
81
452k
#endif
82
1.35M
        SPLIT_ADD(str, j, i);
83
1.35M
    }
84
85
61.7k
    if (i < str_len) {
86
        /* Only occurs when maxcount was reached */
87
        /* Skip any remaining whitespace and copy to end of string */
88
29.6k
        while (i < str_len && STRINGLIB_ISSPACE(str[i]))
89
15.3k
            i++;
90
14.3k
        if (i != str_len)
91
14.3k
            SPLIT_ADD(str, i, str_len);
92
14.3k
    }
93
61.7k
    FIX_PREALLOC_SIZE(list);
94
61.7k
    return list;
95
96
0
  onError:
97
0
    Py_DECREF(list);
98
    return NULL;
99
61.7k
}
unicodeobject.c:ucs1lib_split_whitespace
Line
Count
Source
57
25.5k
{
58
25.5k
    Py_ssize_t i, j, count=0;
59
25.5k
    PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
60
25.5k
    PyObject *sub;
61
62
25.5k
    if (list == NULL)
63
0
        return NULL;
64
65
25.5k
    i = j = 0;
66
611k
    while (maxcount-- > 0) {
67
1.17M
        while (i < str_len && STRINGLIB_ISSPACE(str[i]))
68
570k
            i++;
69
599k
        if (i == str_len) break;
70
587k
        j = i; i++;
71
25.0M
        while (i < str_len && !STRINGLIB_ISSPACE(str[i]))
72
24.4M
            i++;
73
587k
#if !STRINGLIB_MUTABLE
74
587k
        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.64k
            Py_INCREF(str_obj);
77
1.64k
            PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
78
1.64k
            count++;
79
1.64k
            break;
80
1.64k
        }
81
586k
#endif
82
1.75M
        SPLIT_ADD(str, j, i);
83
1.75M
    }
84
85
25.5k
    if (i < str_len) {
86
        /* Only occurs when maxcount was reached */
87
        /* Skip any remaining whitespace and copy to end of string */
88
27.0k
        while (i < str_len && STRINGLIB_ISSPACE(str[i]))
89
15.3k
            i++;
90
11.6k
        if (i != str_len)
91
11.6k
            SPLIT_ADD(str, i, str_len);
92
11.6k
    }
93
25.5k
    FIX_PREALLOC_SIZE(list);
94
25.5k
    return list;
95
96
0
  onError:
97
0
    Py_DECREF(list);
98
    return NULL;
99
25.5k
}
unicodeobject.c:ucs2lib_split_whitespace
Line
Count
Source
57
43.5k
{
58
43.5k
    Py_ssize_t i, j, count=0;
59
43.5k
    PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
60
43.5k
    PyObject *sub;
61
62
43.5k
    if (list == NULL)
63
0
        return NULL;
64
65
43.5k
    i = j = 0;
66
269k
    while (maxcount-- > 0) {
67
588k
        while (i < str_len && STRINGLIB_ISSPACE(str[i]))
68
320k
            i++;
69
268k
        if (i == str_len) break;
70
238k
        j = i; i++;
71
15.6M
        while (i < str_len && !STRINGLIB_ISSPACE(str[i]))
72
15.4M
            i++;
73
238k
#if !STRINGLIB_MUTABLE
74
238k
        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
12.6k
            Py_INCREF(str_obj);
77
12.6k
            PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
78
12.6k
            count++;
79
12.6k
            break;
80
12.6k
        }
81
225k
#endif
82
676k
        SPLIT_ADD(str, j, i);
83
676k
    }
84
85
43.5k
    if (i < str_len) {
86
        /* Only occurs when maxcount was reached */
87
        /* Skip any remaining whitespace and copy to end of string */
88
1.67k
        while (i < str_len && STRINGLIB_ISSPACE(str[i]))
89
835
            i++;
90
835
        if (i != str_len)
91
835
            SPLIT_ADD(str, i, str_len);
92
835
    }
93
43.5k
    FIX_PREALLOC_SIZE(list);
94
43.5k
    return list;
95
96
0
  onError:
97
0
    Py_DECREF(list);
98
    return NULL;
99
43.5k
}
unicodeobject.c:ucs4lib_split_whitespace
Line
Count
Source
57
9.56k
{
58
9.56k
    Py_ssize_t i, j, count=0;
59
9.56k
    PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
60
9.56k
    PyObject *sub;
61
62
9.56k
    if (list == NULL)
63
0
        return NULL;
64
65
9.56k
    i = j = 0;
66
58.1k
    while (maxcount-- > 0) {
67
106k
        while (i < str_len && STRINGLIB_ISSPACE(str[i]))
68
48.9k
            i++;
69
57.8k
        if (i == str_len) break;
70
52.0k
        j = i; i++;
71
7.61M
        while (i < str_len && !STRINGLIB_ISSPACE(str[i]))
72
7.56M
            i++;
73
52.0k
#if !STRINGLIB_MUTABLE
74
52.0k
        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.46k
            Py_INCREF(str_obj);
77
3.46k
            PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
78
3.46k
            count++;
79
3.46k
            break;
80
3.46k
        }
81
48.5k
#endif
82
145k
        SPLIT_ADD(str, j, i);
83
145k
    }
84
85
9.56k
    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
9.56k
    FIX_PREALLOC_SIZE(list);
94
9.56k
    return list;
95
96
0
  onError:
97
0
    Py_DECREF(list);
98
    return NULL;
99
9.56k
}
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
20.8M
{
107
20.8M
    Py_ssize_t i, j, count=0;
108
20.8M
    PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
109
20.8M
    PyObject *sub;
110
111
20.8M
    if (list == NULL)
112
0
        return NULL;
113
114
20.8M
    i = j = 0;
115
70.2M
    while ((j < str_len) && (maxcount-- > 0)) {
116
347M
        for(; j < str_len; j++) {
117
            /* I found that using memchr makes no difference */
118
328M
            if (str[j] == ch) {
119
30.5M
                SPLIT_ADD(str, i, j);
120
30.5M
                i = j = j + 1;
121
30.5M
                break;
122
30.5M
            }
123
328M
        }
124
49.3M
    }
125
#if !STRINGLIB_MUTABLE
126
20.8M
    if (count == 0 && STRINGLIB_CHECK_EXACT(str_obj)) {
127
        /* ch not in str_obj, so just use str_obj as list[0] */
128
6.16M
        Py_INCREF(str_obj);
129
6.16M
        PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
130
6.16M
        count++;
131
6.16M
    } else
132
14.7M
#endif
133
14.7M
    if (i <= str_len) {
134
29.4M
        SPLIT_ADD(str, i, str_len);
135
29.4M
    }
136
20.8M
    FIX_PREALLOC_SIZE(list);
137
20.8M
    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.00M
{
107
3.00M
    Py_ssize_t i, j, count=0;
108
3.00M
    PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
109
3.00M
    PyObject *sub;
110
111
3.00M
    if (list == NULL)
112
0
        return NULL;
113
114
3.00M
    i = j = 0;
115
8.95M
    while ((j < str_len) && (maxcount-- > 0)) {
116
52.9M
        for(; j < str_len; j++) {
117
            /* I found that using memchr makes no difference */
118
50.3M
            if (str[j] == ch) {
119
3.34M
                SPLIT_ADD(str, i, j);
120
3.34M
                i = j = j + 1;
121
3.34M
                break;
122
3.34M
            }
123
50.3M
        }
124
5.94M
    }
125
3.00M
#if !STRINGLIB_MUTABLE
126
3.00M
    if (count == 0 && STRINGLIB_CHECK_EXACT(str_obj)) {
127
        /* ch not in str_obj, so just use str_obj as list[0] */
128
2.53M
        Py_INCREF(str_obj);
129
2.53M
        PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
130
2.53M
        count++;
131
2.53M
    } else
132
468k
#endif
133
468k
    if (i <= str_len) {
134
937k
        SPLIT_ADD(str, i, str_len);
135
937k
    }
136
3.00M
    FIX_PREALLOC_SIZE(list);
137
3.00M
    return list;
138
139
0
  onError:
140
0
    Py_DECREF(list);
141
    return NULL;
142
3.00M
}
unicodeobject.c:asciilib_split_char
Line
Count
Source
106
16.6M
{
107
16.6M
    Py_ssize_t i, j, count=0;
108
16.6M
    PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
109
16.6M
    PyObject *sub;
110
111
16.6M
    if (list == NULL)
112
0
        return NULL;
113
114
16.6M
    i = j = 0;
115
50.6M
    while ((j < str_len) && (maxcount-- > 0)) {
116
210M
        for(; j < str_len; j++) {
117
            /* I found that using memchr makes no difference */
118
194M
            if (str[j] == ch) {
119
18.0M
                SPLIT_ADD(str, i, j);
120
18.0M
                i = j = j + 1;
121
18.0M
                break;
122
18.0M
            }
123
194M
        }
124
33.9M
    }
125
16.6M
#if !STRINGLIB_MUTABLE
126
16.6M
    if (count == 0 && STRINGLIB_CHECK_EXACT(str_obj)) {
127
        /* ch not in str_obj, so just use str_obj as list[0] */
128
3.43M
        Py_INCREF(str_obj);
129
3.43M
        PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
130
3.43M
        count++;
131
3.43M
    } else
132
13.1M
#endif
133
13.1M
    if (i <= str_len) {
134
26.3M
        SPLIT_ADD(str, i, str_len);
135
26.3M
    }
136
16.6M
    FIX_PREALLOC_SIZE(list);
137
16.6M
    return list;
138
139
0
  onError:
140
0
    Py_DECREF(list);
141
    return NULL;
142
16.6M
}
unicodeobject.c:ucs1lib_split_char
Line
Count
Source
106
1.13M
{
107
1.13M
    Py_ssize_t i, j, count=0;
108
1.13M
    PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
109
1.13M
    PyObject *sub;
110
111
1.13M
    if (list == NULL)
112
0
        return NULL;
113
114
1.13M
    i = j = 0;
115
9.57M
    while ((j < str_len) && (maxcount-- > 0)) {
116
49.0M
        for(; j < str_len; j++) {
117
            /* I found that using memchr makes no difference */
118
48.8M
            if (str[j] == ch) {
119
8.25M
                SPLIT_ADD(str, i, j);
120
8.25M
                i = j = j + 1;
121
8.25M
                break;
122
8.25M
            }
123
48.8M
        }
124
8.43M
    }
125
1.13M
#if !STRINGLIB_MUTABLE
126
1.13M
    if (count == 0 && STRINGLIB_CHECK_EXACT(str_obj)) {
127
        /* ch not in str_obj, so just use str_obj as list[0] */
128
160k
        Py_INCREF(str_obj);
129
160k
        PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
130
160k
        count++;
131
160k
    } else
132
974k
#endif
133
974k
    if (i <= str_len) {
134
1.94M
        SPLIT_ADD(str, i, str_len);
135
1.94M
    }
136
1.13M
    FIX_PREALLOC_SIZE(list);
137
1.13M
    return list;
138
139
0
  onError:
140
0
    Py_DECREF(list);
141
    return NULL;
142
1.13M
}
unicodeobject.c:ucs2lib_split_char
Line
Count
Source
106
107k
{
107
107k
    Py_ssize_t i, j, count=0;
108
107k
    PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
109
107k
    PyObject *sub;
110
111
107k
    if (list == NULL)
112
0
        return NULL;
113
114
107k
    i = j = 0;
115
868k
    while ((j < str_len) && (maxcount-- > 0)) {
116
21.6M
        for(; j < str_len; j++) {
117
            /* I found that using memchr makes no difference */
118
21.5M
            if (str[j] == ch) {
119
683k
                SPLIT_ADD(str, i, j);
120
683k
                i = j = j + 1;
121
683k
                break;
122
683k
            }
123
21.5M
        }
124
760k
    }
125
107k
#if !STRINGLIB_MUTABLE
126
107k
    if (count == 0 && STRINGLIB_CHECK_EXACT(str_obj)) {
127
        /* ch not in str_obj, so just use str_obj as list[0] */
128
30.8k
        Py_INCREF(str_obj);
129
30.8k
        PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
130
30.8k
        count++;
131
30.8k
    } else
132
76.2k
#endif
133
76.2k
    if (i <= str_len) {
134
152k
        SPLIT_ADD(str, i, str_len);
135
152k
    }
136
107k
    FIX_PREALLOC_SIZE(list);
137
107k
    return list;
138
139
0
  onError:
140
0
    Py_DECREF(list);
141
    return NULL;
142
107k
}
unicodeobject.c:ucs4lib_split_char
Line
Count
Source
106
13.3k
{
107
13.3k
    Py_ssize_t i, j, count=0;
108
13.3k
    PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
109
13.3k
    PyObject *sub;
110
111
13.3k
    if (list == NULL)
112
0
        return NULL;
113
114
13.3k
    i = j = 0;
115
238k
    while ((j < str_len) && (maxcount-- > 0)) {
116
13.6M
        for(; j < str_len; j++) {
117
            /* I found that using memchr makes no difference */
118
13.6M
            if (str[j] == ch) {
119
214k
                SPLIT_ADD(str, i, j);
120
214k
                i = j = j + 1;
121
214k
                break;
122
214k
            }
123
13.6M
        }
124
224k
    }
125
13.3k
#if !STRINGLIB_MUTABLE
126
13.3k
    if (count == 0 && STRINGLIB_CHECK_EXACT(str_obj)) {
127
        /* ch not in str_obj, so just use str_obj as list[0] */
128
822
        Py_INCREF(str_obj);
129
822
        PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
130
822
        count++;
131
822
    } else
132
12.5k
#endif
133
12.5k
    if (i <= str_len) {
134
25.0k
        SPLIT_ADD(str, i, str_len);
135
25.0k
    }
136
13.3k
    FIX_PREALLOC_SIZE(list);
137
13.3k
    return list;
138
139
0
  onError:
140
0
    Py_DECREF(list);
141
    return NULL;
142
13.3k
}
Unexecuted instantiation: bytearrayobject.c:stringlib_split_char
143
144
Py_LOCAL_INLINE(PyObject *)
145
STRINGLIB(split)(PyObject* str_obj,
146
                const STRINGLIB_CHAR* str, Py_ssize_t str_len,
147
                const STRINGLIB_CHAR* sep, Py_ssize_t sep_len,
148
                Py_ssize_t maxcount)
149
21.0M
{
150
21.0M
    Py_ssize_t i, j, pos, count=0;
151
21.0M
    PyObject *list, *sub;
152
153
21.0M
    if (sep_len == 0) {
154
0
        PyErr_SetString(PyExc_ValueError, "empty separator");
155
0
        return NULL;
156
0
    }
157
21.0M
    else if (sep_len == 1)
158
20.8M
        return STRINGLIB(split_char)(str_obj, str, str_len, sep[0], maxcount);
159
160
171k
    list = PyList_New(PREALLOC_SIZE(maxcount));
161
171k
    if (list == NULL)
162
0
        return NULL;
163
164
171k
    i = j = 0;
165
300k
    while (maxcount-- > 0) {
166
171k
        pos = FASTSEARCH(str+i, str_len-i, sep, sep_len, -1, FAST_SEARCH);
167
171k
        if (pos < 0)
168
41.8k
            break;
169
129k
        j = i + pos;
170
258k
        SPLIT_ADD(str, i, j);
171
258k
        i = j + sep_len;
172
258k
    }
173
#if !STRINGLIB_MUTABLE
174
171k
    if (count == 0 && STRINGLIB_CHECK_EXACT(str_obj)) {
175
        /* No match in str_obj, so just use it as list[0] */
176
41.8k
        Py_INCREF(str_obj);
177
41.8k
        PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
178
41.8k
        count++;
179
41.8k
    } else
180
129k
#endif
181
129k
    {
182
258k
        SPLIT_ADD(str, i, str_len);
183
258k
    }
184
171k
    FIX_PREALLOC_SIZE(list);
185
171k
    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.00M
{
150
3.00M
    Py_ssize_t i, j, pos, count=0;
151
3.00M
    PyObject *list, *sub;
152
153
3.00M
    if (sep_len == 0) {
154
0
        PyErr_SetString(PyExc_ValueError, "empty separator");
155
0
        return NULL;
156
0
    }
157
3.00M
    else if (sep_len == 1)
158
3.00M
        return STRINGLIB(split_char)(str_obj, str, str_len, sep[0], maxcount);
159
160
0
    list = PyList_New(PREALLOC_SIZE(maxcount));
161
0
    if (list == NULL)
162
0
        return NULL;
163
164
0
    i = j = 0;
165
0
    while (maxcount-- > 0) {
166
0
        pos = FASTSEARCH(str+i, str_len-i, sep, sep_len, -1, FAST_SEARCH);
167
0
        if (pos < 0)
168
0
            break;
169
0
        j = i + pos;
170
0
        SPLIT_ADD(str, i, j);
171
0
        i = j + sep_len;
172
0
    }
173
0
#if !STRINGLIB_MUTABLE
174
0
    if (count == 0 && STRINGLIB_CHECK_EXACT(str_obj)) {
175
        /* No match in str_obj, so just use it as list[0] */
176
0
        Py_INCREF(str_obj);
177
0
        PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
178
0
        count++;
179
0
    } else
180
0
#endif
181
0
    {
182
0
        SPLIT_ADD(str, i, str_len);
183
0
    }
184
0
    FIX_PREALLOC_SIZE(list);
185
0
    return list;
186
187
0
  onError:
188
0
    Py_DECREF(list);
189
    return NULL;
190
0
}
unicodeobject.c:asciilib_split
Line
Count
Source
149
16.6M
{
150
16.6M
    Py_ssize_t i, j, pos, count=0;
151
16.6M
    PyObject *list, *sub;
152
153
16.6M
    if (sep_len == 0) {
154
0
        PyErr_SetString(PyExc_ValueError, "empty separator");
155
0
        return NULL;
156
0
    }
157
16.6M
    else if (sep_len == 1)
158
16.6M
        return STRINGLIB(split_char)(str_obj, str, str_len, sep[0], maxcount);
159
160
63.2k
    list = PyList_New(PREALLOC_SIZE(maxcount));
161
63.2k
    if (list == NULL)
162
0
        return NULL;
163
164
63.2k
    i = j = 0;
165
99.4k
    while (maxcount-- > 0) {
166
63.2k
        pos = FASTSEARCH(str+i, str_len-i, sep, sep_len, -1, FAST_SEARCH);
167
63.2k
        if (pos < 0)
168
27.0k
            break;
169
36.2k
        j = i + pos;
170
72.4k
        SPLIT_ADD(str, i, j);
171
72.4k
        i = j + sep_len;
172
72.4k
    }
173
63.2k
#if !STRINGLIB_MUTABLE
174
63.2k
    if (count == 0 && STRINGLIB_CHECK_EXACT(str_obj)) {
175
        /* No match in str_obj, so just use it as list[0] */
176
27.0k
        Py_INCREF(str_obj);
177
27.0k
        PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
178
27.0k
        count++;
179
27.0k
    } else
180
36.2k
#endif
181
36.2k
    {
182
72.4k
        SPLIT_ADD(str, i, str_len);
183
72.4k
    }
184
63.2k
    FIX_PREALLOC_SIZE(list);
185
63.2k
    return list;
186
187
0
  onError:
188
0
    Py_DECREF(list);
189
    return NULL;
190
63.2k
}
unicodeobject.c:ucs1lib_split
Line
Count
Source
149
1.15M
{
150
1.15M
    Py_ssize_t i, j, pos, count=0;
151
1.15M
    PyObject *list, *sub;
152
153
1.15M
    if (sep_len == 0) {
154
0
        PyErr_SetString(PyExc_ValueError, "empty separator");
155
0
        return NULL;
156
0
    }
157
1.15M
    else if (sep_len == 1)
158
1.13M
        return STRINGLIB(split_char)(str_obj, str, str_len, sep[0], maxcount);
159
160
16.6k
    list = PyList_New(PREALLOC_SIZE(maxcount));
161
16.6k
    if (list == NULL)
162
0
        return NULL;
163
164
16.6k
    i = j = 0;
165
30.5k
    while (maxcount-- > 0) {
166
16.6k
        pos = FASTSEARCH(str+i, str_len-i, sep, sep_len, -1, FAST_SEARCH);
167
16.6k
        if (pos < 0)
168
2.66k
            break;
169
13.9k
        j = i + pos;
170
27.9k
        SPLIT_ADD(str, i, j);
171
27.9k
        i = j + sep_len;
172
27.9k
    }
173
16.6k
#if !STRINGLIB_MUTABLE
174
16.6k
    if (count == 0 && STRINGLIB_CHECK_EXACT(str_obj)) {
175
        /* No match in str_obj, so just use it as list[0] */
176
2.66k
        Py_INCREF(str_obj);
177
2.66k
        PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
178
2.66k
        count++;
179
2.66k
    } else
180
13.9k
#endif
181
13.9k
    {
182
27.9k
        SPLIT_ADD(str, i, str_len);
183
27.9k
    }
184
16.6k
    FIX_PREALLOC_SIZE(list);
185
16.6k
    return list;
186
187
0
  onError:
188
0
    Py_DECREF(list);
189
    return NULL;
190
16.6k
}
unicodeobject.c:ucs2lib_split
Line
Count
Source
149
180k
{
150
180k
    Py_ssize_t i, j, pos, count=0;
151
180k
    PyObject *list, *sub;
152
153
180k
    if (sep_len == 0) {
154
0
        PyErr_SetString(PyExc_ValueError, "empty separator");
155
0
        return NULL;
156
0
    }
157
180k
    else if (sep_len == 1)
158
107k
        return STRINGLIB(split_char)(str_obj, str, str_len, sep[0], maxcount);
159
160
73.5k
    list = PyList_New(PREALLOC_SIZE(maxcount));
161
73.5k
    if (list == NULL)
162
0
        return NULL;
163
164
73.5k
    i = j = 0;
165
137k
    while (maxcount-- > 0) {
166
73.5k
        pos = FASTSEARCH(str+i, str_len-i, sep, sep_len, -1, FAST_SEARCH);
167
73.5k
        if (pos < 0)
168
9.51k
            break;
169
64.0k
        j = i + pos;
170
128k
        SPLIT_ADD(str, i, j);
171
128k
        i = j + sep_len;
172
128k
    }
173
73.5k
#if !STRINGLIB_MUTABLE
174
73.5k
    if (count == 0 && STRINGLIB_CHECK_EXACT(str_obj)) {
175
        /* No match in str_obj, so just use it as list[0] */
176
9.51k
        Py_INCREF(str_obj);
177
9.51k
        PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
178
9.51k
        count++;
179
9.51k
    } else
180
64.0k
#endif
181
64.0k
    {
182
128k
        SPLIT_ADD(str, i, str_len);
183
128k
    }
184
73.5k
    FIX_PREALLOC_SIZE(list);
185
73.5k
    return list;
186
187
0
  onError:
188
0
    Py_DECREF(list);
189
    return NULL;
190
73.5k
}
unicodeobject.c:ucs4lib_split
Line
Count
Source
149
31.0k
{
150
31.0k
    Py_ssize_t i, j, pos, count=0;
151
31.0k
    PyObject *list, *sub;
152
153
31.0k
    if (sep_len == 0) {
154
0
        PyErr_SetString(PyExc_ValueError, "empty separator");
155
0
        return NULL;
156
0
    }
157
31.0k
    else if (sep_len == 1)
158
13.3k
        return STRINGLIB(split_char)(str_obj, str, str_len, sep[0], maxcount);
159
160
17.7k
    list = PyList_New(PREALLOC_SIZE(maxcount));
161
17.7k
    if (list == NULL)
162
0
        return NULL;
163
164
17.7k
    i = j = 0;
165
32.8k
    while (maxcount-- > 0) {
166
17.7k
        pos = FASTSEARCH(str+i, str_len-i, sep, sep_len, -1, FAST_SEARCH);
167
17.7k
        if (pos < 0)
168
2.63k
            break;
169
15.0k
        j = i + pos;
170
30.1k
        SPLIT_ADD(str, i, j);
171
30.1k
        i = j + sep_len;
172
30.1k
    }
173
17.7k
#if !STRINGLIB_MUTABLE
174
17.7k
    if (count == 0 && STRINGLIB_CHECK_EXACT(str_obj)) {
175
        /* No match in str_obj, so just use it as list[0] */
176
2.63k
        Py_INCREF(str_obj);
177
2.63k
        PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
178
2.63k
        count++;
179
2.63k
    } else
180
15.0k
#endif
181
15.0k
    {
182
30.1k
        SPLIT_ADD(str, i, str_len);
183
30.1k
    }
184
17.7k
    FIX_PREALLOC_SIZE(list);
185
17.7k
    return list;
186
187
0
  onError:
188
0
    Py_DECREF(list);
189
    return NULL;
190
17.7k
}
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
62
{
248
62
    Py_ssize_t i, j, count=0;
249
62
    PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
250
62
    PyObject *sub;
251
252
62
    if (list == NULL)
253
0
        return NULL;
254
255
62
    i = j = str_len - 1;
256
124
    while ((i >= 0) && (maxcount-- > 0)) {
257
62
        for(; i >= 0; i--) {
258
62
            if (str[i] == ch) {
259
62
                SPLIT_ADD(str, i + 1, j + 1);
260
62
                j = i = i - 1;
261
62
                break;
262
62
            }
263
62
        }
264
62
    }
265
#if !STRINGLIB_MUTABLE
266
62
    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
62
#endif
273
62
    if (j >= -1) {
274
124
        SPLIT_ADD(str, 0, j + 1);
275
124
    }
276
62
    FIX_PREALLOC_SIZE(list);
277
62
    if (PyList_Reverse(list) < 0)
278
0
        goto onError;
279
62
    return list;
280
281
0
  onError:
282
0
    Py_DECREF(list);
283
0
    return NULL;
284
62
}
Unexecuted instantiation: bytesobject.c:stringlib_rsplit_char
unicodeobject.c:asciilib_rsplit_char
Line
Count
Source
247
62
{
248
62
    Py_ssize_t i, j, count=0;
249
62
    PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
250
62
    PyObject *sub;
251
252
62
    if (list == NULL)
253
0
        return NULL;
254
255
62
    i = j = str_len - 1;
256
124
    while ((i >= 0) && (maxcount-- > 0)) {
257
62
        for(; i >= 0; i--) {
258
62
            if (str[i] == ch) {
259
62
                SPLIT_ADD(str, i + 1, j + 1);
260
62
                j = i = i - 1;
261
62
                break;
262
62
            }
263
62
        }
264
62
    }
265
62
#if !STRINGLIB_MUTABLE
266
62
    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
62
#endif
273
62
    if (j >= -1) {
274
124
        SPLIT_ADD(str, 0, j + 1);
275
124
    }
276
62
    FIX_PREALLOC_SIZE(list);
277
62
    if (PyList_Reverse(list) < 0)
278
0
        goto onError;
279
62
    return list;
280
281
0
  onError:
282
0
    Py_DECREF(list);
283
    return NULL;
284
62
}
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
62
{
292
62
    Py_ssize_t j, pos, count=0;
293
62
    PyObject *list, *sub;
294
295
62
    if (sep_len == 0) {
296
0
        PyErr_SetString(PyExc_ValueError, "empty separator");
297
0
        return NULL;
298
0
    }
299
62
    else if (sep_len == 1)
300
62
        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
62
{
292
62
    Py_ssize_t j, pos, count=0;
293
62
    PyObject *list, *sub;
294
295
62
    if (sep_len == 0) {
296
0
        PyErr_SetString(PyExc_ValueError, "empty separator");
297
0
        return NULL;
298
0
    }
299
62
    else if (sep_len == 1)
300
62
        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.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
31.7M
    for (i = j = 0; i < str_len; ) {
357
31.7M
        Py_ssize_t eol;
358
359
        /* Find a line and append it */
360
229M
        while (i < str_len && !STRINGLIB_ISLINEBREAK(str[i]))
361
197M
            i++;
362
363
        /* Skip the line break reading CRLF as one line break */
364
31.7M
        eol = i;
365
31.7M
        if (i < str_len) {
366
31.7M
            if (str[i] == '\r' && i + 1 < str_len && str[i+1] == '\n')
367
59.5k
                i += 2;
368
31.6M
            else
369
31.6M
                i++;
370
31.7M
            if (keepends)
371
0
                eol = i;
372
31.7M
        }
373
#if !STRINGLIB_MUTABLE
374
31.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
5.56k
            if (PyList_Append(list, str_obj))
377
0
                goto onError;
378
5.56k
            break;
379
5.56k
        }
380
31.7M
#endif
381
63.4M
        SPLIT_APPEND(str, j, eol);
382
31.7M
        j = i;
383
31.7M
    }
384
13.8k
    return list;
385
386
0
  onError:
387
0
    Py_DECREF(list);
388
    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.79M
    for (i = j = 0; i < str_len; ) {
357
8.79M
        Py_ssize_t eol;
358
359
        /* Find a line and append it */
360
29.1M
        while (i < str_len && !STRINGLIB_ISLINEBREAK(str[i]))
361
20.3M
            i++;
362
363
        /* Skip the line break reading CRLF as one line break */
364
8.79M
        eol = i;
365
8.79M
        if (i < str_len) {
366
8.78M
            if (str[i] == '\r' && i + 1 < str_len && str[i+1] == '\n')
367
1.79k
                i += 2;
368
8.78M
            else
369
8.78M
                i++;
370
8.78M
            if (keepends)
371
0
                eol = i;
372
8.78M
        }
373
8.79M
#if !STRINGLIB_MUTABLE
374
8.79M
        if (j == 0 && eol == str_len && STRINGLIB_CHECK_EXACT(str_obj)) {
375
            /* No linebreak in str_obj, so just use it as list[0] */
376
962
            if (PyList_Append(list, str_obj))
377
0
                goto onError;
378
962
            break;
379
962
        }
380
8.79M
#endif
381
17.5M
        SPLIT_APPEND(str, j, eol);
382
8.79M
        j = i;
383
8.79M
    }
384
2.70k
    return list;
385
386
0
  onError:
387
0
    Py_DECREF(list);
388
    return NULL;
389
2.70k
}
unicodeobject.c:ucs1lib_splitlines
Line
Count
Source
339
862
{
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
862
    Py_ssize_t i;
349
862
    Py_ssize_t j;
350
862
    PyObject *list = PyList_New(0);
351
862
    PyObject *sub;
352
353
862
    if (list == NULL)
354
0
        return NULL;
355
356
951k
    for (i = j = 0; i < str_len; ) {
357
950k
        Py_ssize_t eol;
358
359
        /* Find a line and append it */
360
9.87M
        while (i < str_len && !STRINGLIB_ISLINEBREAK(str[i]))
361
8.92M
            i++;
362
363
        /* Skip the line break reading CRLF as one line break */
364
950k
        eol = i;
365
950k
        if (i < str_len) {
366
950k
            if (str[i] == '\r' && i + 1 < str_len && str[i+1] == '\n')
367
1.00k
                i += 2;
368
949k
            else
369
949k
                i++;
370
950k
            if (keepends)
371
0
                eol = i;
372
950k
        }
373
950k
#if !STRINGLIB_MUTABLE
374
950k
        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
232
            if (PyList_Append(list, str_obj))
377
0
                goto onError;
378
232
            break;
379
232
        }
380
950k
#endif
381
1.90M
        SPLIT_APPEND(str, j, eol);
382
950k
        j = i;
383
950k
    }
384
862
    return list;
385
386
0
  onError:
387
0
    Py_DECREF(list);
388
    return NULL;
389
862
}
unicodeobject.c:ucs2lib_splitlines
Line
Count
Source
339
7.17k
{
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.17k
    Py_ssize_t i;
349
7.17k
    Py_ssize_t j;
350
7.17k
    PyObject *list = PyList_New(0);
351
7.17k
    PyObject *sub;
352
353
7.17k
    if (list == NULL)
354
0
        return NULL;
355
356
11.1M
    for (i = j = 0; i < str_len; ) {
357
11.1M
        Py_ssize_t eol;
358
359
        /* Find a line and append it */
360
83.0M
        while (i < str_len && !STRINGLIB_ISLINEBREAK(str[i]))
361
71.9M
            i++;
362
363
        /* Skip the line break reading CRLF as one line break */
364
11.1M
        eol = i;
365
11.1M
        if (i < str_len) {
366
11.1M
            if (str[i] == '\r' && i + 1 < str_len && str[i+1] == '\n')
367
24.2k
                i += 2;
368
11.1M
            else
369
11.1M
                i++;
370
11.1M
            if (keepends)
371
0
                eol = i;
372
11.1M
        }
373
11.1M
#if !STRINGLIB_MUTABLE
374
11.1M
        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.15k
            if (PyList_Append(list, str_obj))
377
0
                goto onError;
378
3.15k
            break;
379
3.15k
        }
380
11.1M
#endif
381
22.3M
        SPLIT_APPEND(str, j, eol);
382
11.1M
        j = i;
383
11.1M
    }
384
7.17k
    return list;
385
386
0
  onError:
387
0
    Py_DECREF(list);
388
    return NULL;
389
7.17k
}
unicodeobject.c:ucs4lib_splitlines
Line
Count
Source
339
3.07k
{
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.07k
    Py_ssize_t i;
349
3.07k
    Py_ssize_t j;
350
3.07k
    PyObject *list = PyList_New(0);
351
3.07k
    PyObject *sub;
352
353
3.07k
    if (list == NULL)
354
0
        return NULL;
355
356
10.8M
    for (i = j = 0; i < str_len; ) {
357
10.8M
        Py_ssize_t eol;
358
359
        /* Find a line and append it */
360
107M
        while (i < str_len && !STRINGLIB_ISLINEBREAK(str[i]))
361
96.3M
            i++;
362
363
        /* Skip the line break reading CRLF as one line break */
364
10.8M
        eol = i;
365
10.8M
        if (i < str_len) {
366
10.8M
            if (str[i] == '\r' && i + 1 < str_len && str[i+1] == '\n')
367
32.4k
                i += 2;
368
10.7M
            else
369
10.7M
                i++;
370
10.8M
            if (keepends)
371
0
                eol = i;
372
10.8M
        }
373
10.8M
#if !STRINGLIB_MUTABLE
374
10.8M
        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.21k
            if (PyList_Append(list, str_obj))
377
0
                goto onError;
378
1.21k
            break;
379
1.21k
        }
380
10.8M
#endif
381
21.6M
        SPLIT_APPEND(str, j, eol);
382
10.8M
        j = i;
383
10.8M
    }
384
3.07k
    return list;
385
386
0
  onError:
387
0
    Py_DECREF(list);
388
    return NULL;
389
3.07k
}
Unexecuted instantiation: bytearrayobject.c:stringlib_splitlines
390