Coverage Report

Created: 2025-09-05 07:10

/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
89.0M
#define MAX_PREALLOC 12
15
16
/* 5 splits gives 6 elements */
17
#define PREALLOC_SIZE(maxsplit) \
18
19.1M
    (maxsplit >= MAX_PREALLOC ? MAX_PREALLOC : maxsplit+1)
19
20
#define SPLIT_APPEND(data, left, right)         \
21
27.5M
    sub = STRINGLIB_NEW((data) + (left),        \
22
27.5M
                        (right) - (left));      \
23
27.5M
    if (sub == NULL)                            \
24
27.5M
        goto onError;                           \
25
27.5M
    if (PyList_Append(list, sub)) {             \
26
0
        Py_DECREF(sub);                         \
27
0
        goto onError;                           \
28
0
    }                                           \
29
27.5M
    else                                        \
30
27.5M
        Py_DECREF(sub);
31
32
47.6M
#define SPLIT_ADD(data, left, right) {          \
33
47.6M
    sub = STRINGLIB_NEW((data) + (left),        \
34
47.6M
                        (right) - (left));      \
35
47.6M
    if (sub == NULL)                            \
36
47.6M
        goto onError;                           \
37
47.6M
    if (count < MAX_PREALLOC) {                 \
38
27.0M
        PyList_SET_ITEM(list, count, sub);      \
39
27.0M
    } else {                                    \
40
20.6M
        if (PyList_Append(list, sub)) {         \
41
0
            Py_DECREF(sub);                     \
42
0
            goto onError;                       \
43
0
        }                                       \
44
20.6M
        else                                    \
45
20.6M
            Py_DECREF(sub);                     \
46
20.6M
    }                                           \
47
47.6M
    count++; }
48
49
50
/* Always force the list to the expected size. */
51
19.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
142k
{
58
142k
    Py_ssize_t i, j, count=0;
59
142k
    PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
60
142k
    PyObject *sub;
61
62
142k
    if (list == NULL)
63
0
        return NULL;
64
65
142k
    i = j = 0;
66
1.83M
    while (maxcount-- > 0) {
67
3.60M
        while (i < str_len && STRINGLIB_ISSPACE(str[i]))
68
1.80M
            i++;
69
1.80M
        if (i == str_len) break;
70
1.72M
        j = i; i++;
71
103M
        while (i < str_len && !STRINGLIB_ISSPACE(str[i]))
72
101M
            i++;
73
#if !STRINGLIB_MUTABLE
74
1.72M
        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
32.7k
            Py_INCREF(str_obj);
77
32.7k
            PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
78
32.7k
            count++;
79
32.7k
            break;
80
32.7k
        }
81
1.68M
#endif
82
5.06M
        SPLIT_ADD(str, j, i);
83
5.06M
    }
84
85
142k
    if (i < str_len) {
86
        /* Only occurs when maxcount was reached */
87
        /* Skip any remaining whitespace and copy to end of string */
88
54.6k
        while (i < str_len && STRINGLIB_ISSPACE(str[i]))
89
29.3k
            i++;
90
25.2k
        if (i != str_len)
91
25.2k
            SPLIT_ADD(str, i, str_len);
92
25.2k
    }
93
142k
    FIX_PREALLOC_SIZE(list);
94
142k
    return list;
95
96
0
  onError:
97
0
    Py_DECREF(list);
98
0
    return NULL;
99
142k
}
Unexecuted instantiation: bytesobject.c:stringlib_split_whitespace
unicodeobject.c:asciilib_split_whitespace
Line
Count
Source
57
67.8k
{
58
67.8k
    Py_ssize_t i, j, count=0;
59
67.8k
    PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
60
67.8k
    PyObject *sub;
61
62
67.8k
    if (list == NULL)
63
0
        return NULL;
64
65
67.8k
    i = j = 0;
66
571k
    while (maxcount-- > 0) {
67
1.04M
        while (i < str_len && STRINGLIB_ISSPACE(str[i]))
68
492k
            i++;
69
555k
        if (i == str_len) break;
70
518k
        j = i; i++;
71
35.9M
        while (i < str_len && !STRINGLIB_ISSPACE(str[i]))
72
35.4M
            i++;
73
518k
#if !STRINGLIB_MUTABLE
74
518k
        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
15.7k
            Py_INCREF(str_obj);
77
15.7k
            PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
78
15.7k
            count++;
79
15.7k
            break;
80
15.7k
        }
81
503k
#endif
82
1.50M
        SPLIT_ADD(str, j, i);
83
1.50M
    }
