cython

Форк
0
/
unicode_indexing.pyx 
269 строк · 7.8 Кб
1

2
cimport cython
3

4
cdef unicode _ustring = u'azerty123456'
5
ustring = _ustring
6

7

8
@cython.test_assert_path_exists("//CoerceToPyTypeNode",
9
                                "//IndexNode")
10
@cython.test_fail_if_path_exists("//IndexNode//CoerceToPyTypeNode")
11
def index(unicode ustring, Py_ssize_t i):
12
    """
13
    >>> index(ustring, 0) == 'a'
14
    True
15
    >>> index(ustring, 2) == 'e'
16
    True
17
    >>> index(ustring, -1) == '6'
18
    True
19
    >>> index(ustring, -len(ustring)) == 'a'
20
    True
21

22
    >>> index(ustring, len(ustring))
23
    Traceback (most recent call last):
24
    IndexError: string index out of range
25
    """
26
    return ustring[i]
27

28

29
@cython.test_assert_path_exists("//IndexNode")
30
@cython.test_fail_if_path_exists("//CoerceToPyTypeNode")
31
def index_pyindex(unicode ustring, i):
32
    """
33
    >>> index(ustring, 0) == 'a'
34
    True
35
    >>> index(ustring, 2) == 'e'
36
    True
37
    >>> index(ustring, -1) == '6'
38
    True
39
    >>> index(ustring, -len(ustring)) == 'a'
40
    True
41

42
    >>> index(ustring, len(ustring))
43
    Traceback (most recent call last):
44
    IndexError: string index out of range
45
    """
46
    return ustring[i]
47

48

49

50
@cython.test_assert_path_exists("//CoerceToPyTypeNode",
51
                                "//IndexNode")
52
@cython.test_fail_if_path_exists("//IndexNode//CoerceToPyTypeNode")
53
def index_literal(Py_ssize_t i):
54
    """
55
    >>> index_literal(0) == 'a'
56
    True
57
    >>> index_literal(2) == 'e'
58
    True
59
    >>> index_literal(-1) == '6'
60
    True
61
    >>> index_literal(-len('azerty123456')) == 'a'
62
    True
63

64
    >>> index_literal(len(ustring))
65
    Traceback (most recent call last):
66
    IndexError: string index out of range
67
    """
68
    return u'azerty123456'[i]
69

70

71
@cython.test_assert_path_exists("//IndexNode")
72
@cython.test_fail_if_path_exists("//IndexNode//CoerceToPyTypeNode")
73
def index_literal_pyunicode_cast(int i):
74
    """
75
    >>> index_literal_pyunicode_cast(0) == '1'
76
    True
77
    >>> index_literal_pyunicode_cast(-5) == '1'
78
    True
79
    >>> index_literal_pyunicode_cast(2) == '3'
80
    True
81
    >>> index_literal_pyunicode_cast(4) == '5'
82
    True
83
    >>> index_literal_pyunicode_coerce(6)
84
    Traceback (most recent call last):
85
    IndexError: string index out of range
86
    """
87
    return <Py_UNICODE>(u"12345"[i])
88

89

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):
94
    """
95
    >>> index_literal_pyunicode_coerce(0) == '1'
96
    True
97
    >>> index_literal_pyunicode_coerce(-5) == '1'
98
    True
99
    >>> index_literal_pyunicode_coerce(2) == '3'
100
    True
101
    >>> index_literal_pyunicode_coerce(4) == '5'
102
    True
103
    >>> index_literal_pyunicode_coerce(6)
104
    Traceback (most recent call last):
105
    IndexError: string index out of range
106
    """
107
    cdef Py_UNICODE result = u"12345"[i]
108
    return result
109

110

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):
115
    """
116
    >>> index_literal_pyunicode_coerce_no_check(0) == '1'
117
    True
118
    >>> index_literal_pyunicode_coerce_no_check(-5) == '1'
119
    True
120
    >>> index_literal_pyunicode_coerce_no_check(2) == '3'
121
    True
122
    >>> index_literal_pyunicode_coerce_no_check(4) == '5'
123
    True
124
    """
125
    cdef Py_UNICODE result = u"12345"[i]
126
    return result
127

128

129
@cython.test_assert_path_exists("//CoerceToPyTypeNode",
130
                                "//IndexNode")
131
@cython.test_fail_if_path_exists("//IndexNode//CoerceToPyTypeNode")
132
@cython.boundscheck(False)
133
def index_no_boundscheck(unicode ustring, Py_ssize_t i):
134
    """
135
    >>> index_no_boundscheck(ustring, 0) == 'a'
136
    True
137
    >>> index_no_boundscheck(ustring, 2) == 'e'
138
    True
139
    >>> index_no_boundscheck(ustring, -1) == '6'
140
    True
141
    >>> index_no_boundscheck(ustring, len(ustring)-1) == '6'
142
    True
143
    >>> index_no_boundscheck(ustring, -len(ustring)) == 'a'
144
    True
145
    """
