cython

Форк
0
/
unicodemethods.pyx 
952 строки · 25.3 Кб
1
# -*- coding: utf-8 -*-
2

3
cimport cython
4

5

6
text = u'ab jd  sdflk as sa  sadas asdas fsdf '
7
sep = u'  '
8
format1 = u'abc%sdef'
9
format2 = u'abc%sdef%sghi'
10
unicode_sa = u'sa'
11

12
multiline_text = u'''\
13
ab jd
14
sdflk as sa
15
sadas asdas fsdf '''
16

17
def print_all(l):
18
    for s in l:
19
        print(s)
20

21

22
# unicode.split(s, [sep, [maxsplit]])
23

24
@cython.test_assert_path_exists(
25
    "//PythonCapiCallNode")
26
def split(unicode s):
27
    """
28
    >>> def test_split():
29
    ...     py = text.split()
30
    ...     cy = split(text)
31
    ...     assert py == cy, (py, cy)
32
    ...     return cy
33
    >>> print_all( test_split() )
34
    ab
35
    jd
36
    sdflk
37
    as
38
    sa
39
    sadas
40
    asdas
41
    fsdf
42
    """
43
    return s.split()
44

45
@cython.test_assert_path_exists(
46
    "//PythonCapiCallNode")
47
def split_sep(unicode s, sep):
48
    """
49
    >>> def test_split_sep(sep):
50
    ...     py = text.split(sep)
51
    ...     cy = split_sep(text, sep)
52
    ...     assert py == cy, (py, cy)
53
    ...     return cy
54
    >>> print_all( test_split_sep(sep) )
55
    ab jd
56
    sdflk as sa
57
    sadas asdas fsdf\x20
58
    >>> print_all( test_split_sep(None) )
59
    ab
60
    jd
61
    sdflk
62
    as
63
    sa
64
    sadas
65
    asdas
66
    fsdf
67
    """
68
    return s.split(sep)
69

70
@cython.test_fail_if_path_exists(
71
    "//CoerceToPyTypeNode",
72
    "//CastNode", "//TypecastNode")
73
@cython.test_assert_path_exists(
74
    "//CoerceFromPyTypeNode",
75
    "//PythonCapiCallNode")
76
def split_sep_max(unicode s, sep, max):
77
    """
78
    >>> def test_split_sep_max(sep, max):
79
    ...     py = text.split(sep, max)
80
    ...     cy = split_sep_max(text, sep, max)
81
    ...     assert py == cy, (py, cy)
82
    ...     return cy
83
    >>> print_all( test_split_sep_max(sep, 1) )
84
    ab jd
85
    sdflk as sa  sadas asdas fsdf\x20
86
    >>> print_all( test_split_sep_max(None, 2) )
87
    ab
88
    jd
89
    sdflk as sa  sadas asdas fsdf\x20
90
    >>> print_all( text.split(None, 2) )
91
    ab
92
    jd
93
    sdflk as sa  sadas asdas fsdf\x20
94
    >>> print_all( split_sep_max(text, None, 2) )
95
    ab
96
    jd
97
    sdflk as sa  sadas asdas fsdf\x20
98
    """
99
    return s.split(sep, max)
100

101
@cython.test_fail_if_path_exists(
102
    "//CoerceToPyTypeNode", "//CoerceFromPyTypeNode",
103
    "//CastNode", "//TypecastNode")
104
@cython.test_assert_path_exists(
105
    "//PythonCapiCallNode")
106
def split_sep_max_int(unicode s, sep):
107
    """
108
    >>> def test_split_sep_max_int(sep):
109
    ...     py = text.split(sep, 1)
110
    ...     cy = split_sep_max_int(text, sep)
111
    ...     assert py == cy, (py, cy)
112
    ...     return cy
113
    >>> print_all( test_split_sep_max_int(sep) )
114
    ab jd
115
    sdflk as sa  sadas asdas fsdf\x20
116
    >>> print_all( test_split_sep_max_int(None) )
117
    ab
118
    jd  sdflk as sa  sadas asdas fsdf\x20
119
    """
120
    return s.split(sep, 1)
121

122

123
# unicode.splitlines(s, [keepends])
124

125
@cython.test_assert_path_exists(
126
    "//PythonCapiCallNode")
127
def splitlines(unicode s):
128
    """
129
    >>> def test_splitlines(s):
130
    ...     py = s.splitlines()
131
    ...     cy = splitlines(s)
132
    ...     assert py == cy, (py, cy)
133
    ...     return cy
134
    >>> len(test_splitlines(multiline_text))
135
    3
136
    >>> print_all( test_splitlines(multiline_text) )
137
    ab jd
138
    sdflk as sa
139
    sadas asdas fsdf\x20
140
    """
