summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorpiaip <piaip@63ad8ddf-47c3-0310-b6dd-a9e9d9715204>2014-02-11 20:00:15 +0800
committerpiaip <piaip@63ad8ddf-47c3-0310-b6dd-a9e9d9715204>2014-02-11 20:00:15 +0800
commit462d5e0eb97d36c4ee21f2b45a19ce69eee26334 (patch)
tree807d7eab13d5852d84ace65f74a4986755991530
parent5776c2bd08a4018096d833c4dc732d010b6f7e97 (diff)
downloadpttbbs-462d5e0eb97d36c4ee21f2b45a19ce69eee26334.tar
pttbbs-462d5e0eb97d36c4ee21f2b45a19ce69eee26334.tar.gz
pttbbs-462d5e0eb97d36c4ee21f2b45a19ce69eee26334.tar.bz2
pttbbs-462d5e0eb97d36c4ee21f2b45a19ce69eee26334.tar.lz
pttbbs-462d5e0eb97d36c4ee21f2b45a19ce69eee26334.tar.xz
pttbbs-462d5e0eb97d36c4ee21f2b45a19ce69eee26334.tar.zst
pttbbs-462d5e0eb97d36c4ee21f2b45a19ce69eee26334.zip
Add big5 module for python.
git-svn-id: http://opensvn.csie.org/pttbbs/trunk@5926 63ad8ddf-47c3-0310-b6dd-a9e9d9715204
-rw-r--r--pttbbs/util/pyutil/big5.py30
-rwxr-xr-xpttbbs/util/pyutil/big5_gen.py39
2 files changed, 69 insertions, 0 deletions
diff --git a/pttbbs/util/pyutil/big5.py b/pttbbs/util/pyutil/big5.py
new file mode 100644
index 00000000..90fbe7c7
--- /dev/null
+++ b/pttbbs/util/pyutil/big5.py
@@ -0,0 +1,30 @@
+#!/usr/bin/env python
+
+import big5_tbl
+
+def decode(s, strip_zero=True):
+ ret = u''
+ b = 0
+ for i in s:
+ if b:
+ b = (b << 8) | (ord(i))
+ else:
+ b = ord(i)
+ if b >= 0x80:
+ continue
+ if b == 0:
+ break
+ ret += unichr(big5_tbl.b2u_table[b])
+ b = 0
+ return ret
+
+def encode(u):
+ ret = ''
+ for i in u:
+ c = big5_tbl.u2b_table[ord(i)]
+ b0 = (c) & 0xff
+ b1 = (c >> 8) & 0xff
+ if b1:
+ ret = ret + chr(b1)
+ ret = ret + chr(b0)
+ return ret
diff --git a/pttbbs/util/pyutil/big5_gen.py b/pttbbs/util/pyutil/big5_gen.py
new file mode 100755
index 00000000..adafeb0f
--- /dev/null
+++ b/pttbbs/util/pyutil/big5_gen.py
@@ -0,0 +1,39 @@
+#!/usr/bin/env python
+# Usage: ./big5_gen.py > big5_tbl.py
+
+import os
+
+COMMON_SYS = '../../common/sys'
+B2U_FILE = os.path.join(COMMON_SYS, 'uao250-b2u.big5.txt')
+U2B_FILE = os.path.join(COMMON_SYS, 'uao250-u2b.big5.txt')
+
+# b2u
+b2u = open(B2U_FILE, 'r').readlines()
+b2u = [line.strip().split(' ')
+ for line in b2u
+ if line.strip().startswith('0x')]
+b2u = dict((int(b, 0), int(u, 0)) for (b, u) in b2u)
+
+print """#!/usr/bin/env python
+
+"""
+print "b2u_table = ("
+for i in range(0x10000):
+ print '0x%04x,' % (i if i not in b2u else b2u[i]),
+ if i % 10 == 9:
+ print ''
+print ")\n"
+
+# u2b
+u2b = open(U2B_FILE, 'r').readlines()
+u2b = [line.strip().split(' ')
+ for line in u2b
+ if line.strip().startswith('0x')]
+u2b = dict((int(u, 0), int(b, 0)) for (b, u) in u2b)
+
+print "u2b_table = ("
+for i in range(0x10000):
+ print '0x%04x,' % (i if i not in u2b else u2b[i]),
+ if i % 10 == 9:
+ print ''
+print ")\n"