Show More
@@ -1,34 +1,36 b'' | |||||
1 | # strutil.py - string utilities for Mercurial |
|
1 | # strutil.py - string utilities for Mercurial | |
2 | # |
|
2 | # | |
3 | # Copyright 2006 Vadim Gelfer <vadim.gelfer@gmail.com> |
|
3 | # Copyright 2006 Vadim Gelfer <vadim.gelfer@gmail.com> | |
4 | # |
|
4 | # | |
5 | # This software may be used and distributed according to the terms of the |
|
5 | # This software may be used and distributed according to the terms of the | |
6 | # GNU General Public License version 2 or any later version. |
|
6 | # GNU General Public License version 2 or any later version. | |
7 |
|
7 | |||
|
8 | from __future__ import absolute_import | |||
|
9 | ||||
8 | def findall(haystack, needle, start=0, end=None): |
|
10 | def findall(haystack, needle, start=0, end=None): | |
9 | if end is None: |
|
11 | if end is None: | |
10 | end = len(haystack) |
|
12 | end = len(haystack) | |
11 | if end < 0: |
|
13 | if end < 0: | |
12 | end += len(haystack) |
|
14 | end += len(haystack) | |
13 | if start < 0: |
|
15 | if start < 0: | |
14 | start += len(haystack) |
|
16 | start += len(haystack) | |
15 | while start < end: |
|
17 | while start < end: | |
16 | c = haystack.find(needle, start, end) |
|
18 | c = haystack.find(needle, start, end) | |
17 | if c == -1: |
|
19 | if c == -1: | |
18 | break |
|
20 | break | |
19 | yield c |
|
21 | yield c | |
20 | start = c + 1 |
|
22 | start = c + 1 | |
21 |
|
23 | |||
22 | def rfindall(haystack, needle, start=0, end=None): |
|
24 | def rfindall(haystack, needle, start=0, end=None): | |
23 | if end is None: |
|
25 | if end is None: | |
24 | end = len(haystack) |
|
26 | end = len(haystack) | |
25 | if end < 0: |
|
27 | if end < 0: | |
26 | end += len(haystack) |
|
28 | end += len(haystack) | |
27 | if start < 0: |
|
29 | if start < 0: | |
28 | start += len(haystack) |
|
30 | start += len(haystack) | |
29 | while end >= 0: |
|
31 | while end >= 0: | |
30 | c = haystack.rfind(needle, start, end) |
|
32 | c = haystack.rfind(needle, start, end) | |
31 | if c == -1: |
|
33 | if c == -1: | |
32 | break |
|
34 | break | |
33 | yield c |
|
35 | yield c | |
34 | end = c - 1 |
|
36 | end = c - 1 |
General Comments 0
You need to be logged in to leave comments.
Login now