84
85
67.8k
    if (i < str_len) {
86
        /* Only occurs when maxcount was reached */
87
        /* Skip any remaining whitespace and copy to end of string */
88
27.1k
        while (i < str_len && STRINGLIB_ISSPACE(str[i]))
89
13.9k
            i++;
90
13.1k
        if (i != str_len)
91
13.1k
            SPLIT_ADD(str, i, str_len);
92
13.1k
    }
93
67.8k
    FIX_PREALLOC_SIZE(list);
94
67.8k
    return list;
95
96
0
  onError:
97
0
    Py_DECREF(list);
98
0
    return NULL;
99
67.8k
}
unicodeobject.c:ucs1lib_split_whitespace
Line
Count
Source
57
24.2k
{
58
24.2k
    Py_ssize_t i, j, count=0;
59
24.2k
    PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
60
24.2k
    PyObject *sub;
61
62
24.2k
    if (list == NULL)
63
0
        return NULL;
64
65
24.2k
    i = j = 0;
66
511k
    while (maxcount-- > 0) {
67
974k
        while (i < str_len && STRINGLIB_ISSPACE(str[i]))
68
474k
            i++;
69
499k
        if (i == str_len) break;
70
490k
        j = i; i++;
71
21.4M
        while (i < str_len && !STRINGLIB_ISSPACE(str[i]))
72
20.9M
            i++;
73
490k
#if !STRINGLIB_MUTABLE
74
490k
        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.04k
            Py_INCREF(str_obj);
77
3.04k
            PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
78
3.04k
            count++;
79
3.04k
            break;
80
3.04k
        }
81
487k
#endif
82
1.46M
        SPLIT_ADD(str, j, i);
83
1.46M
    }
84
85
24.2k
    if (i < str_len) {
86
        /* Only occurs when maxcount was reached */
87
        /* Skip any remaining whitespace and copy to end of string */
88
25.8k
        while (i < str_len && STRINGLIB_ISSPACE(str[i]))
89
14.5k
            i++;
90
11.2k
        if (i != str_len)
91
11.2k
            SPLIT_ADD(str, i, str_len);
92
11.2k
    }
93
24.2k
    FIX_PREALLOC_SIZE(list);
94
24.2k
    return list;
95
96
0
  onError:
97
0
    Py_DECREF(list);
98
0
    return NULL;
99
24.2k
}
unicodeobject.c:ucs2lib_split_whitespace
Line
Count
Source
57
40.2k
{
58
40.2k
    Py_ssize_t i, j, count=0;
59
40.2k
    PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
60
40.2k
    PyObject *sub;
61
62
40.2k
    if (list == NULL)
63
0
        return NULL;
64
65
40.2k
    i = j = 0;
66
639k
    while (maxcount-- > 0) {
67
1.37M
        while (i < str_len && STRINGLIB_ISSPACE(str[i]))
68
731k
            i++;
69
639k
        if (i == str_len) break;
70
610k
        j = i; i++;
71
36.7M
        while (i < str_len && !STRINGLIB_ISSPACE(str[i]))
72
36.1M
            i++;
73
610k
#if !STRINGLIB_MUTABLE
74
610k
        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.1k
            Py_INCREF(str_obj);
77
11.1k
            PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
78
11.1k
            count++;
79
11.1k
            break;
80
11.1k
        }
81
599k
#endif
82
1.79M
        SPLIT_ADD(str, j, i);
83
1.79M
    }
84
85
40.2k
    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
838
            i++;
90
838
        if (i != str_len)
91
838
            SPLIT_ADD(str, i, str_len);
92
838
    }
93
40.2k
    FIX_PREALLOC_SIZE(list);
94
40.2k
    return list;
95
96
0
  onError:
97
0
    Py_DECREF(list);
98
0
    return NULL;
