Gray C++ Libraries  0.0.2
A set of C++ libraries for MSVC, GNU on Windows, WinCE, Linux
Gray::StrFormat< TYPE > Class Template Reference

#include <StrFormat.h>

Inheritance diagram for Gray::StrFormat< TYPE >:
Gray::StrFormatBase

Public Types

typedef StrLen_t(_cdecl * STRFORMAT_t) (TYPE *pszOut, StrLen_t iLenOutMax, const TYPE *pszFormat,...)
 

Public Member Functions

StrLen_t ParseParam (const TYPE *pszFormat)
 
StrLen_t RenderString (TYPE *pszOut, StrLen_t nLenOutMax, const TYPE *pszParam, StrLen_t nParamLen, short nPrecision) const
 
StrLen_t RenderUInt (TYPE *pszOut, StrLen_t nLenOutMax, const TYPE *pszPrefix, RADIX_t nRadix, char chRadixA, UINT64 uVal) const
 
StrLen_t RenderFloat (TYPE *pszOut, StrLen_t nLenOutMax, char chRadixA, double dVal) const
 
StrLen_t RenderParam (TYPE *pszOut, StrLen_t nLenOutMax, va_list *pvlist) const
 

Static Public Member Functions

static StrLen_t __stdcall FormatV (TYPE *pszOut, StrLen_t nLenOutMax, const TYPE *pszFormat, va_list vlist)
 
static StrLen_t _cdecl FormatF (TYPE *pszOut, StrLen_t nLenOutMax, const TYPE *pszFormat,...)
 
- Static Public Member Functions inherited from Gray::StrFormatBase
static char FindSpec (char ch) noexcept
 

Friends

class StrFormatTests
 

Additional Inherited Members

- Public Attributes inherited from Gray::StrFormatBase
char m_nSpec
 % type. 0 = invalid. from k_Specs. More...
 
BYTE m_nWidthMin
 specifies minimum field width. Total width of what we place in pszOut More...
 
short m_nPrecision
 how many chars from pszParam do we take? (not including '\0') -1 = default. More...
 
BYTE m_nLong
 0=int, 1=long (32 bit usually), or 2=long long (64 bit usually). 'l' or 'll'. (char or wchar_t?) More...
 
bool m_bAlignLeft
 
bool m_bPlusSign
 
bool m_bLeadZero
 StrNum::k_LEN_MAX_DIGITS_INT. More...
 
bool m_bWidthArg
 Get an argument that will supply the m_nWidth. More...
 
bool m_bAddPrefix
 Add a prefix 0x for hex or 0 for octal. More...
 
- Static Public Attributes inherited from Gray::StrFormatBase
static const char k_Specs [16] = "EFGXcdefgiopsux"
 

Detailed Description

template<typename TYPE = char>
class Gray::StrFormat< TYPE >

A single formatter for a string. Like printf(). Hold a single printf type format parameter/specifier and render it.

Member Typedef Documentation

◆ STRFORMAT_t

template<typename TYPE = char>
typedef StrLen_t(_cdecl * Gray::StrFormat< TYPE >::STRFORMAT_t) (TYPE *pszOut, StrLen_t iLenOutMax, const TYPE *pszFormat,...)

Member Function Documentation

◆ FormatF()

template<typename TYPE >
StrLen_t _cdecl Gray::StrFormat< TYPE >::FormatF ( TYPE pszOut,
StrLen_t  nLenOutMax,
const TYPE pszFormat,
  ... 
)
static

◆ FormatV()

template<typename TYPE >
StrLen_t GRAYCALL Gray::StrFormat< TYPE >::FormatV ( TYPE pszOut,
StrLen_t  nLenOutMax,
const TYPE pszFormat,
va_list  vlist 
)
static

Replace vsnprintf,_vsnprintf_s,_vsnprintf like StrT::vsprintfN Make consistent overflow behavior for linux and _WIN32 http://opensource.apple.com//source/ruby/ruby-67.6/ruby/sprintf.c Emulate the _TRUNCATE flag in _WIN32. just stop at the overflow of nLenOutMax Do not throw exceptions from this !?

Todo:
Allow .NET style "{0}" format for strings.
  • pszOut = nullptr = just estimate for 2 pass formatting.
  • nLenOutMax = max length allowing for '\0' terminator.
    Returns
    the size i used (in chars)(or would use if pszOut==nullptr). Not including '\0'.
    Examples: (must pass UnitTestFormat) "{ts'%04d-%02d-%02d %02d:%02d:%02d'}", "%s %0 %1 %2"
Note
Windows _snprintf and _vsnprintf are not compatible to Linux versions. Linux result is the size of the buffer that it should have. Windows Result value is not size of buffer needed, but -1 if no fit is possible. Newer Windows versions have a _TRUNCATE option to just truncate the string and return used size. _vscwprintf can be used to estimate the size needed in advance using a 2 pass method.

◆ ParseParam()

template<typename TYPE >
StrLen_t Gray::StrFormat< TYPE >::ParseParam ( const TYPE pszFormat)

Parse the mode for 1 single argument/param from a printf() format string. e.g. d,s,u,g,f, etc. http://www.cplusplus.com/reference/cstdio/printf/

  • pszFormat = "%[flags][width][.precision][length]specifier"
    Returns
    0 = not a valid mode format. just flush the format.

◆ RenderFloat()

template<typename TYPE >
StrLen_t Gray::StrFormat< TYPE >::RenderFloat ( TYPE pszOut,
StrLen_t  nLenOutMax,
char  chRadixA,
double  dVal 
) const

◆ RenderParam()

template<typename TYPE >
StrLen_t Gray::StrFormat< TYPE >::RenderParam ( TYPE pszOut,
StrLen_t  nLenOutMax,
va_list *  pvlist 
) const

Render a single parameter/spec.

  • nLenOutMax = max string chars including a space for terminator. (even though we don't terminate)

◆ RenderString()

template<typename TYPE >
StrLen_t Gray::StrFormat< TYPE >::RenderString ( TYPE pszOut,
StrLen_t  nLenOutMax,
const TYPE pszParam,
StrLen_t  nParamLen,
short  nPrecision 
) const
  • nLenOutMax = max string chars including a space for terminator. (even though we don't terminate)
  • pszParam = a properly terminated string.
  • nPrecision = how many chars from pszParam do we take? (not including '\0') Does not terminate.

◆ RenderUInt()

template<typename TYPE >
StrLen_t Gray::StrFormat< TYPE >::RenderUInt ( TYPE pszOut,
StrLen_t  nLenOutMax,
const TYPE pszPrefix,
RADIX_t  nRadix,
char  chRadixA,
UINT64  uVal 
) const
  • nLenOutMax = max string chars including a space for terminator. (even though we don't terminate)

Friends And Related Function Documentation

◆ StrFormatTests

template<typename TYPE = char>
friend class StrFormatTests
friend

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