Esempio n. 1
0
def string_to_bytes_locale(str, errbyte, start, end):
    # assert errbyte is values.w_false
    # ignore for now
    assert start.value == 0
    assert end is None
    # FIXME: This ignores the locale
    return values.W_Bytes(str.as_charlist_utf8())
Esempio n. 2
0
def rmpe(pat, input, inp_start, inp_end, output_port, prefix, count, env,
         cont):
    from pycket.interpreter import return_multi_vals

    start = inp_start.value
    if inp_end is values.w_false:
        end = sys.maxint
    elif isinstance(inp_end, values.W_Fixnum):
        end = inp_end.value
    else:
        raise SchemeException(
            "regexp-match-positions/end: expected fixnum or #f for argument 3")

    assert output_port is values.w_false, "output port not supported yet"

    matches = match_positions(pat, input, start, end)
    if matches is None:
        return return_multi_vals(NO_MATCH, env, cont)

    acc, end = make_match_list(matches)

    length = count.value
    start = max(0, end - length)

    assert start >= 0 and end >= 0
    matched = input.getslice(start, end)
    bytestring = ['\0'] * (end - start)
    for i in range(end - start):
        bytestring[i] = chr(ord(matched.getitem(i)) % 256)
    bytes = values.W_Bytes(bytestring)
    result = values.Values._make2(acc, bytes)
    return return_multi_vals(result, env, cont)
Esempio n. 3
0
def integer_to_integer_bytes(n, _size, signed):
    if isinstance(n, values.W_Fixnum):
        intval = n.value
    elif isinstance(n, values.W_Bignum):
        raise NotImplementedError("not implemented yet")
    else:
        raise SchemeException("integer->integer-bytes: expected exact integer")

    size = _size.value
    if size not in (2, 4, 8):
        raise SchemeException("integer->integer-bytes: size not 2, 4, or 8")

    chars  = [chr((intval >> (i * 8)) % 256) for i in range(size)]
    return values.W_Bytes(chars)
Esempio n. 4
0
def integer_to_integer_bytes(n, w_size, signed, big_endian, w_dest, w_start):
    from rpython.rtyper.lltypesystem import rffi
    if isinstance(n, values.W_Fixnum):
        intval = n.value
    elif isinstance(n, values.W_Bignum):
        raise NotImplementedError("not implemented yet")
    else:
        raise SchemeException("integer->integer-bytes: expected exact integer")

    size = jit.promote(w_size.value)
    if size not in (2, 4, 8):
        raise SchemeException("integer->integer-bytes: size not 2, 4, or 8")

    size = size
    start = w_start.value
    if w_dest is not None:
        chars = w_dest.value
        result = w_dest
    else:
        chars = ['\x00'] * size
        result = values.W_Bytes(chars)

    if start < 0:
        raise SchemeException(
            "integer->integer-bytes: start value less than zero")

    if start + size > len(chars):
        raise SchemeException(
            "integer->integer-bytes: byte string length is less than starting "
            "position plus size")

    is_signed = signed is not values.w_false
    for i in range(start, start + size):
        chars[i] = chr(intval & 0xFF)
        intval >>= 8

    if big_endian is values.w_false:
        return result

    # Swap the bytes if for big endian
    left = start
    right = start + size - 1
    while left < right:
        chars[left], chars[right] = chars[right], chars[left]
        left, right = left + 1, right - 1

    return result
Esempio n. 5
0
File: numeric.py Progetto: 8l/pycket
def real_floating_point_bytes(n, _size, big_endian):
    if isinstance(n, values.W_Flonum):
        v = n.value
    elif isinstance(n, values.W_Fixnum):
        v = float(n.value)
    elif isinstance(n, values.W_Bignum):
        v = rbigint.tofloat(n.value)
    else:
        raise SchemeException("real->floating-point-bytes: expected real")

    size = _size.value
    if size != 4 and size != 8:
        raise SchemeException("real->floating-point-bytes: size not 4 or 8")

    intval = longlong2float.float2longlong(v)

    if big_endian is not values.w_false:
        intval = rarithmetic.byteswap(intval)

    chars = [chr((intval >> (i * 8)) % 256) for i in range(size)]
    return values.W_Bytes(chars)