sortemes: Simplify some logic.
[python-collate.git] / collate / _abcollator.py
index bc43dc3..0ae5d45 100644 (file)
@@ -1,4 +1,4 @@
-import collate._strings
+import collate.strings
 
 class Collator(object):
     def cmp(self, string1, string2):
@@ -6,19 +6,27 @@ class Collator(object):
         return cmp(self.key(string1), self.key(string2))
 
     def words(self, string):
-        """Split the string into separate words.
-
-        This split is done using Unicode's definition of whitespace.
-        """
+        """Split the string along word boundries."""
+        if isinstance(string, str):
+            string = string.decode(self.encoding, 'replace')
         return string.split()
 
-    def sortemes(self, string):
-        words = []
-        for word in self.words(string):
-            words.extend(collate._strings.alnumsplit(word))
-        return filter(collate._strings.wordlike, words)
+    def sortemekey(self, string, invalid=float('inf')):
+        """Return a key based on sortemes of a string.
 
-    def sortemekey(self, string):
-        words = map(collate._strings.numeric, self.sortemes(string))
-        words = [(i, self.key(word)) for (i, word) in words]
-        return words
+        If the string is a str instance, it is decoded to a unicode
+        instance according to the 'encoding' attribute of the
+        Collator.
+        """
+        keys = []
+        if isinstance(string, str):
+            string = string.decode(self.encoding, 'replace')
+        for sorteme in collate.strings.sortemes(string):
+            num, alpha = collate.strings.numeric(sorteme, invalid)
+            if num == invalid:
+                keys.append(self.key(alpha))
+            else:
+                keys.append(num)
+        # Shove the sortkeyed original string on the end to resolve
+        # ties intelligently.
+        return (keys, self.key(string))