141
    return s.splitlines()
142

143
@cython.test_assert_path_exists(
144
    "//PythonCapiCallNode")
145
def splitlines_keep(unicode s, keep):
146
    """
147
    >>> def test_splitlines_keep(s, keep):
148
    ...     py = s.splitlines(keep)
149
    ...     cy = splitlines_keep(s, keep)
150
    ...     assert py == cy, (py, cy)
151
    ...     return cy
152
    >>> len(test_splitlines_keep(multiline_text, True))
153
    3
154
    >>> print_all( test_splitlines_keep(multiline_text, True) )
155
    ab jd
156
    <BLANKLINE>
157
    sdflk as sa
158
    <BLANKLINE>
159
    sadas asdas fsdf\x20
160
    """
161
    return s.splitlines(keep)
162

163
@cython.test_fail_if_path_exists(
164
    "//CoerceToPyTypeNode", "//CoerceFromPyTypeNode",
165
    "//CastNode", "//TypecastNode")
166
@cython.test_assert_path_exists(
167
    "//PythonCapiCallNode")
168
def splitlines_keep_bint(unicode s):
169
    """
170
    >>> def test_splitlines_keep_bint(s):
171
    ...     py = s.splitlines(True) + ['--'] + s.splitlines(False)
172
    ...     cy = splitlines_keep_bint(s)
173
    ...     assert py == cy, (py, cy)
174
    ...     return cy
175
    >>> len(test_splitlines_keep_bint(multiline_text))
176
    7
177
    >>> print_all( test_splitlines_keep_bint(multiline_text) )
178
    ab jd
179
    <BLANKLINE>
180
    sdflk as sa
181
    <BLANKLINE>
182
    sadas asdas fsdf\x20
183
    --
184
    ab jd
185
    sdflk as sa
186
    sadas asdas fsdf\x20
187
    """
188
    return s.splitlines(True) + ['--'] + s.splitlines(False)
189

190

191
# unicode.join(s, iterable)
192

193
pipe_sep = u'|'
194

195
@cython.test_fail_if_path_exists(
196
    "//CoerceToPyTypeNode", "//CoerceFromPyTypeNode",
197
    "//CastNode", "//TypecastNode",
198
    "//SimpleCallNode//AttributeNode[@is_py_attr = true]")
199
@cython.test_assert_path_exists(
200
    "//PythonCapiCallNode",
201
)
202
def join(unicode sep, l):
203
    """
204
    >>> def test_join(sep, l):
205
    ...     py = sep.join(l)
206
    ...     cy = join(sep, l)
207
    ...     assert py == cy, (py, cy)
208
    ...     return cy
209
    >>> l = text.split()
210
    >>> len(l)
211
    8
212
    >>> print( test_join(pipe_sep, l) )
213
    ab|jd|sdflk|as|sa|sadas|asdas|fsdf
214
    """
215
    return sep.join(l)
216

217

218
@cython.test_fail_if_path_exists(
219
    "//CoerceToPyTypeNode", "//CoerceFromPyTypeNode",
220
    "//CastNode", "//TypecastNode", "//NoneCheckNode",
221
    "//SimpleCallNode//AttributeNode[@is_py_attr = true]")
222
@cython.test_assert_path_exists(
223
    "//PythonCapiCallNode",
224
)
225
def join_sep(l):
226
    """
227
    >>> def test_join_sep(l):
228
    ...     py = '|'.join(l)
229
    ...     cy = join_sep(l)
230
    ...     assert py == cy, (py, cy)
231
    ...     return cy
232
    >>> l = text.split()
233
    >>> len(l)
234
    8
235
    >>> print( test_join_sep(l) )
236
    ab|jd|sdflk|as|sa|sadas|asdas|fsdf
237
    """
238
    result = u'|'.join(l)
239
    assert cython.typeof(result) == "str object", cython.typeof(result)
240
    return result
241

242

243
@cython.test_fail_if_path_exists(
244
    "//CoerceToPyTypeNode", "//CoerceFromPyTypeNode",
245
    "//CastNode", "//TypecastNode", "//NoneCheckNode",
246
    "//SimpleCallNode//AttributeNode[@is_py_attr = true]"
247
)
248
@cython.test_assert_path_exists(
249
    "//PythonCapiCallNode",
250
    "//InlinedGeneratorExpressionNode"
251
)
252
def join_sep_genexpr(l):
253
    """
254
    >>> def test_join_sep_genexpr(l):
255
    ...     py = '|'.join(s + ' ' for s in l)
256
    ...     cy = join_sep_genexpr(l)
257
    ...     assert py == cy, (py, cy)
258
    ...     return cy
259
    >>> l = text.split()
260
    >>> len(l)
261
    8
262
    >>> print( '<<%s>>' % test_join_sep_genexpr(l) )
263
    <<ab |jd |sdflk |as |sa |sadas |asdas |fsdf >>
264
    """
