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