Module: core.convert

Source: ./core/convert.reef


Overview

core.convert - Type conversion functions

Provides functions for converting between primitive types and strings, including base conversions (hex, binary, octal) and formatted output.


Functions

fn char_to_digit(c: char): int

Helper: Convert digit character to int value (private)

fn digit_to_char(digit: int): char

Helper: Convert digit value to char (private)

fn digit_to_float(digit: int): float

Helper: Convert single digit int (0-9) to float using lookup

fn to_string(n: int): string

Converts an integer to string (pure Reef implementation) Example: to_string(42) returns "42", to_string(-123) returns "-123"

fn to_string_float(f: float): string

Converts a float to string (pure Reef implementation) Uses 6 decimal places by default (like printf %f) Example: to_string_float(3.14159) returns "3.141590"

fn to_string_float_precision(f: float, precision: int): string

Converts a float to string with specified decimal precision Pure Reef implementation Example: to_string_float_precision(3.14159, 2) returns "3.14"

fn to_string_bool(b: bool): string

Converts a boolean to string

fn to_string_char(c: char): string

Converts a char to a single-character string

fn toInt(s: string): result.Result[int, error.Error]

Parses a string as a signed decimal integer (pure Reef implementation). STRICT: the entire string (aside from optional leading/trailing whitespace and an optional leading +/- sign) must be digits, or this returns Err — trailing garbage is rejected, not silently dropped (matching the strict contract of from_hex/from_binary/from_octal and str.to_int). Returns Err(...) if the string contains no digits at all (empty input, whitespace/sign only) or has any non-digit character after the digits (other than trailing whitespace) — e.g. toInt("123abc") returns Err. Example: toInt("42") returns Ok(42), toInt("-123") returns Ok(-123), toInt(" 42 ") returns Ok(42)

fn toFloat(s: string): result.Result[float, error.Error]

Parses a string as a float (pure Reef implementation). STRICT: the entire string (aside from optional leading/trailing whitespace) must be consumed by the number, or this returns Err — trailing garbage is rejected, not silently dropped, matching the strict contract of from_hex/from_binary/from_octal and str.to_int. Returns Err(...) if no float digits were consumed (empty input, whitespace/sign only, or garbage with no digits before or after a decimal point) or if there is any non-whitespace trailing garbage after the number — e.g. toFloat("abc"), toFloat("-"), toFloat(" "), and toFloat("3.14xyz") all return Err. Handles: optional sign, integer part, decimal part, exponent (e/E) Example: toFloat("3.14") returns Ok(3.14), toFloat("-2.5e3") returns Ok(-2500.0)

fn toBool(s: string): result.Result[bool, error.Error]

Parses a string as a boolean. Recognizes: "true", "false", "1", "0", "yes", "no". Returns Ok(true)/Ok(false) for recognized strings, Err(...) otherwise.

fn to_hex(n: int): string

Converts an integer to hexadecimal string (uppercase, no prefix) Pure Reef implementation Example: to_hex(255) returns "FF", to_hex(16) returns "10"

fn to_binary(n: int): string

Converts an integer to binary string (no prefix) Example: to_binary(10) returns "1010"

fn to_octal(n: int): string

Converts an integer to octal string (no prefix) Pure Reef implementation Example: to_octal(63) returns "77", to_octal(8) returns "10"

fn to_hex_lower(n: int): string

Converts an integer to lowercase hexadecimal string (no prefix) Pure Reef implementation Example: to_hex_lower(255) returns "ff", to_hex_lower(16) returns "10"

fn to_hex_prefixed(n: int): string

Converts an integer to hexadecimal string with "0x" prefix Pure Reef implementation Example: to_hex_prefixed(255) returns "0xFF"

fn to_string_padded(n: int, width: int): string

Converts an integer to zero-padded decimal string Pure Reef implementation Example: to_string_padded(42, 4) returns "0042"

fn to_hex_padded(n: int, width: int): string

Converts an integer to zero-padded hexadecimal string (uppercase) Pure Reef implementation Example: to_hex_padded(255, 4) returns "00FF"

fn from_hex(s: string): result.Result[int, error.Error]

Parses a hexadecimal string to integer (pure Reef implementation). Returns Ok(n) on success, Err(...) on invalid input — an empty string, a string with no digits after an optional "0x"/"0X" prefix, or any character (after the prefix) that isn't a valid hex digit. Example: from_hex("FF") returns Ok(255), from_hex("0xFF") returns Ok(255)

fn from_binary(s: string): result.Result[int, error.Error]

Parses a binary string to integer (pure Reef implementation). Returns Ok(n) on success, Err(...) on invalid input — an empty string, a string with no digits after an optional "0b"/"0B" prefix, or any character (after the prefix) that isn't '0' or '1'. Example: from_binary("1010") returns Ok(10), from_binary("0b1010") returns Ok(10)

fn from_octal(s: string): result.Result[int, error.Error]

Parses an octal string to integer (pure Reef implementation). Returns Ok(n) on success, Err(...) on invalid input — an empty string, a string with no digits after an optional "0o"/"0O" prefix, or any character (after the prefix) that isn't a valid octal digit (0-7). Example: from_octal("77") returns Ok(63), from_octal("0o77") returns Ok(63)

fn to_char(code: int): char

Converts an ASCII code to a character Example: to_char(65) returns 'A'

fn to_code(c: char): int

Converts a character to its ASCII code Example: to_code('A') returns 65


Generated by reefc doc