+ def key(self, string):
+ """Return a good sorting key for the string.
+
+ The sort key should be considered an opaque value which is
+ only meaningful when compared to other sort keys from the same
+ collator.
+ """
+ return self.unicode(string)
+
+ def words(self, string, sep=re.compile(r"(\s+)", re.UNICODE)):
+ """Split the string into separate words."""
+ return re.split(sep, self.unicode(string))
+
+ def sortemekey(self, string):
+ """Return a key based on sortemes of a string.
+
+ A sorteme, by analogy with grapheme/morpheme/etc. is an atom
+ of sort information. This is larger than a word boundry but
+ smaller than a sentence boundry; roughly, a sorteme boundry
+ occurs between letters and numbers, between numbers and
+ numbers if 'too much' punctuation exists in between, between
+ lines.
+ """
+ string = self.unicode(string)
+ # Shove the sortkeyed original string on the end to resolve
+ # ties intelligently.
+ return (collate.strings.sortemes(string, self.key),
+ self.key(string))
+
+ def unicode(self, string):
+ """Convert a str to a unicode using the collator encoding."""
+ try:
+ return unicode(string)
+ except UnicodeError:
+ return string.decode(self.encoding, 'replace')
+
+ def str(self, string):
+ """Convert a unicode to a str using the collator encoding."""
+ try:
+ return str(string)
+ except UnicodeError:
+ return string.encode(self.encoding, 'replace')