Coverage Report

Created: 2025-07-04 06:49

/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
101M
#define MAX_PREALLOC 12
15
16
/* 5 splits gives 6 elements */
17
#define PREALLOC_SIZE(maxsplit) \
18
20.5M
    (maxsplit >= MAX_PREALLOC ? MAX_PREALLOC : maxsplit+1)
19
20
#define SPLIT_APPEND(data, left, right)         \
21
33.4M
    sub = STRINGLIB_NEW((data) + (left),        \
22
33.4M
                        (right) - (left));      \
23
33.4M
    if (sub == NULL)                            \
24
33.4M
        goto onError;                           \
25
33.4M
    if (PyList_Append(list, sub)) {             \
26
0
        Py_DECREF(sub);                         \
27
0
        goto onError;                           \
28
0
    }                                           \
29
33.4M
    else                                        \
30
33.4M
        Py_DECREF(sub);
31
32
57.2M
#define SPLIT_ADD(data, left, right) {          \
33
57.2M
    sub = STRINGLIB_NEW((data) + (left),        \
34
57.2M
                        (right) - (left));      \
35
57.2M
    if (sub == NULL)                            \
36
57.2M
        goto onError;                           \
37
57.2M
    if (count < MAX_PREALLOC) {                 \
38
31.7M
        PyList_SET_ITEM(list, count, sub);      \
39
31.7M
    } else {                                    \
40
25.5M
        if (PyList_Append(list, sub)) {         \
41
0
            Py_DECREF(sub);                     \
42
0
            goto onError;                       \
43
0
        }                                       \
44
25.5M
        else                                    \
45
25.5M
            Py_DECREF(sub);                     \
46
25.5M
    }                                           \
47
57.2M
    count++; }
48
49
50
/* Always force the list to the expected size. */
51
20.5M
#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
143k
{
58
143k
    Py_ssize_t i, j, count=0;
59
143k
    PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
60
143k
    PyObject *sub;
61
62
143k
    if (list == NULL)
63
0
        return NULL;
64
65
143k
    i = j = 0;
66
2.21M
    while (maxcount-- > 0) {
67
4.32M
        while (i < str_len && STRINGLIB_ISSPACE(str[i]))
68
2.14M
            i++;
69
2.17M
        if (i == str_len) break;
70
2.10M
        j = i; i++;
71
122M
        while (i < str_len && !STRINGLIB_ISSPACE(str[i]))
72
120M
            i++;
73
#if !STRINGLIB_MUTABLE
74
2.10M
        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.5k
            Py_INCREF(str_obj);
77
34.5k
            PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
78
34.5k
            count++;
79
34.5k
            break;
80
34.5k
        }
81
2.06M
#endif
82
6.20M
        SPLIT_ADD(str, j, i);
83
6.20M
    }
84
85
143k
    if (i < str_len) {
86
        /* Only occurs when maxcount was reached */
87
        /* Skip any remaining whitespace and copy to end of string */
88
61.3k
        while (i < str_len && STRINGLIB_ISSPACE(str[i]))
89
33.1k
            i++;
90
28.2k
        if (i != str_len)
91
28.2k
            SPLIT_ADD(str, i, str_len);
92
28.2k
    }
93
143k
    FIX_PREALLOC_SIZE(list);
94
143k
    return list;
95
96
0
  onError:
97
0
    Py_DECREF(list);
98
0
    return NULL;
99
143k
}
Unexecuted instantiation: bytesobject.c:stringlib_split_whitespace
unicodeobject.c:asciilib_split_whitespace
Line
Count
Source
57
71.9k
{
58
71.9k
    Py_ssize_t i, j, count=0;
59
71.9k
    PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
60
71.9k
    PyObject *sub;
61
62
71.9k
    if (list == NULL)
63
0
        return NULL;
64
65
71.9k
    i = j = 0;
66
633k
    while (maxcount-- > 0) {
67
1.14M
        while (i < str_len && STRINGLIB_ISSPACE(str[i]))
68
525k
            i++;
69
616k
        if (i == str_len) break;
70
581k
        j = i; i++;
71
40.7M
        while (i < str_len && !STRINGLIB_ISSPACE(str[i]))
72
40.1M
            i++;
73
581k
#if !STRINGLIB_MUTABLE
74
581k
        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
19.9k
            Py_INCREF(str_obj);
77
19.9k
            PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
78
19.9k
            count++;
79
19.9k
            break;
80
19.9k
        }
81
561k
#endif
82
1.68M
        SPLIT_ADD(str, j, i);
83
1.68M
    }
