Gray C++ Libraries  0.0.2
A set of C++ libraries for MSVC, GNU on Windows, WinCE, Linux
StrFormat.h
Go to the documentation of this file.
1 //
4 //
5 
6 #ifndef _INC_StrFormat_H
7 #define _INC_StrFormat_H
8 #ifndef NO_PRAGMA_ONCE
9 #pragma once
10 #endif
11 
12 #include "StrChar.h"
13 #include "StrConst.h"
14 #include "cDebugAssert.h"
15 
16 namespace Gray
17 {
19  {
24 
25  public:
26  static const char k_Specs[16]; // "EFGXcdefgiosux" // Omit "S" "apnA"
27 
28  char m_nSpec;
29 
30  BYTE m_nWidthMin;
31  short m_nPrecision;
32 
33  // Flags.
34  BYTE m_nLong;
35  bool m_bAlignLeft;
36  bool m_bPlusSign;
37  bool m_bLeadZero;
38  bool m_bWidthArg;
39  bool m_bAddPrefix;
40 
41  public:
42  static inline char FindSpec(char ch) noexcept
43  {
45  for (size_t i = 0; i < _countof(k_Specs) - 1; i++)
46  {
47  const char chCur = k_Specs[i];
48  if (ch > chCur) // keep looking.
49  continue;
50  if (ch < chCur) // they are sorted.
51  return '\0';
52  return ch;
53  }
54  return '\0'; // nothing.
55  }
56  };
57 
58  template< typename TYPE = char >
60  {
64 
65  public:
66  typedef StrLen_t(_cdecl *STRFORMAT_t)(TYPE* pszOut, StrLen_t iLenOutMax, const TYPE* pszFormat, ...); // for testing.
67 
68  public:
69  StrLen_t ParseParam(const TYPE* pszFormat);
70  StrLen_t RenderString(TYPE* pszOut, StrLen_t nLenOutMax, const TYPE* pszParam, StrLen_t nParamLen, short nPrecision) const;
71  StrLen_t RenderUInt(TYPE* pszOut, StrLen_t nLenOutMax, const TYPE* pszPrefix, RADIX_t nRadix, char chRadixA, UINT64 uVal) const;
72  StrLen_t RenderFloat(TYPE* pszOut, StrLen_t nLenOutMax, char chRadixA, double dVal) const;
73 
74  StrLen_t RenderParam(TYPE* pszOut, StrLen_t nLenOutMax, va_list* pvlist) const;
75 
76  static StrLen_t GRAYCALL FormatV(TYPE* pszOut, StrLen_t nLenOutMax, const TYPE* pszFormat, va_list vlist);
77  static StrLen_t _cdecl FormatF(TYPE* pszOut, StrLen_t nLenOutMax, const TYPE* pszFormat, ...);
78 
80  };
81 }
82 
83 #endif
#define GRAYCORE_LINK
Definition: GrayCore.h:47
#define GRAYCALL
declare calling convention for static functions so everyone knows the arg passing scheme....
Definition: GrayCore.h:36
#define TYPE
Definition: StrT.cpp:38
#define _countof(a)
Definition: cKernel.h:35
#define UNITTEST_FRIEND(n)
Define this in the class body to be unit tested. Allow the unit test to access private/protected stuf...
Definition: cUnitTestDecl.h:17
Definition: StrFormat.h:60
< The main namespace for all Core functions.
Definition: GrayCore.cpp:14
int StrLen_t
the length of a string in chars (bytes for UTF8, wchar_t for UNICODE). or offset in characters....
Definition: StrConst.h:32
WORD RADIX_t
Base for convert of numbers to strings. e.g. 10 base vs 16 base hex numbers.
Definition: StrChar.h:27
Definition: StrFormat.h:19
bool m_bWidthArg
Get an argument that will supply the m_nWidth.
Definition: StrFormat.h:38
char m_nSpec
% type. 0 = invalid. from k_Specs.
Definition: StrFormat.h:28
bool m_bAlignLeft
Definition: StrFormat.h:35
BYTE m_nLong
0=int, 1=long (32 bit usually), or 2=long long (64 bit usually). 'l' or 'll'. (char or wchar_t?...
Definition: StrFormat.h:34
static char FindSpec(char ch) noexcept
Definition: StrFormat.h:42
bool m_bAddPrefix
Add a prefix 0x for hex or 0 for octal.
Definition: StrFormat.h:39
bool m_bPlusSign
Definition: StrFormat.h:36
short m_nPrecision
how many chars from pszParam do we take? (not including '\0') -1 = default.
Definition: StrFormat.h:31
BYTE m_nWidthMin
specifies minimum field width. Total width of what we place in pszOut
Definition: StrFormat.h:30
bool m_bLeadZero
StrNum::k_LEN_MAX_DIGITS_INT.
Definition: StrFormat.h:37