Prism Ruby parser
Loading...
Searching...
No Matches
Data Structures | Enumerations | Functions
pm_integer.h File Reference

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>
Include dependency graph for pm_integer.h:
This graph shows which files directly or indirectly include this file:

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.
 
int pm_integer_compare (const pm_integer_t *left, const pm_integer_t *right)
 Compare two integers.
 
void pm_integers_reduce (pm_integer_t *numerator, pm_integer_t *denominator)
 Reduce a ratio of integers to its simplest form.
 
PRISM_EXPORTED_FUNCTION void pm_integer_string (pm_buffer_t *buffer, const pm_integer_t *integer)
 Convert an integer to a decimal string.
 
PRISM_EXPORTED_FUNCTION void pm_integer_free (pm_integer_t *integer)
 Free the internal memory of an integer.
 

Detailed Description

This module provides functions for working with arbitrary-sized integers.

Enumeration Type Documentation

◆ 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.

Enumerator
PM_INTEGER_BASE_DEFAULT 

The default decimal base, with no prefix.

Leading 0s will be ignored.

PM_INTEGER_BASE_BINARY 

The binary base, indicated by a 0b or 0B prefix.

PM_INTEGER_BASE_OCTAL 

The octal base, indicated by a 0, 0o, or 0O prefix.

PM_INTEGER_BASE_DECIMAL 

The decimal base, indicated by a 0d, 0D, or empty prefix.

PM_INTEGER_BASE_HEXADECIMAL 

The hexadecimal base, indicated by a 0x or 0X prefix.

PM_INTEGER_BASE_UNKNOWN 

An unknown base, in which case pm_integer_parse will derive it based on the content of the string.

This is less efficient and does more comparisons, so if callers know the base ahead of time, they should use that instead.

Function Documentation

◆ 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.

This assumes that the format of the integer has already been validated, as internal validation checks are not performed here.

Parameters
integerThe integer to parse into.
baseThe base of the integer.
startThe start of the string.
endThe end of the string.

This assumes that the format of the integer has already been validated, as internal validation checks are not performed here.

◆ pm_integer_compare()

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.

Parameters
leftThe left integer to compare.
rightThe right integer to compare.
Returns
The result of the comparison.

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.

◆ pm_integers_reduce()

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.

Parameters
numeratorThe numerator of the ratio.
denominatorThe denominator of the ratio.

◆ pm_integer_string()

PRISM_EXPORTED_FUNCTION void pm_integer_string ( pm_buffer_t buffer,
const pm_integer_t integer 
)

Convert an integer to a decimal string.

Parameters
bufferThe buffer to append the string to.
integerThe integer to convert to a string.

◆ pm_integer_free()

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.

Parameters
integerThe integer to free.

This memory will only be allocated if the integer exceeds the size of a single uint32_t.