cimport cython
+import sys
+
+PY_VERSION = sys.version_info
+
text = u'ab jd sdflk as sa sadas asdas fsdf '
sep = u' '
@cython.test_fail_if_path_exists(
"//CoerceToPyTypeNode", "//CoerceFromPyTypeNode",
- "//CastNode", "//TypecastNode")
+ "//CastNode", "//TypecastNode",
+ "//SimpleCallNode//AttributeNode[@is_py_attr = true]")
@cython.test_assert_path_exists(
- "//PythonCapiCallNode")
+ "//SimpleCallNode",
+ "//SimpleCallNode//NoneCheckNode",
+ "//SimpleCallNode//AttributeNode[@is_py_attr = false]")
def join(unicode sep, l):
"""
>>> l = text.split()
@cython.test_fail_if_path_exists(
"//CoerceToPyTypeNode", "//CoerceFromPyTypeNode",
- "//CastNode", "//TypecastNode", "//NoneCheckNode")
+ "//CastNode", "//TypecastNode", "//NoneCheckNode",
+ "//SimpleCallNode//AttributeNode[@is_py_attr = true]")
@cython.test_assert_path_exists(
- "//PythonCapiCallNode")
+ "//SimpleCallNode",
+ "//SimpleCallNode//AttributeNode[@is_py_attr = false]")
def join_sep(l):
"""
>>> l = text.split()
"""
return u'|'.join(l)
+@cython.test_assert_path_exists(
+ "//SimpleCallNode",
+ "//SimpleCallNode//NameNode")
+def join_unbound(unicode sep, l):
+ """
+ >>> l = text.split()
+ >>> len(l)
+ 8
+ >>> print( pipe_sep.join(l) )
+ ab|jd|sdflk|as|sa|sadas|asdas|fsdf
+ >>> print( join_unbound(pipe_sep, l) )
+ ab|jd|sdflk|as|sa|sadas|asdas|fsdf
+ """
+ join = unicode.join
+ return join(sep, l)
+
# unicode.startswith(s, prefix, [start, [end]])
>>> startswith(text, 'ab X')
'NO MATCH'
- >>> text.startswith(('ab', 'ab '))
+ >>> PY_VERSION < (2,5) or text.startswith(('ab', 'ab '))
True
>>> startswith(text, ('ab', 'ab '))
'MATCH'
- >>> text.startswith((' ab', 'ab X'))
- False
+ >>> PY_VERSION < (2,5) or not text.startswith((' ab', 'ab X'))
+ True
>>> startswith(text, (' ab', 'ab X'))
'NO MATCH'
"""
>>> endswith(text, 'fsdf X')
'NO MATCH'
- >>> text.endswith(('fsdf', 'fsdf '))
+ >>> PY_VERSION < (2,5) or text.endswith(('fsdf', 'fsdf '))
True
>>> endswith(text, ('fsdf', 'fsdf '))
'MATCH'
- >>> text.endswith(('fsdf', 'fsdf X'))
- False
+ >>> PY_VERSION < (2,5) or not text.endswith(('fsdf', 'fsdf X'))
+ True
>>> endswith(text, ('fsdf', 'fsdf X'))
'NO MATCH'
"""
>>> endswith_start_end(text, 'fsdf ', 10, len(text)-1)
'NO MATCH'
- >>> text.endswith(('fsd', 'fsdf'), 10, len(text)-1)
+ >>> PY_VERSION < (2,5) or text.endswith(('fsd', 'fsdf'), 10, len(text)-1)
True
>>> endswith_start_end(text, ('fsd', 'fsdf'), 10, len(text)-1)
'MATCH'
- >>> text.endswith(('fsdf ', 'fsdf X'), 10, len(text)-1)
- False
+ >>> PY_VERSION < (2,5) or not text.endswith(('fsdf ', 'fsdf X'), 10, len(text)-1)
+ True
>>> endswith_start_end(text, ('fsdf ', 'fsdf X'), 10, len(text)-1)
'NO MATCH'
"""
"""
cdef Py_ssize_t pos = s.count(substring, start, end)
return pos
+
+
+# unicode.replace(s, sub, repl, [maxcount])
+
+@cython.test_fail_if_path_exists(
+ "//CoerceFromPyTypeNode",
+ "//CastNode", "//TypecastNode")
+@cython.test_assert_path_exists(
+ "//PythonCapiCallNode")
+def replace(unicode s, substring, repl):
+ """
+ >>> print( text.replace('sa', 'SA') )
+ ab jd sdflk as SA SAdas asdas fsdf
+ >>> print( replace(text, 'sa', 'SA') )
+ ab jd sdflk as SA SAdas asdas fsdf
+ """
+ return s.replace(substring, repl)
+
+@cython.test_fail_if_path_exists(
+ "//CastNode", "//TypecastNode")
+@cython.test_assert_path_exists(
+ "//CoerceFromPyTypeNode",
+ "//PythonCapiCallNode")
+def replace_maxcount(unicode s, substring, repl, maxcount):
+ """
+ >>> print( text.replace('sa', 'SA', 1) )
+ ab jd sdflk as SA sadas asdas fsdf
+ >>> print( replace_maxcount(text, 'sa', 'SA', 1) )
+ ab jd sdflk as SA sadas asdas fsdf
+ """
+ return s.replace(substring, repl, maxcount)