Beispiel #1
0
def make_bytes(length, byte):
    # assert byte_huh(byte) is values.w_true
    if isinstance(byte, values.W_Fixnum):
        v = byte.value
    else:
        raise SchemeException("make-bytes: not a byte")
    if not 0 <= v <= 255:
        raise SchemeException("make-bytes: argument out of range")
    bstr = [chr(v)] * length.value
    return values.W_MutableBytes(bstr)
Beispiel #2
0
def list_to_bytes(w_list):
    l = values.from_list(w_list)
    ll = [' '] * len(l)
    for (i, x) in enumerate(l):
        if not isinstance(x, values.W_Fixnum):
            raise SchemeException("list->bytes: expected fixnum, got %s" % x)
        if x.value < 0 or x.value >= 256:
            raise SchemeException(
                "list->bytes: expected number between 0 and 255, got %s" % x)
        ll[i] = chr(x.value)
    return values.W_MutableBytes(ll)
Beispiel #3
0
def bytes(args):
    if len(args) == 0:
        return values.W_MutableBytes([])
    assert len(args) > 0

    builder = StringBuilder()
    for char in args:
        if not (isinstance(char, values.W_Fixnum) and 0 <= char.value <= 255):
            raise SchemeException("string: expected a character int")
        builder.append(chr(char.value))
    return values.W_Bytes.from_string(builder.build(), immutable=False)
Beispiel #4
0
def bytes_append(args):
    lens = 0
    for a in args:
        if not isinstance(a, values.W_Bytes):
            raise SchemeException(
                "bytes-append: expected a byte string, but got %s" % a)
        lens += len(a.value)

    val = [' '] * lens  # is this the fastest way to do things?
    cnt = 0
    for a in args:
        assert isinstance(a, values.W_Bytes)
        val[cnt:cnt + len(a.value)] = a.value
        cnt += len(a.value)

    return values.W_MutableBytes(val)
Beispiel #5
0
def bytes_append(args):
    total_len = 0
    for a in args:
        if not isinstance(a, values.W_Bytes):
            raise SchemeException(
                "bytes-append: expected a byte string, but got %s" % a)
        total_len += len(a.value)

    result = ['\0'] * total_len  # is this the fastest way to do things?
    cnt = 0
    for a in args:
        assert isinstance(a, values.W_Bytes)
        for byte in a.value:
            result[cnt] = byte
            cnt += 1

    assert cnt == total_len
    return values.W_MutableBytes(result)
Beispiel #6
0
def list_to_bytes(w_list):
    if not w_list.is_proper_list():
        raise SchemeException("list->bytes: expected proper list, got %s" % w_list.tostring())

    ll = []
    while isinstance(w_list, values.W_UnwrappedFixnumConsProper):
        val = w_list._car
        if not (0 <= val < 256):
            break
        ll.append(chr(val))
        w_list = w_list.cdr()
    else:
        if w_list is values.w_null:
            return values.W_MutableBytes(ll[:])

    assert isinstance(w_list, values.W_Cons)
    raise SchemeException("list->bytes: expected a number between 0 and 255, got %s"
                          % w_list.car().tostring())
Beispiel #7
0
def subbytes(w_bytes, w_start, w_end):
    """
    (subbytes bstr start [end]) → bytes?
        bstr : bytes?
        start : exact-nonnegative-integer?
        end : exact-nonnegative-integer? = (bytes-length str)
    """
    bytes = w_bytes.value
    start = w_start.value
    if start > len(bytes) or start < 0:
        raise SchemeException("subbytes: end index out of bounds")
    if w_end is not None:
        end = w_end.value
        if end > len(bytes) or end < 0:
            raise SchemeException("subbytes: end index out of bounds")
    else:
        end = len(bytes)
    if end < start:
        raise SchemeException(
            "subbytes: ending index is smaller than starting index")
    return values.W_MutableBytes(bytes[start:end])