summaryrefslogtreecommitdiff
path: root/unidecode/__init__.py
blob: d52679bbe1d292424b14ea9e56d332796b3a1422 (plain)
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
"""ASCII transliterations of Unicode text
"""
Char = {}

NULLMAP = [ '' * 0x100 ]

def unidecode(string):
	"""Transliterate an Unicode object into an ASCII string

	>>> unidecode(u"\u5317\u4EB0")
	"Bei Jing "
	"""

	retval = []

	for char in string:
		o = ord(char)

		if o < 0x80:
			retval.append(char)
			continue

		h = o >> 8
		l = o & 0xff

		c = Char.get(h, None)
		
		if c == None:
			try:
				mod = __import__('unidecode.x%02x'%(h), [], [], ['data'])
			except ImportError:
				Char[h] = NULLMAP
				retval.append('')
				continue

			Char[h] = mod.data

			try:
				retval.append( mod.data[l] )
			except IndexError:
				retval.append( '' )
		else:
			try:
				retval.append( c[l] )
			except IndexError:
				retval.append( '' )

	return ''.join(retval)