99
40.2k
}
unicodeobject.c:ucs4lib_split_whitespace
Line
Count
Source
57
10.0k
{
58
10.0k
    Py_ssize_t i, j, count=0;
59
10.0k
    PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
60
10.0k
    PyObject *sub;
61
62
10.0k
    if (list == NULL)
63
0
        return NULL;
64
65
10.0k
    i = j = 0;
66
108k
    while (maxcount-- > 0) {
67
210k
        while (i < str_len && STRINGLIB_ISSPACE(str[i]))
68
102k
            i++;
69
108k
        if (i == str_len) break;
70
101k
        j = i; i++;
71
9.45M
        while (i < str_len && !STRINGLIB_ISSPACE(str[i]))
72
9.35M
            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.75k
            Py_INCREF(str_obj);
77
2.75k
            PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
78
2.75k
            count++;
79
2.75k
            break;
80
2.75k
        }
81
98.7k
#endif
82
296k
        SPLIT_ADD(str, j, i);
83
296k
    }
84
85
10.0k
    if (i < str_len) {
86
        /* Only occurs when maxcount was reached */
87
        /* Skip any remaining whitespace and copy to end of string */
88
0
        while (i < str_len && STRINGLIB_ISSPACE(str[i]))
89
0
            i++;
90
0
        if (i != str_len)
91
0
            SPLIT_ADD(str, i, str_len);
92
0
    }
93
10.0k
    FIX_PREALLOC_SIZE(list);
94
10.0k
    return list;
95
96
0
  onError:
97
0
    Py_DECREF(list);
98
0
    return NULL;
99
10.0k
}
Unexecuted instantiation: bytearrayobject.c:stringlib_split_whitespace
100
101
Py_LOCAL_INLINE(PyObject *)
102
STRINGLIB(split_char)(PyObject* str_obj,
103
                     const STRINGLIB_CHAR* str, Py_ssize_t str_len,
104
                     const STRINGLIB_CHAR ch,
105
                     Py_ssize_t maxcount)
106
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
68.6M
    while ((j < str_len) && (maxcount-- > 0)) {
116
345M
        for(; j < str_len; j++) {
117
            /* I found that using memchr makes no difference */
118
328M
            if (str[j] == ch) {
119
32.9M
                SPLIT_ADD(str, i, j);
120
32.9M
                i = j = j + 1;
121
32.9M
                break;
122
32.9M
            }
123
328M
        }
124
49.7M
    }
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.14M
        Py_INCREF(str_obj);
129
6.14M
        PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
130
6.14M
        count++;
131
6.14M
    } else
132
12.7M
#endif
133
12.7M
    if (i <= str_len) {
134
25.4M
        SPLIT_ADD(str, i, str_len);
135
25.4M
    }
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.83M
{
107
2.83M
    Py_ssize_t i, j, count=0;
108
2.83M
    PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
109
2.83M
    PyObject *sub;
110
111
2.83M
    if (list == NULL)
112
0
        return NULL;
113
114
2.83M
    i = j = 0;
115
11.1M
    while ((j < str_len) && (maxcount-- > 0)) {
116
53.8M
        for(; j < str_len; j++) {
117
            /* I found that using memchr makes no difference */
118
51.4M
            if (str[j] == ch) {
119
5.94M
                SPLIT_ADD(str, i, j);
120
5.94M
                i = j = j + 1;
121
5.94M
                break;
122
5.94M
            }
123
51.4M
        }
124
8.31M
    }
125
2.83M
#if !STRINGLIB_MUTABLE
126
2.83M
    if (count == 0 && STRINGLIB_CHECK_EXACT(str_obj)) {
127
        /* ch not in str_obj, so just use str_obj as list[0] */
128
2.29M
        Py_INCREF(str_obj);
129
2.29M
        PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
130
2.29M
        count++;
131
2.29M
    } else
132
548k
#endif
133
548k
    if (i <= str_len) {
134
1.09M
        SPLIT_ADD(str, i, str_len);
135
1.09M
    }
136
2.83M
    FIX_PREALLOC_SIZE(list);
137
2.83M
    return list;
138
139
0
  onError:
140
0
    Py_DECREF(list);
141
0
    return NULL;
142
2.83M
}
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.7M
    while ((j < str_len) && (maxcount-- > 0)) {
116
183M
        for(; j < str_len; j++) {
117
            /* I found that using memchr makes no difference */
118
168M
            if (str[j] == ch) {
119
16.7M
                SPLIT_ADD(str, i, j);
120
16.7M
                i = j = j + 1;
121
16.7M
                break;
122
16.7M
            }
123
168M
        }
124
30.9M
    }
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.71M
        Py_INCREF(str_obj);
129
3.71M
        PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
130
3.71M
        count++;
131
3.71M
    } else
