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§
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.