libpulsar
A modular compiler for the pulsar programming language
Loading...
Searching...
No Matches
itoa.c
Go to the documentation of this file.
1
8#include "itoa.h"
9
10#define ITOA_BUFFER_SIZE 64
12
13#define _iota_routine(v) \
14 do { \
15 while (v) { \
16 buffer[--i] = '0' + (v % 10); \
17 v /= 10; \
18 } \
19 } while (0)
20
21STR ps_itoa(int v) {
22 if (v == 0) return "0";
23
24 int i = ITOA_BUFFER_SIZE - 1;
25
26 // make positive and save sign
27 int sign = v > 0 ? 1 : -1;
28 long v2 = (long)v * sign;
29
30 _iota_routine(v2);
31
32 if (sign == -1) {
33 buffer[--i] = '-';
34 }
35
36 buffer[ITOA_BUFFER_SIZE - 1] = 0;
37 return buffer + i;
38}
39
41 if (v == 0) return "0";
42
43 int i = ITOA_BUFFER_SIZE - 1;
44
46
47 buffer[ITOA_BUFFER_SIZE - 1] = 0;
48 return buffer + i;
49}
#define STR
Definition def.h:40
#define usize
Definition def.h:50
char buffer[ITOA_BUFFER_SIZE]
Definition itoa.c:11
STR ps_ztoa(usize v)
Returns a read-only string of the digits of v.
Definition itoa.c:40
#define _iota_routine(v)
Definition itoa.c:13
STR ps_itoa(int v)
Returns a read-only string of the digits of v.
Definition itoa.c:21
#define ITOA_BUFFER_SIZE
Definition itoa.c:10
Conversion from integers to strings.