Ruby
3.4.0dev (2024-12-06 revision 892c46283a5ea4179500d951c9d4866c0051f27b)
|
This module provides functions for working with arbitrary-sized integers. More...
#include "prism/defines.h"
#include "prism/util/pm_buffer.h"
#include <assert.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
Go to the source code of this file.
Data Structures | |
struct | pm_integer_t |
A structure represents an arbitrary-sized integer. More... | |
Enumerations | |
enum | pm_integer_base_t { PM_INTEGER_BASE_DEFAULT , PM_INTEGER_BASE_BINARY , PM_INTEGER_BASE_OCTAL , PM_INTEGER_BASE_DECIMAL , PM_INTEGER_BASE_HEXADECIMAL , PM_INTEGER_BASE_UNKNOWN } |
An enum controlling the base of an integer. More... | |
Functions | |
void | pm_integer_parse (pm_integer_t *integer, pm_integer_base_t base, const uint8_t *start, const uint8_t *end) |
Parse an integer from a string. More... | |
int | pm_integer_compare (const pm_integer_t *left, const pm_integer_t *right) |
Compare two integers. More... | |
void | pm_integers_reduce (pm_integer_t *numerator, pm_integer_t *denominator) |
Reduce a ratio of integers to its simplest form. More... | |
PRISM_EXPORTED_FUNCTION void | pm_integer_string (pm_buffer_t *buffer, const pm_integer_t *integer) |
Convert an integer to a decimal string. More... | |
PRISM_EXPORTED_FUNCTION void | pm_integer_free (pm_integer_t *integer) |
Free the internal memory of an integer. More... | |
This module provides functions for working with arbitrary-sized integers.
Definition in file pm_integer.h.
enum pm_integer_base_t |
An enum controlling the base of an integer.
It is expected that the base is already known before parsing the integer, even though it could be derived from the string itself.
Definition at line 50 of file pm_integer.h.
int pm_integer_compare | ( | const pm_integer_t * | left, |
const pm_integer_t * | right | ||
) |
Compare two integers.
This function returns -1 if the left integer is less than the right integer, 0 if they are equal, and 1 if the left integer is greater than the right integer.
left | The left integer to compare. |
right | The right integer to compare. |
This function returns -1 if the left integer is less than the right integer, 0 if they are equal, and 1 if the left integer is greater than the right integer.
Definition at line 545 of file pm_integer.c.
PRISM_EXPORTED_FUNCTION void pm_integer_free | ( | pm_integer_t * | integer | ) |
Free the internal memory of an integer.
This memory will only be allocated if the integer exceeds the size of a single node in the linked list.
integer | The integer to free. |
This memory will only be allocated if the integer exceeds the size of a single uint32_t.
Definition at line 666 of file pm_integer.c.
Referenced by pm_integer_string(), and pm_node_destroy().
void pm_integer_parse | ( | pm_integer_t * | integer, |
pm_integer_base_t | base, | ||
const uint8_t * | start, | ||
const uint8_t * | end | ||
) |
Parse an integer from a string.
This assumes that the format of the integer has already been validated, as internal validation checks are not performed here.
integer | The integer to parse into. |
base | The base of the integer. |
start | The start of the string. |
end | The end of the string. |
This assumes that the format of the integer has already been validated, as internal validation checks are not performed here.
Definition at line 475 of file pm_integer.c.
PRISM_EXPORTED_FUNCTION void pm_integer_string | ( | pm_buffer_t * | buffer, |
const pm_integer_t * | integer | ||
) |
Convert an integer to a decimal string.
buffer | The buffer to append the string to. |
integer | The integer to convert to a string. |
Definition at line 607 of file pm_integer.c.
Referenced by pm_dump_json().
void pm_integers_reduce | ( | pm_integer_t * | numerator, |
pm_integer_t * | denominator | ||
) |
Reduce a ratio of integers to its simplest form.
If either the numerator or denominator do not fit into a 32-bit integer, then this function is a no-op. In the future, we may consider reducing even the larger numbers, but for now we're going to keep it simple.
numerator | The numerator of the ratio. |
denominator | The denominator of the ratio. |
Definition at line 573 of file pm_integer.c.