84
85
71.9k
    if (i < str_len) {
86
        /* Only occurs when maxcount was reached */
87
        /* Skip any remaining whitespace and copy to end of string */
88
27.8k
        while (i < str_len && STRINGLIB_ISSPACE(str[i]))
89
14.3k
            i++;
90
13.5k
        if (i != str_len)
91
13.5k
            SPLIT_ADD(str, i, str_len);
92
13.5k
    }
93
71.9k
    FIX_PREALLOC_SIZE(list);
94
71.9k
    return list;
95
96
0
  onError:
97
0
    Py_DECREF(list);
98
0
    return NULL;
99
71.9k
}
unicodeobject.c:ucs1lib_split_whitespace
Line
Count
Source
57
27.7k
{
58
27.7k
    Py_ssize_t i, j, count=0;
59
27.7k
    PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
60
27.7k
    PyObject *sub;
61
62
27.7k
    if (list == NULL)
63
0
        return NULL;
64
65
27.7k
    i = j = 0;
66
448k
    while (maxcount-- > 0) {
67
837k
        while (i < str_len && STRINGLIB_ISSPACE(str[i]))
68
404k
            i++;
69
433k
        if (i == str_len) break;
70
423k
        j = i; i++;
71
18.3M
        while (i < str_len && !STRINGLIB_ISSPACE(str[i]))
72
17.9M
            i++;
73
423k
#if !STRINGLIB_MUTABLE
74
423k
        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.36k
            Py_INCREF(str_obj);
77
3.36k
            PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
78
3.36k
            count++;
79
3.36k
            break;
80
3.36k
        }
81
420k
#endif
82
1.26M
        SPLIT_ADD(str, j, i);
83
1.26M
    }
84
85
27.7k
    if (i < str_len) {
86
        /* Only occurs when maxcount was reached */
87
        /* Skip any remaining whitespace and copy to end of string */
88
32.6k
        while (i < str_len && STRINGLIB_ISSPACE(str[i]))
89
18.3k
            i++;
90
14.2k
        if (i != str_len)
91
14.2k
            SPLIT_ADD(str, i, str_len);
92
14.2k
    }
93
27.7k
    FIX_PREALLOC_SIZE(list);
94
27.7k
    return list;
95
96
0
  onError:
97
0
    Py_DECREF(list);
98
0
    return NULL;
99
27.7k
}
unicodeobject.c:ucs2lib_split_whitespace
Line
Count
Source
57
34.0k
{
58
34.0k
    Py_ssize_t i, j, count=0;
59
34.0k
    PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
60
34.0k
    PyObject *sub;
61
62
34.0k
    if (list == NULL)
63
0
        return NULL;
64
65
34.0k
    i = j = 0;
66
797k
    while (maxcount-- > 0) {
67
1.65M
        while (i < str_len && STRINGLIB_ISSPACE(str[i]))
68
862k
            i++;
69
797k
        if (i == str_len) break;
70
773k
        j = i; i++;
71
42.5M
        while (i < str_len && !STRINGLIB_ISSPACE(str[i]))
72
41.7M
            i++;
73
773k
#if !STRINGLIB_MUTABLE
74
773k
        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
9.55k
            Py_INCREF(str_obj);
77
9.55k
            PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
78
9.55k
            count++;
79
9.55k
            break;
80
9.55k
        }
81
763k
#endif
82
2.29M
        SPLIT_ADD(str, j, i);
83
2.29M
    }
84
85
34.0k
    if (i < str_len) {
86
        /* Only occurs when maxcount was reached */
87
        /* Skip any remaining whitespace and copy to end of string */
88
840
        while (i < str_len && STRINGLIB_ISSPACE(str[i]))
89
420
            i++;
90
420
        if (i != str_len)
91
420
            SPLIT_ADD(str, i, str_len);
92
420
    }
93
34.0k
    FIX_PREALLOC_SIZE(list);
94
34.0k
    return list;
95
96
0
  onError:
97
0
    Py_DECREF(list);
98
0
    return NULL;
99
34.0k
}
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
331k
    while (maxcount-- > 0) {
67
686k
        while (i < str_len && STRINGLIB_ISSPACE(str[i]))
68
354k
            i++;
69
331k
        if (i == str_len) break;
70
323k
        j = i; i++;
71
21.2M
        while (i < str_len && !STRINGLIB_ISSPACE(str[i]))
72
20.9M
            i++;
73
323k
#if !STRINGLIB_MUTABLE
74
323k
        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.70k
            Py_INCREF(str_obj);
77
1.70k
            PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
78
1.70k
            count++;
79
1.70k
            break;
80
1.70k
        }
81
321k
#endif
82
965k
        SPLIT_ADD(str, j, i);
