3 from ._functools import method_cache
9 A case insensitive string class; behaves just like str
10 except compares equal when the only variation is case.
12 >>> s = FoldedCase('hello world')
14 >>> s == 'Hello World'
17 >>> 'Hello World' == s
20 >>> s != 'Hello World'
29 >>> sorted(map(FoldedCase, ['GAMMA', 'alpha', 'Beta']))
30 ['alpha', 'Beta', 'GAMMA']
32 Sequence membership is straightforward.
34 >>> "Hello World" in [s]
36 >>> s in ["Hello World"]
39 You may test for set inclusion, but candidate and elements
42 >>> FoldedCase("Hello World") in {s}
44 >>> s in {FoldedCase("Hello World")}
47 String inclusion works as long as the FoldedCase object
50 >>> "hello" in FoldedCase("Hello World")
53 But not if the FoldedCase object is on the left:
55 >>> FoldedCase('hello') in 'Hello World'
58 In that case, use in_:
60 >>> FoldedCase('hello').in_('Hello World')
63 >>> FoldedCase('hello') > FoldedCase('Hello')
67 def __lt__(self, other):
68 return self.lower() < other.lower()
70 def __gt__(self, other):
71 return self.lower() > other.lower()
73 def __eq__(self, other):
74 return self.lower() == other.lower()
76 def __ne__(self, other):
77 return self.lower() != other.lower()
80 return hash(self.lower())
82 def __contains__(self, other):
83 return super().lower().__contains__(other.lower())
86 "Does self appear in other?"
87 return self in FoldedCase(other)
89 # cache lower since it's likely to be called frequently.
92 return super().lower()
95 return self.lower().index(sub.lower())
97 def split(self, splitter=' ', maxsplit=0):
98 pattern = re.compile(re.escape(splitter), re.I)
99 return pattern.split(self, maxsplit)