Esempio n. 1
0
    def split(self, s, maxsplit=0):
        is_str = isinstance(s, str)
        if is_str:
            s = s.encode()

        res = []
        while True:
            m = self.search(s)
            g = None
            if m:
                g = m.group(0)
            if not m or not g:
                res.append(s)
                break
            beg, end = m.span(0)
            res.append(s[:beg])
            if m.num > 1:
                res.extend(m.groups())
            s = s[end:]
            if maxsplit > 0:
                maxsplit -= 1
                if maxsplit == 0:
                    res.append(s)
                    break

        if is_str:
            for i in range(len(res)):
                x = res[i]
                if x is not None:
                    res[i] = pycopy.icast(x, str)

        return res
Esempio n. 2
0
    def findall(self, s, pos=0, endpos=-1):
        if endpos != -1:
            s = s[:endpos]

        is_str = isinstance(s, str)
        if is_str:
            s = s.encode()

        res = []
        finish = len(s)
        while pos <= finish:
            m = self.search(s, pos)
            if not m:
                break
            if m.num == 1:
                res.append(m.group(0))
            elif m.num == 2:
                res.append(m.group(1))
            else:
                # Will be modified inplace, so must be copy of literal
                x = b""[:]
                res.append(m.groups(x))
            beg, end = m.span(0)
            pos = end
            if beg == end:
                # Have progress on empty matches
                pos += 1

        if is_str:
            for i in range(len(res)):
                x = res[i]
                if isinstance(x, tuple):
                    res[i] = tuple([pycopy.icast(x1, str) for x1 in x])
                else:
                    res[i] = pycopy.icast(x, str)

        return res
Esempio n. 3
0
 def hexdigest(self):
     s = ubinascii.hexlify(self.digest())
     s = pycopy.icast(s, str)
     return s
Esempio n. 4
0
from pycopy import icast

print(repr(icast(b"foo", str)))
print(repr(icast(b"", str)))

b1 = b"foobar"
id1 = id(b1)
b2 = icast(b1, str)
id2 = id(b2)
print(id1 == id2)
print(b1)