Ruby: prism/util/pm_integer.h Source File

Ruby 4.0.6p0 (2026-07-14 revision 03b6d3f8898a28604fe6cb00eae3226b821168f4)

pm_integer.h
Go to the documentation of this file.
1
6#ifndef PRISM_NUMBER_H
7#define PRISM_NUMBER_H
8
9#include "prism/defines.h"
10#include "prism/util/pm_buffer.h"
11
12#include <assert.h>
13#include <stdbool.h>
14#include <stdint.h>
15#include <stdlib.h>
16
20 typedef struct {
25 size_t length;
26
30 uint32_t *values;
31
36 uint32_t value;
37
42 bool negative;
43} pm_integer_t;
44
50 typedef enum {
52 PM_INTEGER_BASE_DEFAULT,
53
55 PM_INTEGER_BASE_BINARY,
56
58 PM_INTEGER_BASE_OCTAL,
59
61 PM_INTEGER_BASE_DECIMAL,
62
64 PM_INTEGER_BASE_HEXADECIMAL,
65
72 PM_INTEGER_BASE_UNKNOWN
73} pm_integer_base_t;
74
85void pm_integer_parse(pm_integer_t *integer, pm_integer_base_t base, const uint8_t *start, const uint8_t *end);
86
96int pm_integer_compare(const pm_integer_t *left, const pm_integer_t *right);
97
108void pm_integers_reduce(pm_integer_t *numerator, pm_integer_t *denominator);
109
118 PRISM_EXPORTED_FUNCTION void pm_integer_string(pm_buffer_t *buffer, const pm_integer_t *integer);
119
128 PRISM_EXPORTED_FUNCTION void pm_integer_free(pm_integer_t *integer);
129
130#endif
assert.h
pm_buffer.h
A wrapper around a contiguous block of allocated memory.
pm_integers_reduce
void pm_integers_reduce(pm_integer_t *numerator, pm_integer_t *denominator)
Reduce a ratio of integers to its simplest form.
Definition pm_integer.c:573
pm_integer_compare
int pm_integer_compare(const pm_integer_t *left, const pm_integer_t *right)
Compare two integers.
Definition pm_integer.c:545
pm_integer_base_t
pm_integer_base_t
An enum controlling the base of an integer.
Definition pm_integer.h:50
PM_INTEGER_BASE_DEFAULT
@ PM_INTEGER_BASE_DEFAULT
The default decimal base, with no prefix.
Definition pm_integer.h:52
PM_INTEGER_BASE_BINARY
@ PM_INTEGER_BASE_BINARY
The binary base, indicated by a 0b or 0B prefix.
Definition pm_integer.h:55
PM_INTEGER_BASE_DECIMAL
@ PM_INTEGER_BASE_DECIMAL
The decimal base, indicated by a 0d, 0D, or empty prefix.
Definition pm_integer.h:61
PM_INTEGER_BASE_HEXADECIMAL
@ PM_INTEGER_BASE_HEXADECIMAL
The hexadecimal base, indicated by a 0x or 0X prefix.
Definition pm_integer.h:64
PM_INTEGER_BASE_OCTAL
@ PM_INTEGER_BASE_OCTAL
The octal base, indicated by a 0, 0o, or 0O prefix.
Definition pm_integer.h:58
PM_INTEGER_BASE_UNKNOWN
@ PM_INTEGER_BASE_UNKNOWN
An unknown base, in which case pm_integer_parse will derive it based on the content of the string.
Definition pm_integer.h:72
pm_integer_parse
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.
Definition pm_integer.c:475
defines.h
Macro definitions used throughout the prism library.
PRISM_EXPORTED_FUNCTION
#define PRISM_EXPORTED_FUNCTION
By default, we compile with -fvisibility=hidden.
Definition defines.h:53
stdbool.h
C99 shim for <stdbool.h>.
pm_buffer_t
A pm_buffer_t is a simple memory buffer that stores data in a contiguous block of memory.
Definition pm_buffer.h:22
pm_integer_t
A structure represents an arbitrary-sized integer.
Definition pm_integer.h:20
pm_integer_t::length
size_t length
The number of allocated values.
Definition pm_integer.h:25
pm_integer_t::value
uint32_t value
Embedded value for small integer.
Definition pm_integer.h:36
pm_integer_t::values
uint32_t * values
List of 32-bit integers.
Definition pm_integer.h:30
pm_integer_t::negative
bool negative
Whether or not the integer is negative.
Definition pm_integer.h:42