265
    result = u'|'.join(s + u' ' for s in l)
266
    assert cython.typeof(result) == "str object", cython.typeof(result)
267
    return result
268

269

270
@cython.test_fail_if_path_exists(
271
    "//CoerceToPyTypeNode", "//CoerceFromPyTypeNode",
272
    "//CastNode", "//TypecastNode",
273
)
274
@cython.test_assert_path_exists(
275
    "//PythonCapiCallNode",
276
    "//InlinedGeneratorExpressionNode"
277
)
278
def join_sep_genexpr_dictiter(dict d):
279
    """
280
    >>> def test_join_sep_genexpr_dictiter(d):
281
    ...     py = '|'.join( sorted(' '.join('%s:%s' % (k, v) for k, v in d.items()).split()) )
282
    ...     cy = '|'.join( sorted(join_sep_genexpr_dictiter(d).split()) )
283
    ...     assert py == cy, (py, cy)
284
    ...     return cy
285
    >>> l = text.split()
286
    >>> d = dict(zip(range(len(l)), l))
287
    >>> print( test_join_sep_genexpr_dictiter(d) )
288
    0:ab|1:jd|2:sdflk|3:as|4:sa|5:sadas|6:asdas|7:fsdf
289
    """
290
    result = u' '.join('%s:%s' % (k, v) for k, v in d.iteritems())
291
    assert cython.typeof(result) == "str object", cython.typeof(result)
292
    return result
293

294

295
@cython.test_assert_path_exists(
296
    "//PythonCapiCallNode",
297
)
298
def join_unbound(unicode sep, l):
299
    """
300
    >>> def test_join_unbound(sep, l):
301
    ...     py = sep.join(l)
302
    ...     cy = join_unbound(sep, l)
303
    ...     assert py == cy, (py, cy)
304
    ...     return cy
305
    >>> l = text.split()
306
    >>> len(l)
307
    8
308
    >>> print( test_join_unbound(pipe_sep, l) )
309
    ab|jd|sdflk|as|sa|sadas|asdas|fsdf
310
    """
311
    join = unicode.join
312
    return join(sep, l)
313

314

315
# unicode.startswith(s, prefix, [start, [end]])
316

317
@cython.test_fail_if_path_exists(
318
    "//CoerceToPyTypeNode",
319
    "//CoerceFromPyTypeNode",
320
    "//CastNode", "//TypecastNode")
321
@cython.test_assert_path_exists(
322
    "//PythonCapiCallNode")
323
def startswith(unicode s, sub):
324
    """
325
    >>> def test_startswith(s, sub):
326
    ...     py = s.startswith(sub)
327
    ...     cy = startswith(s, sub)
328
    ...     assert py == cy, (py, cy)
329
    ...     return cy
330
    >>> test_startswith(text, 'ab ')
331
    True
332
    >>> test_startswith(text, 'ab X')
333
    False
334

335
    >>> test_startswith(text, ('ab', 'ab '))
336
    True
337
    >>> not test_startswith(text, (' ab', 'ab X'))
338
    True
339
    """
340
    if s.startswith(sub):
341
        return True
342
    else:
343
        return False
344

345
@cython.test_fail_if_path_exists(
346
    "//CoerceToPyTypeNode",
347
    "//CastNode", "//TypecastNode")
348
@cython.test_assert_path_exists(
349
    "//CoerceFromPyTypeNode",
350
    "//PythonCapiCallNode")
351
def startswith_start_end(unicode s, sub, start, end):
352
    """
353
    >>> def test_startswith_start_end(s, sub, start, end):
354
    ...     py = s.startswith(sub, start, end)
355
    ...     cy = startswith_start_end(s, sub, start, end)
356
    ...     assert py == cy, (py, cy)
357
    ...     return cy
358
    >>> test_startswith_start_end(text, 'b ', 1, 5)
359
    True
360
    >>> test_startswith_start_end(text, 'ab ', -1000, 5000)
361
    True
362
    >>> test_startswith_start_end(text, 'b X', 1, 5)
363
    False
364

365
    >>> test_startswith_start_end(text, 'ab ', None, None)
366
    True
367
    >>> test_startswith_start_end(text, 'ab ', 1, None)
368
    False
369
    >>> test_startswith_start_end(text, 'b ',  1, None)
370
    True
371
    >>> test_startswith_start_end(text, 'ab ', None, 3)
372
    True
373
    >>> test_startswith_start_end(text, 'ab ', None, 2)
374
    False
375
    """