83
965k
    }
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
20.2M
{
107
20.2M
    Py_ssize_t i, j, count=0;
108
20.2M
    PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
109
20.2M
    PyObject *sub;
110
111
20.2M
    if (list == NULL)
112
0
        return NULL;
113
114
20.2M
    i = j = 0;
115
78.4M
    while ((j < str_len) && (maxcount-- > 0)) {
116
384M
        for(; j < str_len; j++) {
117
            /* I found that using memchr makes no difference */
118
366M
            if (str[j] == ch) {
119
39.8M
                SPLIT_ADD(str, i, j);
120
39.8M
                i = j = j + 1;
121
39.8M
                break;
122
39.8M
            }
123
366M
        }
124
58.1M
    }
125
#if !STRINGLIB_MUTABLE
126
20.2M
    if (count == 0 && STRINGLIB_CHECK_EXACT(str_obj)) {
127
        /* ch not in str_obj, so just use str_obj as list[0] */
128
5.21M
        Py_INCREF(str_obj);
129
5.21M
        PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
130
5.21M
        count++;
131
5.21M
    } else
132
15.0M
#endif
133
15.0M
    if (i <= str_len) {
134
30.0M
        SPLIT_ADD(str, i, str_len);
135
30.0M
    }
136
20.2M
    FIX_PREALLOC_SIZE(list);
137
20.2M
    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.50M
{
107
2.50M
    Py_ssize_t i, j, count=0;
108
2.50M
    PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
109
2.50M
    PyObject *sub;
110
111
2.50M
    if (list == NULL)
112
0
        return NULL;
113
114
2.50M
    i = j = 0;
115
10.1M
    while ((j < str_len) && (maxcount-- > 0)) {
116
50.0M
        for(; j < str_len; j++) {
117
            /* I found that using memchr makes no difference */
118
48.0M
            if (str[j] == ch) {
119
5.61M
                SPLIT_ADD(str, i, j);
120
5.61M
                i = j = j + 1;
121
5.61M
                break;
122
5.61M
            }
123
48.0M
        }
124
7.66M
    }
125
2.50M
#if !STRINGLIB_MUTABLE
126
2.50M
    if (count == 0 && STRINGLIB_CHECK_EXACT(str_obj)) {
127
        /* ch not in str_obj, so just use str_obj as list[0] */
128
1.97M
        Py_INCREF(str_obj);
129
1.97M
        PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
130
1.97M
        count++;
131
1.97M
    } else
132
521k
#endif
133
521k
    if (i <= str_len) {
134
1.04M
        SPLIT_ADD(str, i, str_len);
135
1.04M
    }
136
2.50M
    FIX_PREALLOC_SIZE(list);
137
2.50M
    return list;
138
139
0
  onError:
140
0
    Py_DECREF(list);
141
0
    return NULL;
142
2.50M
}
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
52.6M
    while ((j < str_len) && (maxcount-- > 0)) {
116
211M
        for(; j < str_len; j++) {
117
            /* I found that using memchr makes no difference */
118
195M
            if (str[j] == ch) {
119
19.8M
                SPLIT_ADD(str, i, j);
120
19.8M
                i = j = j + 1;
121
19.8M
                break;
122
19.8M
            }
123
195M
        }
124
35.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.09M
        Py_INCREF(str_obj);
129
3.09M
        PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
130
3.09M
        count++;
131
3.09M
    } else
132
13.5M
#endif
133
13.5M
    if (i <= str_len) {
134
27.1M
        SPLIT_ADD(str, i, str_len);
135
27.1M
    }
136
16.6M
    FIX_PREALLOC_SIZE(list);
137
16.6M
    return list;
138
139
0
  onError:
140
0
    Py_DECREF(list);
141
0
    return NULL;
142
16.6M
}
unicodeobject.c:ucs1lib_split_char
Line
Count
Source
106
987k
{
107
987k
    Py_ssize_t i, j, count=0;
108
987k
    PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
109
987k
    PyObject *sub;
110
111
987k
    if (list == NULL)
112
0
        return NULL;
113
114
987k
    i = j = 0;
115
7.10M
    while ((j < str_len) && (maxcount-- > 0)) {
116
38.6M
        for(; j < str_len; j++) {
117
            /* I found that using memchr makes no difference */
118
38.4M
            if (str[j] == ch) {
119
5.96M
                SPLIT_ADD(str, i, j);
120
5.96M
                i = j = j + 1;
121
5.96M
                break;
122
5.96M
            }
123
38.4M
        }
124
6.11M
    }
125
987k
#if !STRINGLIB_MUTABLE
126
987k
    if (count == 0 && STRINGLIB_CHECK_EXACT(str_obj)) {
127
        /* ch not in str_obj, so just use str_obj as list[0] */
128
128k
        Py_INCREF(str_obj);
129
128k
        PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
130
128k
        count++;
131
128k
    } else
132
858k
#endif
133
858k
    if (i <= str_len) {
134
1.71M
        SPLIT_ADD(str, i, str_len);
135
1.71M
    }
136
987k
    FIX_PREALLOC_SIZE(list);
137
987k
    return list;
138
139
0
  onError:
140
0
    Py_DECREF(list);
141
0
    return NULL;
142
987k
}
unicodeobject.c:ucs2lib_split_char
Line
Count
Source
106
75.4k
{
107
75.4k
    Py_ssize_t i, j, count=0;
108
75.4k
    PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
109
75.4k
    PyObject *sub;
110
111
75.4k
    if (list == NULL)
112
0
        return NULL;
113
114
75.4k
    i = j = 0;
115
5.30M
    while ((j < str_len) && (maxcount-- > 0)) {
116
50.0M
        for(; j < str_len; j++) {
117
            /* I found that using memchr makes no difference */
118
50.0M
            if (str[j] == ch) {
119
5.19M
                SPLIT_ADD(str, i, j);
120
5.19M
                i = j = j + 1;
121
5.19M
                break;
122
5.19M
            }
123
50.0M
        }
124
5.23M
    }
125
75.4k
#if !STRINGLIB_MUTABLE
126
75.4k
    if (count == 0 && STRINGLIB_CHECK_EXACT(str_obj)) {
127
        /* ch not in str_obj, so just use str_obj as list[0] */
128
6.94k
        Py_INCREF(str_obj);
129
6.94k
        PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
130
6.94k
        count++;
131
6.94k
    } else
132
68.4k
#endif
133
68.4k
    if (i <= str_len) {
134
136k
        SPLIT_ADD(str, i, str_len);
135
136k
    }
136
75.4k
    FIX_PREALLOC_SIZE(list);
137
75.4k
    return list;
138
139
0
  onError:
140
0
    Py_DECREF(list);
141
0
    return NULL;
142
75.4k
}
unicodeobject.c:ucs4lib_split_char
Line
Count
Source
106
14.7k
{
107
14.7k
    Py_ssize_t i, j, count=0;
108
14.7k
    PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
109
14.7k
    PyObject *sub;
110
111
14.7k
    if (list == NULL)
112
0
        return NULL;
113
114
14.7k
    i = j = 0;
115
3.25M
    while ((j < str_len) && (maxcount-- > 0)) {
116
34.6M
        for(; j < str_len; j++) {
117
            /* I found that using memchr makes no difference */
118
34.6M
            if (str[j] == ch) {
119
3.22M
                SPLIT_ADD(str, i, j);
120
3.22M
                i = j = j + 1;
121
3.22M
                break;
122
3.22M
            }
123
34.6M
        }
124
3.23M
    }
125
14.7k
#if !STRINGLIB_MUTABLE
126
14.7k
    if (count == 0 && STRINGLIB_CHECK_EXACT(str_obj)) {
127
        /* ch not in str_obj, so just use str_obj as list[0] */
128
438
        Py_INCREF(str_obj);
129
438
        PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
130
438
        count++;
131
438
    } else
132
14.3k
#endif
133
14.3k
    if (i <= str_len) {
134
28.6k
        SPLIT_ADD(str, i, str_len);
135
28.6k
    }
136
14.7k
    FIX_PREALLOC_SIZE(list);
137
14.7k
    return list;
138
139
0
  onError:
140
0
    Py_DECREF(list);
141
0
    return NULL;
142
14.7k
}
Unexecuted instantiation: bytearrayobject.c:stringlib_split_char
143
144
Py_LOCAL_INLINE(PyObject *)
145
STRINGLIB(split)(PyObject* str_obj,
146
                const STRINGLIB_CHAR* str, Py_ssize_t str_len,
147
                const STRINGLIB_CHAR* sep, Py_ssize_t sep_len,
148
                Py_ssize_t maxcount)
