9dce32bee99f5a7d9b746310fa7b3e4a90281af7
[python-collate.git] / collate / _abcollator.py
1 import collate.strings
2
3 class Collator(object):
4 def cmp(self, string1, string2):
5 """Return negative if a < b, zero if a == b, positive if a > b."""
6 return cmp(self.key(string1), self.key(string2))
7
8 def words(self, string):
9 """Split the string along word boundries."""
10 if isinstance(string, str):
11 string = string.decode(self.encoding, 'replace')
12 return string.split()
13
14 def sortemekey(self, string, invalid=float('inf')):
15 """Return a key based on sortemes of a string.
16
17 If the string is a str instance, it is decoded to a unicode
18 instance according to the 'encoding' attribute of the
19 Collator.
20 """
21 if isinstance(string, str):
22 string = string.decode(self.encoding, 'replace')
23
24 # Shove the sortkeyed original string on the end to resolve
25 # ties intelligently.
26 return (collate.strings.sortemes(string, self.key),
27 self.key(string))
28