Ruby 4.1.0dev (2026-08-28 revision 5eb9a6925b805a17dced976d5b741afe5086aab1)
integer.h
1/*
2 * This module provides functions for working with arbitrary-sized integers.
3 */
4#ifndef PRISM_INTERNAL_INTEGER_H
5#define PRISM_INTERNAL_INTEGER_H
6
7#include "prism/buffer.h"
8#include "prism/integer.h"
9
10#include <stdint.h>
11
12/*
13 * An enum controlling the base of an integer. It is expected that the base is
14 * already known before parsing the integer, even though it could be derived
15 * from the string itself.
16 */
17typedef enum {
18 /* The default decimal base, with no prefix. Leading 0s will be ignored. */
19 PM_INTEGER_BASE_DEFAULT,
20
21 /* The binary base, indicated by a 0b or 0B prefix. */
22 PM_INTEGER_BASE_BINARY,
23
24 /* The octal base, indicated by a 0, 0o, or 0O prefix. */
25 PM_INTEGER_BASE_OCTAL,
26
27 /* The decimal base, indicated by a 0d, 0D, or empty prefix. */
28 PM_INTEGER_BASE_DECIMAL,
29
30 /* The hexadecimal base, indicated by a 0x or 0X prefix. */
31 PM_INTEGER_BASE_HEXADECIMAL,
32
33 /*
34 * An unknown base, in which case pm_integer_parse will derive it based on
35 * the content of the string. This is less efficient and does more
36 * comparisons, so if callers know the base ahead of time, they should use
37 * that instead.
38 */
39 PM_INTEGER_BASE_UNKNOWN
40} pm_integer_base_t;
41
42/*
43 * Parse an integer from a string. This assumes that the format of the integer
44 * has already been validated, as internal validation checks are not performed
45 * here.
46 */
47void pm_integer_parse(pm_integer_t *integer, pm_integer_base_t base, const uint8_t *start, const uint8_t *end);
48
49/*
50 * Compare two integers. This function returns -1 if the left integer is less
51 * than the right integer, 0 if they are equal, and 1 if the left integer is
52 * greater than the right integer.
53 */
54int pm_integer_compare(const pm_integer_t *left, const pm_integer_t *right);
55
56/*
57 * Reduce a ratio of integers to its simplest form.
58 *
59 * If either the numerator or denominator do not fit into a 32-bit integer, then
60 * this function is a no-op. In the future, we may consider reducing even the
61 * larger numbers, but for now we're going to keep it simple.
62 */
63void pm_integers_reduce(pm_integer_t *numerator, pm_integer_t *denominator);
64
65/* Convert an integer to a decimal string. */
66void pm_integer_string(pm_buffer_t *buffer, const pm_integer_t *integer);
67
68#endif
This module provides functions for working with arbitrary-sized integers.
A wrapper around a contiguous block of allocated memory.
A structure represents an arbitrary-sized integer.
Definition integer.h:16