149
20.4M
{
150
20.4M
    Py_ssize_t i, j, pos, count=0;
151
20.4M
    PyObject *list, *sub;
152
153
20.4M
    if (sep_len == 0) {
154
0
        PyErr_SetString(PyExc_ValueError, "empty separator");
155
0
        return NULL;
156
0
    }
157
20.4M
    else if (sep_len == 1)
158
20.2M
        return STRINGLIB(split_char)(str_obj, str, str_len, sep[0], maxcount);
159
160
165k
    list = PyList_New(PREALLOC_SIZE(maxcount));
161
165k
    if (list == NULL)
162
0
        return NULL;
163
164
165k
    i = j = 0;
165
296k
    while (maxcount-- > 0) {
166
165k
        pos = FASTSEARCH(str+i, str_len-i, sep, sep_len, -1, FAST_SEARCH);
167
165k
        if (pos < 0)
168
35.5k
            break;
169
130k
        j = i + pos;
170
260k
        SPLIT_ADD(str, i, j);
171
260k
        i = j + sep_len;
172
260k
    }
173
#if !STRINGLIB_MUTABLE
174
165k
    if (count == 0 && STRINGLIB_CHECK_EXACT(str_obj)) {
175
        /* No match in str_obj, so just use it as list[0] */
176
35.5k
        Py_INCREF(str_obj);
177
35.5k
        PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
178
35.5k
        count++;
179
35.5k
    } else
180
130k
#endif
181
130k
    {
182
260k
        SPLIT_ADD(str, i, str_len);
183
260k
    }
184
165k
    FIX_PREALLOC_SIZE(list);
185
165k
    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.50M
{
150
2.50M
    Py_ssize_t i, j, pos, count=0;
151
2.50M
    PyObject *list, *sub;
152
153
2.50M
    if (sep_len == 0) {
154
0
        PyErr_SetString(PyExc_ValueError, "empty separator");
155
0
        return NULL;
156
0
    }
157
2.50M
    else if (sep_len == 1)
158
2.50M
        return STRINGLIB(split_char)(str_obj, str, str_len, sep[0], maxcount);
159
160
0
    list = PyList_New(PREALLOC_SIZE(maxcount));
161
0
    if (list == NULL)
162
0
        return NULL;
163
164
0
    i = j = 0;
165
0
    while (maxcount-- > 0) {
166
0
        pos = FASTSEARCH(str+i, str_len-i, sep, sep_len, -1, FAST_SEARCH);
167
0
        if (pos < 0)
168
0
            break;
169
0
        j = i + pos;
170
0
        SPLIT_ADD(str, i, j);
171
0
        i = j + sep_len;
172
0
    }
173
0
#if !STRINGLIB_MUTABLE
174
0
    if (count == 0 && STRINGLIB_CHECK_EXACT(str_obj)) {
175
        /* No match in str_obj, so just use it as list[0] */
176
0
        Py_INCREF(str_obj);
177
0
        PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
178
0
        count++;
179
0
    } else
180
0
#endif
181
0
    {
182
0
        SPLIT_ADD(str, i, str_len);
183
0
    }
184
0
    FIX_PREALLOC_SIZE(list);
185
0
    return list;
186
187
0
  onError:
188
0
    Py_DECREF(list);
189
0
    return NULL;
190
0
}
unicodeobject.c:asciilib_split
Line
Count
Source
149
16.7M
{
150
16.7M
    Py_ssize_t i, j, pos, count=0;
151
16.7M
    PyObject *list, *sub;
152
153
16.7M
    if (sep_len == 0) {
154
0
        PyErr_SetString(PyExc_ValueError, "empty separator");
155
0
        return NULL;
156
0
    }
157
16.7M
    else if (sep_len == 1)
158
16.6M
        return STRINGLIB(split_char)(str_obj, str, str_len, sep[0], maxcount);
159
160
74.2k
    list = PyList_New(PREALLOC_SIZE(maxcount));
161
74.2k
    if (list == NULL)
162
0
        return NULL;
163
164
74.2k
    i = j = 0;
165
122k
    while (maxcount-- > 0) {
166
74.2k
        pos = FASTSEARCH(str+i, str_len-i, sep, sep_len, -1, FAST_SEARCH);
167
74.2k
        if (pos < 0)
168
26.4k
            break;
169
47.8k
        j = i + pos;
170
95.6k
        SPLIT_ADD(str, i, j);
171
95.6k
        i = j + sep_len;
172
95.6k
    }
173
74.2k
#if !STRINGLIB_MUTABLE
174
74.2k
    if (count == 0 && STRINGLIB_CHECK_EXACT(str_obj)) {
175
        /* No match in str_obj, so just use it as list[0] */
176
26.4k
        Py_INCREF(str_obj);
177
26.4k
        PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
178
26.4k
        count++;
179
26.4k
    } else
180
47.8k
#endif
181
47.8k
    {
182
95.6k
        SPLIT_ADD(str, i, str_len);
183
95.6k
    }
184
74.2k
    FIX_PREALLOC_SIZE(list);
185
74.2k
    return list;
186
187
0
  onError:
188
0
    Py_DECREF(list);
189
0
    return NULL;
190
74.2k
}
unicodeobject.c:ucs1lib_split
Line
Count
Source
149
998k
{
150
998k
    Py_ssize_t i, j, pos, count=0;
151
998k
    PyObject *list, *sub;
152
153
998k
    if (sep_len == 0) {
154
0
        PyErr_SetString(PyExc_ValueError, "empty separator");
155
0
        return NULL;
156
0
    }
157
998k
    else if (sep_len == 1)
158
987k
        return STRINGLIB(split_char)(str_obj, str, str_len, sep[0], maxcount);
159
160
10.6k
    list = PyList_New(PREALLOC_SIZE(maxcount));
161
10.6k
    if (list == NULL)
162
0
        return NULL;
163
164
10.6k
    i = j = 0;
165
20.2k
    while (maxcount-- > 0) {
166
10.6k
        pos = FASTSEARCH(str+i, str_len-i, sep, sep_len, -1, FAST_SEARCH);
167
10.6k
        if (pos < 0)
168
1.05k
            break;
169
9.60k
        j = i + pos;
170
19.2k
        SPLIT_ADD(str, i, j);
171
19.2k
        i = j + sep_len;
172
19.2k
    }
173
10.6k
#if !STRINGLIB_MUTABLE
174
10.6k
    if (count == 0 && STRINGLIB_CHECK_EXACT(str_obj)) {
175
        /* No match in str_obj, so just use it as list[0] */
176
1.05k
        Py_INCREF(str_obj);
177
1.05k
        PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
178
1.05k
        count++;
179
1.05k
    } else
180
9.60k
#endif
181
9.60k
    {
182
19.2k
        SPLIT_ADD(str, i, str_len);
183
19.2k
    }
184
10.6k
    FIX_PREALLOC_SIZE(list);
185
10.6k
    return list;
186
187
0
  onError:
188
0
    Py_DECREF(list);
189
0
    return NULL;
190
10.6k
}
unicodeobject.c:ucs2lib_split
Line
Count
Source
149
139k
{
150
139k
    Py_ssize_t i, j, pos, count=0;
151
139k
    PyObject *list, *sub;
152
153
139k
    if (sep_len == 0) {
154
0
        PyErr_SetString(PyExc_ValueError, "empty separator");
155
0
        return NULL;
156
0
    }
157
139k
    else if (sep_len == 1)
158
75.4k
        return STRINGLIB(split_char)(str_obj, str, str_len, sep[0], maxcount);
159
160
64.4k
    list = PyList_New(PREALLOC_SIZE(maxcount));
161
64.4k
    if (list == NULL)
162
0
        return NULL;
163
164
64.4k
    i = j = 0;
165
123k
    while (maxcount-- > 0) {
166
64.4k
        pos = FASTSEARCH(str+i, str_len-i, sep, sep_len, -1, FAST_SEARCH);
167
64.4k
        if (pos < 0)
168
5.58k
            break;
169
58.8k
        j = i + pos;
170
117k
        SPLIT_ADD(str, i, j);
171
117k
        i = j + sep_len;
172
117k
    }
173
64.4k
#if !STRINGLIB_MUTABLE
174
64.4k
    if (count == 0 && STRINGLIB_CHECK_EXACT(str_obj)) {
175
        /* No match in str_obj, so just use it as list[0] */
176
5.58k
        Py_INCREF(str_obj);
177
5.58k
        PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
178
5.58k
        count++;
179
5.58k
    } else
180
58.8k
#endif
181
58.8k
    {
182
117k
        SPLIT_ADD(str, i, str_len);
183
117k
    }
184
64.4k
    FIX_PREALLOC_SIZE(list);
185
64.4k
    return list;
186
187
0
  onError:
188
0
    Py_DECREF(list);
189
0
    return NULL;
190
64.4k
}
unicodeobject.c:ucs4lib_split
Line
Count
Source
149
31.3k
{
150
31.3k
    Py_ssize_t i, j, pos, count=0;
151
31.3k
    PyObject *list, *sub;
152
153
31.3k
    if (sep_len == 0) {
154
0
        PyErr_SetString(PyExc_ValueError, "empty separator");
155
0
        return NULL;
156
0
    }
157
31.3k
    else if (sep_len == 1)
158
14.7k
        return STRINGLIB(split_char)(str_obj, str, str_len, sep[0], maxcount);
159
160
16.5k
    list = PyList_New(PREALLOC_SIZE(maxcount));
161
16.5k
    if (list == NULL)
162
0
        return NULL;
163
164
16.5k
    i = j = 0;
165
30.6k
    while (maxcount-- > 0) {
166
16.5k
        pos = FASTSEARCH(str+i, str_len-i, sep, sep_len, -1, FAST_SEARCH);
167
16.5k
        if (pos < 0)
168
2.55k
            break;
169
14.0k
        j = i + pos;
170
28.0k
        SPLIT_ADD(str, i, j);
171
28.0k
        i = j + sep_len;
172
28.0k
    }
173
16.5k
#if !STRINGLIB_MUTABLE
174
16.5k
    if (count == 0 && STRINGLIB_CHECK_EXACT(str_obj)) {
175
        /* No match in str_obj, so just use it as list[0] */
176
2.55k
        Py_INCREF(str_obj);
177
2.55k
        PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
178
2.55k
        count++;
179
2.55k
    } else
180
14.0k
#endif
181
14.0k
    {
182
28.0k
        SPLIT_ADD(str, i, str_len);
183
28.0k
    }
184
16.5k
    FIX_PREALLOC_SIZE(list);
185
16.5k
    return list;
186
187
0
  onError:
188
0
    Py_DECREF(list);
189
0
    return NULL;
190
16.5k
}
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
14.3k
{
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
14.3k
    Py_ssize_t i;
349
14.3k
    Py_ssize_t j;
350
14.3k
    PyObject *list = PyList_New(0);
351
14.3k
    PyObject *sub;
352
353
14.3k
    if (list == NULL)
354
0
        return NULL;
355
356
33.4M
    for (i = j = 0; i < str_len; ) {
357
33.4M
        Py_ssize_t eol;
358
359
        /* Find a line and append it */
360
241M
        while (i < str_len && !STRINGLIB_ISLINEBREAK(str[i]))
361
208M
            i++;
362
363
        /* Skip the line break reading CRLF as one line break */
364
33.4M
        eol = i;
365
33.4M
        if (i < str_len) {
366
33.4M
            if (str[i] == '\r' && i + 1 < str_len && str[i+1] == '\n')
367
78.7k
                i += 2;
368
33.3M
            else
369
33.3M
                i++;
370
33.4M
            if (keepends)
371
0
                eol = i;
372
33.4M
        }
373
#if !STRINGLIB_MUTABLE
374
33.4M
        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.51k
            if (PyList_Append(list, str_obj))
377
0
                goto onError;
378
5.51k
            break;
379
5.51k
        }
380
33.4M
#endif
381
66.9M
        SPLIT_APPEND(str, j, eol);
382
33.4M
        j = i;
383
33.4M
    }
384
14.3k
    return list;
385
386
0
  onError:
387
0
    Py_DECREF(list);
388
0
    return NULL;
389
14.3k
}
Unexecuted instantiation: bytesobject.c:stringlib_splitlines
unicodeobject.c:asciilib_splitlines
Line
Count
Source
339
2.82k
{
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.82k
    Py_ssize_t i;
349
2.82k
    Py_ssize_t j;
350
2.82k
    PyObject *list = PyList_New(0);
351
2.82k
    PyObject *sub;
352
353
2.82k
    if (list == NULL)
354
0
        return NULL;
355
356
10.2M
    for (i = j = 0; i < str_len; ) {
357
10.2M
        Py_ssize_t eol;
358
359
        /* Find a line and append it */
360
41.9M
        while (i < str_len && !STRINGLIB_ISLINEBREAK(str[i]))
361
31.7M
            i++;
362
363
        /* Skip the line break reading CRLF as one line break */
364
10.2M
        eol = i;
365
10.2M
        if (i < str_len) {
366
10.2M
            if (str[i] == '\r' && i + 1 < str_len && str[i+1] == '\n')
367
3.85k
                i += 2;
368
10.2M
            else
369
10.2M
                i++;
370
10.2M
            if (keepends)
371
0
                eol = i;
372
10.2M
        }
373
10.2M
#if !STRINGLIB_MUTABLE
374
10.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
1.02k
            if (PyList_Append(list, str_obj))
377
0
                goto onError;
378
1.02k
            break;
379
1.02k
        }
380
10.2M
#endif
381
20.4M
        SPLIT_APPEND(str, j, eol);
382
10.2M
        j = i;
383
10.2M
    }