376
    if s.startswith(sub, start, end):
377
        return True
378
    else:
379
        return False
380

381

382
# unicode.endswith(s, prefix, [start, [end]])
383

384
@cython.test_fail_if_path_exists(
385
    "//CoerceToPyTypeNode",
386
    "//CoerceFromPyTypeNode",
387
    "//CastNode", "//TypecastNode")
388
@cython.test_assert_path_exists(
389
    "//PythonCapiCallNode")
390
def endswith(unicode s, sub):
391
    """
392
    >>> def test_endswith(s, sub):
393
    ...     py = s.endswith(sub)
394
    ...     cy = endswith(s, sub)
395
    ...     assert py == cy, (py, cy)
396
    ...     return cy
397
    >>> test_endswith(text, 'fsdf ')
398
    True
399
    >>> test_endswith(text, 'fsdf X')
400
    False
401

402
    >>> test_endswith(text, ('fsdf', 'fsdf '))
403
    True
404
    >>> test_endswith(text, ('fsdf', 'fsdf X'))
405
    False
406
    """
407
    if s.endswith(sub):
408
        return True
409
    else:
410
        return False
411

412
@cython.test_fail_if_path_exists(
413
    "//CoerceToPyTypeNode",
414
    "//CastNode", "//TypecastNode")
415
@cython.test_assert_path_exists(
416
    "//CoerceFromPyTypeNode",
417
    "//PythonCapiCallNode")
418
def endswith_start_end(unicode s, sub, start, end):
419
    """
420
    >>> def test_endswith_start_end(s, sub, start, end):
421
    ...     py = s.endswith(sub, start, end)
422
    ...     cy = endswith_start_end(s, sub, start, end)
423
    ...     assert py == cy, (py, cy)
424
    ...     return cy
425
    >>> test_endswith_start_end(text, 'fsdf', 10, len(text)-1)
426
    True
427
    >>> test_endswith_start_end(text, 'fsdf ', 10, len(text)-1)
428
    False
429

430
    >>> test_endswith_start_end(text, 'fsdf ', -1000, 5000)
431
    True
432

433
    >>> test_endswith_start_end(text, ('fsd', 'fsdf'), 10, len(text)-1)
434
    True
435
    >>> test_endswith_start_end(text, ('fsdf ', 'fsdf X'), 10, len(text)-1)
436
    False
437

438
    >>> test_endswith_start_end(text, 'fsdf ', None, None)
439
    True
440
    >>> test_endswith_start_end(text, 'fsdf ', 32, None)
441
    True
442
    >>> test_endswith_start_end(text, 'fsdf ', 33, None)
443
    False
444
    >>> test_endswith_start_end(text, 'fsdf ', None, 37)
445
    True
446
    >>> test_endswith_start_end(text, 'fsdf ', None, 36)
447
    False
448
    """
449
    if s.endswith(sub, start, end):
450
        return True
451
    else:
452
        return False
453

454

455
# unicode.__contains__(s, sub)
456

457
@cython.test_fail_if_path_exists(
458
    "//CoerceFromPyTypeNode", "//AttributeNode")
459
@cython.test_assert_path_exists(
460
    "//CoerceToPyTypeNode", "//PrimaryCmpNode")
461
def in_test(unicode s, substring):
462
    """
463
    >>> in_test(text, 'sa')
464
    True
465
    >>> in_test(text, 'XYZ')
466
    False
467
    >>> in_test(None, 'sa')
468
    Traceback (most recent call last):
469
    TypeError: 'NoneType' object is not iterable
470
    """
471
    return substring in s
472

473

474
# unicode.__concat__(s, suffix)
475

476
def concat_any(unicode s, suffix):
477
    """
478
    >>> concat(text, 'sa') == text + 'sa'  or  concat(text, 'sa')
479
    True
480
    >>> concat(None, 'sa')   # doctest: +ELLIPSIS
481
    Traceback (most recent call last):
482
    TypeError: ...
483
    >>> concat(text, None)   # doctest: +ELLIPSIS
484
    Traceback (most recent call last):
485
    TypeError: ...
486
    >>> class RAdd(object):
487
    ...     def __radd__(self, other):
488
    ...         return 123
489
    >>> concat(None, 'sa')   # doctest: +ELLIPSIS
490
    Traceback (most recent call last):
491
    TypeError: ...
492
    """
