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 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
|
#include "double-conversion.h"
#include "hs-double-conversion.h"
#include <stdio.h>
using namespace double_conversion;
static const int kToShortestLength = 26;
extern "C"
int _hs_ToShortestLength(void)
{
return kToShortestLength;
}
static const int kToFixedLength =
1 + DoubleToStringConverter::kMaxFixedDigitsBeforePoint +
1 + DoubleToStringConverter::kMaxFixedDigitsAfterPoint;
extern "C"
int _hs_ToFixedLength(void)
{
return kToFixedLength;
}
static const int kToExponentialLength =
DoubleToStringConverter::kMaxExponentialDigits + 8;
extern "C"
int _hs_ToExponentialLength(void)
{
return kToExponentialLength;
}
static const int kToPrecisionLength =
DoubleToStringConverter::kMaxPrecisionDigits + 7;
extern "C"
int _hs_ToPrecisionLength(void)
{
return kToPrecisionLength;
}
static int copy(uint16_t *buf, const StringBuilder& builder, const char *cbuf)
{
const int pos = builder.position();
for (int i = 0; i < pos; i++)
buf[i] = cbuf[i];
return pos;
}
static int copy(uint16_t *buf, const char *cbuf, const int len)
{
for (int i = 0; i < len; i++)
buf[i] = cbuf[i];
return len;
}
static inline const DoubleToStringConverter& defaultConverter(void)
{
const int flags = DoubleToStringConverter::UNIQUE_ZERO;
static DoubleToStringConverter converter(flags,
"Infinity",
"NaN",
'e',
-6, 21,
6, 0);
return converter;
}
static inline const DoubleToStringConverter& floatConverter(void)
{
const int flags = DoubleToStringConverter::UNIQUE_ZERO;
static DoubleToStringConverter converter(flags,
"Infinity",
"NaN",
'e',
-6, 6,
6, 0);
return converter;
}
extern "C"
int _hs_ToShortest(double value, char *buf)
{
StringBuilder builder(buf, kToShortestLength);
return defaultConverter().ToShortest(value, &builder)
? builder.position() : -1;
}
extern "C"
int _hs_ToShortestFloat(float value, char *buf)
{
StringBuilder builder(buf, kToShortestLength);
return floatConverter().ToShortestSingle(value, &builder)
? builder.position() : -1;
}
extern "C"
int _hs_Text_ToShortest(double value, uint16_t *buf)
{
char cbuf[kToShortestLength];
return copy(buf, cbuf, _hs_ToShortest(value, cbuf));
}
extern "C"
int _hs_Text_ToShortestFloat(float value, uint16_t *buf)
{
char cbuf[kToShortestLength];
return copy(buf, cbuf, _hs_ToShortestFloat(value, cbuf));
}
extern "C"
int _hs_ToFixed(double value, char *buf, const int ndigits)
{
StringBuilder builder(buf, kToFixedLength);
return defaultConverter().ToFixed(value, ndigits, &builder)
? builder.position() : -1;
}
int _hs_ToFixedFloat(float value, char *buf, const int ndigits)
{
StringBuilder builder(buf, kToFixedLength);
return floatConverter().ToFixed(value, ndigits, &builder)
? builder.position() : -1;
}
extern "C"
int _hs_Text_ToFixed(double value, uint16_t *buf, const int ndigits)
{
char cbuf[kToFixedLength];
return copy(buf, cbuf, _hs_ToFixed(value, cbuf, ndigits));
}
extern "C"
int _hs_Text_ToFixedFloat(float value, uint16_t *buf, const int ndigits)
{
char cbuf[kToFixedLength];
return copy(buf, cbuf, _hs_ToFixedFloat(value, cbuf, ndigits));
}
extern "C"
int _hs_ToExponential(double value, char *buf, const int ndigits)
{
StringBuilder builder(buf, kToExponentialLength);
return defaultConverter().ToExponential(value, ndigits, &builder)
? builder.position() : -1;
}
extern "C"
int _hs_ToExponentialFloat(float value, char *buf, const int ndigits)
{
StringBuilder builder(buf, kToExponentialLength);
return floatConverter().ToExponential(value, ndigits, &builder)
? builder.position() : -1;
}
extern "C"
int _hs_Text_ToExponential(double value, uint16_t *buf, const int ndigits)
{
char cbuf[kToExponentialLength];
return copy(buf, cbuf, _hs_ToExponential(value, cbuf, ndigits));
}
extern "C"
int _hs_Text_ToExponentialFloat(float value, uint16_t *buf, const int ndigits)
{
char cbuf[kToExponentialLength];
return copy(buf, cbuf, _hs_ToExponentialFloat(value, cbuf, ndigits));
}
extern "C"
int _hs_ToPrecision(double value, char *buf, const int precision)
{
StringBuilder builder(buf, kToPrecisionLength);
return defaultConverter().ToPrecision(value, precision, &builder)
? builder.position() : -1;
}
extern "C"
int _hs_ToPrecisionFloat(float value, char *buf, const int precision)
{
StringBuilder builder(buf, kToPrecisionLength);
return floatConverter().ToPrecision(value, precision, &builder)
? builder.position() : -1;
}
extern "C"
int _hs_Text_ToPrecision(double value, uint16_t *buf, const int precision)
{
char cbuf[kToPrecisionLength];
return copy(buf, cbuf, _hs_ToPrecision(value, cbuf, precision));
}
extern "C"
int _hs_Text_ToPrecisionFloat(float value, uint16_t *buf, const int precision)
{
char cbuf[kToPrecisionLength];
return copy(buf, cbuf, _hs_ToPrecisionFloat(value, cbuf, precision));
}
|