当前位置: 首页>>代码示例>>Python>>正文


Python stringtype.sliced函数代码示例

本文整理汇总了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)
开发者ID:gorakhargosh,项目名称:pypy,代码行数:35,代码来源:stringobject.py

示例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)
开发者ID:TheDunn,项目名称:flex-pypy,代码行数:25,代码来源:stringobject.py

示例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)]
        )
开发者ID:alkorzt,项目名称:pypy,代码行数:12,代码来源:stringobject.py

示例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)])
开发者ID:TheDunn,项目名称:flex-pypy,代码行数:13,代码来源:strsliceobject.py

示例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)
开发者ID:charred,项目名称:pypy,代码行数:12,代码来源:stringobject.py


注:本文中的pypy.objspace.std.stringtype.sliced函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。