493
    assert cython.typeof(s + suffix) == 'Python object', cython.typeof(s + suffix)
494
    return s + suffix
495

496

497
def concat(unicode s, str suffix):
498
    """
499
    >>> concat(text, 'sa') == text + 'sa'  or  concat(text, 'sa')
500
    True
501
    >>> concat(None, 'sa')   # doctest: +ELLIPSIS
502
    Traceback (most recent call last):
503
    TypeError: ...
504
    >>> concat(text, None)   # doctest: +ELLIPSIS
505
    Traceback (most recent call last):
506
    TypeError: ...
507
    >>> class RAdd(object):
508
    ...     def __radd__(self, other):
509
    ...         return 123
510
    >>> concat(None, 'sa')   # doctest: +ELLIPSIS
511
    Traceback (most recent call last):
512
    TypeError: ...
513
    """
514
    assert cython.typeof(s + object()) == 'Python object', cython.typeof(s + object())
515
    assert cython.typeof(s + suffix) == "str object", cython.typeof(s + suffix)
516
    return s + suffix
517

518

519
def concat_literal_str(str suffix):
520
    """
521
    >>> concat_literal_str('sa') == 'abcsa'  or  concat_literal_str('sa')
522
    True
523
    >>> concat_literal_str(None)  # doctest: +ELLIPSIS
524
    Traceback (most recent call last):
525
    TypeError: ...NoneType...
526
    """
527
    assert cython.typeof(u'abc' + object()) == 'Python object', cython.typeof(u'abc' + object())
528
    assert cython.typeof(u'abc' + suffix) == "str object", cython.typeof(u'abc' + suffix)
529
    return u'abc' + suffix
530

531

532
def concat_literal_unicode(unicode suffix):
533
    """
534
    >>> concat_literal_unicode(unicode_sa) == 'abcsa'  or  concat_literal_unicode(unicode_sa)
535
    True
536
    >>> concat_literal_unicode(None)  # doctest: +ELLIPSIS
537
    Traceback (most recent call last):
538
    TypeError: ...NoneType...
539
    """
540
    assert cython.typeof(u'abc' + suffix) == "str object", cython.typeof(u'abc' + suffix)
541
    return u'abc' + suffix
542

543

544
# unicode.__mod__(format, values)
545

546
def mod_format(unicode s, values):
547
    """
548
    >>> mod_format(format1, 'sa') == 'abcsadef'  or  mod_format(format1, 'sa')
549
    True
550
    >>> mod_format(format2, ('XYZ', 'ABC')) == 'abcXYZdefABCghi'  or  mod_format(format2, ('XYZ', 'ABC'))
551
    True
552

553
    Exact TypeError message is different in PyPy
554
    >>> mod_format(None, 'sa')   # doctest: +IGNORE_EXCEPTION_DETAIL
555
    Traceback (most recent call last):
556
    TypeError: unsupported operand type(s) for %: 'NoneType' and 'str'
557
    >>> class RMod(object):
558
    ...     def __rmod__(self, other):
559
    ...         return 123
560
    >>> mod_format(None, RMod())
561
    123
562
    """
563
    assert cython.typeof(s % values) == 'Python object', cython.typeof(s % values)
564
    return s % values
565

566

567
def mod_format_literal(values):
568
    """
569
    >>> mod_format_literal('sa') == 'abcsadef'  or  mod_format(format1, 'sa')
570
    True
571
    >>> mod_format_literal(('sa',)) == 'abcsadef'  or  mod_format(format1, ('sa',))
572
    True
573
    >>> mod_format_literal(['sa']) == "abc['sa']def"  or  mod_format(format1, ['sa'])
574
    True
575
    """
576
    assert cython.typeof(u'abc%sdef' % values) == "str object", cython.typeof(u'abc%sdef' % values)
577
    return u'abc%sdef' % values
578

579

580
def mod_format_tuple(*values):
581
    """
582
    >>> mod_format_tuple('sa') == 'abcsadef'  or  mod_format(format1, 'sa')
583
    True
584
    >>> mod_format_tuple()
585
    Traceback (most recent call last):
586
    TypeError: not enough arguments for format string
587
    """
588
    assert cython.typeof(u'abc%sdef' % values) == "str object", cython.typeof(u'abc%sdef' % values)
589
    return u'abc%sdef' % values
590

591

592
# unicode.find(s, sub, [start, [end]])
593

594
@cython.test_fail_if_path_exists(
595
    "//CoerceFromPyTypeNode",
596
    "//CastNode", "//TypecastNode")
