1 | #!/usr/bin/env python2
|
2 | """
|
3 | strings.py
|
4 | """
|
5 | from __future__ import print_function
|
6 |
|
7 | import os
|
8 | from mycpp import mylib
|
9 | from mycpp.mylib import log
|
10 |
|
11 | from typing import List
|
12 |
|
13 | __all__ = ['should', 'not', 'appear', 'in C++']
|
14 | MYLIST = ['zzyyxx']
|
15 |
|
16 |
|
17 | def banner(s):
|
18 | # type: (str) -> None
|
19 | print('')
|
20 | print('=== %s ===' % s)
|
21 | print('')
|
22 |
|
23 |
|
24 | class Foo(object):
|
25 |
|
26 | def __init__(self):
|
27 | # type: () -> None
|
28 | self.s = 'mystr'
|
29 |
|
30 |
|
31 | def TestMethods():
|
32 | # type: () -> None
|
33 |
|
34 | s = 'a1bc'
|
35 |
|
36 | if s.startswith(''):
|
37 | print('empty yes')
|
38 |
|
39 | if s.startswith('a1'):
|
40 | print('a1 yes')
|
41 |
|
42 | if not s.startswith('zz'):
|
43 | print('zz no')
|
44 |
|
45 | if s.endswith(''):
|
46 | print('empty yes')
|
47 |
|
48 | if s.endswith('bc'):
|
49 | print('bc yes')
|
50 |
|
51 | if s.endswith('c'):
|
52 | print('bc yes')
|
53 |
|
54 | if not s.endswith('zzzzzz'):
|
55 | print('zzzzzz no')
|
56 |
|
57 | # This file is out of date! It thinks it happens in Python 3, but we have
|
58 | # it in Python 2.7
|
59 |
|
60 | # /home/andy/wedge/oils-for-unix.org/pkg/mypy/0.780/mypy/typeshed/stdlib/2/__builtin__.pyi:509: note: "startswith" of "str" defined here
|
61 |
|
62 | # Fixed here - https://github.com/python/typeshed/blob/main/stdlib/builtins.pyi
|
63 | #
|
64 | # It can be fixed by patching, gah
|
65 |
|
66 | # start pos
|
67 | #if s.startswith('bc', start=2):
|
68 | # print('bc YES')
|
69 |
|
70 | # find(s, start, end) can be used to implement TokenStartsWith() and
|
71 | # TokenEndsWith(), TokenEquals(), IsPlusEquals(), TokenContains(), etc.
|
72 |
|
73 | s = 'aaa-bb-cc'
|
74 | substrs = [
|
75 | 'aa',
|
76 | 'b',
|
77 | 'z',
|
78 | 'aaaa', # too long
|
79 | '',
|
80 | ]
|
81 | for substr in substrs:
|
82 | for start in xrange(0, len(s)):
|
83 | pos = s.find(substr, start)
|
84 | print('%s find %s start:%d => %d' % (s, substr, start, pos))
|
85 |
|
86 | print('---')
|
87 |
|
88 | for substr in substrs:
|
89 | for end in xrange(0, len(s)):
|
90 | pos = s.find(substr, 0, end)
|
91 | print('%s find %s end:%d => %d' % (s, substr, end, pos))
|
92 |
|
93 | print('---')
|
94 |
|
95 | # empty string test
|
96 | for start in xrange(0, 3):
|
97 | for end in xrange(0, 3):
|
98 | pos = s.find('', start, end)
|
99 | print('%s find empty [%d, %d) => %d' % (s, start, end, pos))
|
100 |
|
101 |
|
102 | def TestFormat():
|
103 | # type: () -> None
|
104 |
|
105 | banner('TestFormat')
|
106 |
|
107 | print('foo' + 'bar')
|
108 | print('foo' * 3)
|
109 | obj = Foo()
|
110 | print('foo' + obj.s)
|
111 |
|
112 | s = 'mystr'
|
113 | print('[%s]' % s)
|
114 |
|
115 | s = 'mystr'
|
116 | print('[%s, %s]' % (s, 'abc'))
|
117 |
|
118 | print('%s: 5%%-100%%' % 'abc')
|
119 |
|
120 | print('<a href="foo.html">%s</a>' % 'anchor')
|
121 |
|
122 | print("foo? %d" % ('f' in s))
|
123 | print("str? %d" % ('s' in s))
|
124 |
|
125 | print("int 5d %5d" % 35)
|
126 |
|
127 | print("'single'")
|
128 | print('"double"')
|
129 |
|
130 | # test escape codes
|
131 | print("a\tb\nc\td\n")
|
132 |
|
133 | x = 'x'
|
134 | print("%s\tb\n%s\td\n" % (x, x))
|
135 |
|
136 | fmt = "%dfoo"
|
137 | print(fmt % 10)
|
138 |
|
139 | fmts = ["foo%d"]
|
140 | print(fmts[0] % 10)
|
141 |
|
142 | print(("foo " + "%s") % "bar")
|
143 |
|
144 | # NUL bytes
|
145 | s = "spam\0%s" % "eggs"
|
146 |
|
147 | # TODO: There's a bug here -- we get len == 4 in C++, but it should be 9.
|
148 | # It's either StrFormat() or the bad JSON literals \u0000
|
149 | if 0:
|
150 | print("len(s) = %d" % len(s))
|
151 | print(s)
|
152 |
|
153 | s = "foo%s" % "\0bar"
|
154 | print("len(s) = %d" % len(s))
|
155 |
|
156 | print("%o" % 12345)
|
157 | print("%17o" % 12345)
|
158 | print("%017o" % 12345)
|
159 |
|
160 | print("%%%d%%%%" % 12345)
|
161 |
|
162 | print("%r" % "tab\tline\nline\r\n")
|
163 |
|
164 | s = 'a1b2c3d4e5'
|
165 | # Disable step support
|
166 | # print(s[0:10:2])
|
167 | # print(s[1:10:2])
|
168 | print(s.upper())
|
169 |
|
170 |
|
171 | def TestByteOperations():
|
172 | # type: () -> None
|
173 | banner('TestByteOperations')
|
174 |
|
175 | s = 'foo' * 10
|
176 |
|
177 | i = 0
|
178 | n = len(s)
|
179 | total = 0
|
180 | total2 = 0
|
181 | while i < n:
|
182 | byte = ord(s[i])
|
183 | byte2 = mylib.ByteAt(s, i)
|
184 |
|
185 | total += byte
|
186 | total2 += byte2
|
187 |
|
188 | i += 1
|
189 |
|
190 | if total != total2:
|
191 | raise AssertionError()
|
192 |
|
193 | print('total = %d' % total)
|
194 | print('total2 = %d' % total2)
|
195 |
|
196 |
|
197 | def TestBytes2():
|
198 | # type: () -> None
|
199 |
|
200 | banner('TestBytes2')
|
201 |
|
202 | b = [] # type: List[int]
|
203 | ch = [] # type: List[str]
|
204 | for i in xrange(256):
|
205 | # Shuffle it a bit, make it a better test
|
206 | j = 255 - i
|
207 | if j == 2:
|
208 | j = 0
|
209 |
|
210 | b.append(j)
|
211 | ch.append(chr(j))
|
212 |
|
213 | print('len(b) = %d' % len(b))
|
214 | print('len(ch) = %d' % len(ch))
|
215 |
|
216 | all_bytes = ''.join(ch)
|
217 |
|
218 | b2 = mylib.JoinBytes(b)
|
219 | if all_bytes == b2:
|
220 | print('EQUAL ==')
|
221 | else:
|
222 | raise AssertionError('should be equal')
|
223 |
|
224 | n = len(all_bytes)
|
225 | print('len(all_bytes) = %d' % n)
|
226 | print('')
|
227 | #print('[%s]' % all_bytes)
|
228 |
|
229 | i = 0
|
230 | while i < n:
|
231 | byte = mylib.ByteAt(all_bytes, i)
|
232 | #log('byte = %d', byte)
|
233 |
|
234 | if mylib.ByteEquals(byte, '['):
|
235 | print('LEFT')
|
236 | if mylib.ByteEquals(byte, ']'):
|
237 | print('RIGHT')
|
238 | if mylib.ByteEquals(byte, '\\'):
|
239 | print('BACKSLASH')
|
240 |
|
241 | # TODO: get rid of JSON crap
|
242 | #if mylib.ByteEqualsStr(byte, '\xff'):
|
243 | # print('0xff')
|
244 |
|
245 | if mylib.ByteEquals(byte, chr(255)):
|
246 | print('0xff')
|
247 |
|
248 | if mylib.ByteInSet(byte, 'abcXYZ'):
|
249 | print('abcXYZ')
|
250 |
|
251 | i += 1
|
252 |
|
253 | print('')
|
254 |
|
255 |
|
256 | def run_tests():
|
257 | # type: () -> None
|
258 |
|
259 | TestFormat()
|
260 | TestMethods()
|
261 | TestByteOperations()
|
262 | TestBytes2()
|
263 |
|
264 | print('len(MYLIST) = %d' % len(MYLIST))
|
265 |
|
266 |
|
267 | def run_benchmarks():
|
268 | # type: () -> None
|
269 | pass
|
270 |
|
271 |
|
272 | if __name__ == '__main__':
|
273 | if os.getenv('BENCHMARK'):
|
274 | log('Benchmarking...')
|
275 | run_benchmarks()
|
276 | else:
|
277 | run_tests()
|