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 @cython.test_assert_path_exists(
185 "//PythonCapiCallNode")
186 def join(unicode sep, l):
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
198 @cython.test_fail_if_path_exists(
199 "//CoerceToPyTypeNode", "//CoerceFromPyTypeNode",
200 "//CastNode", "//TypecastNode", "//NoneCheckNode")
201 @cython.test_assert_path_exists(
202 "//PythonCapiCallNode")
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
216 # unicode.startswith(s, prefix, [start, [end]])
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):
226 >>> text.startswith('ab ')
228 >>> startswith(text, 'ab ')
230 >>> text.startswith('ab X')
232 >>> startswith(text, 'ab X')
235 >>> PY_VERSION < (2,5) or text.startswith(('ab', 'ab '))
237 >>> startswith(text, ('ab', 'ab '))
239 >>> PY_VERSION < (2,5) or not text.startswith((' ab', 'ab X'))
241 >>> startswith(text, (' ab', 'ab X'))
244 if s.startswith(sub):
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):
257 >>> text.startswith('b ', 1, 5)
259 >>> startswith_start_end(text, 'b ', 1, 5)
261 >>> text.startswith('b X', 1, 5)
263 >>> startswith_start_end(text, 'b X', 1, 5)
266 if s.startswith(sub, start, end):
272 # unicode.endswith(s, prefix, [start, [end]])
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):
282 >>> text.endswith('fsdf ')
284 >>> endswith(text, 'fsdf ')
286 >>> text.endswith('fsdf X')
288 >>> endswith(text, 'fsdf X')
291 >>> PY_VERSION < (2,5) or text.endswith(('fsdf', 'fsdf '))
293 >>> endswith(text, ('fsdf', 'fsdf ')) == 'MATCH'
295 >>> PY_VERSION < (2,5) or not text.endswith(('fsdf', 'fsdf X'))
297 >>> endswith(text, ('fsdf', 'fsdf X')) == 'NO MATCH'
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):
313 >>> text.endswith('fsdf', 10, len(text)-1)
315 >>> endswith_start_end(text, 'fsdf', 10, len(text)-1)
317 >>> text.endswith('fsdf ', 10, len(text)-1)
319 >>> endswith_start_end(text, 'fsdf ', 10, len(text)-1)
322 >>> PY_VERSION < (2,5) or text.endswith(('fsd', 'fsdf'), 10, len(text)-1)
324 >>> endswith_start_end(text, ('fsd', 'fsdf'), 10, len(text)-1) == 'MATCH'
326 >>> PY_VERSION < (2,5) or not text.endswith(('fsdf ', 'fsdf X'), 10, len(text)-1)
328 >>> endswith_start_end(text, ('fsdf ', 'fsdf X'), 10, len(text)-1) == 'NO MATCH'
331 if s.endswith(sub, start, end):
337 # unicode.find(s, sub, [start, [end]])
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):
352 cdef Py_ssize_t pos = s.find(substring)
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):
362 >>> text.find('sa', 17, 25)
364 >>> find_start_end(text, 'sa', 17, 25)
367 cdef Py_ssize_t pos = s.find(substring, start, end)
371 # unicode.rfind(s, sub, [start, [end]])
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):
383 >>> rfind(text, 'sa')
386 cdef Py_ssize_t pos = s.rfind(substring)
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):
396 >>> text.rfind('sa', 14, 19)
398 >>> rfind_start_end(text, 'sa', 14, 19)
401 cdef Py_ssize_t pos = s.rfind(substring, start, end)
405 # unicode.count(s, sub, [start, [end]])
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):
417 >>> count(text, 'sa')
420 cdef Py_ssize_t pos = s.count(substring)
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):
430 >>> text.count('sa', 14, 21)
432 >>> text.count('sa', 14, 22)
434 >>> count_start_end(text, 'sa', 14, 21)
436 >>> count_start_end(text, 'sa', 14, 22)
439 cdef Py_ssize_t pos = s.count(substring, start, end)
443 # unicode.replace(s, sub, repl, [maxcount])
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):
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
457 return s.replace(substring, repl)
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):
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
471 return s.replace(substring, repl, maxcount)