| 1 |
/* melder_ftoa.cpp |
| 2 |
* |
| 3 |
* Copyright (C) 1992-2008,2010-2012,2014-2026 Paul Boersma |
| 4 |
* |
| 5 |
* This code is free software; you can redistribute it and/or modify |
| 6 |
* it under the terms of the GNU General Public License as published by |
| 7 |
* the Free Software Foundation; either version 3 of the License, or (at |
| 8 |
* your option) any later version. |
| 9 |
* |
| 10 |
* This code is distributed in the hope that it will be useful, but |
| 11 |
* WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 13 |
* See the GNU General Public License for more details. |
| 14 |
* |
| 15 |
* You should have received a copy of the GNU General Public License |
| 16 |
* along with this work. If not, see <http://www.gnu.org/licenses/>. |
| 17 |
*/ |
| 18 |
|
| 19 |
#include "melder.h" |
| 20 |
|
| 21 |
/********** NUMBER TO STRING CONVERSION **********/ |
| 22 |
|
| 23 |
inline constexpr int NUMBER_OF_BUFFERS = 32; // = maximum number of arguments to a function call |
| 24 |
inline constexpr int extraRoom = 2; |
| 25 |
inline constexpr int MAXIMUM_NUMERIC_STRING_LENGTH = 1200; // = sign + 324 zeroes + point + 60 precision + 6 "·10^^^" + sign + 3, times 3, + i + null byte, + 10 extra |
| 26 |
|
| 27 |
static char buffers8 [NUMBER_OF_BUFFERS] [MAXIMUM_NUMERIC_STRING_LENGTH + 1]; |
| 28 |
static char32 buffers32 [NUMBER_OF_BUFFERS] [MAXIMUM_NUMERIC_STRING_LENGTH + 1]; |
| 29 |
static int ibuffer = 0; |
| 30 |
|
| 31 |
#define CONVERT_BUFFER_TO_CHAR32 \ |
| 32 |
char32 *q = buffers32 [ibuffer]; \ |
| 33 |
while (*p != '\0') \ |
| 34 |
* q ++ = (char32) (char8) * p ++; /* change signedness before extending (should be unnecessary, because all characters should be below 128) */ \ |
| 35 |
*q = U'\0'; \ |
| 36 |
return buffers32 [ibuffer]; |
| 37 |
|
| 38 |
conststring8 Melder8_integer (int64 value) { |
| 39 |
if (++ ibuffer == NUMBER_OF_BUFFERS) |
| 40 |
ibuffer = 0; |
| 41 |
if (sizeof (long_not_integer) == 8) { |
| 42 |
const int n = snprintf (buffers8 [ibuffer],MAXIMUM_NUMERIC_STRING_LENGTH+1, "%ld", (long_not_integer) value); // cast to identical type, to make compiler happy |
| 43 |
Melder_assert (n > 0); |
| 44 |
Melder_assert (n <= MAXIMUM_NUMERIC_STRING_LENGTH); |
| 45 |
} else if (sizeof (long long) == 8) { |
| 46 |
/* |
| 47 |
There are buggy platforms (namely 32-bit Mingw on Windows XP) that support long long and %lld but that convert |
| 48 |
the argument to a 32-bit long. |
| 49 |
There are also buggy platforms (namely 32-bit gcc on Linux) that support long long and %I64d but that convert |
| 50 |
the argument to a 32-bit long. |
| 51 |
*/ |
| 52 |
static const char *formatString = nullptr; |
| 53 |
if (! formatString) { |
| 54 |
char tryBuffer [MAXIMUM_NUMERIC_STRING_LENGTH + 1]; |
| 55 |
formatString = "%lld"; |
| 56 |
snprintf (tryBuffer,MAXIMUM_NUMERIC_STRING_LENGTH+1, formatString, 1000000000000LL); |
| 57 |
if (! strequ (tryBuffer, "1000000000000")) { |
| 58 |
formatString = "%I64d"; |
| 59 |
snprintf (tryBuffer,MAXIMUM_NUMERIC_STRING_LENGTH+1, formatString, 1000000000000LL); |
| 60 |
if (! strequ (tryBuffer, "1000000000000")) |
| 61 |
Melder_crash (U"Found no way to print 64-bit integers on this machine."); |
| 62 |
} |
| 63 |
} |
| 64 |
const int n = snprintf (buffers8 [ibuffer],MAXIMUM_NUMERIC_STRING_LENGTH+1, formatString, value); |
| 65 |
Melder_assert (n > 0); |
| 66 |
Melder_assert (n <= MAXIMUM_NUMERIC_STRING_LENGTH); |
| 67 |
} else { |
| 68 |
Melder_crash (U"Neither long nor long long is 8 bytes on this machine."); |
| 69 |
} |
| 70 |
return buffers8 [ibuffer]; |
| 71 |
} |
| 72 |
conststring32 Melder_integer (int64 value) { |
| 73 |
conststring8 p = Melder8_integer (value); |
| 74 |
CONVERT_BUFFER_TO_CHAR32 |
| 75 |
} |
| 76 |
|
| 77 |
const char * Melder8_bigInteger (int64 value) { |
| 78 |
if (value == INT64_MIN) |
| 79 |
return "-9,223,372,036,854,775,808"; |
| 80 |
if (++ ibuffer == NUMBER_OF_BUFFERS) |
| 81 |
ibuffer = 0; |
| 82 |
static_assert (MAXIMUM_NUMERIC_STRING_LENGTH > 200); // so that we can be sure that even the longest int64 will fit |
| 83 |
char *pout = buffers8 [ibuffer]; |
| 84 |
pout [0] = '\0'; |
| 85 |
if (value < 0) { |
| 86 |
pout += snprintf (pout,100, "-"); // "100" for performance and simplicity; the onus is upon us to have this algorithm right |
| 87 |
value = - value; |
| 88 |
} |
| 89 |
char * const startOfDigits = pout; |
| 90 |
const int quintillions = int (value / 1'000'000'000'000'000'000LL); |
| 91 |
value -= quintillions * 1'000'000'000'000'000'000LL; |
| 92 |
const int quadrillions = int (value / 1'000'000'000'000'000LL); |
| 93 |
value -= quadrillions * 1'000'000'000'000'000LL; |
| 94 |
const int trillions = int (value / 1'000'000'000'000LL); |
| 95 |
value -= trillions * 1'000'000'000'000LL; |
| 96 |
const int billions = int (value / 1'000'000'000LL); // cast to silence warning; the onus is upon us to have this algorithm right |
| 97 |
value -= billions * 1'000'000'000LL; |
| 98 |
const int millions = int (value / 1'000'000LL); |
| 99 |
value -= millions * 1'000'000LL; |
| 100 |
const int thousands = int (value / 1'000LL); |
| 101 |
value -= thousands * 1'000LL; |
| 102 |
const int units = int (value); |
| 103 |
if (quintillions) |
| 104 |
pout += snprintf (pout,100, "%d,", quintillions); // "100" for performance and simplicity; the onus is upon us to have this algorithm right |
| 105 |
if (quadrillions || pout > startOfDigits) |
| 106 |
pout += snprintf (pout,100, pout > startOfDigits ? "%03d," : "%d,", quadrillions); |
| 107 |
if (trillions || pout > startOfDigits) |
| 108 |
pout += snprintf (pout,100, pout > startOfDigits ? "%03d," : "%d,", trillions); |
| 109 |
if (billions || pout > startOfDigits) |
| 110 |
pout += snprintf (pout,100, pout > startOfDigits ? "%03d," : "%d,", billions); |
| 111 |
if (millions || pout > startOfDigits) |
| 112 |
pout += snprintf (pout,100, pout > startOfDigits ? "%03d," : "%d,", millions); |
| 113 |
if (thousands || pout > startOfDigits) |
| 114 |
pout += snprintf (pout,100, pout > startOfDigits ? "%03d," : "%d,", thousands); |
| 115 |
pout += snprintf (pout,100, pout > startOfDigits ? "%03d" : "%d", units); |
| 116 |
Melder_post (pout - buffers8 [ibuffer] <= 27); // negative quintillions can go up to 27 characters |
| 117 |
return buffers8 [ibuffer]; |
| 118 |
} |
| 119 |
conststring32 Melder_bigInteger (int64 value) { |
| 120 |
conststring8 p = Melder8_bigInteger (value); |
| 121 |
CONVERT_BUFFER_TO_CHAR32 |
| 122 |
} |
| 123 |
|
| 124 |
const char * Melder8_boolean (bool value) { |
| 125 |
return value ? "yes" : "no"; |
| 126 |
} |
| 127 |
conststring32 Melder_boolean (bool value) { |
| 128 |
return value ? U"yes" : U"no"; |
| 129 |
} |
| 130 |
|
| 131 |
const char * Melder8_onoff (bool value) { |
| 132 |
return value ? "on" : "off"; |
| 133 |
} |
| 134 |
conststring32 Melder_onoff (bool value) { |
| 135 |
return value ? U"on" : U"off"; |
| 136 |
} |
| 137 |
|
| 138 |
const char * Melder8_kleenean (kleenean valueK) { |
| 139 |
return valueK ? "yes" : ! valueK ? "no": "unknown"; |
| 140 |
} |
| 141 |
conststring32 Melder_kleenean (kleenean valueK) { |
| 142 |
return valueK ? U"yes" : ! valueK ? U"no": U"unknown"; |
| 143 |
} |
| 144 |
|
| 145 |
inline static int common_snprintf_double (const mutablestring8 buffer, const int bufferSize, const double value) { |
| 146 |
Melder_pre (bufferSize > 0); // make sure that it can be cast to size_t |
| 147 |
/* mutable adjust */ int numberOfPrintedCharacters = snprintf (buffer, size_t (bufferSize), "%.15g", value); // guarded cast |
| 148 |
if (strtod (buffer, nullptr) != value) { |
| 149 |
numberOfPrintedCharacters = snprintf (buffer, size_t (bufferSize), "%.16g", value); // guarded cast |
| 150 |
if (strtod (buffer, nullptr) != value) |
| 151 |
numberOfPrintedCharacters = snprintf (buffer, size_t (bufferSize), "%.17g", value); // guarded cast |
| 152 |
} |
| 153 |
Melder_post (numberOfPrintedCharacters > 0); |
| 154 |
Melder_post (numberOfPrintedCharacters < bufferSize); // truncation is a fatal error |
| 155 |
return numberOfPrintedCharacters; |
| 156 |
} |
| 157 |
inline static int common_snprintf_single (const mutablestring8 buffer, const int bufferSize, const double value) { |
| 158 |
Melder_pre (bufferSize > 0); // make sure that it can be cast to size_t |
| 159 |
const int numberOfPrintedCharacters = snprintf (buffer, size_t (bufferSize), "%.9g", value); // guarded cast |
| 160 |
Melder_post (numberOfPrintedCharacters > 0); |
| 161 |
Melder_post (numberOfPrintedCharacters < bufferSize); // truncation is a fatal error |
| 162 |
return numberOfPrintedCharacters; |
| 163 |
} |
| 164 |
inline static int common_snprintf_half (const mutablestring8 buffer, const int bufferSize, const double value) { |
| 165 |
Melder_pre (bufferSize > 0); // make sure that it can be cast to size_t |
| 166 |
const int numberOfPrintedCharacters = snprintf (buffer, size_t (bufferSize), "%.4g", value); // guarded cast |
| 167 |
Melder_post (numberOfPrintedCharacters > 0); |
| 168 |
Melder_post (numberOfPrintedCharacters < bufferSize); // truncation is a fatal error |
| 169 |
return numberOfPrintedCharacters; |
| 170 |
} |
| 171 |
|
| 172 |
/*@praat |
| 173 |
assert string$ (1000000000000) = "1000000000000" |
| 174 |
assert string$ (undefined) = "--undefined--" |
| 175 |
@*/ |
| 176 |
const char * Melder8_double (double value) { |
| 177 |
if (++ ibuffer == NUMBER_OF_BUFFERS) // should come before any return! |
| 178 |
ibuffer = 0; |
| 179 |
if (isundef (value)) |
| 180 |
return "--undefined--"; |
| 181 |
common_snprintf_double (buffers8 [ibuffer], MAXIMUM_NUMERIC_STRING_LENGTH+1, value); |
| 182 |
return buffers8 [ibuffer]; |
| 183 |
} |
| 184 |
conststring32 Melder_double (double value) { |
| 185 |
conststring8 p = Melder8_double (value); |
| 186 |
CONVERT_BUFFER_TO_CHAR32 |
| 187 |
} |
| 188 |
|
| 189 |
const char * Melder8_double_overtlyReal (double value) { |
| 190 |
if (++ ibuffer == NUMBER_OF_BUFFERS) // should come before any return! |
| 191 |
ibuffer = 0; |
| 192 |
if (isundef (value)) |
| 193 |
return "--undefined--"; |
| 194 |
constexpr int roomForDotZero = 2; |
| 195 |
common_snprintf_double (buffers8 [ibuffer], MAXIMUM_NUMERIC_STRING_LENGTH+1 - roomForDotZero, value); |
| 196 |
if (! strchr (buffers8 [ibuffer], '.') && ! strchr (buffers8 [ibuffer], 'e') && ! strchr (buffers8 [ibuffer], 'E')) |
| 197 |
strcat (buffers8 [ibuffer], ".0"); // dot zero |
| 198 |
return buffers8 [ibuffer]; |
| 199 |
} |
| 200 |
conststring32 Melder_double_overtlyReal (double value) { |
| 201 |
conststring8 p = Melder8_double_overtlyReal (value); |
| 202 |
CONVERT_BUFFER_TO_CHAR32 |
| 203 |
} |
| 204 |
|
| 205 |
const char * Melder8_single (double value) { |
| 206 |
if (++ ibuffer == NUMBER_OF_BUFFERS) // should come before any return! |
| 207 |
ibuffer = 0; |
| 208 |
if (isundef (value)) |
| 209 |
return "--undefined--"; |
| 210 |
common_snprintf_single (buffers8 [ibuffer], MAXIMUM_NUMERIC_STRING_LENGTH+1, value); |
| 211 |
return buffers8 [ibuffer]; |
| 212 |
} |
| 213 |
conststring32 Melder_single (double value) { |
| 214 |
conststring8 p = Melder8_single (value); |
| 215 |
CONVERT_BUFFER_TO_CHAR32 |
| 216 |
} |
| 217 |
|
| 218 |
const char * Melder8_half (double value) { |
| 219 |
if (++ ibuffer == NUMBER_OF_BUFFERS) // should come before any return! |
| 220 |
ibuffer = 0; |
| 221 |
if (isundef (value)) |
| 222 |
return "--undefined--"; |
| 223 |
common_snprintf_half (buffers8 [ibuffer], MAXIMUM_NUMERIC_STRING_LENGTH+1, value); |
| 224 |
return buffers8 [ibuffer]; |
| 225 |
} |
| 226 |
conststring32 Melder_half (double value) { |
| 227 |
conststring8 p = Melder8_half (value); |
| 228 |
CONVERT_BUFFER_TO_CHAR32 |
| 229 |
} |
| 230 |
|
| 231 |
const char * Melder8_fixed (double value, integer precision) { |
| 232 |
if (++ ibuffer == NUMBER_OF_BUFFERS) // should come before any return! |
| 233 |
ibuffer = 0; |
| 234 |
if (value == 0.0) |
| 235 |
return "0"; |
| 236 |
if (isundef (value)) |
| 237 |
return "--undefined--"; |
| 238 |
Melder_clipRight (& precision, 60_integer); |
| 239 |
const int minimumPrecision = - (int) floor (log10 (fabs (value))); |
| 240 |
const int numberOfPrintedCharacters = snprintf ( |
| 241 |
buffers8 [ibuffer], MAXIMUM_NUMERIC_STRING_LENGTH+1, |
| 242 |
"%.*f", |
| 243 |
(int) std::max (minimumPrecision, |
| 244 |
(int) precision), // guarded cast |
| 245 |
value |
| 246 |
); |
| 247 |
Melder_post (numberOfPrintedCharacters > 0); |
| 248 |
Melder_post (numberOfPrintedCharacters < MAXIMUM_NUMERIC_STRING_LENGTH+1); |
| 249 |
return buffers8 [ibuffer]; |
| 250 |
} |
| 251 |
conststring32 Melder_fixed (double value, integer precision) { |
| 252 |
conststring8 p = Melder8_fixed (value, precision); |
| 253 |
CONVERT_BUFFER_TO_CHAR32 |
| 254 |
} |
| 255 |
|
| 256 |
const char * Melder8_fixedExponent (double value, integer exponent, integer precision) { |
| 257 |
if (++ ibuffer == NUMBER_OF_BUFFERS) // should come before any return! |
| 258 |
ibuffer = 0; |
| 259 |
if (value == 0.0) |
| 260 |
return "0"; |
| 261 |
if (isundef (value)) |
| 262 |
return "--undefined--"; |
| 263 |
Melder_clipRight (& precision, 60_integer); |
| 264 |
const double factor = pow (10.0, exponent); |
| 265 |
if (factor == 0.0) |
| 266 |
return "--undefined--"; |
| 267 |
if (isundef (factor)) |
| 268 |
return "--undefined--"; |
| 269 |
value /= factor; |
| 270 |
const int minimumPrecision = - (int) floor (log10 (fabs (value))); |
| 271 |
const int numberOfPrintedCharacters = snprintf ( |
| 272 |
buffers8 [ibuffer], MAXIMUM_NUMERIC_STRING_LENGTH+1, |
| 273 |
"%.*fE%d", |
| 274 |
(int) std::max (minimumPrecision, |
| 275 |
(int) precision), // guarded cast |
| 276 |
value, |
| 277 |
(int) exponent |
| 278 |
); |
| 279 |
Melder_post (numberOfPrintedCharacters > 0); |
| 280 |
Melder_post (numberOfPrintedCharacters < MAXIMUM_NUMERIC_STRING_LENGTH+1); |
| 281 |
return buffers8 [ibuffer]; |
| 282 |
} |
| 283 |
conststring32 Melder_fixedExponent (double value, integer exponent, integer precision) { |
| 284 |
conststring8 p = Melder8_fixedExponent (value, exponent, precision); |
| 285 |
CONVERT_BUFFER_TO_CHAR32 |
| 286 |
} |
| 287 |
|
| 288 |
static const char * common_Melder8_percent (bool graphical, double value, integer precision) { |
| 289 |
if (++ ibuffer == NUMBER_OF_BUFFERS) // should come before any return! |
| 290 |
ibuffer = 0; |
| 291 |
if (value == 0.0) |
| 292 |
return "0"; |
| 293 |
value *= 100.0; |
| 294 |
if (isundef (value)) |
| 295 |
return "--undefined--"; |
| 296 |
Melder_clipRight (& precision, 60_integer); |
| 297 |
const int minimumPrecision = - (int) floor (log10 (fabs (value))); |
| 298 |
const int numberOfPrintedCharacters = snprintf ( |
| 299 |
buffers8 [ibuffer], MAXIMUM_NUMERIC_STRING_LENGTH+1, |
| 300 |
graphical ? "%.*f\\%% " : "%.*f%%", |
| 301 |
(int) std::max (minimumPrecision, |
| 302 |
(int) precision), // guarded cast |
| 303 |
value |
| 304 |
); |
| 305 |
Melder_post (numberOfPrintedCharacters > 0); |
| 306 |
Melder_post (numberOfPrintedCharacters < MAXIMUM_NUMERIC_STRING_LENGTH+1); |
| 307 |
return buffers8 [ibuffer]; |
| 308 |
} |
| 309 |
conststring8 Melder8_percent (double value, integer precision) { |
| 310 |
return common_Melder8_percent (false, value, precision); |
| 311 |
} |
| 312 |
conststring32 Melder_percent (double value, integer precision) { |
| 313 |
conststring8 p = Melder8_percent (value, precision); |
| 314 |
CONVERT_BUFFER_TO_CHAR32 |
| 315 |
} |
| 316 |
conststring8 Melder8_graphicalPercent (double value, integer precision) { |
| 317 |
return common_Melder8_percent (true, value, precision); |
| 318 |
} |
| 319 |
conststring32 Melder_graphicalPercent (double value, integer precision) { |
| 320 |
conststring8 p = Melder8_graphicalPercent (value, precision); |
| 321 |
CONVERT_BUFFER_TO_CHAR32 |
| 322 |
} |
| 323 |
|
| 324 |
conststring8 Melder8_hexadecimal (integer value, integer precision) { |
| 325 |
if (++ ibuffer == NUMBER_OF_BUFFERS) // should come before any return! |
| 326 |
ibuffer = 0; |
| 327 |
if (value < 0) |
| 328 |
return "--undefined--"; |
| 329 |
Melder_clipRight (& precision, 60_integer); |
| 330 |
const integer integerValue = Melder_iround (value); |
| 331 |
const int numberOfPrintedCharacters = snprintf ( |
| 332 |
buffers8 [ibuffer], MAXIMUM_NUMERIC_STRING_LENGTH+1, |
| 333 |
"%.*llX", |
| 334 |
(int) precision, // guarded cast |
| 335 |
(unsigned long long) integerValue |
| 336 |
); |
| 337 |
Melder_post (numberOfPrintedCharacters > 0); |
| 338 |
Melder_post (numberOfPrintedCharacters < MAXIMUM_NUMERIC_STRING_LENGTH+1); |
| 339 |
return buffers8 [ibuffer]; |
| 340 |
} |
| 341 |
conststring32 Melder_hexadecimal (integer value, integer precision) { |
| 342 |
conststring8 p = Melder8_hexadecimal (value, precision); |
| 343 |
CONVERT_BUFFER_TO_CHAR32 |
| 344 |
} |
| 345 |
|
| 346 |
conststring8 Melder8_dcomplex (dcomplex value) { |
| 347 |
if (++ ibuffer == NUMBER_OF_BUFFERS) // should come before any return! |
| 348 |
ibuffer = 0; |
| 349 |
if (isundef (value.real()) || isundef (value.imag())) |
| 350 |
return "--undefined--"; |
| 351 |
constexpr int roomForAdditionOrSubtractionSymbol = 1; |
| 352 |
constexpr int roomForISymbol = 1; |
| 353 |
constexpr int totalRoom = roomForAdditionOrSubtractionSymbol + roomForISymbol; |
| 354 |
static_assert (totalRoom <= extraRoom); |
| 355 |
const int numberOfPrintedCharacters = common_snprintf_double (buffers8 [ibuffer], MAXIMUM_NUMERIC_STRING_LENGTH/2+1 - roomForAdditionOrSubtractionSymbol, value.real()); |
| 356 |
char *p = buffers8 [ibuffer] + numberOfPrintedCharacters; |
| 357 |
* (p ++) = ( value.imag() < 0.0 ? '-' : '+' ); |
| 358 |
common_snprintf_double (p, MAXIMUM_NUMERIC_STRING_LENGTH/2+1 - roomForISymbol, fabs (value.imag())); |
| 359 |
strcat (buffers8 [ibuffer], "i"); |
| 360 |
return buffers8 [ibuffer]; |
| 361 |
} |
| 362 |
conststring32 Melder_dcomplex (dcomplex value) { |
| 363 |
const char *p = Melder8_dcomplex (value); |
| 364 |
CONVERT_BUFFER_TO_CHAR32 |
| 365 |
} |
| 366 |
|
| 367 |
conststring8 Melder8_scomplex (dcomplex value) { |
| 368 |
if (++ ibuffer == NUMBER_OF_BUFFERS) // should come before any return! |
| 369 |
ibuffer = 0; |
| 370 |
if (isundef (value.real()) || isundef (value.imag())) |
| 371 |
return "--undefined--"; |
| 372 |
constexpr int roomForAdditionOrSubtractionSymbol = 1; |
| 373 |
constexpr int roomForISymbol = 1; |
| 374 |
constexpr int totalRoom = roomForAdditionOrSubtractionSymbol + roomForISymbol; |
| 375 |
static_assert (totalRoom <= extraRoom); |
| 376 |
const int numberOfPrintedCharacters = common_snprintf_single (buffers8 [ibuffer], MAXIMUM_NUMERIC_STRING_LENGTH/2+1 - roomForAdditionOrSubtractionSymbol, value.real()); |
| 377 |
char *p = buffers8 [ibuffer] + numberOfPrintedCharacters; |
| 378 |
* (p ++) = ( value.imag() < 0.0 ? '-' : '+' ); |
| 379 |
common_snprintf_single (p, MAXIMUM_NUMERIC_STRING_LENGTH/2+1 - roomForISymbol, fabs (value.imag())); |
| 380 |
strcat (buffers8 [ibuffer], "i"); |
| 381 |
return buffers8 [ibuffer]; |
| 382 |
} |
| 383 |
conststring32 Melder_scomplex (dcomplex value) { |
| 384 |
const char *p = Melder8_scomplex (value); |
| 385 |
CONVERT_BUFFER_TO_CHAR32 |
| 386 |
} |
| 387 |
|
| 388 |
conststring8 Melder8_naturalLogarithm (double lnNumber) { |
| 389 |
if (++ ibuffer == NUMBER_OF_BUFFERS) // should come before any return! |
| 390 |
ibuffer = 0; |
| 391 |
//if (lnNumber == -INFINITY) return "0"; // this would have been nice, but cannot be relied upon |
| 392 |
if (isundef (lnNumber)) |
| 393 |
return "--undefined--"; |
| 394 |
const double log10Number = lnNumber * NUMlog10e; |
| 395 |
if (log10Number < -1e15) { |
| 396 |
return "0"; |
| 397 |
} else if (log10Number < -41.0) { |
| 398 |
/* mutable adjust */ double ceiling = (integer) ceil (log10Number); |
| 399 |
const double remainder = log10Number - ceiling; |
| 400 |
double remainder10 = pow (10.0, remainder); |
| 401 |
if (remainder10 == 0.0) |
| 402 |
return "0"; |
| 403 |
while (remainder10 < 1.0) { |
| 404 |
remainder10 *= 10.0; |
| 405 |
ceiling --; |
| 406 |
} |
| 407 |
constexpr int roomForExponent = 20; |
| 408 |
const int numberOfPrintedCharacters = common_snprintf_double (buffers8 [ibuffer], MAXIMUM_NUMERIC_STRING_LENGTH+1 - roomForExponent, remainder10); |
| 409 |
snprintf (buffers8 [ibuffer] + numberOfPrintedCharacters, roomForExponent+1, "e%lld", (long long) ceiling); // BUG: this just doesn't work on Windows |
| 410 |
} else { |
| 411 |
return Melder8_double (exp (lnNumber)); |
| 412 |
} |
| 413 |
return buffers8 [ibuffer]; |
| 414 |
} |
| 415 |
conststring32 Melder_naturalLogarithm (double lnNumber) { |
| 416 |
conststring8 p = Melder8_naturalLogarithm (lnNumber); |
| 417 |
CONVERT_BUFFER_TO_CHAR32 |
| 418 |
} |
| 419 |
|
| 420 |
static conststring32 commmon_Melder_graphicalFloat (conststring32 number) { |
| 421 |
if (++ ibuffer == NUMBER_OF_BUFFERS) // should come before any return! |
| 422 |
ibuffer = 0; |
| 423 |
if (! str32chr (number, 'e')) { |
| 424 |
str32cpy (buffers32 [ibuffer], number); |
| 425 |
} else { |
| 426 |
char32 *b = buffers32 [ibuffer]; |
| 427 |
const char32 *n = & number [0]; |
| 428 |
while (*n != U'e') |
| 429 |
*(b++) = *(n++); |
| 430 |
*b = U'\0'; |
| 431 |
if (number [0] == U'1' && number [1] == U'e') { |
| 432 |
str32cpy (buffers32 [ibuffer], U"10^^"); |
| 433 |
b = buffers32 [ibuffer] + 4; |
| 434 |
} else { |
| 435 |
str32cat (buffers32 [ibuffer], U"·10^^"); |
| 436 |
b += 5; |
| 437 |
} |
| 438 |
Melder_assert (*n == U'e'); |
| 439 |
if (*++n == U'+') |
| 440 |
n ++; // ignore leading plus sign in exponent |
| 441 |
if (*n == U'-') |
| 442 |
*(b++) = *(n++); // copy sign of negative exponent |
| 443 |
while (*n == U'0') |
| 444 |
n ++; // ignore leading zeroes in exponent |
| 445 |
while (*n >= U'0' && *n <= U'9') |
| 446 |
*(b++) = *(n++); |
| 447 |
*(b++) = U'^'; |
| 448 |
while (*n != U'\0') |
| 449 |
*(b++) = *(n++); |
| 450 |
*b = U'\0'; |
| 451 |
} |
| 452 |
return buffers32 [ibuffer]; |
| 453 |
} |
| 454 |
conststring32 Melder_graphicalHalf (double number) { |
| 455 |
return commmon_Melder_graphicalFloat (Melder_half (number)); |
| 456 |
} |
| 457 |
conststring32 Melder_graphicalSingle (double number) { |
| 458 |
return commmon_Melder_graphicalFloat (Melder_single (number)); |
| 459 |
} |
| 460 |
conststring32 Melder_graphicalDouble (double number) { |
| 461 |
return commmon_Melder_graphicalFloat (Melder_double (number)); |
| 462 |
} |
| 463 |
conststring32 Melder_graphicalNaturalLogarithm (double number) { |
| 464 |
return commmon_Melder_graphicalFloat (Melder_naturalLogarithm (number)); |
| 465 |
} |
| 466 |
|
| 467 |
conststring8 Melder8_pointer (const void *pointer) { |
| 468 |
if (++ ibuffer == NUMBER_OF_BUFFERS) |
| 469 |
ibuffer = 0; |
| 470 |
snprintf (buffers8 [ibuffer],MAXIMUM_NUMERIC_STRING_LENGTH+1, "%p", pointer); |
| 471 |
return buffers8 [ibuffer]; |
| 472 |
} |
| 473 |
conststring32 Melder_pointer (const void *pointer) { |
| 474 |
conststring8 p = Melder8_pointer (pointer); |
| 475 |
CONVERT_BUFFER_TO_CHAR32 |
| 476 |
} |
| 477 |
|
| 478 |
conststring32 Melder_character (char32 kar) { |
| 479 |
if (++ ibuffer == NUMBER_OF_BUFFERS) |
| 480 |
ibuffer = 0; |
| 481 |
buffers32 [ibuffer] [0] = kar; |
| 482 |
buffers32 [ibuffer] [1] = U'\0'; |
| 483 |
return buffers32 [ibuffer]; |
| 484 |
} |
| 485 |
|
| 486 |
conststring8 Melder8_colour (MelderColour colour) { |
| 487 |
if (++ ibuffer == NUMBER_OF_BUFFERS) // should come before any return! |
| 488 |
ibuffer = 0; |
| 489 |
if (isundef (colour.red) || isundef (colour.green) || isundef (colour.blue)) |
| 490 |
return "{--undefined--,--undefined--,--undefined--}"; |
| 491 |
char *p = & buffers8 [ibuffer] [0]; |
| 492 |
constexpr int roomForBracesAndCommas = 4; |
| 493 |
constexpr int roomPerBraceOrComma = (roomForBracesAndCommas - 1) / 3 + 1; // rounded up; this reserves 6 extra characters (out of a maximum of 10) |
| 494 |
strcpy (p ++, "{"); |
| 495 |
/* mutable reuse */ int numberOfPrintedCharacters = common_snprintf_double (p, MAXIMUM_NUMERIC_STRING_LENGTH/3+1 - roomPerBraceOrComma, colour.red); |
| 496 |
p += numberOfPrintedCharacters; |
| 497 |
strcpy (p ++, ","); |
| 498 |
numberOfPrintedCharacters = common_snprintf_double (p, MAXIMUM_NUMERIC_STRING_LENGTH/3+1 - roomPerBraceOrComma, colour.green); |
| 499 |
p += numberOfPrintedCharacters; |
| 500 |
strcpy (p ++, ","); |
| 501 |
numberOfPrintedCharacters = common_snprintf_double (p, MAXIMUM_NUMERIC_STRING_LENGTH/3+1 - roomPerBraceOrComma, colour.blue); |
| 502 |
p += numberOfPrintedCharacters; |
| 503 |
strcpy (p, "}"); |
| 504 |
return buffers8 [ibuffer]; |
| 505 |
} |
| 506 |
conststring32 Melder_colour (MelderColour colour) { |
| 507 |
conststring8 p = Melder8_colour (colour); |
| 508 |
CONVERT_BUFFER_TO_CHAR32 |
| 509 |
} |
| 510 |
|
| 511 |
/********** TENSOR TO STRING CONVERSION **********/ |
| 512 |
|
| 513 |
#define NUMBER_OF_TENSOR_BUFFERS 3 |
| 514 |
static MelderString theTensorBuffers [NUMBER_OF_TENSOR_BUFFERS]; |
| 515 |
static int iTensorBuffer { 0 }; |
| 516 |
|
| 517 |
conststring32 Melder_VEC (constVECVU const& value, const bool horizontal) { |
| 518 |
if (++ iTensorBuffer == NUMBER_OF_TENSOR_BUFFERS) |
| 519 |
iTensorBuffer = 0; |
| 520 |
MelderString *string = & theTensorBuffers [iTensorBuffer]; |
| 521 |
MelderString_empty (string); |
| 522 |
if (! NUMisEmpty (value)) |
| 523 |
for (integer i = 1; i <= value.size; i ++) |
| 524 |
MelderString_append (string, value [i], horizontal ? U' ' : U'\n'); |
| 525 |
return string -> string; |
| 526 |
} |
| 527 |
conststring32 Melder_fixed (constVECVU const& value, integer precision, const bool horizontal) { |
| 528 |
if (++ iTensorBuffer == NUMBER_OF_TENSOR_BUFFERS) |
| 529 |
iTensorBuffer = 0; |
| 530 |
MelderString *string = & theTensorBuffers [iTensorBuffer]; |
| 531 |
MelderString_empty (string); |
| 532 |
if (! NUMisEmpty (value)) |
| 533 |
for (integer i = 1; i <= value.size; i ++) |
| 534 |
MelderString_append (string, Melder_fixed (value [i], precision), horizontal ? U' ' : U'\n'); |
| 535 |
return string -> string; |
| 536 |
} |
| 537 |
|
| 538 |
conststring32 Melder_MAT (constMATVU const& value) { |
| 539 |
if (++ iTensorBuffer == NUMBER_OF_TENSOR_BUFFERS) |
| 540 |
iTensorBuffer = 0; |
| 541 |
MelderString *string = & theTensorBuffers [iTensorBuffer]; |
| 542 |
MelderString_empty (string); |
| 543 |
if (! NUMisEmpty (value)) { |
| 544 |
for (integer irow = 1; irow <= value.nrow; irow ++) { |
| 545 |
for (integer icol = 1; icol <= value.ncol; icol ++) { |
| 546 |
MelderString_append (string, value [irow] [icol]); |
| 547 |
if (icol < value.ncol) |
| 548 |
MelderString_appendCharacter (string, U' '); |
| 549 |
} |
| 550 |
if (irow < value.nrow) |
| 551 |
MelderString_appendCharacter (string, U'\n'); |
| 552 |
} |
| 553 |
} |
| 554 |
return string -> string; |
| 555 |
} |
| 556 |
conststring32 Melder_fixed (constMATVU const& value, integer precision) { |
| 557 |
if (++ iTensorBuffer == NUMBER_OF_TENSOR_BUFFERS) |
| 558 |
iTensorBuffer = 0; |
| 559 |
MelderString *string = & theTensorBuffers [iTensorBuffer]; |
| 560 |
MelderString_empty (string); |
| 561 |
if (! NUMisEmpty (value)) { |
| 562 |
for (integer irow = 1; irow <= value.nrow; irow ++) { |
| 563 |
for (integer icol = 1; icol <= value.ncol; icol ++) { |
| 564 |
MelderString_append (string, Melder_fixed (value [irow] [icol], precision)); |
| 565 |
if (icol < value.ncol) |
| 566 |
MelderString_appendCharacter (string, U' '); |
| 567 |
} |
| 568 |
if (irow < value.nrow) |
| 569 |
MelderString_appendCharacter (string, U'\n'); |
| 570 |
} |
| 571 |
} |
| 572 |
return string -> string; |
| 573 |
} |
| 574 |
|
| 575 |
conststring32 Melder_STRVEC (constSTRVEC const& value) { |
| 576 |
if (++ iTensorBuffer == NUMBER_OF_TENSOR_BUFFERS) |
| 577 |
iTensorBuffer = 0; |
| 578 |
MelderString *string = & theTensorBuffers [iTensorBuffer]; |
| 579 |
MelderString_empty (string); |
| 580 |
if (! NUMisEmpty (value)) |
| 581 |
for (integer i = 1; i <= value.size; i ++) |
| 582 |
MelderString_append (string, value [i], U'\n'); |
| 583 |
return string -> string; |
| 584 |
} |
| 585 |
|
| 586 |
/********** STRING TO STRING CONVERSION **********/ |
| 587 |
|
| 588 |
static MelderString thePadBuffers [NUMBER_OF_BUFFERS]; |
| 589 |
static int iPadBuffer { 0 }; |
| 590 |
|
| 591 |
conststring32 Melder_padLeft (const conststring32 string, const integer width, const conststring32 pad) { |
| 592 |
if (++ iPadBuffer == NUMBER_OF_BUFFERS) |
| 593 |
iPadBuffer = 0; |
| 594 |
const integer length = Melder_length (string); |
| 595 |
Melder_assert (length >= 0); |
| 596 |
if (width <= length) |
| 597 |
return string; |
| 598 |
Melder_assert (width > 0); |
| 599 |
const integer tooShort = width - length; |
| 600 |
// guarded subtraction (length cannot be negative, so no overflow, and width is positive, so no underflow) |
| 601 |
Melder_assert (tooShort > 0); |
| 602 |
const integer padLength = Melder_length (pad); |
| 603 |
Melder_require (padLength > 0, |
| 604 |
U"Empty pad string."); |
| 605 |
MelderString_empty (& thePadBuffers [iPadBuffer]); |
| 606 |
/* mutable cycle */ integer ipad = 0; |
| 607 |
for (integer i = 0; i < tooShort; i ++) { |
| 608 |
Melder_assert (ipad >= 0); |
| 609 |
Melder_assert (ipad < padLength); |
| 610 |
MelderString_appendCharacter (& thePadBuffers [iPadBuffer], pad [ipad]); |
| 611 |
if (++ ipad >= padLength) |
| 612 |
ipad = 0; |
| 613 |
} |
| 614 |
MelderString_append (& thePadBuffers [iPadBuffer], string); |
| 615 |
return thePadBuffers [iPadBuffer].string; |
| 616 |
} |
| 617 |
|
| 618 |
conststring32 Melder_padRight (const conststring32 string, const integer width, const conststring32 pad) { |
| 619 |
if (++ iPadBuffer == NUMBER_OF_BUFFERS) |
| 620 |
iPadBuffer = 0; |
| 621 |
const integer length = Melder_length (string); |
| 622 |
Melder_assert (length >= 0); |
| 623 |
if (width <= length) |
| 624 |
return string; |
| 625 |
Melder_assert (width > 0); |
| 626 |
const integer tooShort = width - length; |
| 627 |
// guarded subtraction (length cannot be negative, so no overflow, and width is positive, so no underflow) |
| 628 |
Melder_assert (tooShort > 0); |
| 629 |
const integer padLength = Melder_length (pad); |
| 630 |
Melder_require (padLength > 0, |
| 631 |
U"Empty pad string."); |
| 632 |
MelderString_copy (& thePadBuffers [iPadBuffer], string); |
| 633 |
/* mutable cycle */ integer ipad = padLength - 1 - ((tooShort - 1) % padLength); |
| 634 |
for (integer i = 0; i < tooShort; i ++) { |
| 635 |
Melder_assert (ipad >= 0); |
| 636 |
Melder_assert (ipad < padLength); |
| 637 |
MelderString_appendCharacter (& thePadBuffers [iPadBuffer], pad [ipad]); |
| 638 |
if (++ ipad >= padLength) |
| 639 |
ipad = 0; |
| 640 |
} |
| 641 |
return thePadBuffers [iPadBuffer].string; |
| 642 |
} |
| 643 |
|
| 644 |
conststring32 Melder_truncateLeft (const conststring32 string, const integer width) { |
| 645 |
if (++ iPadBuffer == NUMBER_OF_BUFFERS) |
| 646 |
iPadBuffer = 0; |
| 647 |
Melder_require (width >= 0, |
| 648 |
U"Can never truncate a string down to ", width, U" characters."); |
| 649 |
const integer length = Melder_length (string); // BUG: this can be slow if used in a loop, e.g. if 10'000'000 -> 1000 |
| 650 |
Melder_assert (length >= 0); |
| 651 |
if (length <= width) |
| 652 |
return string; |
| 653 |
const integer tooLong = length - width; |
| 654 |
// guarded subtraction (length cannot be negative, so no underflow, and width cannot be negative, so no overflow) |
| 655 |
Melder_assert (tooLong > 0); |
| 656 |
MelderString_ncopy (& thePadBuffers [iPadBuffer], string + tooLong, width); |
| 657 |
return thePadBuffers [iPadBuffer].string; |
| 658 |
} |
| 659 |
|
| 660 |
conststring32 Melder_truncateRight (const conststring32 string, const integer width) { |
| 661 |
if (++ iPadBuffer == NUMBER_OF_BUFFERS) |
| 662 |
iPadBuffer = 0; |
| 663 |
Melder_require (width >= 0, |
| 664 |
U"Can never truncate a string down to ", width, U" characters."); |
| 665 |
const integer length = Melder_length (string); // BUG: this can be slow if used in a loop, e.g. if 10'000'000 -> 1000 |
| 666 |
Melder_assert (length >= 0); |
| 667 |
if (length <= width) |
| 668 |
return string; |
| 669 |
const integer tooLong = length - width; |
| 670 |
// guarded subtraction (length cannot be negative, so no underflow, and width is nonnegative, so no overflow) |
| 671 |
Melder_assert (tooLong > 0); |
| 672 |
MelderString_ncopy (& thePadBuffers [iPadBuffer], string, width); |
| 673 |
return thePadBuffers [iPadBuffer].string; |
| 674 |
} |
| 675 |
|
| 676 |
conststring32 Melder_padOrTruncateLeft (const conststring32 string, const integer width, const conststring32 pad) { |
| 677 |
if (++ iPadBuffer == NUMBER_OF_BUFFERS) |
| 678 |
iPadBuffer = 0; |
| 679 |
Melder_require (width >= 0, |
| 680 |
U"Can never truncate a string down to ", width, U" characters."); |
| 681 |
const integer length = Melder_length (string); |
| 682 |
Melder_assert (length >= 0); |
| 683 |
const integer tooLong = length - width; |
| 684 |
// guarded subtraction (length cannot be negative, so no underflow or INTEGER_MIN, and width cannot be negative, so no overflow) |
| 685 |
if (tooLong == 0) |
| 686 |
return string; |
| 687 |
if (tooLong < 0) { |
| 688 |
Melder_assert (tooLong > INTEGER_MIN); |
| 689 |
const integer tooShort = - tooLong; // guarded integer sign flip |
| 690 |
Melder_assert (tooShort > 0); |
| 691 |
const integer padLength = Melder_length (pad); |
| 692 |
Melder_require (padLength > 0, |
| 693 |
U"Empty pad string."); |
| 694 |
MelderString_empty (& thePadBuffers [iPadBuffer]); |
| 695 |
/* mutable cycle */ integer ipad = 0; |
| 696 |
for (integer i = 0; i < tooShort; i ++) { |
| 697 |
Melder_assert (ipad >= 0); |
| 698 |
Melder_assert (ipad < padLength); |
| 699 |
MelderString_appendCharacter (& thePadBuffers [iPadBuffer], pad [ipad]); |
| 700 |
if (++ ipad >= padLength) |
| 701 |
ipad = 0; |
| 702 |
} |
| 703 |
MelderString_append (& thePadBuffers [iPadBuffer], string); |
| 704 |
} else { |
| 705 |
Melder_assert (tooLong <= length); |
| 706 |
MelderString_ncopy (& thePadBuffers [iPadBuffer], string + tooLong, width); |
| 707 |
} |
| 708 |
return thePadBuffers [iPadBuffer].string; |
| 709 |
} |
| 710 |
|
| 711 |
conststring32 Melder_padOrTruncateRight (conststring32 string, integer width, const conststring32 pad) { |
| 712 |
if (++ iPadBuffer == NUMBER_OF_BUFFERS) |
| 713 |
iPadBuffer = 0; |
| 714 |
Melder_require (width >= 0, |
| 715 |
U"Can never truncate a string down to ", width, U" characters."); |
| 716 |
const integer length = Melder_length (string); |
| 717 |
Melder_assert (length >= 0); |
| 718 |
const integer tooLong = length - width; |
| 719 |
// guarded subtraction (length cannot be negative, so no underflow or INTEGER_MIN, and width cannot be negative, so no overflow) |
| 720 |
if (tooLong == 0) |
| 721 |
return string; |
| 722 |
if (tooLong < 0) { |
| 723 |
Melder_assert (tooLong > INTEGER_MIN); |
| 724 |
const integer tooShort = - tooLong; // guarded integer sign flip |
| 725 |
Melder_assert (tooShort > 0); |
| 726 |
const integer padLength = Melder_length (pad); |
| 727 |
Melder_require (padLength > 0, |
| 728 |
U"Empty pad string."); |
| 729 |
MelderString_copy (& thePadBuffers [iPadBuffer], string); |
| 730 |
/* mutable cycle */ integer ipad = padLength - 1 - ((tooShort - 1) % padLength); |
| 731 |
for (integer i = 0; i < tooShort; i ++) { |
| 732 |
Melder_assert (ipad >= 0); |
| 733 |
Melder_assert (ipad < padLength); |
| 734 |
MelderString_appendCharacter (& thePadBuffers [iPadBuffer], pad [ipad]); |
| 735 |
if (++ ipad >= padLength) |
| 736 |
ipad = 0; |
| 737 |
} |
| 738 |
} else { |
| 739 |
Melder_assert (tooLong <= length); |
| 740 |
MelderString_ncopy (& thePadBuffers [iPadBuffer], string, width); |
| 741 |
} |
| 742 |
return thePadBuffers [iPadBuffer].string; |
| 743 |
} |
| 744 |
|
| 745 |
/* End of file melder_ftoa.cpp */ |