132
11.1M
#endif
133
11.1M
    if (i <= str_len) {
134
22.3M
        SPLIT_ADD(str, i, str_len);
135
22.3M
    }
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
44.0M
        for(; j < str_len; j++) {
117
            /* I found that using memchr makes no difference */
118
43.8M
            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.8M
        }
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
131k
        Py_INCREF(str_obj);
129
131k
        PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
130
131k
        count++;
131
131k
    } else
132
921k
#endif
133
921k
    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.2k
{
107
81.2k
    Py_ssize_t i, j, count=0;
108
81.2k
    PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
109
81.2k
    PyObject *sub;
110
111
81.2k
    if (list == NULL)
112
0
        return NULL;
113
114
81.2k
    i = j = 0;
115
3.07M
    while ((j < str_len) && (maxcount-- > 0)) {
116
43.2M
        for(; j < str_len; j++) {
117
            /* I found that using memchr makes no difference */
118
43.2M
            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.2M
        }
124
2.99M
    }
125
81.2k
#if !STRINGLIB_MUTABLE
126
81.2k
    if (count == 0 && STRINGLIB_CHECK_EXACT(str_obj)) {
127
        /* ch not in str_obj, so just use str_obj as list[0] */
128
7.25k
        Py_INCREF(str_obj);
129
7.25k
        PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
130
7.25k
        count++;
131
7.25k
    } else
132
73.9k
#endif
133
73.9k
    if (i <= str_len) {
134
147k
        SPLIT_ADD(str, i, str_len);
135
147k
    }
136
81.2k
    FIX_PREALLOC_SIZE(list);
137
81.2k
    return list;
138
139
0
  onError:
140
0
    Py_DECREF(list);
141
0
    return NULL;
142
81.2k
}
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
385k
    while ((j < str_len) && (maxcount-- > 0)) {
116
21.1M
        for(; j < str_len; j++) {
117
            /* I found that using memchr makes no difference */
118
21.1M
            if (str[j] == ch) {
119
360k
                SPLIT_ADD(str, i, j);
120
360k
                i = j = j + 1;
121
360k
                break;
122
360k
            }
123
21.1M
        }
124
371k
    }
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
370
        Py_INCREF(str_obj);
129
370
        PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
130
370
        count++;
131
370
    } 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
172k
    list = PyList_New(PREALLOC_SIZE(maxcount));
161
172k
    if (list == NULL)
162
0
        return NULL;
163
164
172k
    i = j = 0;
165
308k
    while (maxcount-- > 0) {
166
172k
        pos = FASTSEARCH(str+i, str_len-i, sep, sep_len, -1, FAST_SEARCH);
167
172k
        if (pos < 0)
168
35.3k
            break;
169
136k
        j = i + pos;
170
273k
        SPLIT_ADD(str, i, j);
171
273k
        i = j + sep_len;
172
273k
    }
173
#if !STRINGLIB_MUTABLE
174
172k
    if (count == 0 && STRINGLIB_CHECK_EXACT(str_obj)) {
175
        /* No match in str_obj, so just use it as list[0] */
176
35.3k
        Py_INCREF(str_obj);
177
35.3k
        PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
178
35.3k
        count++;
179
35.3k
    } else
180
136k
#endif
181
136k
    {
182
273k
        SPLIT_ADD(str, i, str_len);
183
273k
    }
184
172k
    FIX_PREALLOC_SIZE(list);
185
172k
    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.83M
{
150
2.83M
    Py_ssize_t i, j, pos, count=0;
151
2.83M
    PyObject *list, *sub;
152
153
2.83M
    if (sep_len == 0) {
154
0
        PyErr_SetString(PyExc_ValueError, "empty separator");
155
0
        return NULL;
156
0
    }
157
2.83M
    else if (sep_len == 1)
158
2.83M
        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
68.4k
    list = PyList_New(PREALLOC_SIZE(maxcount));
161
68.4k
    if (list == NULL)
162
0
        return NULL;
163
164
68.4k
    i = j = 0;
165
113k
    while (maxcount-- > 0) {
166
68.4k
        pos = FASTSEARCH(str+i, str_len-i, sep, sep_len, -1, FAST_SEARCH);
167
68.4k
        if (pos < 0)
168
23.4k
            break;
169
45.0k
        j = i + pos;
170
90.0k
        SPLIT_ADD(str, i, j);
171
90.0k
        i = j + sep_len;
172
90.0k
    }
173
68.4k
#if !STRINGLIB_MUTABLE
174
68.4k
    if (count == 0 && STRINGLIB_CHECK_EXACT(str_obj)) {
175
        /* No match in str_obj, so just use it as list[0] */
176
23.4k
        Py_INCREF(str_obj);
177
23.4k
        PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
178
23.4k
        count++;
179
23.4k
    } else
180
45.0k
#endif
181
45.0k
    {
182
90.0k
        SPLIT_ADD(str, i, str_len);
183
90.0k
    }
184
68.4k
    FIX_PREALLOC_SIZE(list);
185
68.4k
    return list;
186
187
0
  onError:
188
0
    Py_DECREF(list);
189
0
    return NULL;
190
68.4k
}
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
10.8k
    list = PyList_New(PREALLOC_SIZE(maxcount));
