Coverage Report

Created: 2025-09-04 06:25

/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
90.0M
#define MAX_PREALLOC 12
15
16
/* 5 splits gives 6 elements */
17
#define PREALLOC_SIZE(maxsplit) \
18
19.2M
    (maxsplit >= MAX_PREALLOC ? MAX_PREALLOC : maxsplit+1)
19
20
#define SPLIT_APPEND(data, left, right)         \
21
30.2M
    sub = STRINGLIB_NEW((data) + (left),        \
22
30.2M
                        (right) - (left));      \
23
30.2M
    if (sub == NULL)                            \
24
30.2M
        goto onError;                           \
25
30.2M
    if (PyList_Append(list, sub)) {             \
26
0
        Py_DECREF(sub);                         \
27
0
        goto onError;                           \
28
0
    }                                           \
29
30.2M
    else                                        \
30
30.2M
        Py_DECREF(sub);
31
32
48.5M
#define SPLIT_ADD(data, left, right) {          \
33
48.5M
    sub = STRINGLIB_NEW((data) + (left),        \
34
48.5M
                        (right) - (left));      \
35
48.5M
    if (sub == NULL)                            \
36
48.5M
        goto onError;                           \
37
48.5M
    if (count < MAX_PREALLOC) {                 \
38
27.4M
        PyList_SET_ITEM(list, count, sub);      \
39
27.4M
    } else {                                    \
40
21.1M
        if (PyList_Append(list, sub)) {         \
41
0
            Py_DECREF(sub);                     \
42
0
            goto onError;                       \
43
0
        }                                       \
44
21.1M
        else                                    \
45
21.1M
            Py_DECREF(sub);                     \
46
21.1M
    }                                           \
47
48.5M
    count++; }
48
49
50
/* Always force the list to the expected size. */
51
19.2M
#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
147k
{
58
147k
    Py_ssize_t i, j, count=0;
59
147k
    PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
60
147k
    PyObject *sub;
61
62
147k
    if (list == NULL)
63
0
        return NULL;
64
65
147k
    i = j = 0;
66
1.84M
    while (maxcount-- > 0) {
67
3.62M
        while (i < str_len && STRINGLIB_ISSPACE(str[i]))
68
1.80M
            i++;
69
1.81M
        if (i == str_len) break;
70
1.73M
        j = i; i++;
71
106M
        while (i < str_len && !STRINGLIB_ISSPACE(str[i]))
72
104M
            i++;
73
#if !STRINGLIB_MUTABLE
74
1.73M
        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.3k
            Py_INCREF(str_obj);
77
34.3k
            PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
78
34.3k
            count++;
79
34.3k
            break;
80
34.3k
        }
81
1.69M
#endif
82
5.09M
        SPLIT_ADD(str, j, i);
83
5.09M
    }
84
85
147k
    if (i < str_len) {
86
        /* Only occurs when maxcount was reached */
87
        /* Skip any remaining whitespace and copy to end of string */
88
55.2k
        while (i < str_len && STRINGLIB_ISSPACE(str[i]))
89
29.6k
            i++;
90
25.5k
        if (i != str_len)
91
25.5k
            SPLIT_ADD(str, i, str_len);
92
25.5k
    }
93
147k
    FIX_PREALLOC_SIZE(list);
94
147k
    return list;
95
96
0
  onError:
97
0
    Py_DECREF(list);
98
0
    return NULL;
99
147k
}
Unexecuted instantiation: bytesobject.c:stringlib_split_whitespace
unicodeobject.c:asciilib_split_whitespace
Line
Count
Source
57
69.6k
{
58
69.6k
    Py_ssize_t i, j, count=0;
59
69.6k
    PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
60
69.6k
    PyObject *sub;
61
62
69.6k
    if (list == NULL)
63
0
        return NULL;
64
65
69.6k
    i = j = 0;
66
574k
    while (maxcount-- > 0) {
67
1.05M
        while (i < str_len && STRINGLIB_ISSPACE(str[i]))
68
493k
            i++;
69
557k
        if (i == str_len) break;
70
521k
        j = i; i++;
71
38.0M
        while (i < str_len && !STRINGLIB_ISSPACE(str[i]))
72
37.5M
            i++;
73
521k
#if !STRINGLIB_MUTABLE
74
521k
        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
16.5k
            Py_INCREF(str_obj);
77
16.5k
            PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
78
16.5k
            count++;
79
16.5k
            break;
80
16.5k
        }
81
505k
#endif
82
1.51M
        SPLIT_ADD(str, j, i);
83
1.51M
    }