384
2.82k
    return list;
385
386
0
  onError:
387
0
    Py_DECREF(list);
388
0
    return NULL;
389
2.82k
}
unicodeobject.c:ucs1lib_splitlines
Line
Count
Source
339
892
{
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
892
    Py_ssize_t i;
349
892
    Py_ssize_t j;
350
892
    PyObject *list = PyList_New(0);
351
892
    PyObject *sub;
352
353
892
    if (list == NULL)
354
0
        return NULL;
355
356
1.87M
    for (i = j = 0; i < str_len; ) {
357
1.87M
        Py_ssize_t eol;
358
359
        /* Find a line and append it */
360
8.60M
        while (i < str_len && !STRINGLIB_ISLINEBREAK(str[i]))
361
6.73M
            i++;
362
363
        /* Skip the line break reading CRLF as one line break */
364
1.87M
        eol = i;
365
1.87M
        if (i < str_len) {
366
1.87M
            if (str[i] == '\r' && i + 1 < str_len && str[i+1] == '\n')
367
2.33k
                i += 2;
368
1.87M
            else
369
1.87M
                i++;
370
1.87M
            if (keepends)
371
0
                eol = i;
372
1.87M
        }
373
1.87M
#if !STRINGLIB_MUTABLE
374
1.87M
        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
224
            if (PyList_Append(list, str_obj))
377
0
                goto onError;
378
224
            break;
379
224
        }
380
1.87M
#endif
381
3.74M
        SPLIT_APPEND(str, j, eol);
382
1.87M
        j = i;
383
1.87M
    }
