def find(self,sub, start = 0, end = sys.maxsize): '''S.find(sub [,start [,end]]) -> int Return the lowest index in S where substring sub is found, such that sub is contained within S[start:end]. Optional arguments start and end are interpreted as in slice notation. Return -1 on failure. ''' string = self[start:end] c = 0 # corner cases if string == '': return 0 if sub == '' else -1 if sub == '': return 0 it = StringSearch(sub,string,self._factory.collator) it.setAttribute(USearchAttribute.OVERLAP,False) while True: try: r = next(it) match = it.getMatchedText() if self.__class__(match) == self.__class__(sub): return start+r except StopIteration: return -1
def count(self, sub, start=0, end = sys.maxsize): '''S.count(sub[, start[, end]]) -> int Return the number of non-overlapping occurrences of substring sub in Unicode string S[start:end]. Optional arguments start and end are interpreted as in slice notation. ''' string=self[start:end] c = 0 # corner cases if string == '': return 1 if sub == '' else 0 if sub == '': return len(string)+1 it = StringSearch(sub,string,self._factory.collator) it.setAttribute(USearchAttribute.OVERLAP,False) try: while True: next(it) match = it.getMatchedText() if self.__class__(match) == self.__class__(sub): c+=1 except StopIteration: pass return c
def __contains__(self,other): '''x.__contains__(y) <==> y in x ''' if self == '': return other == '' if other == '': return True it = StringSearch(other,self,self._factory.collator) while True: try: next(it) match = it.getMatchedText() if self.__class__(match) == self.__class__(other): return True except StopIteration: return False
def rfind(self, sub ,start = 0 ,end = sys.maxsize): '''S.rfind(sub [,start [,end]]) -> int Return the highest index in S where substring sub is found, such that sub is contained within S[start:end]. Optional arguments start and end are interpreted as in slice notation. Return -1 on failure. ''' string = self[start:end] # corner cases if string == '': return 0 if sub == '' else -1 if sub == '': return len(string)+start c = 0 it = StringSearch(sub,self,self._factory.collator) it.setAttribute(USearchAttribute.OVERLAP,False) r = it.last() while r>= 0: match = it.getMatchedText() if self.__class__(match) == self.__class__(sub): return start + r r = it.preceding(r) return r