84
85
69.6k
    if (i < str_len) {
86
        /* Only occurs when maxcount was reached */
87
        /* Skip any remaining whitespace and copy to end of string */
88
27.2k
        while (i < str_len && STRINGLIB_ISSPACE(str[i]))
89
13.9k
            i++;
90
13.2k
        if (i != str_len)
91
13.2k
            SPLIT_ADD(str, i, str_len);
92
13.2k
    }
93
69.6k
    FIX_PREALLOC_SIZE(list);
94
69.6k
    return list;
95
96
0
  onError:
97
0
    Py_DECREF(list);
98
0
    return NULL;
99
69.6k
}
unicodeobject.c:ucs1lib_split_whitespace
Line
Count
Source
57
25.4k
{
58
25.4k
    Py_ssize_t i, j, count=0;
59
25.4k
    PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
60
25.4k
    PyObject *sub;
61
62
25.4k
    if (list == NULL)
63
0
        return NULL;
64
65
25.4k
    i = j = 0;
66
513k
    while (maxcount-- > 0) {
67
976k
        while (i < str_len && STRINGLIB_ISSPACE(str[i]))
68
474k
            i++;
69
501k
        if (i == str_len) break;
70
492k
        j = i; i++;
71
21.2M
        while (i < str_len && !STRINGLIB_ISSPACE(str[i]))
72
20.7M
            i++;
73
492k
#if !STRINGLIB_MUTABLE
74
492k
        if (j == 0 && i == str_len && STRINGLIB_CHECK_EXACT(str_obj)) {
75
            /* No whitespace in str_obj, so just use it as list[0] */
76
4.04k
            Py_INCREF(str_obj);
77
4.04k
            PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
78
4.04k
            count++;
79
4.04k
            break;
80
4.04k
        }
81
488k
#endif
82
1.46M
        SPLIT_ADD(str, j, i);
83
1.46M
    }
84
85
25.4k
    if (i < str_len) {
86
        /* Only occurs when maxcount was reached */
87
        /* Skip any remaining whitespace and copy to end of string */
88
26.0k
        while (i < str_len && STRINGLIB_ISSPACE(str[i]))
89
14.7k
            i++;
90
11.3k
        if (i != str_len)
91
11.3k
            SPLIT_ADD(str, i, str_len);
92
11.3k
    }
93
25.4k
    FIX_PREALLOC_SIZE(list);
94
25.4k
    return list;
95
96
0
  onError:
97
0
    Py_DECREF(list);
98
0
    return NULL;
99
25.4k
}
unicodeobject.c:ucs2lib_split_whitespace
Line
Count
Source
57
42.5k
{
58
42.5k
    Py_ssize_t i, j, count=0;
59
42.5k
    PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
60
42.5k
    PyObject *sub;
61
62
42.5k
    if (list == NULL)
63
0
        return NULL;
64
65
42.5k
    i = j = 0;
66
647k
    while (maxcount-- > 0) {
67
1.38M
        while (i < str_len && STRINGLIB_ISSPACE(str[i]))
68
735k
            i++;
69
646k
        if (i == str_len) break;
70
615k
        j = i; i++;
71
37.1M
        while (i < str_len && !STRINGLIB_ISSPACE(str[i]))
72
36.4M
            i++;
73
615k
#if !STRINGLIB_MUTABLE
74
615k
        if (j == 0 && i == str_len && STRINGLIB_CHECK_EXACT(str_obj)) {
75
            /* No whitespace in str_obj, so just use it as list[0] */
76
11.0k
            Py_INCREF(str_obj);
77
11.0k
            PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
78
11.0k
            count++;
79
11.0k
            break;
80
11.0k
        }
81
604k
#endif
82
1.81M
        SPLIT_ADD(str, j, i);
83
1.81M
    }
84
85
42.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.95k
        while (i < str_len && STRINGLIB_ISSPACE(str[i]))
89
978
            i++;
90
978
        if (i != str_len)
91
978
            SPLIT_ADD(str, i, str_len);
92
978
    }
93
42.5k
    FIX_PREALLOC_SIZE(list);
94
42.5k
    return list;
95
96
0
  onError:
