Ruby  3.4.0dev (2024-12-06 revision 892c46283a5ea4179500d951c9d4866c0051f27b)
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 {
53 
56 
59 
62 
65 
74 
85 void pm_integer_parse(pm_integer_t *integer, pm_integer_base_t base, const uint8_t *start, const uint8_t *end);
86 
96 int pm_integer_compare(const pm_integer_t *left, const pm_integer_t *right);
97 
108 void pm_integers_reduce(pm_integer_t *numerator, pm_integer_t *denominator);
109 
117 
125 
126 #endif
A wrapper around a contiguous block of allocated memory.
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
PRISM_EXPORTED_FUNCTION void pm_integer_free(pm_integer_t *integer)
Free the internal memory of an integer.
Definition: pm_integer.c:666
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
An enum controlling the base of an integer.
Definition: pm_integer.h:50
@ PM_INTEGER_BASE_DEFAULT
The default decimal base, with no prefix.
Definition: pm_integer.h:52
@ PM_INTEGER_BASE_BINARY
The binary base, indicated by a 0b or 0B prefix.
Definition: pm_integer.h:55
@ PM_INTEGER_BASE_DECIMAL
The decimal base, indicated by a 0d, 0D, or empty prefix.
Definition: pm_integer.h:61
@ PM_INTEGER_BASE_HEXADECIMAL
The hexadecimal base, indicated by a 0x or 0X prefix.
Definition: pm_integer.h:64
@ PM_INTEGER_BASE_OCTAL
The octal base, indicated by a 0, 0o, or 0O prefix.
Definition: pm_integer.h:58
@ 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
PRISM_EXPORTED_FUNCTION void pm_integer_string(pm_buffer_t *buffer, const pm_integer_t *integer)
Convert an integer to a decimal string.
Definition: pm_integer.c:607
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
Macro definitions used throughout the prism library.
#define PRISM_EXPORTED_FUNCTION
By default, we compile with -fvisibility=hidden.
Definition: defines.h:50
C99 shim for <stdbool.h>
A pm_buffer_t is a simple memory buffer that stores data in a contiguous block of memory.
Definition: pm_buffer.h:22
A structure represents an arbitrary-sized integer.
Definition: pm_integer.h:20
size_t length
The number of allocated values.
Definition: pm_integer.h:25
uint32_t value
Embedded value for small integer.
Definition: pm_integer.h:36
uint32_t * values
List of 32-bit integers.
Definition: pm_integer.h:30
bool negative
Whether or not the integer is negative.
Definition: pm_integer.h:42