+ 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')
+
+ def lstripwords(
+ self, string, strip=collate.strings.INITIAL_STOPS, append=u", "):
+ """Strip words and whitespace from the start of a string.
+
+ If append is not empty, it and the words stripped from the
+ front are appended to the end.
+ """
+ string = self.unicode(string)
+ stripped = []
+ words = self.words(string)
+ while words and (words[0].isspace() or words[0].lower() in strip):
+ stripped.append(words.pop(0))
+ while stripped and stripped[-1].isspace():
+ stripped.pop()
+ if append and stripped:
+ if words:
+ words.append(append)
+ words.extend(stripped)
+ return u"".join(words)
+
+ def lstripsortemekey(
+ self, string, strip=collate.strings.INITIAL_STOPS, append=u", "):
+ """Return a key based on sortemes of a prefix-stripped string."""
+ string = self.unicode(string)
+ stripped = self.lstripwords(string, strip, append)
+ return (self.sortemekey(stripped), self.key(string))