Public Member Functions | Private Types | Private Member Functions | Private Attributes | List of all members
format_int Class Reference

#include <format.h>

Public Member Functions

 format_int (int value)
 
 format_int (long value)
 
 format_int (long long value)
 
 format_int (unsigned value)
 
 format_int (unsigned long value)
 
 format_int (unsigned long long value)
 
std::size_t size () const
 
const char * data () const
 
const char * c_str () const
 
std::string str () const
 

Private Types

enum  { BUFFER_SIZE = std::numeric_limits<unsigned long long>::digits10 + 3 }
 

Private Member Functions

char * format_decimal (unsigned long long value)
 
void format_signed (long long value)
 

Private Attributes

char buffer_ [BUFFER_SIZE]
 
char * str_
 

Detailed Description

Fast integer formatter.

Definition at line 2813 of file format.h.

Member Enumeration Documentation

anonymous enum
private
Enumerator
BUFFER_SIZE 

Definition at line 2817 of file format.h.

2817 {BUFFER_SIZE = std::numeric_limits<unsigned long long>::digits10 + 3};

Constructor & Destructor Documentation

format_int::format_int ( int  value)
inlineexplicit

Definition at line 2854 of file format.h.

2854 { format_signed(value); }
void format_signed(long long value)
Definition: format.h:2843
const GenericPointer< typename T::ValueType > T2 value
Definition: pointer.h:1225
format_int::format_int ( long  value)
inlineexplicit

Definition at line 2855 of file format.h.

2855 { format_signed(value); }
void format_signed(long long value)
Definition: format.h:2843
const GenericPointer< typename T::ValueType > T2 value
Definition: pointer.h:1225
format_int::format_int ( long long  value)
inlineexplicit

Definition at line 2856 of file format.h.

2856 { format_signed(value); }
void format_signed(long long value)
Definition: format.h:2843
const GenericPointer< typename T::ValueType > T2 value
Definition: pointer.h:1225
format_int::format_int ( unsigned  value)
inlineexplicit

Definition at line 2857 of file format.h.

2857 : str_(format_decimal(value)) {}
char * str_
Definition: format.h:2819
const GenericPointer< typename T::ValueType > T2 value
Definition: pointer.h:1225
char * format_decimal(unsigned long long value)
Definition: format.h:2822
format_int::format_int ( unsigned long  value)
inlineexplicit

Definition at line 2858 of file format.h.

2858 : str_(format_decimal(value)) {}
char * str_
Definition: format.h:2819
const GenericPointer< typename T::ValueType > T2 value
Definition: pointer.h:1225
char * format_decimal(unsigned long long value)
Definition: format.h:2822
format_int::format_int ( unsigned long long  value)
inlineexplicit

Definition at line 2859 of file format.h.

2859 : str_(format_decimal(value)) {}
char * str_
Definition: format.h:2819
const GenericPointer< typename T::ValueType > T2 value
Definition: pointer.h:1225
char * format_decimal(unsigned long long value)
Definition: format.h:2822

Member Function Documentation

const char* format_int::c_str ( ) const
inline

Returns a pointer to the output buffer content with terminating null character appended.

Definition at line 2876 of file format.h.

2876  {
2877  buffer_[BUFFER_SIZE - 1] = '\0';
2878  return str_;
2879  }
char * str_
Definition: format.h:2819
char buffer_[BUFFER_SIZE]
Definition: format.h:2818
const char* format_int::data ( ) const
inline

Returns a pointer to the output buffer content. No terminating null character is appended.

Definition at line 2870 of file format.h.

2870 { return str_; }
char * str_
Definition: format.h:2819
char* format_int::format_decimal ( unsigned long long  value)
inlineprivate

Definition at line 2822 of file format.h.

2822  {
2823  char *ptr = buffer_ + (BUFFER_SIZE - 1); // Parens to workaround MSVC bug.
2824  while (value >= 100) {
2825  // Integer division is slow so do it for a group of two digits instead
2826  // of for every digit. The idea comes from the talk by Alexandrescu
2827  // "Three Optimization Tips for C++". See speed-test for a comparison.
2828  unsigned index = static_cast<unsigned>((value % 100) * 2);
2829  value /= 100;
2830  *--ptr = internal::data::DIGITS[index + 1];
2831  *--ptr = internal::data::DIGITS[index];
2832  }
2833  if (value < 10) {
2834  *--ptr = static_cast<char>('0' + value);
2835  return ptr;
2836  }
2837  unsigned index = static_cast<unsigned>(value * 2);
2838  *--ptr = internal::data::DIGITS[index + 1];
2839  *--ptr = internal::data::DIGITS[index];
2840  return ptr;
2841  }
static const char DIGITS[]
Definition: format.h:753
char buffer_[BUFFER_SIZE]
Definition: format.h:2818
const void * ptr(const T *p)
Definition: format.h:3138
const GenericPointer< typename T::ValueType > T2 value
Definition: pointer.h:1225
void format_int::format_signed ( long long  value)
inlineprivate

Definition at line 2843 of file format.h.

2843  {
2844  unsigned long long abs_value = static_cast<unsigned long long>(value);
2845  bool negative = value < 0;
2846  if (negative)
2847  abs_value = 0 - abs_value;
2848  str_ = format_decimal(abs_value);
2849  if (negative)
2850  *--str_ = '-';
2851  }
char * str_
Definition: format.h:2819
const GenericPointer< typename T::ValueType > T2 value
Definition: pointer.h:1225
char * format_decimal(unsigned long long value)
Definition: format.h:2822
std::size_t format_int::size ( void  ) const
inline

Returns the number of characters written to the output buffer.

Definition at line 2862 of file format.h.

2862  {
2864  }
char * str_
Definition: format.h:2819
char buffer_[BUFFER_SIZE]
Definition: format.h:2818
FMT_CONSTEXPR std::make_unsigned< Int >::type to_unsigned(Int value)
Definition: core.h:208
std::string format_int::str ( ) const
inline

Returns the content of the output buffer as an std::string.

Definition at line 2886 of file format.h.

2886 { return std::string(str_, size()); }
std::string string
Definition: nybbler.cc:12
char * str_
Definition: format.h:2819
std::size_t size() const
Definition: format.h:2862

Member Data Documentation

char format_int::buffer_[BUFFER_SIZE]
mutableprivate

Definition at line 2818 of file format.h.

char* format_int::str_
private

Definition at line 2819 of file format.h.


The documentation for this class was generated from the following file: