Programming Language

Nocter

A self-contained systems language built around simplicity, encapsulation, and foolproof design.

index.nct

//! Allocation-free conversion between scalar values and byte sequences.
//!
//! This module owns byte order only. Borrowed cursor progress belongs to std/scan, bounded storage
//! belongs to std/fixed, and external transport belongs to std/io.

see ./scalar.nct
see ./tests.nct
see ./variable.nct

/// The result of decoding one scalar from the beginning of a byte view.
///
/// Only `decoded` carries a value or permits a cursor to advance. `incomplete` means that a longer
/// prefix could become valid, while `overflow` and `non_canonical` are terminal for the inspected
/// representation.
pub enum PrefixDecode<T> {
    decoded(value: T, consumed: usize)
    incomplete
    overflow
    non_canonical
}

/// Decodes the first two bytes as one big-endian unsigned integer.
pub noalloc func decode_u16_be(input: &[u8]): u16?

/// Decodes the first two bytes as one little-endian unsigned integer.
pub noalloc func decode_u16_le(input: &[u8]): u16?

/// Decodes the first four bytes as one big-endian unsigned integer.
pub noalloc func decode_u32_be(input: &[u8]): u32?

/// Decodes the first four bytes as one little-endian unsigned integer.
pub noalloc func decode_u32_le(input: &[u8]): u32?

/// Decodes the first eight bytes as one big-endian unsigned integer.
pub noalloc func decode_u64_be(input: &[u8]): u64?

/// Decodes the first eight bytes as one little-endian unsigned integer.
pub noalloc func decode_u64_le(input: &[u8]): u64?

/// Decodes the first two bytes as one big-endian signed two's-complement integer.
pub noalloc func decode_i16_be(input: &[u8]): i16?

/// Decodes the first two bytes as one little-endian signed two's-complement integer.
pub noalloc func decode_i16_le(input: &[u8]): i16?

/// Decodes the first four bytes as one big-endian signed two's-complement integer.
pub noalloc func decode_i32_be(input: &[u8]): i32?

/// Decodes the first four bytes as one little-endian signed two's-complement integer.
pub noalloc func decode_i32_le(input: &[u8]): i32?

/// Decodes the first eight bytes as one big-endian signed two's-complement integer.
pub noalloc func decode_i64_be(input: &[u8]): i64?

/// Decodes the first eight bytes as one little-endian signed two's-complement integer.
pub noalloc func decode_i64_le(input: &[u8]): i64?

/// Decodes the first four bytes as one big-endian IEEE binary32 value.
pub noalloc func decode_f32_be(input: &[u8]): f32?

/// Decodes the first four bytes as one little-endian IEEE binary32 value.
pub noalloc func decode_f32_le(input: &[u8]): f32?

/// Decodes the first eight bytes as one big-endian IEEE binary64 value.
pub noalloc func decode_f64_be(input: &[u8]): f64?

/// Decodes the first eight bytes as one little-endian IEEE binary64 value.
pub noalloc func decode_f64_le(input: &[u8]): f64?

/// Encodes one unsigned integer into the first two output bytes in big-endian order.
/// Returns false and leaves output unchanged when fewer than two bytes are available.
pub noalloc func encode_u16_be(output: &+[u8], value: u16): bool

/// Encodes one unsigned integer into the first two output bytes in little-endian order.
/// Returns false and leaves output unchanged when fewer than two bytes are available.
pub noalloc func encode_u16_le(output: &+[u8], value: u16): bool

/// Encodes one unsigned integer into the first four output bytes in big-endian order.
/// Returns false and leaves output unchanged when fewer than four bytes are available.
pub noalloc func encode_u32_be(output: &+[u8], value: u32): bool

/// Encodes one unsigned integer into the first four output bytes in little-endian order.
/// Returns false and leaves output unchanged when fewer than four bytes are available.
pub noalloc func encode_u32_le(output: &+[u8], value: u32): bool

/// Encodes one unsigned integer into the first eight output bytes in big-endian order.
/// Returns false and leaves output unchanged when fewer than eight bytes are available.
pub noalloc func encode_u64_be(output: &+[u8], value: u64): bool

/// Encodes one unsigned integer into the first eight output bytes in little-endian order.
/// Returns false and leaves output unchanged when fewer than eight bytes are available.
pub noalloc func encode_u64_le(output: &+[u8], value: u64): bool

/// Encodes one signed integer into the first two output bytes in big-endian order.
/// Returns false and leaves output unchanged when fewer than two bytes are available.
pub noalloc func encode_i16_be(output: &+[u8], value: i16): bool

/// Encodes one signed integer into the first two output bytes in little-endian order.
/// Returns false and leaves output unchanged when fewer than two bytes are available.
pub noalloc func encode_i16_le(output: &+[u8], value: i16): bool

/// Encodes one signed integer into the first four output bytes in big-endian order.
/// Returns false and leaves output unchanged when fewer than four bytes are available.
pub noalloc func encode_i32_be(output: &+[u8], value: i32): bool

/// Encodes one signed integer into the first four output bytes in little-endian order.
/// Returns false and leaves output unchanged when fewer than four bytes are available.
pub noalloc func encode_i32_le(output: &+[u8], value: i32): bool

/// Encodes one signed integer into the first eight output bytes in big-endian order.
/// Returns false and leaves output unchanged when fewer than eight bytes are available.
pub noalloc func encode_i64_be(output: &+[u8], value: i64): bool

/// Encodes one signed integer into the first eight output bytes in little-endian order.
/// Returns false and leaves output unchanged when fewer than eight bytes are available.
pub noalloc func encode_i64_le(output: &+[u8], value: i64): bool

/// Encodes one IEEE binary32 value into the first four output bytes in big-endian order.
/// Returns false and leaves output unchanged when fewer than four bytes are available.
pub noalloc func encode_f32_be(output: &+[u8], value: f32): bool

/// Encodes one IEEE binary32 value into the first four output bytes in little-endian order.
/// Returns false and leaves output unchanged when fewer than four bytes are available.
pub noalloc func encode_f32_le(output: &+[u8], value: f32): bool

/// Encodes one IEEE binary64 value into the first eight output bytes in big-endian order.
/// Returns false and leaves output unchanged when fewer than eight bytes are available.
pub noalloc func encode_f64_be(output: &+[u8], value: f64): bool

/// Encodes one IEEE binary64 value into the first eight output bytes in little-endian order.
/// Returns false and leaves output unchanged when fewer than eight bytes are available.
pub noalloc func encode_f64_le(output: &+[u8], value: f64): bool

/// Decodes one canonical unsigned LEB128 value from the beginning of a byte view.
pub noalloc func decode_uleb128(input: &[u8]): PrefixDecode<u64>

/// Returns the canonical unsigned LEB128 width of one value.
pub noalloc func uleb128_width(value: u64): usize

/// Encodes one unsigned LEB128 value and returns its width.
/// Returns none and leaves output unchanged when the complete encoding does not fit.
pub noalloc func encode_uleb128(output: &+[u8], value: u64): usize?

/// Decodes one canonical ZigZag-signed LEB128 value from the beginning of a byte view.
pub noalloc func decode_zigzag_uleb128(input: &[u8]): PrefixDecode<i64>

/// Returns the canonical ZigZag-signed LEB128 width of one value.
pub noalloc func zigzag_uleb128_width(value: i64): usize

/// ZigZag-encodes one signed value as unsigned LEB128 and returns its width.
/// Returns none and leaves output unchanged when the complete encoding does not fit.
pub noalloc func encode_zigzag_uleb128(output: &+[u8], value: i64): usize?