Mercurial > hg
comparison mercurial/util.py @ 9480:44758742ad2e
util: do not corrupt multi-byte characters in wrap
author | Martin Geisler <mg@lazybytes.net> |
---|---|
date | Sun, 27 Sep 2009 01:44:46 +0200 |
parents | 7116494c48ab |
children | ca3390c19f88 b2d65ee49a72 |
comparison
equal
deleted
inserted
replaced
9479:f3569d95c2ab | 9480:44758742ad2e |
---|---|
12 This contains helper routines that are independent of the SCM core and | 12 This contains helper routines that are independent of the SCM core and |
13 hide platform-specific details from the core. | 13 hide platform-specific details from the core. |
14 """ | 14 """ |
15 | 15 |
16 from i18n import _ | 16 from i18n import _ |
17 import error, osutil | 17 import error, osutil, encoding |
18 import cStringIO, errno, re, shutil, sys, tempfile, traceback | 18 import cStringIO, errno, re, shutil, sys, tempfile, traceback |
19 import os, stat, time, calendar, random, textwrap | 19 import os, stat, time, calendar, random, textwrap |
20 import imp | 20 import imp |
21 | 21 |
22 # Python compatibility | 22 # Python compatibility |
1274 pass | 1274 pass |
1275 return 80 | 1275 return 80 |
1276 | 1276 |
1277 def wrap(line, hangindent, width=78): | 1277 def wrap(line, hangindent, width=78): |
1278 padding = '\n' + ' ' * hangindent | 1278 padding = '\n' + ' ' * hangindent |
1279 return padding.join(textwrap.wrap(line, width=width - hangindent)) | 1279 # To avoid corrupting multi-byte characters in line, we must wrap |
1280 # a Unicode string instead of a bytestring. | |
1281 u = line.decode(encoding.encoding) | |
1282 w = padding.join(textwrap.wrap(u, width=width - hangindent)) | |
1283 return w.encode(encoding.encoding) | |
1280 | 1284 |
1281 def iterlines(iterator): | 1285 def iterlines(iterator): |
1282 for chunk in iterator: | 1286 for chunk in iterator: |
1283 for line in chunk.splitlines(): | 1287 for line in chunk.splitlines(): |
1284 yield line | 1288 yield line |