161
10.8k
    if (list == NULL)
162
0
        return NULL;
163
164
10.8k
    i = j = 0;
165
21.0k
    while (maxcount-- > 0) {
166
10.8k
        pos = FASTSEARCH(str+i, str_len-i, sep, sep_len, -1, FAST_SEARCH);
167
10.8k
        if (pos < 0)
168
664
            break;
169
10.1k
        j = i + pos;
170
20.3k
        SPLIT_ADD(str, i, j);
171
20.3k
        i = j + sep_len;
172
20.3k
    }
173
10.8k
#if !STRINGLIB_MUTABLE
174
10.8k
    if (count == 0 && STRINGLIB_CHECK_EXACT(str_obj)) {
175
        /* No match in str_obj, so just use it as list[0] */
176
664
        Py_INCREF(str_obj);
177
664
        PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
178
664
        count++;
179
664
    } else
180
10.1k
#endif
181
10.1k
    {
182
20.3k
        SPLIT_ADD(str, i, str_len);
183
20.3k
    }
184
10.8k
    FIX_PREALLOC_SIZE(list);
185
10.8k
    return list;
186
187
0
  onError:
188
0
    Py_DECREF(list);
189
0
    return NULL;
190
10.8k
}
unicodeobject.c:ucs2lib_split
Line
Count
Source
149
156k
{
150
156k
    Py_ssize_t i, j, pos, count=0;
151
156k
    PyObject *list, *sub;
152
153
156k
    if (sep_len == 0) {
154
0
        PyErr_SetString(PyExc_ValueError, "empty separator");
155
0
        return NULL;
156
0
    }
157
156k
    else if (sep_len == 1)
158
81.2k
        return STRINGLIB(split_char)(str_obj, str, str_len, sep[0], maxcount);
159
160
75.6k
    list = PyList_New(PREALLOC_SIZE(maxcount));
161
75.6k
    if (list == NULL)
162
0
        return NULL;
163
164
75.6k
    i = j = 0;
165
142k
    while (maxcount-- > 0) {
166
75.6k
        pos = FASTSEARCH(str+i, str_len-i, sep, sep_len, -1, FAST_SEARCH);
167
75.6k
        if (pos < 0)
168
9.10k
            break;
169
66.5k
        j = i + pos;
170
133k
        SPLIT_ADD(str, i, j);
171
133k
        i = j + sep_len;
172
133k
    }
173
75.6k
#if !STRINGLIB_MUTABLE
174
75.6k
    if (count == 0 && STRINGLIB_CHECK_EXACT(str_obj)) {
175
        /* No match in str_obj, so just use it as list[0] */
176
9.10k
        Py_INCREF(str_obj);
177
9.10k
        PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
178
9.10k
        count++;
179
9.10k
    } else
180
66.5k
#endif
181
66.5k
    {
182
133k
        SPLIT_ADD(str, i, str_len);
183
133k
    }
184
75.6k
    FIX_PREALLOC_SIZE(list);
185
75.6k
    return list;
186
187
0
  onError:
188
0
    Py_DECREF(list);
189
0
    return NULL;
190
75.6k
}
unicodeobject.c:ucs4lib_split
Line
Count
Source
149
31.4k
{
150
31.4k
    Py_ssize_t i, j, pos, count=0;
151
31.4k
    PyObject *list, *sub;
152
153
31.4k
    if (sep_len == 0) {
154
0
        PyErr_SetString(PyExc_ValueError, "empty separator");
155
0
        return NULL;
156
0
    }
157
31.4k
    else if (sep_len == 1)
158
14.3k
        return STRINGLIB(split_char)(str_obj, str, str_len, sep[0], maxcount);
159
160
17.0k
    list = PyList_New(PREALLOC_SIZE(maxcount));
161
17.0k
    if (list == NULL)
162
0
        return NULL;
163
164
17.0k
    i = j = 0;
165
31.9k
    while (maxcount-- > 0) {
166
17.0k
        pos = FASTSEARCH(str+i, str_len-i, sep, sep_len, -1, FAST_SEARCH);
167
17.0k
        if (pos < 0)
168
2.17k
            break;
169
14.8k
        j = i + pos;
170
29.7k
        SPLIT_ADD(str, i, j);
171
29.7k
        i = j + sep_len;
172
29.7k
    }
173
17.0k
#if !STRINGLIB_MUTABLE
174
17.0k
    if (count == 0 && STRINGLIB_CHECK_EXACT(str_obj)) {
175
        /* No match in str_obj, so just use it as list[0] */
176
2.17k
        Py_INCREF(str_obj);
177
2.17k
        PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
178
2.17k
        count++;
179
2.17k
    } else
180
14.8k
#endif
181
14.8k
    {
182
29.7k
        SPLIT_ADD(str, i, str_len);
183
29.7k
    }
184
17.0k
    FIX_PREALLOC_SIZE(list);
185
17.0k
    return list;
186
187
0
  onError:
188
0
    Py_DECREF(list);
189
0
    return NULL;
190
17.0k
}
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.7k
{
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.7k
    Py_ssize_t i;
349
13.7k
    Py_ssize_t j;
350
13.7k
    PyObject *list = PyList_New(0);
351
13.7k
    PyObject *sub;
352
353
13.7k
    if (list == NULL)
354
0
        return NULL;
355
356
27.5M
    for (i = j = 0; i < str_len; ) {
357
27.5M
        Py_ssize_t eol;
358
359
        /* Find a line and append it */
360
236M
        while (i < str_len && !STRINGLIB_ISLINEBREAK(str[i]))
361
208M
            i++;
362
363
        /* Skip the line break reading CRLF as one line break */
364
27.5M
        eol = i;
365
27.5M
        if (i < str_len) {
366
27.5M
            if (str[i] == '\r' && i + 1 < str_len && str[i+1] == '\n')
367
53.2k
                i += 2;
368
27.4M
            else
369
27.4M
                i++;
370
27.5M
            if (keepends)
371
0
                eol = i;
372
27.5M
        }
373
#if !STRINGLIB_MUTABLE
374
27.5M
        if (j == 0 && eol == str_len && STRINGLIB_CHECK_EXACT(str_obj)) {
375
            /* No linebreak in str_obj, so just use it as list[0] */
376
5.31k
            if (PyList_Append(list, str_obj))
377
0
                goto onError;
378
5.31k
            break;
379
5.31k
        }
380
27.5M
#endif
381
55.0M
        SPLIT_APPEND(str, j, eol);
382
27.5M
        j = i;
383
27.5M
    }
384
13.7k
    return list;
385
386
0
  onError:
387
0
    Py_DECREF(list);
388
0
    return NULL;
389
13.7k
}
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
7.49M
    for (i = j = 0; i < str_len; ) {
357
7.49M
        Py_ssize_t eol;
358
359
        /* Find a line and append it */
360
39.4M
        while (i < str_len && !STRINGLIB_ISLINEBREAK(str[i]))
361
31.9M
            i++;
362
363
        /* Skip the line break reading CRLF as one line break */
364
7.49M
        eol = i;
365
7.49M
        if (i < str_len) {
366
7.49M
            if (str[i] == '\r' && i + 1 < str_len && str[i+1] == '\n')
367
7.74k
                i += 2;
368
7.48M
            else
369
7.48M
                i++;
370
7.49M
            if (keepends)
371
0
                eol = i;
372
7.49M
        }
373
7.49M
#if !STRINGLIB_MUTABLE
374
7.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
991
            if (PyList_Append(list, str_obj))
377
0
                goto onError;
378
991
            break;
379
991
        }
