1 # -*- coding: utf-8 -*-
7 PY_VERSION = sys.version_info
9 text = u'ab jd sdflk as sa sadas asdas fsdf '
12 multiline_text = u'''\
22 # unicode.split(s, [sep, [maxsplit]])
24 @cython.test_assert_path_exists(
25 "//PythonCapiCallNode")
28 >>> print_all( text.split() )
37 >>> print_all( split(text) )
49 @cython.test_assert_path_exists(
50 "//PythonCapiCallNode")
51 def split_sep(unicode s, sep):
53 >>> print_all( text.split(sep) )
57 >>> print_all( split_sep(text, sep) )
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):
72 >>> print_all( text.split(sep, 1) )
74 sdflk as sa sadas asdas fsdf
75 >>> print_all( split_sep_max(text, sep, 1) )
77 sdflk as sa sadas asdas fsdf
79 return s.split(sep, max)
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):
88 >>> print_all( text.split(sep, 1) )
90 sdflk as sa sadas asdas fsdf
91 >>> print_all( split_sep_max_int(text, sep) )
93 sdflk as sa sadas asdas fsdf
95 return s.split(sep, 1)
98 # unicode.splitlines(s, [keepends])
100 @cython.test_assert_path_exists(
101 "//PythonCapiCallNode")
102 def splitlines(unicode s):
104 >>> len(multiline_text.splitlines())
106 >>> print_all( multiline_text.splitlines() )
110 >>> len(splitlines(multiline_text))
112 >>> print_all( splitlines(multiline_text) )
117 return s.splitlines()
119 @cython.test_assert_path_exists(
120 "//PythonCapiCallNode")
121 def splitlines_keep(unicode s, keep):
123 >>> len(multiline_text.splitlines(True))
125 >>> print_all( multiline_text.splitlines(True) )
131 >>> len(splitlines_keep(multiline_text, True))
133 >>> print_all( splitlines_keep(multiline_text, True) )
140 return s.splitlines(keep)
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):
149 >>> len(multiline_text.splitlines(True))
151 >>> print_all( multiline_text.splitlines(True) )
157 >>> print_all( multiline_text.splitlines(False) )
161 >>> len(splitlines_keep_bint(multiline_text))
163 >>> print_all( splitlines_keep_bint(multiline_text) )
174 return s.splitlines(True) + ['--'] + s.splitlines(False)
177 # unicode.join(s, iterable)
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(
187 "//SimpleCallNode//NoneCheckNode",
188 "//SimpleCallNode//AttributeNode[@is_py_attr = false]")
189 def join(unicode sep, l):
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
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(
207 "//SimpleCallNode//AttributeNode[@is_py_attr = false]")
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
220 @cython.test_assert_path_exists(
222 "//SimpleCallNode//NameNode")
223 def join_unbound(unicode sep, l):
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
237 # unicode.startswith(s, prefix, [start, [end]])
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):
247 >>> text.startswith('ab ')
249 >>> startswith(text, 'ab ')
251 >>> text.startswith('ab X')
253 >>> startswith(text, 'ab X')
256 >>> PY_VERSION < (2,5) or text.startswith(('ab', 'ab '))
258 >>> startswith(text, ('ab', 'ab '))
260 >>> PY_VERSION < (2,5) or not text.startswith((' ab', 'ab X'))
262 >>> startswith(text, (' ab', 'ab X'))
265 if s.startswith(sub):
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):
278 >>> text.startswith('b ', 1, 5)
280 >>> startswith_start_end(text, 'b ', 1, 5)
282 >>> text.startswith('b X', 1, 5)
284 >>> startswith_start_end(text, 'b X', 1, 5)
287 if s.startswith(sub, start, end):
293 # unicode.endswith(s, prefix, [start, [end]])
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):
303 >>> text.endswith('fsdf ')
305 >>> endswith(text, 'fsdf ')
307 >>> text.endswith('fsdf X')
309 >>> endswith(text, 'fsdf X')
312 >>> PY_VERSION < (2,5) or text.endswith(('fsdf', 'fsdf '))
314 >>> endswith(text, ('fsdf', 'fsdf '))
316 >>> PY_VERSION < (2,5) or not text.endswith(('fsdf', 'fsdf X'))
318 >>> endswith(text, ('fsdf', 'fsdf X'))
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):
334 >>> text.endswith('fsdf', 10, len(text)-1)
336 >>> endswith_start_end(text, 'fsdf', 10, len(text)-1)
338 >>> text.endswith('fsdf ', 10, len(text)-1)
340 >>> endswith_start_end(text, 'fsdf ', 10, len(text)-1)
343 >>> PY_VERSION < (2,5) or text.endswith(('fsd', 'fsdf'), 10, len(text)-1)
345 >>> endswith_start_end(text, ('fsd', 'fsdf'), 10, len(text)-1)
347 >>> PY_VERSION < (2,5) or not text.endswith(('fsdf ', 'fsdf X'), 10, len(text)-1)
349 >>> endswith_start_end(text, ('fsdf ', 'fsdf X'), 10, len(text)-1)
352 if s.endswith(sub, start, end):
358 # unicode.find(s, sub, [start, [end]])
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):
373 cdef Py_ssize_t pos = s.find(substring)
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):
383 >>> text.find('sa', 17, 25)
385 >>> find_start_end(text, 'sa', 17, 25)
388 cdef Py_ssize_t pos = s.find(substring, start, end)
392 # unicode.rfind(s, sub, [start, [end]])
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):
404 >>> rfind(text, 'sa')
407 cdef Py_ssize_t pos = s.rfind(substring)
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):
417 >>> text.rfind('sa', 14, 19)
419 >>> rfind_start_end(text, 'sa', 14, 19)
422 cdef Py_ssize_t pos = s.rfind(substring, start, end)
426 # unicode.count(s, sub, [start, [end]])
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):
438 >>> count(text, 'sa')
441 cdef Py_ssize_t pos = s.count(substring)
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):
451 >>> text.count('sa', 14, 21)
453 >>> text.count('sa', 14, 22)
455 >>> count_start_end(text, 'sa', 14, 21)
457 >>> count_start_end(text, 'sa', 14, 22)
460 cdef Py_ssize_t pos = s.count(substring, start, end)
464 # unicode.replace(s, sub, repl, [maxcount])
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):
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
478 return s.replace(substring, repl)
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):
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
492 return s.replace(substring, repl, maxcount)