597
@cython.test_assert_path_exists(
598
    "//CoerceToPyTypeNode",
599
    "//PythonCapiCallNode")
600
def find(unicode s, substring):
601
    """
602
    >>> def test_find(s, substring):
603
    ...     py = s.find(substring)
604
    ...     cy = find(s, substring)
605
    ...     assert py == cy, (py, cy)
606
    ...     return cy
607
    >>> test_find(text, 'sa')
608
    16
609
    """
610
    cdef Py_ssize_t pos = s.find(substring)
611
    return pos
612

613
@cython.test_fail_if_path_exists(
614
    "//CastNode", "//TypecastNode")
615
@cython.test_assert_path_exists(
616
    "//CoerceToPyTypeNode",
617
    "//PythonCapiCallNode")
618
def find_start_end(unicode s, substring, start, end):
619
    """
620
    >>> def test_find_start_end(s, substring, start, end):
621
    ...     py = s.find(substring, start, end)
622
    ...     cy = find_start_end(s, substring, start, end)
623
    ...     assert py == cy, (py, cy)
624
    ...     return cy
625
    >>> test_find_start_end(text, 'sa', 17, 25)
626
    20
627
    >>> test_find_start_end(text, 'sa', None, None)
628
    16
629
    >>> test_find_start_end(text, 'sa', 16, None)
630
    16
631
    >>> test_find_start_end(text, 'sa', 17, None)
632
    20
633
    >>> test_find_start_end(text, 'sa', None, 16)
634
    -1
635
    >>> test_find_start_end(text, 'sa', None, 19)
636
    16
637
    """
638
    cdef Py_ssize_t pos = s.find(substring, start, end)
639
    return pos
640

641

642
# unicode.rfind(s, sub, [start, [end]])
643

644
@cython.test_fail_if_path_exists(
645
    "//CoerceFromPyTypeNode",
646
    "//CastNode", "//TypecastNode")
647
@cython.test_assert_path_exists(
648
    "//CoerceToPyTypeNode",
649
    "//PythonCapiCallNode")
650
def rfind(unicode s, substring):
651
    """
652
    >>> def test_rfind(s, substring):
653
    ...     py = s.rfind(substring)
654
    ...     cy = rfind(s, substring)
655
    ...     assert py == cy, (py, cy)
656
    ...     return cy
657
    >>> test_rfind(text, 'sa')
658
    20
659
    """
660
    cdef Py_ssize_t pos = s.rfind(substring)
661
    return pos
662

663
@cython.test_fail_if_path_exists(
664
    "//CastNode", "//TypecastNode")
665
@cython.test_assert_path_exists(
666
    "//CoerceToPyTypeNode",
667
    "//PythonCapiCallNode")
668
def rfind_start_end(unicode s, substring, start, end):
669
    """
670
    >>> def test_rfind_start_end(s, substring, start, end):
671
    ...     py = s.rfind(substring, start, end)
672
    ...     cy = rfind_start_end(s, substring, start, end)
673
    ...     assert py == cy, (py, cy)
674
    ...     return cy
675
    >>> test_rfind_start_end(text, 'sa', 14, 19)
676
    16
677
    >>> test_rfind_start_end(text, 'sa', None, None)
678
    20
679
    >>> test_rfind_start_end(text, 'sa', 16, None)
680
    20
681
    >>> test_rfind_start_end(text, 'sa', 21, None)
682
    -1
683
    >>> test_rfind_start_end(text, 'sa', None, 22)
684
    20
685
    >>> test_rfind_start_end(text, 'sa', None, 21)
686
    16
687
    """
688
    cdef Py_ssize_t pos = s.rfind(substring, start, end)
689
    return pos
690

691

692
# unicode.count(s, sub, [start, [end]])
693

694
@cython.test_fail_if_path_exists(
695
    "//CoerceFromPyTypeNode",
696
    "//CastNode", "//TypecastNode")
697
@cython.test_assert_path_exists(
698
    "//CoerceToPyTypeNode",
699
    "//PythonCapiCallNode")
700
def count(unicode s, substring):
701
    """
702
    >>> def test_count(s, substring):
703
    ...     py = s.count(substring)
704
    ...     cy = count(s, substring)
705
    ...     assert py == cy, (py, cy)
706
    ...     return cy
707
    >>> test_count(text, 'sa')
708
    2
709
    """
710
    cdef Py_ssize_t pos = s.count(substring)
711
    return pos
712

713
@cython.test_fail_if_path_exists(
714
    "//CastNode", "//TypecastNode")
715
@cython.test_assert_path_exists(
716
    "//CoerceToPyTypeNode",
717
    "//PythonCapiCallNode")
