Import Debian changes 20180207-1
[hcoop/debian/mlton.git] / runtime / util / to-string.c
CommitLineData
7f918cf1
CE
1/* Copyright (C) 2012,2013 Matthew Fluet.
2 * Copyright (C) 2004-2008 Henry Cejtin, Matthew Fluet, Suresh
3 * Jagannathan, and Stephen Weeks.
4 *
5 * MLton is released under a BSD-style license.
6 * See the file MLton-LICENSE for details.
7 */
8
9#include "util.h"
10
11const char* boolToString (bool b) {
12 return b ? "TRUE" : "FALSE";
13}
14
15#define BUF_SIZE 64
16char* intmaxToCommaString (intmax_t n) {
17 static char buf1[BUF_SIZE];
18 static char buf2[BUF_SIZE];
19 static char buf3[BUF_SIZE];
20 static char buf4[BUF_SIZE];
21 static char buf5[BUF_SIZE];
22 static char *bufs[] = {buf1, buf2, buf3, buf4, buf5};
23 static int bufIndex = 0;
24 static char *buf;
25 char tmp[BUF_SIZE];
26 int i, j, k, l;
27
28 buf = bufs[bufIndex++];
29 bufIndex %= 5;
30
31 l = snprintf(tmp, BUF_SIZE, "%"PRIdMAX, n);
32 if (tmp[0] == '-') {
33 buf[0] = '-';
34 i = 1;
35 j = 1;
36 k = (l - 1) % 3;
37 } else {
38 i = 0;
39 j = 0;
40 k = l % 3;
41 }
42 if (k == 0) {
43 k = 3;
44 }
45 buf[j++] = tmp[i++];
46 k--;
47 while (tmp[i] != '\000') {
48 if (k == 0) {
49 buf[j++] = ',';
50 k = 3;
51 }
52 buf[j++] = tmp[i++];
53 k--;
54 }
55 buf[j] = '\000';
56
57 return buf;
58}
59
60char* uintmaxToCommaString (uintmax_t n) {
61 static char buf1[BUF_SIZE];
62 static char buf2[BUF_SIZE];
63 static char buf3[BUF_SIZE];
64 static char buf4[BUF_SIZE];
65 static char buf5[BUF_SIZE];
66 static char *bufs[] = {buf1, buf2, buf3, buf4, buf5};
67 static int bufIndex = 0;
68 static char *buf;
69 char tmp[BUF_SIZE];
70 int i, j, k, l;
71
72 buf = bufs[bufIndex++];
73 bufIndex %= 5;
74
75 l = snprintf(tmp, BUF_SIZE, "%"PRIuMAX, n);
76 i = 0;
77 j = 0;
78 k = l % 3;
79 if (k == 0) {
80 k = 3;
81 }
82 buf[j++] = tmp[i++];
83 k--;
84 while (tmp[i] != '\000') {
85 if (k == 0) {
86 buf[j++] = ',';
87 k = 3;
88 }
89 buf[j++] = tmp[i++];
90 k--;
91 }
92 buf[j] = '\000';
93
94 return buf;
95}
96#undef BUF_SIZE