380
7.49M
#endif
381
14.9M
        SPLIT_APPEND(str, j, eol);
382
7.49M
        j = i;
383
7.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
848
{
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
848
    Py_ssize_t i;
349
848
    Py_ssize_t j;
350
848
    PyObject *list = PyList_New(0);
351
848
    PyObject *sub;
352
353
848
    if (list == NULL)
354
0
        return NULL;
355
356
1.07M
    for (i = j = 0; i < str_len; ) {
357
1.07M
        Py_ssize_t eol;
358
359
        /* Find a line and append it */
360
9.93M
        while (i < str_len && !STRINGLIB_ISLINEBREAK(str[i]))
361
8.85M
            i++;
362
363
        /* Skip the line break reading CRLF as one line break */
364
1.07M
        eol = i;
365
1.07M
        if (i < str_len) {
366
1.07M
            if (str[i] == '\r' && i + 1 < str_len && str[i+1] == '\n')
367
1.43k
                i += 2;
368
1.07M
            else
369
1.07M
                i++;
370
1.07M
            if (keepends)
371
0
                eol = i;
372
1.07M
        }
373
1.07M
#if !STRINGLIB_MUTABLE
374
1.07M
        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
215
            if (PyList_Append(list, str_obj))
377
0
                goto onError;
378
215
            break;
379
215
        }
380
1.07M
#endif
381
2.14M
        SPLIT_APPEND(str, j, eol);
382
1.07M
        j = i;
383
1.07M
    }
