Show More
@@ -23,9 +23,27 b' import util, encoding' | |||||
23 | from i18n import _ |
|
23 | from i18n import _ | |
24 |
|
24 | |||
25 | def replace(text, substs): |
|
25 | def replace(text, substs): | |
|
26 | ''' | |||
|
27 | Apply a list of (find, replace) pairs to a text. | |||
|
28 | ||||
|
29 | >>> replace("foo bar", [('f', 'F'), ('b', 'B')]) | |||
|
30 | 'Foo Bar' | |||
|
31 | >>> encoding.encoding = 'latin1' | |||
|
32 | >>> replace('\\x81\\\\', [('\\\\', '/')]) | |||
|
33 | '\\x81/' | |||
|
34 | >>> encoding.encoding = 'shiftjis' | |||
|
35 | >>> replace('\\x81\\\\', [('\\\\', '/')]) | |||
|
36 | '\\x81\\\\' | |||
|
37 | ''' | |||
|
38 | ||||
|
39 | # some character encodings (cp932 for Japanese, at least) use | |||
|
40 | # ASCII characters other than control/alphabet/digit as a part of | |||
|
41 | # multi-bytes characters, so direct replacing with such characters | |||
|
42 | # on strings in local encoding causes invalid byte sequences. | |||
|
43 | utext = text.decode(encoding.encoding) | |||
26 | for f, t in substs: |
|
44 | for f, t in substs: | |
27 | text = text.replace(f, t) |
|
45 | utext = utext.replace(f, t) | |
28 | return text |
|
46 | return utext.encode(encoding.encoding) | |
29 |
|
47 | |||
30 | _blockre = re.compile(r"\n(?:\s*\n)+") |
|
48 | _blockre = re.compile(r"\n(?:\s*\n)+") | |
31 |
|
49 |
General Comments 0
You need to be logged in to leave comments.
Login now