Remove trailing whitespace.
[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     "//SimpleCallNode//AttributeNode[@is_py_attr = true]")
185 @cython.test_assert_path_exists(
186     "//SimpleCallNode",
187     "//SimpleCallNode//NoneCheckNode",
188     "//SimpleCallNode//AttributeNode[@is_py_attr = false]")
189 def join(unicode sep, l):
190     """
191     >>> l = text.split()
192     >>> len(l)
193     8
194     >>> print( pipe_sep.join(l) )
195     ab|jd|sdflk|as|sa|sadas|asdas|fsdf
196     >>> print( join(pipe_sep, l) )
197     ab|jd|sdflk|as|sa|sadas|asdas|fsdf
198     """
199     return sep.join(l)
200
201 @cython.test_fail_if_path_exists(
202     "//CoerceToPyTypeNode", "//CoerceFromPyTypeNode",
203     "//CastNode", "//TypecastNode", "//NoneCheckNode",
204     "//SimpleCallNode//AttributeNode[@is_py_attr = true]")
205 @cython.test_assert_path_exists(
206     "//SimpleCallNode",
207     "//SimpleCallNode//AttributeNode[@is_py_attr = false]")
208 def join_sep(l):
209     """
210     >>> l = text.split()
211     >>> len(l)
212     8
213     >>> print( '|'.join(l) )
214     ab|jd|sdflk|as|sa|sadas|asdas|fsdf
215     >>> print( join_sep(l) )
216     ab|jd|sdflk|as|sa|sadas|asdas|fsdf
217     """
218     return u'|'.join(l)
219
220 @cython.test_assert_path_exists(
221     "//SimpleCallNode",
222     "//SimpleCallNode//NameNode")
223 def join_unbound(unicode sep, l):
224     """
225     >>> l = text.split()
226     >>> len(l)
227     8
228     >>> print( pipe_sep.join(l) )
229     ab|jd|sdflk|as|sa|sadas|asdas|fsdf
230     >>> print( join_unbound(pipe_sep, l) )
231     ab|jd|sdflk|as|sa|sadas|asdas|fsdf
232     """
233     join = unicode.join
234     return join(sep, l)
235
236
237 # unicode.startswith(s, prefix, [start, [end]])
238
239 @cython.test_fail_if_path_exists(
240     "//CoerceToPyTypeNode",
241     "//CoerceFromPyTypeNode",
242     "//CastNode", "//TypecastNode")
243 @cython.test_assert_path_exists(
244     "//PythonCapiCallNode")
245 def startswith(unicode s, sub):
246     """
247     >>> text.startswith('ab ')
248     True
249     >>> startswith(text, 'ab ')
250     'MATCH'
251     >>> text.startswith('ab X')
252     False
253     >>> startswith(text, 'ab X')
254     'NO MATCH'
255
256     >>> PY_VERSION < (2,5) or text.startswith(('ab', 'ab '))
257     True
258     >>> startswith(text, ('ab', 'ab '))
259     'MATCH'
260     >>> PY_VERSION < (2,5) or not text.startswith((' ab', 'ab X'))
261     True
262     >>> startswith(text, (' ab', 'ab X'))
263     'NO MATCH'
264     """
265     if s.startswith(sub):
266         return 'MATCH'
267     else:
268         return 'NO MATCH'
269
270 @cython.test_fail_if_path_exists(
271     "//CoerceToPyTypeNode",
272     "//CastNode", "//TypecastNode")
273 @cython.test_assert_path_exists(
274     "//CoerceFromPyTypeNode",
275     "//PythonCapiCallNode")
276 def startswith_start_end(unicode s, sub, start, end):
277     """
278     >>> text.startswith('b ', 1, 5)
279     True
280     >>> startswith_start_end(text, 'b ', 1, 5)
281     'MATCH'
282     >>> text.startswith('b X', 1, 5)
283     False
284     >>> startswith_start_end(text, 'b X', 1, 5)
285     'NO MATCH'
286     """
287     if s.startswith(sub, start, end):
288         return 'MATCH'
289     else:
290         return 'NO MATCH'
291
292
293 # unicode.endswith(s, prefix, [start, [end]])
294
295 @cython.test_fail_if_path_exists(
296     "//CoerceToPyTypeNode",
297     "//CoerceFromPyTypeNode",
298     "//CastNode", "//TypecastNode")
299 @cython.test_assert_path_exists(
300     "//PythonCapiCallNode")
301 def endswith(unicode s, sub):
302     """
303     >>> text.endswith('fsdf ')
304     True
305     >>> endswith(text, 'fsdf ')
306     'MATCH'
307     >>> text.endswith('fsdf X')
308     False
309     >>> endswith(text, 'fsdf X')
310     'NO MATCH'
311
312     >>> PY_VERSION < (2,5) or text.endswith(('fsdf', 'fsdf '))
313     True
314     >>> endswith(text, ('fsdf', 'fsdf '))
315     'MATCH'
316     >>> PY_VERSION < (2,5) or not text.endswith(('fsdf', 'fsdf X'))
317     True
318     >>> endswith(text, ('fsdf', 'fsdf X'))
319     'NO MATCH'
320     """
321     if s.endswith(sub):
322         return 'MATCH'
323     else:
324         return 'NO MATCH'
325
326 @cython.test_fail_if_path_exists(
327     "//CoerceToPyTypeNode",
328     "//CastNode", "//TypecastNode")
329 @cython.test_assert_path_exists(
330     "//CoerceFromPyTypeNode",
331     "//PythonCapiCallNode")
332 def endswith_start_end(unicode s, sub, start, end):
333     """
334     >>> text.endswith('fsdf', 10, len(text)-1)
335     True
336     >>> endswith_start_end(text, 'fsdf', 10, len(text)-1)
337     'MATCH'
338     >>> text.endswith('fsdf ', 10, len(text)-1)
339     False
340     >>> endswith_start_end(text, 'fsdf ', 10, len(text)-1)
341     'NO MATCH'
342
343     >>> PY_VERSION < (2,5) or text.endswith(('fsd', 'fsdf'), 10, len(text)-1)
344     True
345     >>> endswith_start_end(text, ('fsd', 'fsdf'), 10, len(text)-1)
346     'MATCH'
347     >>> PY_VERSION < (2,5) or not text.endswith(('fsdf ', 'fsdf X'), 10, len(text)-1)
348     True
349     >>> endswith_start_end(text, ('fsdf ', 'fsdf X'), 10, len(text)-1)
350     'NO MATCH'
351     """
352     if s.endswith(sub, start, end):
353         return 'MATCH'
354     else:
355         return 'NO MATCH'
356
357
358 # unicode.find(s, sub, [start, [end]])
359
360 @cython.test_fail_if_path_exists(
361     "//CoerceFromPyTypeNode",
362     "//CastNode", "//TypecastNode")
363 @cython.test_assert_path_exists(
364     "//CoerceToPyTypeNode",
365     "//PythonCapiCallNode")
366 def find(unicode s, substring):
367     """
368     >>> text.find('sa')
369     16
370     >>> find(text, 'sa')
371     16
372     """
373     cdef Py_ssize_t pos = s.find(substring)
374     return pos
375
376 @cython.test_fail_if_path_exists(
377     "//CastNode", "//TypecastNode")
378 @cython.test_assert_path_exists(
379     "//CoerceToPyTypeNode",
380     "//PythonCapiCallNode")
381 def find_start_end(unicode s, substring, start, end):
382     """
383     >>> text.find('sa', 17, 25)
384     20
385     >>> find_start_end(text, 'sa', 17, 25)
386     20
387     """
388     cdef Py_ssize_t pos = s.find(substring, start, end)
389     return pos
390
391
392 # unicode.rfind(s, sub, [start, [end]])
393
394 @cython.test_fail_if_path_exists(
395     "//CoerceFromPyTypeNode",
396     "//CastNode", "//TypecastNode")
397 @cython.test_assert_path_exists(
398     "//CoerceToPyTypeNode",
399     "//PythonCapiCallNode")
400 def rfind(unicode s, substring):
401     """
402     >>> text.rfind('sa')
403     20
404     >>> rfind(text, 'sa')
405     20
406     """
407     cdef Py_ssize_t pos = s.rfind(substring)
408     return pos
409
410 @cython.test_fail_if_path_exists(
411     "//CastNode", "//TypecastNode")
412 @cython.test_assert_path_exists(
413     "//CoerceToPyTypeNode",
414     "//PythonCapiCallNode")
415 def rfind_start_end(unicode s, substring, start, end):
416     """
417     >>> text.rfind('sa', 14, 19)
418     16
419     >>> rfind_start_end(text, 'sa', 14, 19)
420     16
421     """
422     cdef Py_ssize_t pos = s.rfind(substring, start, end)
423     return pos
424
425
426 # unicode.count(s, sub, [start, [end]])
427
428 @cython.test_fail_if_path_exists(
429     "//CoerceFromPyTypeNode",
430     "//CastNode", "//TypecastNode")
431 @cython.test_assert_path_exists(
432     "//CoerceToPyTypeNode",
433     "//PythonCapiCallNode")
434 def count(unicode s, substring):
435     """
436     >>> text.count('sa')
437     2
438     >>> count(text, 'sa')
439     2
440     """
441     cdef Py_ssize_t pos = s.count(substring)
442     return pos
443
444 @cython.test_fail_if_path_exists(
445     "//CastNode", "//TypecastNode")
446 @cython.test_assert_path_exists(
447     "//CoerceToPyTypeNode",
448     "//PythonCapiCallNode")
449 def count_start_end(unicode s, substring, start, end):
450     """
451     >>> text.count('sa', 14, 21)
452     1
453     >>> text.count('sa', 14, 22)
454     2
455     >>> count_start_end(text, 'sa', 14, 21)
456     1
457     >>> count_start_end(text, 'sa', 14, 22)
458     2
459     """
460     cdef Py_ssize_t pos = s.count(substring, start, end)
461     return pos
462
463
464 # unicode.replace(s, sub, repl, [maxcount])
465
466 @cython.test_fail_if_path_exists(
467     "//CoerceFromPyTypeNode",
468     "//CastNode", "//TypecastNode")
469 @cython.test_assert_path_exists(
470     "//PythonCapiCallNode")
471 def replace(unicode s, substring, repl):
472     """
473     >>> print( text.replace('sa', 'SA') )
474     ab jd  sdflk as SA  SAdas asdas fsdf
475     >>> print( replace(text, 'sa', 'SA') )
476     ab jd  sdflk as SA  SAdas asdas fsdf
477     """
478     return s.replace(substring, repl)
479
480 @cython.test_fail_if_path_exists(
481     "//CastNode", "//TypecastNode")
482 @cython.test_assert_path_exists(
483     "//CoerceFromPyTypeNode",
484     "//PythonCapiCallNode")
485 def replace_maxcount(unicode s, substring, repl, maxcount):
486     """
487     >>> print( text.replace('sa', 'SA', 1) )
488     ab jd  sdflk as SA  sadas asdas fsdf
489     >>> print( replace_maxcount(text, 'sa', 'SA', 1) )
490     ab jd  sdflk as SA  sadas asdas fsdf
491     """
492     return s.replace(substring, repl, maxcount)