Static Public Member Functions | Static Public Attributes | Static Private Member Functions | List of all members
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 256 of file FieldConvertors.h.

Member Function Documentation

◆ convert() [1/3]

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

Definition at line 377 of file FieldConvertors.h.

381  {
382  if( value.size() != 1 ) return false;
383  result = value[0];
384  return true;
385  }

◆ convert() [2/3]

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

Definition at line 338 of file FieldConvertors.h.

339  {
340  haveDigit = true;
341  while( IS_DIGIT (*++i) );
342  }
343 
344  if( *i || !haveDigit ) return false;
345 
346  int processed_chars;
347  const int total_length = value.length();
348  const double val = fast_strtod( value.c_str(), total_length, &processed_chars);
349 
350  if ( processed_chars != total_length ||
351  val != val /*test for quite NaN*/ )
352  {
353  return false;
354  }
355 
356  result = val;
357  return true;
358 }
359 
360  static double convert( const std::string& value )
361  throw( FieldConvertError )
362  {
363  double result = 0.0;
364  if( !convert( value, result ) )
365  throw FieldConvertError(value);
366  else
367  return result;
368  }
369 };
370 
372 struct CharConvertor
373 {
374  static std::string convert( char value )
375  {

References IS_DIGIT.

◆ convert() [3/3]

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

Definition at line 272 of file FieldConvertors.h.

275  {
276  end = point;
277  *point = '.';
278  ++size;
279  }
280  int needed = padding - (int)(end - point);
281 
282  if( needed > 0 )
283  {
284  memset( ++end, '0', needed );
285  size += needed;
286  }
287  }
288  }
289  else
290  {
291  size = fast_fixed_dtoa( result, BUFFFER_SIZE, value );
292  if( size == 0 )
293  return std::string();
294 
295  // strip trailing 0's
296  end = result + size - 1;
297 
298  if( padding > 0 )
299  {
300  int discard = SIGNIFICANT_DIGITS - padding;
301 
302  while( (*end == '0') && (discard-- > 0) )
303  {
304  --end;
305  --size;
306  }
307  }
308  else
309  {
310  while( *end == '0' )
311  {
312  --end;
313  --size;
314  }
315  }
316  }
317 
318  return std::string( result, size );
319 }
320 
321 static bool convert( const std::string& value, double& result )
322 {
323  const char * i = value.c_str();
324 
325  // Catch null strings
326  if( !*i ) return false;
327  // Eat leading '-' and recheck for null string
328  if( *i == '-' && !*++i ) return false;
329 
330  bool haveDigit = false;
331 
332  if( IS_DIGIT(*i) )
333  {
334  haveDigit = true;
335  while( IS_DIGIT (*++i) );
336  }

Referenced by FIX::Dictionary::getInt(), FIX::CharField::getValue(), and FIX::CharField::operator char().

◆ fast_dtoa()

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

Definition at line 68 of file FieldConvertors.cpp.

69  {
70  double_conversion::StringBuilder builder( buffer, size );
71  if( !g_dtoa_converter.ToPrecision( value, DoubleConvertor::SIGNIFICANT_DIGITS, &builder ) )
72  {
73  builder.Reset();
74  return 0;
75  }
76 
77  builder.TrimTrailingZeros();
78  return builder.position();
79  }

References FIX::g_dtoa_converter(), and SIGNIFICANT_DIGITS.

◆ fast_fixed_dtoa()

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

Definition at line 81 of file FieldConvertors.cpp.

82  {
83  double_conversion::StringBuilder builder( buffer, size );
84  if( !g_dtoa_converter.ToFixed( value, DoubleConvertor::SIGNIFICANT_DIGITS, &builder ) )
85  {
86  builder.Reset();
87  return 0;
88  }
89 
90  return builder.position();
91  }

References FIX::g_dtoa_converter(), and SIGNIFICANT_DIGITS.

◆ fast_strtod()

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

Definition at line 63 of file FieldConvertors.cpp.

64  {
65  return g_atod_converter.StringToDouble( buffer, size, processed_chars );
66  }

References FIX::g_atod_converter().

Member Data Documentation

◆ BUFFFER_SIZE

const int FIX::DoubleConvertor::BUFFFER_SIZE = 32
static

Definition at line 270 of file FieldConvertors.h.

◆ SIGNIFICANT_DIGITS

const int FIX::DoubleConvertor::SIGNIFICANT_DIGITS = 15
static

Definition at line 269 of file FieldConvertors.h.

Referenced by fast_dtoa(), and fast_fixed_dtoa().


The documentation for this struct was generated from the following files:
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)
FIX::DoubleConvertor::fast_strtod
static double fast_strtod(const char *buffer, int size, int *processed_chars)
Definition: FieldConvertors.cpp:63
FIX::DoubleConvertor::SIGNIFICANT_DIGITS
static const int SIGNIFICANT_DIGITS
Definition: FieldConvertors.h:269
IS_DIGIT
#define IS_DIGIT(x)
Definition: FieldConvertors.h:66
FIX::DoubleConvertor::BUFFFER_SIZE
static const int BUFFFER_SIZE
Definition: FieldConvertors.h:270
FIX::DoubleConvertor::convert
static std::string convert(double value, int padding=0)
Definition: FieldConvertors.h:272
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")
FIX::DoubleConvertor::fast_fixed_dtoa
static int fast_fixed_dtoa(char *buffer, int size, double value)
Definition: FieldConvertors.cpp:81

Generated on Wed Apr 29 2020 19:41:30 for QuickFIX by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2001