[PATCH] use utf-8 as intermediate encoding for string replacement

FUJIWARA Katsunori fujiwara at ascade.co.jp
Tue Jun 29 21:19:46 CDT 2010


# HG changeset patch
# User FUJIWARA Katsunori <foozy at lares.dti.ne.jp>
# Date 1277864214 -32400
# Branch stable
# Node ID dc4b9c61550da69e76a6aaf12f382eafc55dcb97
# Parent  13d02d6677f2ac787e3de59928d53fae3a695163
use utf-8 as intermediate encoding for string replacement

some character encodings use ASCII characters other than
control/alphabet/digit as a part of multi-bytes characters, so direct
replacing with such characters on local encoding strings causes
invalid byte sequence.

At least, CP932 which is known as Shift-JIS and used most widely in
Japan is seriously damaged.

diff -r 13d02d6677f2 -r dc4b9c61550d mercurial/minirst.py
--- a/mercurial/minirst.py	Sat Jun 26 21:37:10 2010 -0300
+++ b/mercurial/minirst.py	Wed Jun 30 11:16:54 2010 +0900
@@ -36,7 +36,13 @@
 """
 
 import re, sys
-import util
+import util, encoding
+
+def replace(text, substs):
+    utext = unicode(text, encoding.encoding)
+    for f, t in substs:
+        utext = utext.replace(f, t)
+    return utext.encode(encoding.encoding)
 
 def findblocks(text):
     """Find continuous blocks of lines in text.
@@ -251,21 +257,22 @@
 
 
 def inlineliterals(blocks):
+    substs = [('``', '"')]
     for b in blocks:
         if b['type'] in ('paragraph', 'section'):
-            b['lines'] = [l.replace('``', '"') for l in b['lines']]
+            b['lines'] = [replace(l, substs) for l in b['lines']]
     return blocks
 
 
 def hgrole(blocks):
+    substs = [(':hg:`', '"hg '), ('`', '"')]
     for b in blocks:
         if b['type'] in ('paragraph', 'section'):
             # Turn :hg:`command` into "hg command". This also works
             # when there is a line break in the command and relies on
             # the fact that we have no stray back-quotes in the input
             # (run the blocks through inlineliterals first).
-            b['lines'] = [l.replace(':hg:`', '"hg ').replace('`', '"')
-                          for l in b['lines']]
+            b['lines'] = [replace(l, substs) for l in b['lines']]
     return blocks
 
 



More information about the Mercurial-devel mailing list