146
    return ustring[i]
147

148

149
@cython.test_assert_path_exists("//CoerceToPyTypeNode",
150
                                "//IndexNode")
151
@cython.test_fail_if_path_exists("//IndexNode//CoerceToPyTypeNode")
152
@cython.boundscheck(False)
153
def unsigned_index_no_boundscheck(unicode ustring, unsigned int i):
154
    """
155
    >>> unsigned_index_no_boundscheck(ustring, 0) == 'a'
156
    True
157
    >>> unsigned_index_no_boundscheck(ustring, 2) == 'e'
158
    True
159
    >>> unsigned_index_no_boundscheck(ustring, len(ustring)-1) == '6'
160
    True
161
    """
162
    return ustring[i]
163

164
@cython.test_assert_path_exists("//CoerceToPyTypeNode",
165
                                "//IndexNode",
166
                                "//PrimaryCmpNode")
167
@cython.test_fail_if_path_exists("//IndexNode//CoerceToPyTypeNode")
168
def index_compare(unicode ustring, Py_ssize_t i):
169
    """
170
    >>> index_compare(ustring, 0)
171
    True
172
    >>> index_compare(ustring, 1)
173
    False
174
    >>> index_compare(ustring, -1)
175
    False
176
    >>> index_compare(ustring, -len(ustring))
177
    True
178

179
    >>> index_compare(ustring, len(ustring))
180
    Traceback (most recent call last):
181
    IndexError: string index out of range
182
    """
183
    return ustring[i] == u'a'
184

185

186
@cython.test_assert_path_exists("//CoerceToPyTypeNode",
187
                                "//IndexNode",
188
                                "//PrimaryCmpNode")
189
@cython.test_fail_if_path_exists("//IndexNode//CoerceToPyTypeNode")
190
def index_compare_string(unicode ustring, Py_ssize_t i, unicode other):
191
    """
192
    >>> index_compare_string(ustring, 0, ustring[0])
193
    True
194
    >>> index_compare_string(ustring, 0, ustring[:4])
195
    False
196
    >>> index_compare_string(ustring, 1, ustring[0])
197
    False
198
    >>> index_compare_string(ustring, 1, ustring[1])
199
    True
200
    >>> index_compare_string(ustring, -1, ustring[0])
201
    False
202
    >>> index_compare_string(ustring, -1, ustring[-1])
203
    True
204
    >>> index_compare_string(ustring, -len(ustring), ustring[-len(ustring)])
205
    True
206

207
    >>> index_compare_string(ustring, len(ustring), ustring)
208
    Traceback (most recent call last):
209
    IndexError: string index out of range
210
    """
211
    return ustring[i] == other
212

213

214
@cython.test_assert_path_exists("//CoerceToPyTypeNode",
215
                                "//IndexNode",
216
                                "//MulNode",
217
                                "//MulNode/CoerceToPyTypeNode")
218
@cython.test_fail_if_path_exists("//IndexNode//CoerceToPyTypeNode")
219
def index_multiply(unicode ustring, Py_ssize_t i, int mul):
220
    """
221
    >>> ustring[0] * 5 == 'aaaaa'
222
    True
223
    >>> index_multiply(ustring, 0, 5) == 'aaaaa'
224
    True
225
    """
226
    return ustring[i] * mul
227

228

229
@cython.test_assert_path_exists("//CoerceToPyTypeNode",
230
                                "//IndexNode",
231
                                "//AddNode",
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):
235
    """
236
    >>> ustring[0] + ustring[-1] == 'a6'
237
    True
238
    >>> index_add(ustring, 0, -1) == 'a6'
239
    True
240
    """
241
    return ustring[i] + ustring[j]
242

243

244
@cython.test_assert_path_exists("//CoerceToPyTypeNode",
245
                                "//IndexNode",
246
                                "//CoerceToPyTypeNode//IndexNode")
247
@cython.test_fail_if_path_exists("//IndexNode//CoerceToPyTypeNode")
248
def index_concat_loop(unicode ustring):
249
    """
250
    >>> index_concat_loop(ustring) == ustring
251
    True
252
    """
253
    cdef int i
254
    cdef unicode s = u''
255
    for i in range(len(ustring)):
256
        s += ustring[i]
257
    return s
258

259

260
@cython.test_assert_path_exists("//CoerceToPyTypeNode",
261
                                "//IndexNode",
262
                                "//CoerceToPyTypeNode//IndexNode")
263
@cython.test_fail_if_path_exists("//IndexNode//CoerceToPyTypeNode")
264
def index_join_loop(unicode ustring):
265
    """
266
    >>> index_join_loop(ustring) == ustring
267
    True
268
    """
269
    cdef int i
270
    return u''.join([ ustring[i] for i in range(len(ustring)) ])
271

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.