384
892
    return list;
385
386
0
  onError:
387
0
    Py_DECREF(list);
388
0
    return NULL;
389
892
}
unicodeobject.c:ucs2lib_splitlines
Line
Count
Source
339
7.51k
{
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.51k
    Py_ssize_t i;
349
7.51k
    Py_ssize_t j;
350
7.51k
    PyObject *list = PyList_New(0);
351
7.51k
    PyObject *sub;
352
353
7.51k
    if (list == NULL)
354
0
        return NULL;
355
356
12.0M
    for (i = j = 0; i < str_len; ) {
357
12.0M
        Py_ssize_t eol;
358
359
        /* Find a line and append it */
360
91.7M
        while (i < str_len && !STRINGLIB_ISLINEBREAK(str[i]))
361
79.6M
            i++;
362
363
        /* Skip the line break reading CRLF as one line break */
364
12.0M
        eol = i;
365
12.0M
        if (i < str_len) {
366
12.0M
            if (str[i] == '\r' && i + 1 < str_len && str[i+1] == '\n')
367
32.9k
                i += 2;
368
12.0M
            else
369
12.0M
                i++;
370
12.0M
            if (keepends)
371
0
                eol = i;
372
12.0M
        }
373
12.0M
#if !STRINGLIB_MUTABLE
374
12.0M
        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.07k
            if (PyList_Append(list, str_obj))
377
0
                goto onError;
378
3.07k
            break;
379
3.07k
        }
380
12.0M
#endif
381
24.1M
        SPLIT_APPEND(str, j, eol);
382
12.0M
        j = i;
383
12.0M
    }
