Collator.lstripwords: Strip words off the start and append to the end.
[python-collate.git] / collate / icu / __init__.py
index eeee418..6d27a6f 100644 (file)
@@ -11,22 +11,39 @@ Avoid this backend if...
 
 """
 
-import collate.icu._ucol
+__all__ = ["Collator"]
+
 import collate._abcollator
 import collate._locale
 import collate.errors
 
+from collate.icu import _icu
+
 class Collator(collate._abcollator.Collator):
     """ICU-based collation."""
 
     def __init__(self, locale, encoding=None):
+        super(Collator, self).__init__(locale, encoding)
         locale, encoding = collate._locale.getpair(locale, encoding)
-        self._collator = collate.icu._ucol.Collator(locale)
+        icu_locale = "root" if locale == "C" else locale
+        self._collator = _icu.Collator(icu_locale)
         self.locale = self._collator.locale
         self.encoding = collate._locale.encoding(encoding)
         if self._collator.used_default_information and locale != "C":
             raise collate.errors.InvalidLocaleError(locale)
 
+        try:
+            self._breaker = _icu.WordBreaker(icu_locale)
+        except ValueError:
+            # Thai is the only language with a special break locale,
+            # so this is a harmless error.
+            self._breaker = _icu.WordBreaker("root")
+
+    def words(self, string):
+        """Split the string along word boundries."""
+        string = self.unicode(string)
+        return self._breaker.words(string)
+
     def key(self, string):
         """Sort key for a string.
 
@@ -34,18 +51,6 @@ class Collator(collate._abcollator.Collator):
         instance according to the 'encoding' attribute of the
         Collator.
         """
-        if isinstance(string, str):
-            string = string.decode(self.encoding, 'replace')
+        string = self.unicode(string)
         return self._collator.key(string)
 
-    def cmp(self, a, b):
-        """Return negative if a < b, zero if a == b, positive if a > b.
-
-        If strs rather than unicodes are passed in, they are first
-        decoded according to the 'encoding' attribute of the Collator.
-        """
-        if isinstance(a, str):
-            a = a.decode(self.encoding, 'replace')
-        if isinstance(b, str):
-            b = a.decode(self.encoding, 'replace')
-        return self._collator.cmp(a, b)