718
def count_start_end(unicode s, substring, start, end):
719
    """
720
    >>> def test_count_start_end(s, substring, start, end):
721
    ...     py = s.count(substring, start, end)
722
    ...     cy = count_start_end(s, substring, start, end)
723
    ...     assert py == cy, (py, cy)
724
    ...     return cy
725
    >>> test_count_start_end(text, 'sa', 14, 21)
726
    1
727
    >>> test_count_start_end(text, 'sa', 14, 22)
728
    2
729
    >>> test_count_start_end(text, 'sa', None, None)
730
    2
731
    >>> test_count_start_end(text, 'sa', 14, None)
732
    2
733
    >>> test_count_start_end(text, 'sa', 17, None)
734
    1
735
    >>> test_count_start_end(text, 'sa', None, 23)
736
    2
737
    >>> test_count_start_end(text, 'sa', None, 20)
738
    1
739
    """
740
    cdef Py_ssize_t pos = s.count(substring, start, end)
741
    return pos
742

743

744
# unicode.replace(s, sub, repl, [maxcount])
745

746
@cython.test_fail_if_path_exists(
747
    "//CoerceFromPyTypeNode",
748
    "//CastNode", "//TypecastNode")
749
@cython.test_assert_path_exists(
750
    "//PythonCapiCallNode")
751
def replace(unicode s, substring, repl):
752
    """
753
    >>> def test_replace(s, substring, repl):
754
    ...     py = s.replace(substring, repl)
755
    ...     cy = replace(s, substring, repl)
756
    ...     assert py == cy, (py, cy)
757
    ...     return cy
758
    >>> print( test_replace(text, 'sa', 'SA') )
759
    ab jd  sdflk as SA  SAdas asdas fsdf\x20
760
    """
761
    return s.replace(substring, repl)
762

763
@cython.test_fail_if_path_exists(
764
    "//CastNode", "//TypecastNode")
765
@cython.test_assert_path_exists(
766
    "//CoerceFromPyTypeNode",
767
    "//PythonCapiCallNode")
768
def replace_maxcount(unicode s, substring, repl, maxcount):
769
    """
770
    >>> def test_replace_maxcount(s, substring, repl, maxcount):
771
    ...     py = s.replace(substring, repl, maxcount)
772
    ...     cy = replace_maxcount(s, substring, repl, maxcount)
773
    ...     assert py == cy, (py, cy)
774
    ...     return cy
775
    >>> print( test_replace_maxcount(text, 'sa', 'SA', 1) )
776
    ab jd  sdflk as SA  sadas asdas fsdf\x20
777
    """
778
    return s.replace(substring, repl, maxcount)
779

780

781
# unicode * int
782

783
@cython.test_fail_if_path_exists(
784
    "//CoerceToPyTypeNode",
785
)
786
@cython.test_assert_path_exists(
787
    "//MulNode[@is_sequence_mul = True]",
788
)
789
def multiply(unicode ustring, int mul):
790
    """
791
    >>> astr = u"abc"
792
    >>> ustr = u"abcüöä\\U0001F642"
793

794
    >>> print(multiply(astr, -1))
795
    <BLANKLINE>
796
    >>> print(multiply(ustr, -1))
797
    <BLANKLINE>
798

799
    >>> print(multiply(astr, 0))
800
    <BLANKLINE>
801
    >>> print(multiply(ustr, 0))
802
    <BLANKLINE>
803

804
    >>> print(multiply(astr, 1))
805
    abc
806
    >>> print(multiply(ustr, 1))
807
    abcüöä\U0001F642
808

809
    >>> print(multiply(astr, 2))
810
    abcabc
811
    >>> print(multiply(ustr, 2))
812
    abcüöä\U0001F642abcüöä\U0001F642
813

814
    >>> print(multiply(astr, 5))
815
    abcabcabcabcabc
816
    >>> print(multiply(ustr, 5))
817
    abcüöä\U0001F642abcüöä\U0001F642abcüöä\U0001F642abcüöä\U0001F642abcüöä\U0001F642
818
    """
819
    return ustring * mul
820

821

822
@cython.test_fail_if_path_exists(
823
    "//CoerceToPyTypeNode",
824
)
825
@cython.test_assert_path_exists(
826
    "//MulNode[@is_sequence_mul = True]",
827
)
828
def multiply_call(ustring, int mul):
829
    """
830
    >>> astr = u"abc"
831
    >>> ustr = u"abcüöä\\U0001F642"
832

833
    >>> print(multiply_call(astr, 2))
834
    abcabc
835
    >>> print(multiply_call(ustr, 2))
836
    abcüöä\U0001F642abcüöä\U0001F642
837
    """