384
7.51k
    return list;
385
386
0
  onError:
387
0
    Py_DECREF(list);
388
0
    return NULL;
389
7.51k
}
unicodeobject.c:ucs4lib_splitlines
Line
Count
Source
339
3.16k
{
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.16k
    Py_ssize_t i;
349
3.16k
    Py_ssize_t j;
350
3.16k
    PyObject *list = PyList_New(0);
351
3.16k
    PyObject *sub;
352
353
3.16k
    if (list == NULL)
354
0
        return NULL;
355
356
9.25M
    for (i = j = 0; i < str_len; ) {
357
9.25M
        Py_ssize_t eol;
358
359
        /* Find a line and append it */
360
99.5M
        while (i < str_len && !STRINGLIB_ISLINEBREAK(str[i]))
361
90.2M
            i++;
362
363
        /* Skip the line break reading CRLF as one line break */
364
9.25M
        eol = i;
365
9.25M
        if (i < str_len) {
366
9.24M
            if (str[i] == '\r' && i + 1 < str_len && str[i+1] == '\n')
367
39.5k
                i += 2;
368
9.20M
            else
369
9.20M
                i++;
370
9.24M
            if (keepends)
371
0
                eol = i;
372
9.24M
        }
373
9.25M
#if !STRINGLIB_MUTABLE
374
9.25M
        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.19k
            if (PyList_Append(list, str_obj))
377
0
                goto onError;
378
1.19k
            break;
379
1.19k
        }
380
9.24M
#endif
381
18.4M
        SPLIT_APPEND(str, j, eol);
382
9.24M
        j = i;
383
9.24M
    }
384
3.16k
    return list;
385
386
0
  onError:
387
0
    Py_DECREF(list);
388
0
    return NULL;
389
3.16k
}
Unexecuted instantiation: bytearrayobject.c:stringlib_splitlines
390