optimise unicode.find() and unicode.rfind()
[cython.git] / tests / run / unicodemethods.pyx
1 # -*- coding: utf-8 -*-
2
3 cimport cython
4
5 text = u'ab jd  sdflk as sa  sadas asdas fsdf '
6 sep = u'  '
7
8 multiline_text = u'''\
9 ab jd
10 sdflk as sa
11 sadas asdas fsdf '''
12
13 def print_all(l):
14     for s in l:
15         print(s)
16
17
18 # unicode.split(s, [sep, [maxsplit]])
19
20 @cython.test_assert_path_exists(
21     "//PythonCapiCallNode")
22 def split(unicode s):
23     """
24     >>> print_all( text.split() )
25     ab
26     jd
27     sdflk
28     as
29     sa
30     sadas
31     asdas
32     fsdf
33     >>> print_all( split(text) )
34     ab
35     jd
36     sdflk
37     as
38     sa
39     sadas
40     asdas
41     fsdf
42     """
43     return s.split()
44
45 @cython.test_assert_path_exists(
46     "//PythonCapiCallNode")
47 def split_sep(unicode s, sep):
48     """
49     >>> print_all( text.split(sep) )
50     ab jd
51     sdflk as sa
52     sadas asdas fsdf 
53     >>> print_all( split_sep(text, sep) )
54     ab jd
55     sdflk as sa
56     sadas asdas fsdf 
57     """
58     return s.split(sep)
59
60 @cython.test_fail_if_path_exists(
61     "//CoerceToPyTypeNode",
62     "//CastNode", "//TypecastNode")
63 @cython.test_assert_path_exists(
64     "//CoerceFromPyTypeNode",
65     "//PythonCapiCallNode")
66 def split_sep_max(unicode s, sep, max):
67     """
68     >>> print_all( text.split(sep, 1) )
69     ab jd
70     sdflk as sa  sadas asdas fsdf 
71     >>> print_all( split_sep_max(text, sep, 1) )
72     ab jd
73     sdflk as sa  sadas asdas fsdf 
74     """
75     return s.split(sep, max)
76
77 @cython.test_fail_if_path_exists(
78     "//CoerceToPyTypeNode", "//CoerceFromPyTypeNode",
79     "//CastNode", "//TypecastNode")
80 @cython.test_assert_path_exists(
81     "//PythonCapiCallNode")
82 def split_sep_max_int(unicode s, sep):
83     """
84     >>> print_all( text.split(sep, 1) )
85     ab jd
86     sdflk as sa  sadas asdas fsdf 
87     >>> print_all( split_sep_max_int(text, sep) )
88     ab jd
89     sdflk as sa  sadas asdas fsdf 
90     """
91     return s.split(sep, 1)
92
93
94 # unicode.splitlines(s, [keepends])
95
96 @cython.test_assert_path_exists(
97     "//PythonCapiCallNode")
98 def splitlines(unicode s):
99     """
100     >>> len(multiline_text.splitlines())
101     3
102     >>> print_all( multiline_text.splitlines() )
103     ab jd
104     sdflk as sa
105     sadas asdas fsdf 
106     >>> len(splitlines(multiline_text))
107     3
108     >>> print_all( splitlines(multiline_text) )
109     ab jd
110     sdflk as sa
111     sadas asdas fsdf 
112     """
113     return s.splitlines()
114
115 @cython.test_assert_path_exists(
116     "//PythonCapiCallNode")
117 def splitlines_keep(unicode s, keep):
118     """
119     >>> len(multiline_text.splitlines(True))
120     3
121     >>> print_all( multiline_text.splitlines(True) )
122     ab jd
123     <BLANKLINE>
124     sdflk as sa
125     <BLANKLINE>
126     sadas asdas fsdf 
127     >>> len(splitlines_keep(multiline_text, True))
128     3
129     >>> print_all( splitlines_keep(multiline_text, True) )
130     ab jd
131     <BLANKLINE>
132     sdflk as sa
133     <BLANKLINE>
134     sadas asdas fsdf 
135     """
136     return s.splitlines(keep)
137
138 @cython.test_fail_if_path_exists(
139     "//CoerceToPyTypeNode", "//CoerceFromPyTypeNode",
140     "//CastNode", "//TypecastNode")
141 @cython.test_assert_path_exists(
142     "//PythonCapiCallNode")
143 def splitlines_keep_bint(unicode s):
144     """
145     >>> len(multiline_text.splitlines(True))
146     3
147     >>> print_all( multiline_text.splitlines(True) )
148     ab jd
149     <BLANKLINE>
150     sdflk as sa
151     <BLANKLINE>
152     sadas asdas fsdf 
153     >>> print_all( multiline_text.splitlines(False) )
154     ab jd
155     sdflk as sa
156     sadas asdas fsdf 
157     >>> len(splitlines_keep_bint(multiline_text))
158     7
159     >>> print_all( splitlines_keep_bint(multiline_text) )
160     ab jd
161     <BLANKLINE>
162     sdflk as sa
163     <BLANKLINE>
164     sadas asdas fsdf 
165     --
166     ab jd
167     sdflk as sa
168     sadas asdas fsdf 
169     """
170     return s.splitlines(True) + ['--'] + s.splitlines(False)
171
172
173 # unicode.join(s, iterable)
174
175 pipe_sep = u'|'
176
177 @cython.test_fail_if_path_exists(
178     "//CoerceToPyTypeNode", "//CoerceFromPyTypeNode",
179     "//CastNode", "//TypecastNode")
180 @cython.test_assert_path_exists(
181     "//PythonCapiCallNode")
182 def join(unicode sep, l):
183     """
184     >>> l = text.split()
185     >>> len(l)
186     8
187     >>> print( pipe_sep.join(l) )
188     ab|jd|sdflk|as|sa|sadas|asdas|fsdf
189     >>> print( join(pipe_sep, l) )
190     ab|jd|sdflk|as|sa|sadas|asdas|fsdf
191     """
192     return sep.join(l)
193
194 @cython.test_fail_if_path_exists(
195     "//CoerceToPyTypeNode", "//CoerceFromPyTypeNode",
196     "//CastNode", "//TypecastNode", "//NoneCheckNode")
197 @cython.test_assert_path_exists(
198     "//PythonCapiCallNode")
199 def join_sep(l):
200     """
201     >>> l = text.split()
202     >>> len(l)
203     8
204     >>> print( '|'.join(l) )
205     ab|jd|sdflk|as|sa|sadas|asdas|fsdf
206     >>> print( join_sep(l) )
207     ab|jd|sdflk|as|sa|sadas|asdas|fsdf
208     """
209     return u'|'.join(l)
210
211
212 # unicode.startswith(s, prefix, [start, [end]])
213
214 @cython.test_fail_if_path_exists(
215     "//CoerceToPyTypeNode",
216     "//CoerceFromPyTypeNode",
217     "//CastNode", "//TypecastNode")
218 @cython.test_assert_path_exists(
219     "//PythonCapiCallNode")
220 def startswith(unicode s, sub):
221     """
222     >>> text.startswith('ab ')
223     True
224     >>> startswith(text, 'ab ')
225     'MATCH'
226     >>> text.startswith('ab X')
227     False
228     >>> startswith(text, 'ab X')
229     'NO MATCH'
230
231     >>> text.startswith(('ab', 'ab '))
232     True
233     >>> startswith(text, ('ab', 'ab '))
234     'MATCH'
235     >>> text.startswith((' ab', 'ab X'))
236     False
237     >>> startswith(text, (' ab', 'ab X'))
238     'NO MATCH'
239     """
240     if s.startswith(sub):
241         return 'MATCH'
242     else:
243         return 'NO MATCH'
244
245 @cython.test_fail_if_path_exists(
246     "//CoerceToPyTypeNode",
247     "//CastNode", "//TypecastNode")
248 @cython.test_assert_path_exists(
249     "//CoerceFromPyTypeNode",
250     "//PythonCapiCallNode")
251 def startswith_start_end(unicode s, sub, start, end):
252     """
253     >>> text.startswith('b ', 1, 5)
254     True
255     >>> startswith_start_end(text, 'b ', 1, 5)
256     'MATCH'
257     >>> text.startswith('b X', 1, 5)
258     False
259     >>> startswith_start_end(text, 'b X', 1, 5)
260     'NO MATCH'
261     """
262     if s.startswith(sub, start, end):
263         return 'MATCH'
264     else:
265         return 'NO MATCH'
266
267
268 # unicode.endswith(s, prefix, [start, [end]])
269
270 @cython.test_fail_if_path_exists(
271     "//CoerceToPyTypeNode",
272     "//CoerceFromPyTypeNode",
273     "//CastNode", "//TypecastNode")
274 @cython.test_assert_path_exists(
275     "//PythonCapiCallNode")
276 def endswith(unicode s, sub):
277     """
278     >>> text.endswith('fsdf ')
279     True
280     >>> endswith(text, 'fsdf ')
281     'MATCH'
282     >>> text.endswith('fsdf X')
283     False
284     >>> endswith(text, 'fsdf X')
285     'NO MATCH'
286
287     >>> text.endswith(('fsdf', 'fsdf '))
288     True
289     >>> endswith(text, ('fsdf', 'fsdf '))
290     'MATCH'
291     >>> text.endswith(('fsdf', 'fsdf X'))
292     False
293     >>> endswith(text, ('fsdf', 'fsdf X'))
294     'NO MATCH'
295     """
296     if s.endswith(sub):
297         return 'MATCH'
298     else:
299         return 'NO MATCH'
300
301 @cython.test_fail_if_path_exists(
302     "//CoerceToPyTypeNode",
303     "//CastNode", "//TypecastNode")
304 @cython.test_assert_path_exists(
305     "//CoerceFromPyTypeNode",
306     "//PythonCapiCallNode")
307 def endswith_start_end(unicode s, sub, start, end):
308     """
309     >>> text.endswith('fsdf', 10, len(text)-1)
310     True
311     >>> endswith_start_end(text, 'fsdf', 10, len(text)-1)
312     'MATCH'
313     >>> text.endswith('fsdf ', 10, len(text)-1)
314     False
315     >>> endswith_start_end(text, 'fsdf ', 10, len(text)-1)
316     'NO MATCH'
317
318     >>> text.endswith(('fsd', 'fsdf'), 10, len(text)-1)
319     True
320     >>> endswith_start_end(text, ('fsd', 'fsdf'), 10, len(text)-1)
321     'MATCH'
322     >>> text.endswith(('fsdf ', 'fsdf X'), 10, len(text)-1)
323     False
324     >>> endswith_start_end(text, ('fsdf ', 'fsdf X'), 10, len(text)-1)
325     'NO MATCH'
326     """
327     if s.endswith(sub, start, end):
328         return 'MATCH'
329     else:
330         return 'NO MATCH'
331
332
333 # unicode.find(s, sub, [start, [end]])
334
335 @cython.test_fail_if_path_exists(
336 #    "//CoerceFromPyTypeNode",
337     "//CastNode", "//TypecastNode")
338 @cython.test_assert_path_exists(
339     "//CoerceToPyTypeNode",
340     "//PythonCapiCallNode")
341 def find(unicode s, substring):
342     """
343     >>> text.find('sa')
344     16
345     >>> find(text, 'sa')
346     16
347     """
348     cdef Py_ssize_t pos = s.find(substring)
349     return pos
350
351 @cython.test_fail_if_path_exists(
352 #    "//CoerceFromPyTypeNode",
353     "//CastNode", "//TypecastNode")
354 @cython.test_assert_path_exists(
355     "//CoerceToPyTypeNode",
356     "//PythonCapiCallNode")
357 def find_start_end(unicode s, substring, start, end):
358     """
359     >>> text.find('sa', 17, 25)
360     20
361     >>> find_start_end(text, 'sa', 17, 25)
362     20
363     """
364     cdef Py_ssize_t pos = s.find(substring, start, end)
365     return pos
366
367
368 # unicode.rfind(s, sub, [start, [end]])
369
370 @cython.test_fail_if_path_exists(
371 #    "//CoerceFromPyTypeNode",
372     "//CastNode", "//TypecastNode")
373 @cython.test_assert_path_exists(
374     "//CoerceToPyTypeNode",
375     "//PythonCapiCallNode")
376 def rfind(unicode s, substring):
377     """
378     >>> text.rfind('sa')
379     20
380     >>> rfind(text, 'sa')
381     20
382     """
383     cdef Py_ssize_t pos = s.rfind(substring)
384     return pos
385
386 @cython.test_fail_if_path_exists(
387 #    "//CoerceFromPyTypeNode",
388     "//CastNode", "//TypecastNode")
389 @cython.test_assert_path_exists(
390     "//CoerceToPyTypeNode",
391     "//PythonCapiCallNode")
392 def rfind_start_end(unicode s, substring, start, end):
393     """
394     >>> text.rfind('sa', 14, 19)
395     16
396     >>> rfind_start_end(text, 'sa', 14, 19)
397     16
398     """
399     cdef Py_ssize_t pos = s.rfind(substring, start, end)
400     return pos