1cimport cython
2
3@cython.test_assert_path_exists(
4    "//PythonCapiCallNode")
5def str_startswith(str s, sub, start=None, stop=None):
6    """
7    >>> str_startswith('a', 'a')
8    True
9    >>> str_startswith('ab', 'a')
10    True
11    >>> str_startswith('a', 'b')
12    False
13    >>> str_startswith('ab', 'b')
14    False
15    >>> str_startswith('a', ('a', 'b'))
16    True
17    >>> str_startswith('a', 'a', 1)
18    False
19    >>> str_startswith('a', 'a', 0, 0)
20    False
21    """
22
23    if start is None:
24      return s.startswith(sub)
25    elif stop is None:
26      return s.startswith(sub, start)
27    else:
28      return s.startswith(sub, start, stop)
29
30@cython.test_assert_path_exists(
31    "//PythonCapiCallNode")
32def str_endswith(str s, sub, start=None, stop=None):
33    """
34    >>> str_endswith('a', 'a')
35    True
36    >>> str_endswith('ba', 'a')
37    True
38    >>> str_endswith('a', 'b')
39    False
40    >>> str_endswith('ba', 'b')
41    False
42    >>> str_endswith('a', ('a', 'b'))
43    True
44    >>> str_endswith('a', 'a', 1)
45    False
46    >>> str_endswith('a', 'a', 0, 0)
47    False
48    """
49
50    if start is None:
51      return s.endswith(sub)
52    elif stop is None:
53      return s.endswith(sub, start)
54    else:
55      return s.endswith(sub, start, stop)
56
57
58def object_as_name(object):
59    """
60    >>> object_as_name('abx')
61    True
62    >>> object_as_name('abc')
63    False
64    """
65    return object.endswith("x")
66
67
68def str_as_name(str):
69    """
70    >>> str_as_name('abx')
71    True
72    >>> str_as_name('abc')
73    False
74    """
75    return str.endswith("x")
76
77
78@cython.test_assert_path_exists(
79    "//SimpleCallNode",
80    "//SimpleCallNode//NoneCheckNode",
81    "//SimpleCallNode//AttributeNode[@is_py_attr = false]")
82def str_join(str s, args):
83    """
84    >>> print(str_join('a', list('bbb')))
85    babab
86    """
87    result = s.join(args)
88    assert cython.typeof(result) == 'basestring object', cython.typeof(result)
89    return result
90
91
92@cython.test_fail_if_path_exists(
93    "//SimpleCallNode//NoneCheckNode",
94)
95@cython.test_assert_path_exists(
96    "//SimpleCallNode",
97    "//SimpleCallNode//AttributeNode[@is_py_attr = false]")
98def literal_join(args):
99    """
100    >>> print(literal_join(list('abcdefg')))
101    a|b|c|d|e|f|g
102    """
103    result = '|'.join(args)
104    assert cython.typeof(result) == 'basestring object', cython.typeof(result)
105    return result
106
107
108# unicode.__mod__(format, values)
109
110format1 = 'abc%sdef'
111format2 = 'abc%sdef%sghi'
112
113def mod_format(str s, values):
114    """
115    >>> mod_format(format1, 'sa') == 'abcsadef'  or  mod_format(format1, 'sa')
116    True
117    >>> mod_format(format2, ('XYZ', 'ABC')) == 'abcXYZdefABCghi'  or  mod_format(format2, ('XYZ', 'ABC'))
118    True
119    >>> mod_format(None, 'sa')  # doctest: +ELLIPSIS
120    Traceback (most recent call last):
121    TypeError: ...NoneType...
122    >>> class RMod(object):
123    ...     def __rmod__(self, other):
124    ...         return 123
125    >>> mod_format(None, RMod())
126    123
127    """
128    assert cython.typeof(s % values) == 'basestring object', cython.typeof(s % values)
129    return s % values
130
131
132def mod_format_literal(values):
133    """
134    >>> mod_format_literal('sa') == 'abcsadef'  or  mod_format(format1, 'sa')
135    True
136    >>> mod_format_literal(('sa',)) == 'abcsadef'  or  mod_format(format1, ('sa',))
137    True
138    >>> mod_format_literal(['sa']) == "abc['sa']def"  or  mod_format(format1, ['sa'])
139    True
140    """
141    assert cython.typeof('abc%sdef' % values) == 'basestring object', cython.typeof('abc%sdef' % values)
142    return 'abc%sdef' % values
143
144
145def mod_format_tuple(*values):
146    """
147    >>> mod_format_tuple('sa') == 'abcsadef'  or  mod_format(format1, 'sa')
148    True
149    >>> mod_format_tuple()
150    Traceback (most recent call last):
151    TypeError: not enough arguments for format string
152    """
153    assert cython.typeof('abc%sdef' % values) == 'basestring object', cython.typeof('abc%sdef' % values)
154    return 'abc%sdef' % values
155