Create print module
This commit is contained in:
@@ -6,6 +6,7 @@ add_library(imp
|
||||
memory_stream.c
|
||||
parse.c
|
||||
prim.c
|
||||
print.c
|
||||
store.c
|
||||
token.c
|
||||
)
|
||||
|
||||
8
lib/include/print.h
Normal file
8
lib/include/print.h
Normal file
@@ -0,0 +1,8 @@
|
||||
#ifndef PRINT_H
|
||||
#define PRINT_H
|
||||
|
||||
#include "am.h"
|
||||
|
||||
size_t print(am_t *am, char *buffer, size_t buffer_size);
|
||||
|
||||
#endif
|
||||
26
lib/print.c
Normal file
26
lib/print.c
Normal file
@@ -0,0 +1,26 @@
|
||||
#include "print.h"
|
||||
|
||||
#include <assert.h>
|
||||
|
||||
size_t print(am_t *am, char *buffer, size_t buffer_size)
|
||||
{
|
||||
assert(am->val->is_atom);
|
||||
assert(am->val->atom.type == ATOM_TYPE_INTEGER);
|
||||
|
||||
int64_t value = am->val->atom.integer;
|
||||
int divisor = 10;
|
||||
while (divisor <= value)
|
||||
divisor *= 10;
|
||||
|
||||
size_t i = 0;
|
||||
do {
|
||||
divisor /= 10;
|
||||
const int digit = value / divisor;
|
||||
value = value % divisor;
|
||||
|
||||
assert(i < buffer_size);
|
||||
buffer[i++] = '0' + (char)digit;
|
||||
} while (divisor > 1);
|
||||
|
||||
return i;
|
||||
}
|
||||
Reference in New Issue
Block a user