def _startswith(self, space, value, w_prefix, start, end): prefix = self._op_val(space, w_prefix) if self._starts_ends_unicode: # bug-to-bug compat if len(prefix) == 0: return True else: if start > len(value): return False return startswith(value, prefix, start, end)
def PyUnicode_Tailmatch(space, w_str, w_substr, start, end, direction): """Return 1 if substr matches str[start:end] at the given tail end (direction == -1 means to do a prefix match, direction == 1 a suffix match), 0 otherwise. Return -1 if an error occurred.""" str = space.unicode_w(w_str) substr = space.unicode_w(w_substr) if rffi.cast(lltype.Signed, direction) <= 0: return rstring.startswith(str, substr, start, end) else: return rstring.endswith(str, substr, start, end)
def unicode_startswith__Unicode_ANY_ANY_ANY(space, w_unistr, w_prefixes, w_start, w_end): if not space.isinstance_w(w_prefixes, space.w_tuple): raise FailedToImplement unistr, start, end = _convert_idx_params(space, w_unistr, w_start, w_end, True) for w_prefix in space.fixedview(w_prefixes): prefix = space.unicode_w(w_prefix) if startswith(unistr, prefix, start, end): return space.w_True return space.w_False
def str_startswith__String_ANY_ANY_ANY(space, w_self, w_prefixes, w_start, w_end): if not space.isinstance_w(w_prefixes, space.w_tuple): raise FailedToImplement (u_self, start, end) = _convert_idx_params(space, w_self, w_start, w_end, True) for w_prefix in space.fixedview(w_prefixes): if space.isinstance_w(w_prefix, space.w_unicode): w_u = space.call_function(space.w_unicode, w_self) return space.call_method(w_u, "startswith", w_prefixes, w_start, w_end) prefix = space.str_w(w_prefix) if startswith(u_self, prefix, start, end): return space.w_True return space.w_False
def _startswith(self, space, value, w_prefix, start, end): prefix = self._op_val(space, w_prefix) if start > len(value): return self._starts_ends_overflow(prefix) return startswith(value, prefix, start, end)
def check_startswith(value, sub, *args, **kwargs): result = kwargs['res'] assert startswith(value, sub, *args) is result assert startswith(list(value), sub, *args) is result
def str_startswith__String_String_ANY_ANY(space, w_self, w_prefix, w_start, w_end): (u_self, start, end) = _convert_idx_params(space, w_self, w_start, w_end, True) return space.newbool(startswith(u_self, w_prefix._value, start, end))
def _startswith(self, space, value, w_prefix, start, end): prefix = self._op_val(space, w_prefix) if start > len(value): return False return startswith(value, prefix, start, end)
def _startswith(self, space, value, w_prefix, start, end): return startswith(value, self._op_val(space, w_prefix), start, end)
def test_startswith(): assert startswith('ab', 'ab') is True assert startswith('ab', 'a') is True assert startswith('ab', '') is True assert startswith('x', 'a') is False assert startswith('x', 'x') is True assert startswith('', '') is True assert startswith('', 'a') is False assert startswith('x', 'xx') is False assert startswith('y', 'xx') is False assert startswith('ab', 'a', 0) is True assert startswith('ab', 'a', 1) is False assert startswith('ab', 'b', 1) is True assert startswith('abc', 'bc', 1, 2) is False assert startswith('abc', 'c', -1, 4) is True
def unicode_startswith__Unicode_Unicode_ANY_ANY(space, w_self, w_substr, w_start, w_end): self, start, end = _convert_idx_params(space, w_self, w_start, w_end, True) return space.newbool(startswith(self, w_substr._value, start, end))