384
848
    return list;
385
386
0
  onError:
387
0
    Py_DECREF(list);
388
0
    return NULL;
389
848
}
unicodeobject.c:ucs2lib_splitlines
Line
Count
Source
339
7.15k
{
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.15k
    Py_ssize_t i;
349
7.15k
    Py_ssize_t j;
350
7.15k
    PyObject *list = PyList_New(0);
351
7.15k
    PyObject *sub;
352
353
7.15k
    if (list == NULL)
354
0
        return NULL;
355
356
8.86M
    for (i = j = 0; i < str_len; ) {
357
8.86M
        Py_ssize_t eol;
358
359
        /* Find a line and append it */
360
85.0M
        while (i < str_len && !STRINGLIB_ISLINEBREAK(str[i]))
361
76.1M
            i++;
362
363
        /* Skip the line break reading CRLF as one line break */
364
8.86M
        eol = i;
365
8.86M
        if (i < str_len) {
366
8.85M
            if (str[i] == '\r' && i + 1 < str_len && str[i+1] == '\n')
367
14.3k
                i += 2;
368
8.84M
            else
369
8.84M
                i++;
370
8.85M
            if (keepends)
371
0
                eol = i;
372
8.85M
        }
373
8.86M
#if !STRINGLIB_MUTABLE
374
8.86M
        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.96k
            if (PyList_Append(list, str_obj))
377
0
                goto onError;
378
2.96k
            break;
379
2.96k
        }
380
8.85M
#endif
381
17.7M
        SPLIT_APPEND(str, j, eol);
382
8.85M
        j = i;
383
8.85M
    }
384
7.15k
    return list;
385
386
0
  onError:
387
0
    Py_DECREF(list);
388
0
    return NULL;
389
7.15k
}
unicodeobject.c:ucs4lib_splitlines
Line
Count
Source
339
3.01k
{
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.01k
    Py_ssize_t i;
349
3.01k
    Py_ssize_t j;
350
3.01k
    PyObject *list = PyList_New(0);
351
3.01k
    PyObject *sub;
352
353
3.01k
    if (list == NULL)
354
0
        return NULL;
355
356
10.1M
    for (i = j = 0; i < str_len; ) {
357
10.1M
        Py_ssize_t eol;
358
359
        /* Find a line and append it */
360
101M
        while (i < str_len && !STRINGLIB_ISLINEBREAK(str[i]))
361
91.5M
            i++;
362
363
        /* Skip the line break reading CRLF as one line break */
364
10.1M
        eol = i;
365
10.1M
        if (i < str_len) {
366
10.1M
            if (str[i] == '\r' && i + 1 < str_len && str[i+1] == '\n')
367
29.7k
                i += 2;
368
10.0M
            else
369
10.0M
                i++;
370
10.1M
            if (keepends)
371
0
                eol = i;
372
10.1M
        }
373
10.1M
#if !STRINGLIB_MUTABLE
374
10.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
1.14k
            if (PyList_Append(list, str_obj))
377
0
                goto onError;
378
1.14k
            break;
379
1.14k
        }
380
10.1M
#endif
381
20.2M
        SPLIT_APPEND(str, j, eol);
382
10.1M
        j = i;
383
10.1M
    }
384
3.01k
    return list;
385
386
0
  onError:
387
0
    Py_DECREF(list);
388
0
    return NULL;
389
3.01k
}
Unexecuted instantiation: bytearrayobject.c:stringlib_splitlines
390