Lichen

Change of lib/__builtins__/str.py

413:1fc56d3a9ac8
lib/__builtins__/str.py
     1.1 --- a/lib/__builtins__/str.py	Thu Dec 15 23:24:43 2016 +0100
     1.2 +++ b/lib/__builtins__/str.py	Thu Dec 15 23:25:10 2016 +0100
     1.3 @@ -180,9 +180,46 @@
     1.4      def __bool__(self):
     1.5          return str_nonempty(self.__data__)
     1.6  
     1.7 -    def endswith(self, s): pass
     1.8 -    def find(self, sub, start=None, end=None): pass
     1.9 -    def index(self, sub, start=None, end=None): pass
    1.10 +    def endswith(self, s):
    1.11 +
    1.12 +        "Return whether this string ends with 's'."
    1.13 +
    1.14 +        return self[-s.__len__():] == s
    1.15 +
    1.16 +    def find(self, sub, start=None, end=None):
    1.17 +
    1.18 +        """
    1.19 +        Find 'sub' in the string, starting at 'start' (or 0, if omitted), ending
    1.20 +        at 'end' (or the end of the string, if omitted), returning -1 if 'sub'
    1.21 +        is not present.
    1.22 +        """
    1.23 +
    1.24 +        sublen = sub.__len__()
    1.25 +
    1.26 +        i = start or 0
    1.27 +        end = end or self.__len__()
    1.28 +
    1.29 +        while i < end - sublen:
    1.30 +            if sub == self[i:i+sublen]:
    1.31 +                return i
    1.32 +            i += 1
    1.33 +
    1.34 +        return -1
    1.35 +
    1.36 +    def index(self, sub, start=None, end=None):
    1.37 +
    1.38 +        """
    1.39 +        Find 'sub' in the string, starting at 'start' (or 0, if omitted), ending
    1.40 +        at 'end' (or the end of the string, if omitted), raising ValueError if
    1.41 +        'sub' is not present.
    1.42 +        """
    1.43 +
    1.44 +        i = self.find(sub, start, end)
    1.45 +
    1.46 +        if i == -1:
    1.47 +            raise ValueError(sub)
    1.48 +        else:
    1.49 +            return i
    1.50  
    1.51      def join(self, l):
    1.52  
    1.53 @@ -215,7 +252,13 @@
    1.54      def rstrip(self, chars=None): pass
    1.55      def split(self, sep=None, maxsplit=None): pass
    1.56      def splitlines(self, keepends=False): pass
    1.57 -    def startswith(self, s): pass
    1.58 +
    1.59 +    def startswith(self, s):
    1.60 +
    1.61 +        "Return whether this string starts with 's'."
    1.62 +
    1.63 +        return self[:s.__len__()] == s
    1.64 +
    1.65      def strip(self, chars=None): pass
    1.66      def upper(self): pass
    1.67