File: ucs2.py

package info (click to toggle)
felix 1.1.1-2
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 4,992 kB
  • ctags: 1,178
  • sloc: python: 7,260; makefile: 408; sh: 58
file content (26 lines) | stat: -rw-r--r-- 451 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#line 155 "interscript/src/utf8.ipk"
from array import array

# encoding
def ucs2(i):
  return chr(i >> 8) + chr(i & 0xFF)

def seq_to_ucs2(a):
  s = ''
  for ch in a: s = s + ucs2(ch)
  return s

# decoding
def parse_ucs2(s, i):
  return ord(s[i]) << 8 + ord(s[i+1]), i+2

def ucs2_to_array(s):
  n = len(s)
  a = array('H',(0,)*n/2)
  i = 0
  while i < n:
    a[i/2],i = parse_ucs2(s,i)

def ucs2_to_utf8(s):
  return seq_to_utf8(ucs2_to_array(s))