Skip to main content

Module stdlib

Module stdlib 

Source
Expand description

Standard library functions for Sage.

This module provides runtime helper functions for the Sage standard library. Most stdlib functions are inlined during codegen, but some require runtime helpers for correct Unicode handling or complex logic.

Constants§

MS_PER_DAY
MS_PER_HOUR
MS_PER_MINUTE
MS_PER_SECOND

Functions§

append_file
Append a string to a file, creating it if it doesn’t exist.
chr
Convert a Unicode code point to a single-character string. Returns the Unicode replacement character for invalid code points.
delete_file
Delete a file.
file_exists
Check if a file or directory exists.
format_timestamp
Format a Unix timestamp (in milliseconds) using the given format string.
json_get
Get a field from a JSON object as a string. Returns None if the field doesn’t exist or isn’t a string.
json_get_bool
Get a field from a JSON object as a boolean. Returns None if the field doesn’t exist or isn’t a boolean.
json_get_float
Get a field from a JSON object as a float. Returns None if the field doesn’t exist or isn’t a number.
json_get_int
Get a field from a JSON object as an integer. Returns None if the field doesn’t exist or isn’t a number.
json_get_list
Get a field from a JSON object as a list of strings. Each array element is converted to its JSON string representation. Returns None if the field doesn’t exist or isn’t an array.
json_parse
Parse a JSON string, validating that it’s well-formed JSON. Returns the original string if valid.
json_stringify_string
Convert a value to a JSON string. Works best with strings, but accepts any type via its Debug representation.
list_dir
List the contents of a directory.
list_slice
Slice a list by indices (bounds-safe). Indices are inclusive start, exclusive end.
make_dir
Create a directory and all parent directories.
now_ms
Get the current time in milliseconds since Unix epoch.
now_s
Get the current time in seconds since Unix epoch.
parse_bool
Parse a boolean from a string. Accepts “true”, “false”, “1”, “0” (case-insensitive for true/false).
parse_timestamp
Parse a string into a Unix timestamp (in milliseconds) using the given format.
read_all
Read all content from stdin until EOF.
read_file
Read the entire contents of a file as a string.
read_line
Read a single line from stdin.
str_index_of
Find the index of a substring within a string (Unicode-aware). Returns None if not found, Some(index) otherwise.
str_pad_end
Pad a string at the end to reach the target length (Unicode-aware).
str_pad_start
Pad a string at the start to reach the target length (Unicode-aware).
str_slice
Extract a substring by character indices (Unicode-aware). Indices are inclusive start, exclusive end.
write_file
Write a string to a file, creating it if it doesn’t exist or truncating it.