97
0
    Py_DECREF(list);
98
0
    return NULL;
99
42.5k
}
unicodeobject.c:ucs4lib_split_whitespace
Line
Count
Source
57
9.98k
{
58
9.98k
    Py_ssize_t i, j, count=0;
59
9.98k
    PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
60
9.98k
    PyObject *sub;
61
62
9.98k
    if (list == NULL)
63
0
        return NULL;
64
65
9.98k
    i = j = 0;
66
108k
    while (maxcount-- > 0) {
67
211k
        while (i < str_len && STRINGLIB_ISSPACE(str[i]))
68
102k
            i++;
69
108k
        if (i == str_len) break;
70
101k
        j = i; i++;
71
10.2M
        while (i < str_len && !STRINGLIB_ISSPACE(str[i]))
72
10.1M
            i++;
73
101k
#if !STRINGLIB_MUTABLE
74
101k
        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.70k
            Py_INCREF(str_obj);
77
2.70k
            PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
78
2.70k
            count++;
79
2.70k
            break;
80
2.70k
        }
81
99.0k
#endif
82
297k
        SPLIT_ADD(str, j, i);
83
297k
    }
84
85
9.98k
    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.98k
    FIX_PREALLOC_SIZE(list);
94
9.98k
    return list;
95
96
0
  onError:
97
0
    Py_DECREF(list);
98
0
    return NULL;
99
9.98k
}
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
18.8M
{
107
18.8M
    Py_ssize_t i, j, count=0;
108
18.8M
    PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
109
18.8M
    PyObject *sub;
110
111
18.8M
    if (list == NULL)
112
0
        return NULL;
113
114
18.8M
    i = j = 0;
115
69.3M
    while ((j < str_len) && (maxcount-- > 0)) {
116
353M
        for(; j < str_len; j++) {
117
            /* I found that using memchr makes no difference */
118
337M
            if (str[j] == ch) {
119
33.6M
                SPLIT_ADD(str, i, j);
120
33.6M
                i = j = j + 1;
121
33.6M
                break;
122
33.6M
            }
123
337M
        }
124
50.4M
    }
125
#if !STRINGLIB_MUTABLE
126
18.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.01M
        Py_INCREF(str_obj);
129
6.01M
        PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
130
6.01M
        count++;
131
6.01M
    } else
132
12.8M
#endif
133
12.8M
    if (i <= str_len) {
134
25.7M
        SPLIT_ADD(str, i, str_len);
135
25.7M
    }
136
18.8M
    FIX_PREALLOC_SIZE(list);
137
18.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
2.86M
{
107
2.86M
    Py_ssize_t i, j, count=0;
108
2.86M
    PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
109
2.86M
    PyObject *sub;
110
111
2.86M
    if (list == NULL)
112
0
        return NULL;
113
114
2.86M
    i = j = 0;
115
11.7M
    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.4M
            if (str[j] == ch) {
119
6.46M
                SPLIT_ADD(str, i, j);
120
6.46M
                i = j = j + 1;
121
6.46M
                break;
122
6.46M
            }
123
52.4M
        }
124
8.86M
    }
125
2.86M
#if !STRINGLIB_MUTABLE
126
2.86M
    if (count == 0 && STRINGLIB_CHECK_EXACT(str_obj)) {
127
        /* ch not in str_obj, so just use str_obj as list[0] */
128
2.31M
        Py_INCREF(str_obj);
129
2.31M
        PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
130
2.31M
        count++;
131
2.31M
    } else
132
546k
#endif
133
546k
    if (i <= str_len) {
134
1.09M
        SPLIT_ADD(str, i, str_len);
135
1.09M
    }
136
2.86M
    FIX_PREALLOC_SIZE(list);
137
2.86M
    return list;
138
139
0
  onError:
140
0
    Py_DECREF(list);
141
0
    return NULL;
