1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
|
/* Copyright (C) 2004-2008 Henry Cejtin, Matthew Fluet, Suresh
* Jagannathan, and Stephen Weeks.
*
* MLton is released under a BSD-style license.
* See the file MLton-LICENSE for details.
*/
#include "util.h"
const char* boolToString (bool b) {
return b ? "TRUE" : "FALSE";
}
#define BUF_SIZE 81
char* intmaxToCommaString (intmax_t n) {
static char buf1[BUF_SIZE];
static char buf2[BUF_SIZE];
static char buf3[BUF_SIZE];
static char buf4[BUF_SIZE];
static char buf5[BUF_SIZE];
static char *bufs[] = {buf1, buf2, buf3, buf4, buf5};
static int bufIndex = 0;
static char *buf;
int i;
buf = bufs[bufIndex++];
bufIndex %= 5;
i = BUF_SIZE - 1;
buf[i--] = '\000';
if (0 == n)
buf[i--] = '0';
else if (INTMAX_MIN == n) {
const char* s;
/* must treat INTMAX_MIN specially, because I negate stuff later */
switch (sizeof(intmax_t)) {
case 1:
s = "-128";
break;
case 2:
s = "-32,768";
break;
case 4:
s = "-2,147,483,648";
break;
case 8:
s = "-9,223,372,036,854,775,808";
break;
case 16:
s = "-170,141,183,460,469,231,731,687,303,715,884,105,728";
break;
default:
die ("intmaxToCommaString: sizeof(intmax_t) = %"PRIuMAX"",
(uintmax_t)sizeof(intmax_t));
break;
}
strncpy (buf + 1, s, strlen(s) + 1);
i = 0;
} else {
intmax_t m;
if (n > 0)
m = n;
else
m = -n;
while (m > 0) {
buf[i--] = (char)((m % 10) + '0');
m = m / 10;
if (i % 4 == 0 and m > 0) buf[i--] = ',';
}
if (n < 0) buf[i--] = '-';
}
return buf + i + 1;
}
char* uintmaxToCommaString (uintmax_t n) {
static char buf1[BUF_SIZE];
static char buf2[BUF_SIZE];
static char buf3[BUF_SIZE];
static char buf4[BUF_SIZE];
static char buf5[BUF_SIZE];
static char *bufs[] = {buf1, buf2, buf3, buf4, buf5};
static int bufIndex = 0;
static char *buf;
int i;
buf = bufs[bufIndex++];
bufIndex %= 5;
i = BUF_SIZE - 1;
buf[i--] = '\000';
if (0 == n)
buf[i--] = '0';
else {
while (n > 0) {
buf[i--] = (char)((n % 10) + '0');
n = n / 10;
if (i % 4 == 0 and n > 0) buf[i--] = ',';
}
}
return buf + i + 1;
}
#undef BUF_SIZE
|