File: ucs2le.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-- 466 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 188 "interscript/src/utf8.ipk"
from array import array

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

def seq_to_ucs2le(a):
  s = ''
  for ch in a: s = s + ucs2le(ch)
  return s

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

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

def ucs2le_to_utf8(s):
  return seq_to_utf8(ucs2le_to_array(s))