FIX::DoubleConvertor Struct Reference

Converts double to/from a string. More...

`#include <FieldConvertors.h>`

## Static Public Member Functions

static std::string | convert (double value, int padding=0)
static bool | convert (const std::string &value, double &result)
static double | convert (const std::string &value) throw ( FieldConvertError )

## Static Public Attributes

static const int | SIGNIFICANT_DIGITS = 15
static const int | BUFFFER_SIZE = 32

## Static Private Member Functions

static double | fast_strtod (const char *buffer, int size, int *processed_chars)
static int | fast_dtoa (char *buffer, int size, double value)
static int | fast_fixed_dtoa (char *buffer, int size, double value)

## Detailed Description

Converts double to/from a string.

Definition at line 239 of file FieldConvertors.h.

## Member Function Documentation

## convert() [1/3]

static double FIX::DoubleConvertor::convert | ( | const std::string & | value | ) |
throw | ( | FieldConvertError
| ) |  |

inline static

Definition at line 360 of file FieldConvertors.h.

{
double result = 0.0;
if( !convert( value, result ) )
throw FieldConvertError(value);
else
return result;
}
FIX::DoubleConvertor::convert
static std::string convert(double value, int padding=0)
Definition: FieldConvertors.h:255

References convert().

## convert() [2/3]

static bool FIX::DoubleConvertor::convert | ( | const std::string & | value,
|  | double & | result
| ) |  |

inline static

Definition at line 321 of file FieldConvertors.h.

{
const char * i = value.c_str();
// Catch null strings
if( !*i ) return false;
// Eat leading '-' and recheck for null string
if( *i == '-' && !*++i ) return false;
bool haveDigit = false;
if( IS_DIGIT(*i) )
{
haveDigit = true;
while( IS_DIGIT (*++i) );
}
if( *i == '.' && IS_DIGIT(*++i) )
{
haveDigit = true;
while( IS_DIGIT (*++i) );
}
if( *i || !haveDigit ) return false;
int processed_chars;
const int total_length = value.length();
const double val = fast_strtod( value.c_str(), total_length, &processed_chars);
if ( processed_chars != total_length ||
val != val /*test for quite NaN*/ )
{
return false;
}
result = val;
return true;
}
IS_DIGIT
#define IS_DIGIT(x)
Definition: FieldConvertors.h:49
FIX::DoubleConvertor::fast_strtod
static double fast_strtod(const char *buffer, int size, int *processed_chars)
Definition: FieldConvertors.cpp:63

References fast_strtod(), and IS_DIGIT.

## convert() [3/3]

static std::string FIX::DoubleConvertor::convert | ( | double | value,
|  | int | padding = `0`
| ) |  |

inline static

Definition at line 255 of file FieldConvertors.h.

{
char result[BUFFFER_SIZE];
char *end = 0;
int size;
if( value == 0 || value > 0.0001 || value < -0.0001 )
{
size = fast_dtoa( result, BUFFFER_SIZE, value );
if( size == 0 )
return std::string();
if( padding > 0 )
{
char* point = result;
end = result + size - 1;
while( *point != '.' && *point != 0 )
point++;
if( *point == 0 )
{
end = point;
*point = '.';
++size;
}
int needed = padding - (int)(end - point);
if( needed > 0 )
{
memset( ++end, '0', needed );
size += needed;
}
}
}
else
{
size = fast_fixed_dtoa( result, BUFFFER_SIZE, value );
if( size == 0 )
return std::string();
// strip trailing 0's
end = result + size - 1;
if( padding > 0 )
{
int discard = SIGNIFICANT_DIGITS - padding;
while( (*end == '0') && (discard-- > 0) )
{
--end;
--size;
}
}
else
{
while( *end == '0' )
{
--end;
--size;
}
}
}
return std::string( result, size );
}
FIX::DoubleConvertor::fast_fixed_dtoa
static int fast_fixed_dtoa(char *buffer, int size, double value)
Definition: FieldConvertors.cpp:81
FIX::DoubleConvertor::BUFFFER_SIZE
static const int BUFFFER_SIZE
Definition: FieldConvertors.h:253
FIX::DoubleConvertor::fast_dtoa
static int fast_dtoa(char *buffer, int size, double value)
Definition: FieldConvertors.cpp:68
FIX::DoubleConvertor::SIGNIFICANT_DIGITS
static const int SIGNIFICANT_DIGITS
Definition: FieldConvertors.h:252

References BUFFFER_SIZE, fast_dtoa(), fast_fixed_dtoa(), and SIGNIFICANT_DIGITS.

Referenced by FIX::DataDictionary::checkValidFormat(), convert(), FIX::Dictionary::getDouble(), FIX::DoubleField::getValue(), FIX::Dictionary::setDouble(), and FIX::DoubleField::setValue().

## fast_dtoa()

int FIX::DoubleConvertor::fast_dtoa | ( | char * | buffer,
|  | int | size,
|  | double | value
| ) |  |

static private

Definition at line 68 of file FieldConvertors.cpp.

{
double_conversion::StringBuilder builder( buffer, size );
if( !g_dtoa_converter.ToPrecision( value, DoubleConvertor::SIGNIFICANT_DIGITS, &builder ) )
{
builder.Reset();
return 0;
}
builder.TrimTrailingZeros();
return builder.position();
}
FIX::g_dtoa_converter
static double_conversion::DoubleToStringConverter g_dtoa_converter(double_conversion::DoubleToStringConverter::NO_FLAGS, "INF", "NAN", 'e', -DoubleConvertor::SIGNIFICANT_DIGITS, DoubleConvertor::SIGNIFICANT_DIGITS, DoubleConvertor::SIGNIFICANT_DIGITS - 1, 0)

References FIX::g_dtoa_converter(), and SIGNIFICANT_DIGITS.

Referenced by convert().

## fast_fixed_dtoa()

int FIX::DoubleConvertor::fast_fixed_dtoa | ( | char * | buffer,
|  | int | size,
|  | double | value
| ) |  |

static private

Definition at line 81 of file FieldConvertors.cpp.

{
double_conversion::StringBuilder builder( buffer, size );
if( !g_dtoa_converter.ToFixed( value, DoubleConvertor::SIGNIFICANT_DIGITS, &builder ) )
{
builder.Reset();
return 0;
}
return builder.position();
}

References FIX::g_dtoa_converter(), and SIGNIFICANT_DIGITS.

Referenced by convert().

## fast_strtod()

double FIX::DoubleConvertor::fast_strtod | ( | const char * | buffer,
|  | int | size,
|  | int * | processed_chars
| ) |  |

static private

Definition at line 63 of file FieldConvertors.cpp.

{
return g_atod_converter.StringToDouble( buffer, size, processed_chars );
}
FIX::g_atod_converter
static double_conversion::StringToDoubleConverter g_atod_converter(double_conversion::StringToDoubleConverter::NO_FLAGS, std::numeric_limits< double >::quiet_NaN(), std::numeric_limits< double >::quiet_NaN(), "INF", "NAN")

References FIX::g_atod_converter().

Referenced by convert().

## Member Data Documentation

## BUFFFER_SIZE

const int FIX::DoubleConvertor::BUFFFER_SIZE = 32

static

Definition at line 253 of file FieldConvertors.h.

Referenced by convert().

## SIGNIFICANT_DIGITS

const int FIX::DoubleConvertor::SIGNIFICANT_DIGITS = 15

static

Definition at line 252 of file FieldConvertors.h.

Referenced by convert(), fast_dtoa(), and fast_fixed_dtoa().

The documentation for this struct was generated from the following files:

- FieldConvertors.h
- FieldConvertors.cpp