4
cdef unicode _ustring = u'azerty123456'
8
@cython.test_assert_path_exists("//CoerceToPyTypeNode",
10
@cython.test_fail_if_path_exists("//IndexNode//CoerceToPyTypeNode")
11
def index(unicode ustring, Py_ssize_t i):
13
>>> index(ustring, 0) == 'a'
15
>>> index(ustring, 2) == 'e'
17
>>> index(ustring, -1) == '6'
19
>>> index(ustring, -len(ustring)) == 'a'
22
>>> index(ustring, len(ustring))
23
Traceback (most recent call last):
24
IndexError: string index out of range
29
@cython.test_assert_path_exists("//IndexNode")
30
@cython.test_fail_if_path_exists("//CoerceToPyTypeNode")
31
def index_pyindex(unicode ustring, i):
33
>>> index(ustring, 0) == 'a'
35
>>> index(ustring, 2) == 'e'
37
>>> index(ustring, -1) == '6'
39
>>> index(ustring, -len(ustring)) == 'a'
42
>>> index(ustring, len(ustring))
43
Traceback (most recent call last):
44
IndexError: string index out of range
50
@cython.test_assert_path_exists("//CoerceToPyTypeNode",
52
@cython.test_fail_if_path_exists("//IndexNode//CoerceToPyTypeNode")
53
def index_literal(Py_ssize_t i):
55
>>> index_literal(0) == 'a'
57
>>> index_literal(2) == 'e'
59
>>> index_literal(-1) == '6'
61
>>> index_literal(-len('azerty123456')) == 'a'
64
>>> index_literal(len(ustring))
65
Traceback (most recent call last):
66
IndexError: string index out of range
68
return u'azerty123456'[i]
71
@cython.test_assert_path_exists("//IndexNode")
72
@cython.test_fail_if_path_exists("//IndexNode//CoerceToPyTypeNode")
73
def index_literal_pyunicode_cast(int i):
75
>>> index_literal_pyunicode_cast(0) == '1'
77
>>> index_literal_pyunicode_cast(-5) == '1'
79
>>> index_literal_pyunicode_cast(2) == '3'
81
>>> index_literal_pyunicode_cast(4) == '5'
83
>>> index_literal_pyunicode_coerce(6)
84
Traceback (most recent call last):
85
IndexError: string index out of range
87
return <Py_UNICODE>(u"12345"[i])
90
@cython.test_assert_path_exists("//IndexNode",
91
"//SingleAssignmentNode")
92
@cython.test_fail_if_path_exists("//SingleAssignmentNode//CoerceToPyTypeNode")
93
def index_literal_pyunicode_coerce(int i):
95
>>> index_literal_pyunicode_coerce(0) == '1'
97
>>> index_literal_pyunicode_coerce(-5) == '1'
99
>>> index_literal_pyunicode_coerce(2) == '3'
101
>>> index_literal_pyunicode_coerce(4) == '5'
103
>>> index_literal_pyunicode_coerce(6)
104
Traceback (most recent call last):
105
IndexError: string index out of range
107
cdef Py_UNICODE result = u"12345"[i]
111
@cython.test_assert_path_exists("//SingleAssignmentNode")
112
@cython.test_fail_if_path_exists("//SingleAssignmentNode//CoerceFromPyTypeNode")
113
@cython.boundscheck(False)
114
def index_literal_pyunicode_coerce_no_check(int i):
116
>>> index_literal_pyunicode_coerce_no_check(0) == '1'
118
>>> index_literal_pyunicode_coerce_no_check(-5) == '1'
120
>>> index_literal_pyunicode_coerce_no_check(2) == '3'
122
>>> index_literal_pyunicode_coerce_no_check(4) == '5'
125
cdef Py_UNICODE result = u"12345"[i]
129
@cython.test_assert_path_exists("//CoerceToPyTypeNode",
131
@cython.test_fail_if_path_exists("//IndexNode//CoerceToPyTypeNode")
132
@cython.boundscheck(False)
133
def index_no_boundscheck(unicode ustring, Py_ssize_t i):
135
>>> index_no_boundscheck(ustring, 0) == 'a'
137
>>> index_no_boundscheck(ustring, 2) == 'e'
139
>>> index_no_boundscheck(ustring, -1) == '6'
141
>>> index_no_boundscheck(ustring, len(ustring)-1) == '6'
143
>>> index_no_boundscheck(ustring, -len(ustring)) == 'a'
149
@cython.test_assert_path_exists("//CoerceToPyTypeNode",
151
@cython.test_fail_if_path_exists("//IndexNode//CoerceToPyTypeNode")
152
@cython.boundscheck(False)
153
def unsigned_index_no_boundscheck(unicode ustring, unsigned int i):
155
>>> unsigned_index_no_boundscheck(ustring, 0) == 'a'
157
>>> unsigned_index_no_boundscheck(ustring, 2) == 'e'
159
>>> unsigned_index_no_boundscheck(ustring, len(ustring)-1) == '6'
164
@cython.test_assert_path_exists("//CoerceToPyTypeNode",
167
@cython.test_fail_if_path_exists("//IndexNode//CoerceToPyTypeNode")
168
def index_compare(unicode ustring, Py_ssize_t i):
170
>>> index_compare(ustring, 0)
172
>>> index_compare(ustring, 1)
174
>>> index_compare(ustring, -1)
176
>>> index_compare(ustring, -len(ustring))
179
>>> index_compare(ustring, len(ustring))
180
Traceback (most recent call last):
181
IndexError: string index out of range
183
return ustring[i] == u'a'
186
@cython.test_assert_path_exists("//CoerceToPyTypeNode",
189
@cython.test_fail_if_path_exists("//IndexNode//CoerceToPyTypeNode")
190
def index_compare_string(unicode ustring, Py_ssize_t i, unicode other):
192
>>> index_compare_string(ustring, 0, ustring[0])
194
>>> index_compare_string(ustring, 0, ustring[:4])
196
>>> index_compare_string(ustring, 1, ustring[0])
198
>>> index_compare_string(ustring, 1, ustring[1])
200
>>> index_compare_string(ustring, -1, ustring[0])
202
>>> index_compare_string(ustring, -1, ustring[-1])
204
>>> index_compare_string(ustring, -len(ustring), ustring[-len(ustring)])
207
>>> index_compare_string(ustring, len(ustring), ustring)
208
Traceback (most recent call last):
209
IndexError: string index out of range
211
return ustring[i] == other
214
@cython.test_assert_path_exists("//CoerceToPyTypeNode",
217
"//MulNode/CoerceToPyTypeNode")
218
@cython.test_fail_if_path_exists("//IndexNode//CoerceToPyTypeNode")
219
def index_multiply(unicode ustring, Py_ssize_t i, int mul):
221
>>> ustring[0] * 5 == 'aaaaa'
223
>>> index_multiply(ustring, 0, 5) == 'aaaaa'
226
return ustring[i] * mul
229
@cython.test_assert_path_exists("//CoerceToPyTypeNode",
232
"//AddNode/CoerceToPyTypeNode")
233
@cython.test_fail_if_path_exists("//IndexNode//CoerceToPyTypeNode")
234
def index_add(unicode ustring, Py_ssize_t i, Py_ssize_t j):
236
>>> ustring[0] + ustring[-1] == 'a6'
238
>>> index_add(ustring, 0, -1) == 'a6'
241
return ustring[i] + ustring[j]
244
@cython.test_assert_path_exists("//CoerceToPyTypeNode",
246
"//CoerceToPyTypeNode//IndexNode")
247
@cython.test_fail_if_path_exists("//IndexNode//CoerceToPyTypeNode")
248
def index_concat_loop(unicode ustring):
250
>>> index_concat_loop(ustring) == ustring
255
for i in range(len(ustring)):
260
@cython.test_assert_path_exists("//CoerceToPyTypeNode",
262
"//CoerceToPyTypeNode//IndexNode")
263
@cython.test_fail_if_path_exists("//IndexNode//CoerceToPyTypeNode")
264
def index_join_loop(unicode ustring):
266
>>> index_join_loop(ustring) == ustring
270
return u''.join([ ustring[i] for i in range(len(ustring)) ])