Ruby 4.1.0dev (2026-11-16 revision 9c7bc705b8bd4542beac71542a8ea79f9bb7622b)
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/arena.h"
8#include "prism/buffer.h"
9#include "prism/integer.h"
10
11#include <stdint.h>
12
13/* Return an integer holding the given non-negative value. */
15pm_integer_uint64(uint64_t value) {
16 return (pm_integer_t) {
17 .length = value == 0 ? 0 : value <= UINT32_MAX ? 1 : 2,
18 .words = { .value = { (uint32_t) value, (uint32_t) (value >> 32) } }
19 };
20}
21
22/*
23 * An enum controlling the base of an integer. It is expected that the base is
24 * already known before parsing the integer, even though it could be derived
25 * from the string itself.
26 */
27typedef enum {
28 /* The binary base, indicated by a 0b or 0B prefix. */
29 PM_INTEGER_BASE_BINARY = 2,
30
31 /* The octal base, indicated by a 0, 0o, or 0O prefix. */
32 PM_INTEGER_BASE_OCTAL = 8,
33
34 /* The decimal base, indicated by a 0d, 0D, or empty prefix. */
35 PM_INTEGER_BASE_DECIMAL = 10,
36
37 /* The hexadecimal base, indicated by a 0x or 0X prefix. */
38 PM_INTEGER_BASE_HEXADECIMAL = 16
39} pm_integer_base_t;
40
41/*
42 * Parse an integer from a string. This assumes that the format of the integer
43 * has already been validated, as internal validation checks are not performed
44 * here. The words of an integer that does not fit in two words are allocated
45 * from the given arena.
46 */
47void pm_integer_parse(pm_arena_t *arena, 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 * Parse a float rational literal like 1.5r, given its digits and decimal point
58 * without the r suffix, into a numerator and denominator in lowest terms. Words
59 * of either that do not fit in two words are allocated from the given arena.
60 */
61void pm_integer_parse_rational(pm_arena_t *arena, pm_integer_t *numerator, pm_integer_t *denominator, const uint8_t *start, const uint8_t *end);
62
63/* Convert an integer to a decimal string. */
64void pm_integer_string(pm_buffer_t *buffer, const pm_integer_t *integer);
65
66#endif
A bump allocator for the prism parser.
#define PRISM_INLINE
Old Visual Studio versions do not support the inline keyword, so we need to define it to be __inline.
Definition inline.h:12
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:19
uint32_t length
The number of words in the magnitude, with no leading zero words.
Definition integer.h:24