Adobe Source Libraries
2.0.0
A collection of C++ libraries.
Loading...
Searching...
No Matches
to_string.hpp
Go to the documentation of this file.
1
/*
2
Copyright 2012 Adobe Systems Incorporated
3
Distributed under the Boost Software License - Version 1.0 (see the accompanying file LICENSE
4
or a copy at https://stlab.github.io/adobe_source_libraries/licenses.html)
5
*/
6
7
/**************************************************************************************************/
8
9
#ifndef ADOBE_STRING_TO_STRING_HPP
10
#define ADOBE_STRING_TO_STRING_HPP
11
12
#include <algorithm>
13
#include <cfloat>
14
#include <cstdio>
15
#include <string>
16
17
#include <
adobe/cassert.hpp
>
18
19
/**************************************************************************************************/
20
21
namespace
adobe
{
22
23
/**************************************************************************************************/
24
84
85
template
<
typename
O>
// O models output iterator accepting type char
86
O
to_string
(
double
x, O out,
bool
precise =
false
) {
87
ADOBE_ASSERT
((-DBL_MAX <= x && x <= DBL_MAX) &&
88
"WARNING (sparent) : to_string() only supports finite values."
);
89
90
/*
91
longest possible string result is:
92
1 "-"
93
1 digit (will not be 0)
94
1 "."
95
16 digits (precise, 14 digits otherwise).
96
2 "e+" or "e-"
97
3 digit exponent
98
1 null
99
-------------------
100
= 25 characters
101
Use 32 as the next power-of-two for some safety with no overhead.
102
*/
103
char
buf[32];
104
105
#if defined(_MSC_VER)
106
int
size
= _snprintf_s(&buf[0],
sizeof
(buf),
sizeof
(buf),
"%.*g"
, precise ? 17 : 15, x);
107
#else
108
int
size
= std::snprintf(&buf[0],
sizeof
(buf),
"%.*g"
, precise ? 17 : 15, x);
109
ADOBE_ASSERT
(
size
<
static_cast<
int
>
(
sizeof
(buf)) &&
"WARNING (sparent) : snprintf truncated."
);
110
#endif
111
ADOBE_ASSERT
(
size
< (precise ? 25 : 23) &&
112
"WARNING (sparent) : to_string() result larger than expected."
);
113
ADOBE_ASSERT
(0 <
size
&&
"FATAL (sparent) : snprintf failed in to_string()."
);
114
115
return
std::copy(&buf[0], &buf[0] +
size
, out);
116
}
117
118
/**************************************************************************************************/
151
std::string
to_string
(
double
x);
152
153
/**************************************************************************************************/
154
155
}
// namespace adobe
156
157
/**************************************************************************************************/
158
159
#endif
160
161
/**************************************************************************************************/
cassert.hpp
ADOBE_ASSERT
#define ADOBE_ASSERT(p)
Definition
cassert.hpp:32
adobe::size
boost::range_size< Selection >::type size(const Selection &x)
Definition
selection_algorithms.hpp:374
adobe::to_string
O to_string(double x, O out, bool precise=false)
Convert double precision floating point numbers to ascii representation.
Definition
to_string.hpp:86
adobe
Definition
class_template.hpp:7
adobe
string
to_string.hpp
Generated by
1.14.0