838
    return unicode(ustring) * mul
839

840

841
#@cython.test_fail_if_path_exists(
842
#    "//CoerceToPyTypeNode",
843
#    "//CastNode", "//TypecastNode")
844
#@cython.test_assert_path_exists(
845
#    "//PythonCapiCallNode")
846
def multiply_inplace(unicode ustring, int mul):
847
    """
848
    >>> astr = u"abc"
849
    >>> ustr = u"abcüöä\\U0001F642"
850

851
    >>> print(multiply_inplace(astr, -1))
852
    <BLANKLINE>
853
    >>> print(multiply_inplace(ustr, -1))
854
    <BLANKLINE>
855

856
    >>> print(multiply_inplace(astr, 0))
857
    <BLANKLINE>
858
    >>> print(multiply_inplace(ustr, 0))
859
    <BLANKLINE>
860

861
    >>> print(multiply_inplace(astr, 1))
862
    abc
863
    >>> print(multiply_inplace(ustr, 1))
864
    abcüöä\U0001F642
865

866
    >>> print(multiply_inplace(astr, 2))
867
    abcabc
868
    >>> print(multiply_inplace(ustr, 2))
869
    abcüöä\U0001F642abcüöä\U0001F642
870

871
    >>> print(multiply_inplace(astr, 5))
872
    abcabcabcabcabc
873
    >>> print(multiply_inplace(ustr, 5))
874
    abcüöä\U0001F642abcüöä\U0001F642abcüöä\U0001F642abcüöä\U0001F642abcüöä\U0001F642
875
    """
876
    ustring *= mul
877
    return ustring
878

879

880
@cython.test_fail_if_path_exists(
881
    "//CoerceToPyTypeNode",
882
)
883
@cython.test_assert_path_exists(
884
    "//MulNode[@is_sequence_mul = True]",
885
)
886
def multiply_reversed(unicode ustring, int mul):
887
    """
888
    >>> astr = u"abc"
889
    >>> ustr = u"abcüöä\\U0001F642"
890

891
    >>> print(multiply_reversed(astr, -1))
892
    <BLANKLINE>
893
    >>> print(multiply_reversed(ustr, -1))
894
    <BLANKLINE>
895

896
    >>> print(multiply_reversed(astr, 0))
897
    <BLANKLINE>
898
    >>> print(multiply_reversed(ustr, 0))
899
    <BLANKLINE>
900

901
    >>> print(multiply_reversed(astr, 1))
902
    abc
903
    >>> print(multiply_reversed(ustr, 1))
904
    abcüöä\U0001F642
905

906
    >>> print(multiply_reversed(astr, 2))
907
    abcabc
908
    >>> print(multiply_reversed(ustr, 2))
909
    abcüöä\U0001F642abcüöä\U0001F642
910

911
    >>> print(multiply_reversed(astr, 5))
912
    abcabcabcabcabc
913
    >>> print(multiply_reversed(ustr, 5))
914
    abcüöä\U0001F642abcüöä\U0001F642abcüöä\U0001F642abcüöä\U0001F642abcüöä\U0001F642
915
    """
916
    return mul * ustring
917

918

919
@cython.test_fail_if_path_exists(
920
    "//CoerceToPyTypeNode",
921
)
922
def unicode__mul__(unicode ustring, int mul):
923
    """
924
    >>> astr = u"abc"
925
    >>> ustr = u"abcüöä\\U0001F642"
926

927
    >>> print(unicode__mul__(astr, -1))
928
    <BLANKLINE>
929
    >>> print(unicode__mul__(ustr, -1))
930
    <BLANKLINE>
931

932
    >>> print(unicode__mul__(astr, 0))
933
    <BLANKLINE>
934
    >>> print(unicode__mul__(ustr, 0))
935
    <BLANKLINE>
936

937
    >>> print(unicode__mul__(astr, 1))
938
    abc
939
    >>> print(unicode__mul__(ustr, 1))
940
    abcüöä\U0001F642
941

942
    >>> print(unicode__mul__(astr, 2))
943
    abcabc
944
    >>> print(unicode__mul__(ustr, 2))
945
    abcüöä\U0001F642abcüöä\U0001F642
946

947
    >>> print(unicode__mul__(astr, 5))
948
    abcabcabcabcabc
949
    >>> print(unicode__mul__(ustr, 5))
950
    abcüöä\U0001F642abcüöä\U0001F642abcüöä\U0001F642abcüöä\U0001F642abcüöä\U0001F642
951
    """
952
    return ustring.__mul__(mul)
953

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.