本文整理汇总了Python中pypy.objspace.std.stringtype.sliced函数的典型用法代码示例。如果您正苦于以下问题:Python sliced函数的具体用法?Python sliced怎么用?Python sliced使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了sliced函数的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: str_split__String_String_ANY
def str_split__String_String_ANY(space, w_self, w_by, w_maxsplit=-1):
maxsplit = space.int_w(w_maxsplit)
value = w_self._value
by = w_by._value
bylen = len(by)
if bylen == 0:
raise OperationError(space.w_ValueError, space.wrap("empty separator"))
res_w = []
start = 0
if bylen == 1 and maxsplit < 0:
# fast path: uses str.rfind(character) and str.count(character)
by = by[0] # annotator hack: string -> char
count = value.count(by)
res_w = [None] * (count + 1)
end = len(value)
while count >= 0:
assert end >= 0
prev = value.rfind(by, 0, end)
start = prev + 1
assert start >= 0
res_w[count] = sliced(space, value, start, end, w_self)
count -= 1
end = prev
else:
while maxsplit != 0:
next = value.find(by, start)
if next < 0:
break
res_w.append(sliced(space, value, start, next, w_self))
start = next + bylen
maxsplit -= 1 # NB. if it's already < 0, it stays < 0
res_w.append(sliced(space, value, start, len(value), w_self))
return space.newlist(res_w)
示例2: str_splitlines__String_ANY
def str_splitlines__String_ANY(space, w_self, w_keepends):
data = w_self._value
u_keepends = space.int_w(w_keepends) # truth value, but type checked
selflen = len(data)
strs_w = []
i = j = 0
while i < selflen:
# Find a line and append it
while i < selflen and data[i] != '\n' and data[i] != '\r':
i += 1
# Skip the line break reading CRLF as one line break
eol = i
i += 1
if i < selflen and data[i-1] == '\r' and data[i] == '\n':
i += 1
if u_keepends:
eol = i
strs_w.append(sliced(space, data, j, eol))
j = i
if j < selflen:
strs_w.append(sliced(space, data, j, len(data)))
return space.newlist(strs_w)
示例3: str_rpartition__String_String
def str_rpartition__String_String(space, w_self, w_sub):
self = w_self._value
sub = w_sub._value
if not sub:
raise OperationError(space.w_ValueError, space.wrap("empty separator"))
pos = self.rfind(sub)
if pos == -1:
return space.newtuple([space.wrap(""), space.wrap(""), w_self])
else:
return space.newtuple(
[sliced(space, self, 0, pos, w_self), w_sub, sliced(space, self, pos + len(sub), len(self), w_self)]
)
示例4: str_partition__StringSlice_String
def str_partition__StringSlice_String(space, w_self, w_sub):
self = w_self.str
sub = w_sub._value
if not sub:
raise OperationError(space.w_ValueError,
space.wrap("empty separator"))
pos = self.find(sub, w_self.start, w_self.stop)
if pos == -1:
return space.newtuple([w_self, space.wrap(''), space.wrap('')])
else:
return space.newtuple([sliced(space, self, w_self.start, pos),
w_sub,
sliced(space, self, pos+len(sub), w_self.stop)])
示例5: getitem__String_Slice
def getitem__String_Slice(space, w_str, w_slice):
s = w_str._value
length = len(s)
start, stop, step, sl = w_slice.indices4(space, length)
if sl == 0:
return W_StringObject.EMPTY
elif step == 1:
assert start >= 0 and stop >= 0
return sliced(space, s, start, stop, w_str)
else:
str = "".join([s[start + i*step] for i in range(sl)])
return wrapstr(space, str)