142
2.86M
}
unicodeobject.c:asciilib_split_char
Line
Count
Source
106
14.8M
{
107
14.8M
    Py_ssize_t i, j, count=0;
108
14.8M
    PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
109
14.8M
    PyObject *sub;
110
111
14.8M
    if (list == NULL)
112
0
        return NULL;
113
114
14.8M
    i = j = 0;
115
45.9M
    while ((j < str_len) && (maxcount-- > 0)) {
116
186M
        for(; j < str_len; j++) {
117
            /* I found that using memchr makes no difference */
118
172M
            if (str[j] == ch) {
119
16.8M
                SPLIT_ADD(str, i, j);
120
16.8M
                i = j = j + 1;
121
16.8M
                break;
122
16.8M
            }
123
172M
        }
124
31.0M
    }
125
14.8M
#if !STRINGLIB_MUTABLE
126
14.8M
    if (count == 0 && STRINGLIB_CHECK_EXACT(str_obj)) {
127
        /* ch not in str_obj, so just use str_obj as list[0] */
128
3.55M
        Py_INCREF(str_obj);
129
3.55M
        PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
130
3.55M
        count++;
131
3.55M
    } else
132
11.3M
#endif
133
11.3M
    if (i <= str_len) {
134
22.6M
        SPLIT_ADD(str, i, str_len);
135
22.6M
    }
136
14.8M
    FIX_PREALLOC_SIZE(list);
137
14.8M
    return list;
138
139
0
  onError:
140
0
    Py_DECREF(list);
141
0
    return NULL;
142
14.8M
}
unicodeobject.c:ucs1lib_split_char
Line
Count
Source
106
1.05M
{
107
1.05M
    Py_ssize_t i, j, count=0;
108
1.05M
    PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
109
1.05M
    PyObject *sub;
110
111
1.05M
    if (list == NULL)
112
0
        return NULL;
113
114
1.05M
    i = j = 0;
115
8.21M
    while ((j < str_len) && (maxcount-- > 0)) {
116
43.7M
        for(; j < str_len; j++) {
117
            /* I found that using memchr makes no difference */
118
43.5M
            if (str[j] == ch) {
119
7.00M
                SPLIT_ADD(str, i, j);
120
7.00M
                i = j = j + 1;
121
7.00M
                break;
122
7.00M
            }
123
43.5M
        }
124
7.16M
    }
125
1.05M
#if !STRINGLIB_MUTABLE
126
1.05M
    if (count == 0 && STRINGLIB_CHECK_EXACT(str_obj)) {
127
        /* ch not in str_obj, so just use str_obj as list[0] */
128
132k
        Py_INCREF(str_obj);
129
132k
        PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
130
132k
        count++;
131
132k
    } else
132
920k
#endif
133
920k
    if (i <= str_len) {
134
1.84M
        SPLIT_ADD(str, i, str_len);
135
1.84M
    }
136
1.05M
    FIX_PREALLOC_SIZE(list);
137
1.05M
    return list;
138
139
0
  onError:
140
0
    Py_DECREF(list);
141
0
    return NULL;
142
1.05M
}
unicodeobject.c:ucs2lib_split_char
Line
Count
Source
106
81.0k
{
107
81.0k
    Py_ssize_t i, j, count=0;
108
81.0k
    PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
109
81.0k
    PyObject *sub;
110
111
81.0k
    if (list == NULL)
112
0
        return NULL;
113
114
81.0k
    i = j = 0;
115
3.08M
    while ((j < str_len) && (maxcount-- > 0)) {
116
43.7M
        for(; j < str_len; j++) {
117
            /* I found that using memchr makes no difference */
118
43.6M
            if (str[j] == ch) {
119
2.94M
                SPLIT_ADD(str, i, j);
120
2.94M
                i = j = j + 1;
121
2.94M
                break;
122
2.94M
            }
123
43.6M
        }
124
2.99M
    }
125
81.0k
#if !STRINGLIB_MUTABLE
126
81.0k
    if (count == 0 && STRINGLIB_CHECK_EXACT(str_obj)) {
127
        /* ch not in str_obj, so just use str_obj as list[0] */
128
6.42k
        Py_INCREF(str_obj);
129
6.42k
        PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
130
6.42k
        count++;
131
6.42k
    } else
132
74.5k
#endif
133
74.5k
    if (i <= str_len) {
134
149k
        SPLIT_ADD(str, i, str_len);
135
149k
    }
136
81.0k
    FIX_PREALLOC_SIZE(list);
137
81.0k
    return list;
138
139
0
  onError:
140
0
    Py_DECREF(list);
141
0
    return NULL;
142
81.0k
}
unicodeobject.c:ucs4lib_split_char
Line
Count
Source
106
14.3k
{
107
14.3k
    Py_ssize_t i, j, count=0;
108
14.3k
    PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
109
14.3k
    PyObject *sub;
110
111
14.3k
    if (list == NULL)
112
0
        return NULL;
113
114
14.3k
    i = j = 0;
115
384k
    while ((j < str_len) && (maxcount-- > 0)) {
116
24.8M
        for(; j < str_len; j++) {
117
            /* I found that using memchr makes no difference */
118
24.8M
            if (str[j] == ch) {
119
359k
                SPLIT_ADD(str, i, j);
120
359k
                i = j = j + 1;
121
359k
                break;
122
359k
            }
123
24.8M
        }
124
370k
    }
125
14.3k
#if !STRINGLIB_MUTABLE
126
14.3k
    if (count == 0 && STRINGLIB_CHECK_EXACT(str_obj)) {
127
        /* ch not in str_obj, so just use str_obj as list[0] */
128
361
        Py_INCREF(str_obj);
129
361
        PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
130
361
        count++;
131
361
    } else
132
13.9k
#endif
133
13.9k
    if (i <= str_len) {
134
27.9k
        SPLIT_ADD(str, i, str_len);
135
27.9k
    }
136
14.3k
    FIX_PREALLOC_SIZE(list);
137
14.3k
    return list;
138
139
0
  onError:
140
0
    Py_DECREF(list);
141
0
    return NULL;
142
14.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
19.0M
{
150
19.0M
    Py_ssize_t i, j, pos, count=0;
151
19.0M
    PyObject *list, *sub;
152
153
19.0M
    if (sep_len == 0) {
154
0
        PyErr_SetString(PyExc_ValueError, "empty separator");
155
0
        return NULL;
156
0
    }
157
19.0M
    else if (sep_len == 1)
158
18.8M
        return STRINGLIB(split_char)(str_obj, str, str_len, sep[0], maxcount);
159
160
179k
    list = PyList_New(PREALLOC_SIZE(maxcount));
161
179k
    if (list == NULL)
162
0
        return NULL;
163
164
179k
    i = j = 0;
165
321k
    while (maxcount-- > 0) {
166
179k
        pos = FASTSEARCH(str+i, str_len-i, sep, sep_len, -1, FAST_SEARCH);
167
179k
        if (pos < 0)
168
36.5k
            break;
169
142k
        j = i + pos;
170
285k
        SPLIT_ADD(str, i, j);
171
285k
        i = j + sep_len;
172
285k
    }
173
#if !STRINGLIB_MUTABLE
174
179k
    if (count == 0 && STRINGLIB_CHECK_EXACT(str_obj)) {
175
        /* No match in str_obj, so just use it as list[0] */
176
36.5k
        Py_INCREF(str_obj);
177
36.5k
        PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
178
36.5k
        count++;
179
36.5k
    } else
180
142k
#endif
181
142k
    {
182
285k
        SPLIT_ADD(str, i, str_len);
183
285k
    }
184
179k
    FIX_PREALLOC_SIZE(list);
185
179k
    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.86M
{
150
2.86M
    Py_ssize_t i, j, pos, count=0;
151
2.86M
    PyObject *list, *sub;
152
153
2.86M
    if (sep_len == 0) {
154
0
        PyErr_SetString(PyExc_ValueError, "empty separator");
155
0
        return NULL;
156
0
    }
157
2.86M
    else if (sep_len == 1)
158
2.86M
        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
14.9M
{
150
14.9M
    Py_ssize_t i, j, pos, count=0;
151
14.9M
    PyObject *list, *sub;
152
153
14.9M
    if (sep_len == 0) {
154
0
        PyErr_SetString(PyExc_ValueError, "empty separator");
155
0
        return NULL;
156
0
    }
157
14.9M
    else if (sep_len == 1)
158
14.8M
        return STRINGLIB(split_char)(str_obj, str, str_len, sep[0], maxcount);
159
160
71.0k
    list = PyList_New(PREALLOC_SIZE(maxcount));
161
71.0k
    if (list == NULL)
162
0
        return NULL;
163
164
71.0k
    i = j = 0;
165
117k
    while (maxcount-- > 0) {
166
71.0k
        pos = FASTSEARCH(str+i, str_len-i, sep, sep_len, -1, FAST_SEARCH);
167
71.0k
        if (pos < 0)
168
24.3k
            break;
169
46.7k
        j = i + pos;
170
93.5k
        SPLIT_ADD(str, i, j);
171
93.5k
        i = j + sep_len;
172
93.5k
    }
173
71.0k
#if !STRINGLIB_MUTABLE
174
71.0k
    if (count == 0 && STRINGLIB_CHECK_EXACT(str_obj)) {
175
        /* No match in str_obj, so just use it as list[0] */
176
24.3k
        Py_INCREF(str_obj);
177
24.3k
        PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
178
24.3k
        count++;
179
24.3k
    } else
180
46.7k
#endif
181
46.7k
    {
182
93.5k
        SPLIT_ADD(str, i, str_len);
183
93.5k
    }
184
71.0k
    FIX_PREALLOC_SIZE(list);
185
71.0k
    return list;
186
187
0
  onError:
188
0
    Py_DECREF(list);
189
0
    return NULL;
190
71.0k
}
unicodeobject.c:ucs1lib_split
Line
Count
Source
149
1.06M
{
150
1.06M
    Py_ssize_t i, j, pos, count=0;
151
1.06M
    PyObject *list, *sub;
152
153
1.06M
    if (sep_len == 0) {
154
0
        PyErr_SetString(PyExc_ValueError, "empty separator");
155
0
        return NULL;
156
0
    }
157
1.06M
    else if (sep_len == 1)
158
1.05M
        return STRINGLIB(split_char)(str_obj, str, str_len, sep[0], maxcount);
159
160
11.0k
    list = PyList_New(PREALLOC_SIZE(maxcount));
161
11.0k
    if (list == NULL)
162
0
        return NULL;
163
164
11.0k
    i = j = 0;
165
21.3k
    while (maxcount-- > 0) {
166
11.0k
        pos = FASTSEARCH(str+i, str_len-i, sep, sep_len, -1, FAST_SEARCH);
167
11.0k
        if (pos < 0)
168
669
            break;
169
10.3k
        j = i + pos;
170
20.6k
        SPLIT_ADD(str, i, j);
171
20.6k
        i = j + sep_len;
172
20.6k
    }
173
11.0k
#if !STRINGLIB_MUTABLE
174
11.0k
    if (count == 0 && STRINGLIB_CHECK_EXACT(str_obj)) {
175
        /* No match in str_obj, so just use it as list[0] */
176
669
        Py_INCREF(str_obj);
177
669
        PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
178
669
        count++;
179
669
    } else
180
10.3k
#endif
181
10.3k
    {
182
20.6k
        SPLIT_ADD(str, i, str_len);
183
20.6k
    }
184
11.0k
    FIX_PREALLOC_SIZE(list);
185
11.0k
    return list;
186
187
0
  onError:
188
0
    Py_DECREF(list);
189
0
    return NULL;
190
11.0k
}
unicodeobject.c:ucs2lib_split
Line
Count
Source
149
161k
{
150
161k
    Py_ssize_t i, j, pos, count=0;
151
161k
    PyObject *list, *sub;
152
153
161k
    if (sep_len == 0) {
154
0
        PyErr_SetString(PyExc_ValueError, "empty separator");
155
0
        return NULL;
156
0
    }
157
161k
    else if (sep_len == 1)
158
81.0k
        return STRINGLIB(split_char)(str_obj, str, str_len, sep[0], maxcount);
159
160
80.1k
    list = PyList_New(PREALLOC_SIZE(maxcount));
161
80.1k
    if (list == NULL)
162
0
        return NULL;
163
164
80.1k
    i = j = 0;
165
150k
    while (maxcount-- > 0) {
166
80.1k
        pos = FASTSEARCH(str+i, str_len-i, sep, sep_len, -1, FAST_SEARCH);
167
80.1k
        if (pos < 0)
168
9.37k
            break;
169
70.7k
        j = i + pos;
170
141k
        SPLIT_ADD(str, i, j);
171
141k
        i = j + sep_len;
172
141k
    }
173
80.1k
#if !STRINGLIB_MUTABLE
174
80.1k
    if (count == 0 && STRINGLIB_CHECK_EXACT(str_obj)) {
175
        /* No match in str_obj, so just use it as list[0] */
176
9.37k
        Py_INCREF(str_obj);
177
9.37k
        PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
178
9.37k
        count++;
179
9.37k
    } else
180
70.7k
#endif
181
70.7k
    {
182
141k
        SPLIT_ADD(str, i, str_len);
183
141k
    }
184
80.1k
    FIX_PREALLOC_SIZE(list);
185
80.1k
    return list;
186
187
0
  onError:
188
0
    Py_DECREF(list);
189
0
    return NULL;
190
80.1k
}
unicodeobject.c:ucs4lib_split
Line
Count
Source
149
31.2k
{
150
31.2k
    Py_ssize_t i, j, pos, count=0;
151
31.2k
    PyObject *list, *sub;
152
153
31.2k
    if (sep_len == 0) {
154
0
        PyErr_SetString(PyExc_ValueError, "empty separator");
155
0
        return NULL;
156
0
    }
157
31.2k
    else if (sep_len == 1)
158
14.3k
        return STRINGLIB(split_char)(str_obj, str, str_len, sep[0], maxcount);
159
160
16.9k
    list = PyList_New(PREALLOC_SIZE(maxcount));
161
16.9k
    if (list == NULL)
162
0
        return NULL;
163
164
16.9k
    i = j = 0;
165
31.6k
    while (maxcount-- > 0) {
166
16.9k
        pos = FASTSEARCH(str+i, str_len-i, sep, sep_len, -1, FAST_SEARCH);
167
16.9k
        if (pos < 0)
168
2.18k
            break;
169
14.7k
        j = i + pos;
170
29.4k
        SPLIT_ADD(str, i, j);
171
29.4k
        i = j + sep_len;
172
29.4k
    }
173
16.9k
#if !STRINGLIB_MUTABLE
174
16.9k
    if (count == 0 && STRINGLIB_CHECK_EXACT(str_obj)) {
175
        /* No match in str_obj, so just use it as list[0] */
176
2.18k
        Py_INCREF(str_obj);
177
2.18k
        PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
178
2.18k
        count++;
179
2.18k
    } else
180
14.7k
#endif
181
14.7k
    {
182
29.4k
        SPLIT_ADD(str, i, str_len);
183
29.4k
    }
184
16.9k
    FIX_PREALLOC_SIZE(list);
185
16.9k
    return list;
186
187
0
  onError:
188
0
    Py_DECREF(list);
189
0
    return NULL;
190
16.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
30.2M
    for (i = j = 0; i < str_len; ) {
357
30.2M
        Py_ssize_t eol;
358
359
        /* Find a line and append it */
360
258M
        while (i < str_len && !STRINGLIB_ISLINEBREAK(str[i]))
361
228M
            i++;
362
363
        /* Skip the line break reading CRLF as one line break */
364
30.2M
        eol = i;
365
30.2M
        if (i < str_len) {
366
30.2M
            if (str[i] == '\r' && i + 1 < str_len && str[i+1] == '\n')
367
67.2k
                i += 2;
368
30.2M
            else
369
30.2M
                i++;
370
30.2M
            if (keepends)
371
0
                eol = i;
372
30.2M
        }
373
#if !STRINGLIB_MUTABLE
374
30.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.34k
            if (PyList_Append(list, str_obj))
377
0
                goto onError;
378
5.34k
            break;
379
5.34k
        }
380
30.2M
#endif
381
60.5M
        SPLIT_APPEND(str, j, eol);
382
30.2M
        j = i;
383
30.2M
    }
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.72k
{
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.72k
    Py_ssize_t i;
349
2.72k
    Py_ssize_t j;
350
2.72k
    PyObject *list = PyList_New(0);
351
2.72k
    PyObject *sub;
352
353
2.72k
    if (list == NULL)
354
0
        return NULL;
355
356
8.84M
    for (i = j = 0; i < str_len; ) {
357
8.84M
        Py_ssize_t eol;
358
359
        /* Find a line and append it */
360
44.8M
        while (i < str_len && !STRINGLIB_ISLINEBREAK(str[i]))
361
35.9M
            i++;
362
363
        /* Skip the line break reading CRLF as one line break */
364
8.84M
        eol = i;
365
8.84M
        if (i < str_len) {
366
8.83M
            if (str[i] == '\r' && i + 1 < str_len && str[i+1] == '\n')
367
9.68k
                i += 2;
368
8.82M
            else
369
8.82M
                i++;
370
8.83M
            if (keepends)
371
0
                eol = i;
372
8.83M
        }
373
8.84M
#if !STRINGLIB_MUTABLE
374
8.84M
        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
993
            if (PyList_Append(list, str_obj))
377
0
                goto onError;
378
993
            break;
379
993
        }
380
8.83M
#endif
381
17.6M
        SPLIT_APPEND(str, j, eol);
382
8.83M
        j = i;
383
8.83M
    }
384
2.72k
    return list;
385
386
0
  onError:
387
0
    Py_DECREF(list);
388
0
    return NULL;
389
2.72k
}
unicodeobject.c:ucs1lib_splitlines
Line
Count
Source
339
843
{
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
843
    Py_ssize_t i;
349
843
    Py_ssize_t j;
350
843
    PyObject *list = PyList_New(0);
351
843
    PyObject *sub;
352
353
843
    if (list == NULL)
354
0
        return NULL;
355
356
1.11M
    for (i = j = 0; i < str_len; ) {
357
1.11M
        Py_ssize_t eol;
358
359
        /* Find a line and append it */
360
9.38M
        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
1.11M
        eol = i;
365
1.11M
        if (i < str_len) {
366
1.11M
            if (str[i] == '\r' && i + 1 < str_len && str[i+1] == '\n')
367
1.39k
                i += 2;
368
1.11M
            else
369
1.11M
                i++;
370
1.11M
            if (keepends)
371
0
                eol = i;
372
1.11M
        }
373
1.11M
#if !STRINGLIB_MUTABLE
374
1.11M
        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
213
            if (PyList_Append(list, str_obj))
377
0
                goto onError;
378
213
            break;
379
213
        }
380
1.11M
#endif
381
2.23M
        SPLIT_APPEND(str, j, eol);
382
1.11M
        j = i;
383
1.11M
    }
384
843
    return list;
385
386
0
  onError:
387
0
    Py_DECREF(list);
388
0
    return NULL;
389
843
}
unicodeobject.c:ucs2lib_splitlines
Line
Count
Source
339
7.19k
{
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.19k
    Py_ssize_t i;
349
7.19k
    Py_ssize_t j;
350
7.19k
    PyObject *list = PyList_New(0);
351
7.19k
    PyObject *sub;
352
353
7.19k
    if (list == NULL)
354
0
        return NULL;
355
356
9.54M
    for (i = j = 0; i < str_len; ) {
357
9.53M
        Py_ssize_t eol;
358
359
        /* Find a line and append it */
360
90.1M
        while (i < str_len && !STRINGLIB_ISLINEBREAK(str[i]))
361
80.5M
            i++;
362
363
        /* Skip the line break reading CRLF as one line break */
364
9.53M
        eol = i;
365
9.53M
        if (i < str_len) {
366
9.53M
            if (str[i] == '\r' && i + 1 < str_len && str[i+1] == '\n')
367
20.0k
                i += 2;
368
9.51M
            else
369
9.51M
                i++;
370
9.53M
            if (keepends)
371
0
                eol = i;
372
9.53M
        }
373
9.53M
#if !STRINGLIB_MUTABLE
374
9.53M
        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.98k
            if (PyList_Append(list, str_obj))
377
0
                goto onError;
378
2.98k
            break;
379
2.98k
        }
380
9.53M
#endif
381
19.0M
        SPLIT_APPEND(str, j, eol);
382
9.53M
        j = i;
383
9.53M
    }
384
7.19k
    return list;
385
386
0
  onError:
387
0
    Py_DECREF(list);
388
0
    return NULL;
389
7.19k
}
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
10.7M
    for (i = j = 0; i < str_len; ) {
357
10.7M
        Py_ssize_t eol;
358
359
        /* Find a line and append it */
360
114M
        while (i < str_len && !STRINGLIB_ISLINEBREAK(str[i]))
361
103M
            i++;
362
363
        /* Skip the line break reading CRLF as one line break */
364
10.7M
        eol = i;
365
10.7M
        if (i < str_len) {
366
10.7M
            if (str[i] == '\r' && i + 1 < str_len && str[i+1] == '\n')
367
36.1k
                i += 2;
368
10.7M
            else
369
10.7M
                i++;
370
10.7M
            if (keepends)
371
0
                eol = i;
372
10.7M
        }
373
10.7M
#if !STRINGLIB_MUTABLE
374
10.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.15k
            if (PyList_Append(list, str_obj))
377
0
                goto onError;
378
1.15k
            break;
379
1.15k
        }
380
10.7M
#endif
381
21.5M
        SPLIT_APPEND(str, j, eol);
382
10.7M
        j = i;
383
10.7M
    }
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