This is the mail archive of the libstdc++@gcc.gnu.org mailing list for the libstdc++ project.
| Index Nav: | [Date Index] [Subject Index] [Author Index] [Thread Index] | |
|---|---|---|
| Message Nav: | [Date Prev] [Date Next] | [Thread Prev] [Thread Next] |
| Other format: | [Raw text] | |
On Tue, 2002-03-19 at 11:24, Paolo Carlini wrote:
> // Convert numeric value of type _Tv to string and return length of string
> template<typename _Tv>
> int
> __convert_from_v(char* __out, const int __size, const char* __fmt, _Tv __v,
> const __c_locale&, int __prec = -1)
> {
> int __ret;
> const char* __old = setlocale(LC_ALL, "C");
> if (__prec >= 0)
> #ifdef _GLIBCPP_USE_C99
> __ret = snprintf(__out, __size, __fmt, __prec, __v);
> #else
> __ret = sprintf(__out, __fmt, __prec, __v);
> #endif
> else
> #ifdef _GLIBCPP_USE_C99
> __ret = snprintf(__out, __size, __fmt, __v);
> #else
> __ret = sprintf(__out, __fmt, __v);
> #endif
> setlocale(LC_ALL, __old);
> if (__size); // Only to suppress a warning unused when !C99.
> return __ret;
> }
>
> How does this fit in your reasoning line?
I think it's cleaner if you wrap the function definition:
// Convert numeric value of type _Tv to string and return length of
string
#ifdef _GLIBCPP_USE_C99
template<typename _Tv>
int
__convert_from_v(char* __out, const int __size, const char* __fmt,
_Tv __v,
const __c_locale&, int __prec = -1)
{
int __ret;
const char* __old = setlocale(LC_ALL, "C");
if (__prec >= 0)
ret = snprintf(__out, __size, __fmt, __prec, __v);
else
__ret = snprintf(__out, __size, __fmt, __v);
setlocale(LC_ALL, __old);
return ret;
}
#else
template<typename _Tv>
int
__convert_from_v(char* __out, const int, const char* __fmt, _Tv __v,
const __c_locale&, int __prec = -1)
{
int __ret;
const char* __old = setlocale(LC_ALL, "C");
if (__prec >= 0)
ret = sprintf(__out, __fmt, __prec, __v);
else
__ret = sprintf(__out, __fmt, __v);
setlocale(LC_ALL, __old);
return ret;
}
#endif
This also leave room for an explanation of the second variant and why it
is horrible if it is used.
> The other 2 cases are similar, we are actuallt able to upperbound the required size to
> allocate but it can be often quite large, with no purpose.
I'm not sure whether you tell me that there is no way that the user can
determine who much output snprintf will create. If this is the case
then yes, the code looks fine. Otherwise, if the user can determine the
output size, you have to use the alloca and new split. There is no way
around it. I had to add ugly code like that to glibc's printf
implementation since there are people doing stupid things like using a
million digit precision.
--
---------------. ,-. 1325 Chesapeake Terrace
Ulrich Drepper \ ,-------------------' \ Sunnyvale, CA 94089 USA
Red Hat `--' drepper at redhat.com `------------------------
Attachment:
signature.asc
Description: This is a digitally signed message part
| Index Nav: | [Date Index] [Subject Index] [Author Index] [Thread Index] | |
|---|---|---|
| Message Nav: | [Date Prev] [Date Next] | [Thread Prev] [Thread Next] |