Gray C++ Libraries  0.0.2
A set of C++ libraries for MSVC, GNU on Windows, WinCE, Linux
StrNum.h
Go to the documentation of this file.
1 //
5 //
6 #ifndef _INC_StrNum_H
7 #define _INC_StrNum_H
8 #ifndef NO_PRAGMA_ONCE
9 #pragma once
10 #endif
11 
12 #include "StrChar.h"
13 #include "StrConst.h"
14 #include "cUnitTestDecl.h"
15 
16 namespace Gray
17 {
18  struct GRAYCORE_LINK StrNum // static
19  {
25 
26  static const StrLen_t k_LEN_MAX_DIGITS = (309 + 40);
27  static const StrLen_t k_LEN_MAX_DIGITS_INT = 64;
28 
29  static StrLen_t GRAYCALL GetTrimCharsLen(const char* pStr, StrLen_t nLen, char ch);
30  static StrLen_t GRAYCALL GetNumberString(OUT char* pszOut, const wchar_t* pszInp, StrLen_t iStrMax = k_LEN_MAX_DIGITS);
31 
32  static UINT64 GRAYCALL toUL(const char* pszInp, const char** ppszInpEnd = (const char**)nullptr, RADIX_t nBaseRadix = 0);
33  static INT64 GRAYCALL toIL(const char* pszInp, const char** ppszInpEnd = (const char**)nullptr, RADIX_t nBaseRadix = 10);
34 
35  static UINT32 GRAYCALL toU(const char* pszInp, const char** ppszInpEnd = (const char**)nullptr, RADIX_t nBaseRadix = 0)
36  {
38  return (UINT32)toUL(pszInp, ppszInpEnd, nBaseRadix);
39  }
40  static INT32 GRAYCALL toI(const char* pszInp, const char** ppszInpEnd = (const char**)nullptr, RADIX_t nBaseRadix = 10)
41  {
43  return (INT32)toIL(pszInp, ppszInpEnd, nBaseRadix);
44  }
45 
46  static StrLen_t GRAYCALL ULtoAK(UINT64 uVal, OUT char* pszOut, StrLen_t iStrMax, UINT nKUnit, bool bSpace);
47 
48  static char* GRAYCALL ULtoA2(UINT64 uVal, OUT char* pszOut, StrLen_t iStrMax, RADIX_t nBaseRadix = 10, char chRadixA = 'A');
49  static StrLen_t GRAYCALL ULtoA(UINT64 nVal, OUT char* pszOut, StrLen_t iStrMax, RADIX_t nBaseRadix = 10);
50  static StrLen_t GRAYCALL ILtoA(INT64 nVal, OUT char* pszOut, StrLen_t iStrMax, RADIX_t nBaseRadix = 10);
51 
52  static StrLen_t GRAYCALL UtoA(UINT32 nVal, OUT char* pszOut, StrLen_t iStrMax, RADIX_t nBaseRadix = 10)
53  {
55  return ULtoA(nVal, pszOut, iStrMax, nBaseRadix);
56  }
57  static StrLen_t GRAYCALL ItoA(INT32 nVal, OUT char* pszOut, StrLen_t iStrMax, RADIX_t nBaseRadix = 10)
58  {
60  return ILtoA(nVal, pszOut, iStrMax, nBaseRadix);
61  }
62 
63  static double GRAYCALL toDouble(const char* pszInp, const char** ppszInpEnd = (const char**) nullptr);
64  static StrLen_t GRAYCALL DtoAG2(double dVal, OUT char* pszOut, int iDecPlacesWanted = -1, char chE = -'e');
65  static StrLen_t GRAYCALL DtoAG(double dVal, OUT char* pszOut, StrLen_t iStrMax, int iDecPlacesWanted = -1, char chE = -'e');
66 
67  template < typename _TYPE >
68  static _TYPE inline toValue(const char* pszInp, const char** ppszInpEnd = (const char**)nullptr);
69  template < typename _TYPE >
70  static StrLen_t inline ValueToA(_TYPE val, OUT char* pszOut, StrLen_t iStrMax);
71 
72  template < typename _TYPE >
73  static size_t GRAYCALL ToValArray(OUT _TYPE* pOut, size_t iQtyMax, const char* pszInp);
74 
75  template < typename _TYPE >
76  static StrLen_t GRAYCALL ValArrayToA(OUT char* pszOut, StrLen_t nDstMax, const _TYPE* pSrc, size_t nSrcSize);
77 
78  template < typename _TYPE >
79  static StrLen_t _cdecl ValArrayToAF(OUT char* pszDst, StrLen_t iSizeDstMax, size_t nSrcQty, ...);
80 
82  };
83 
84  template<> inline INT32 StrNum::toValue<INT32>(const char* pszInp, const char** ppszInpEnd)
85  {
86  return (INT32)StrNum::toIL(pszInp, ppszInpEnd);
87  }
88  template<> inline UINT32 StrNum::toValue<UINT32>(const char* pszInp, const char** ppszInpEnd)
89  {
90  return (UINT32)StrNum::toUL(pszInp, ppszInpEnd);
91  }
92  template<> inline INT64 StrNum::toValue<INT64>(const char* pszInp, const char** ppszInpEnd)
93  {
94  return StrNum::toIL(pszInp, ppszInpEnd);
95  }
96  template<> inline UINT64 StrNum::toValue<UINT64>(const char* pszInp, const char** ppszInpEnd)
97  {
98  return StrNum::toUL(pszInp, ppszInpEnd);
99  }
100  template<> inline float StrNum::toValue<float>(const char* pszInp, const char** ppszInpEnd)
101  {
102  return (float)StrNum::toDouble(pszInp, ppszInpEnd);
103  }
104  template<> inline double StrNum::toValue<double>(const char* pszInp, const char** ppszInpEnd)
105  {
106  return StrNum::toDouble(pszInp, ppszInpEnd);
107  }
108 
109  template<> inline StrLen_t StrNum::ValueToA<INT32>(INT32 val, OUT char* pszOut, StrLen_t iStrMax)
110  {
111  return StrNum::ILtoA(val, pszOut, iStrMax);
112  }
113  template<> inline StrLen_t StrNum::ValueToA<UINT32>(UINT32 val, OUT char* pszOut, StrLen_t iStrMax)
114  {
115  return StrNum::ULtoA(val, pszOut, iStrMax);
116  }
117  template<> inline StrLen_t StrNum::ValueToA<INT64>(INT64 val, OUT char* pszOut, StrLen_t iStrMax)
118  {
119  return StrNum::ILtoA(val, pszOut, iStrMax);
120  }
121  template<> inline StrLen_t StrNum::ValueToA<UINT64>(UINT64 val, OUT char* pszOut, StrLen_t iStrMax)
122  {
123  return StrNum::ULtoA(val, pszOut, iStrMax);
124  }
125  template<> inline StrLen_t StrNum::ValueToA<float>(float val, OUT char* pszOut, StrLen_t iStrMax)
126  {
127  return StrNum::DtoAG(val, pszOut, iStrMax);
128  }
129  template<> inline StrLen_t StrNum::ValueToA<double>(double val, OUT char* pszOut, StrLen_t iStrMax)
130  {
131  return StrNum::DtoAG(val, pszOut, iStrMax);
132  }
133 
134  template < typename _TYPE >
135  size_t GRAYCALL StrNum::ToValArray(OUT _TYPE* pOut, size_t iQtyMax, const char* pszInp) // static
136  {
139 
140  if (pszInp == nullptr)
141  return 0;
142  size_t i = 0;
143  for (; i < iQtyMax;)
144  {
145  for (; StrChar::IsSpace(*pszInp); pszInp++)
146  {
147  }
148  const char* pszInpStart = pszInp;
149  if (*pszInpStart == '\0')
150  break;
151  pOut[i++] = StrNum::toValue<_TYPE>(pszInpStart, &pszInp);
152  if (pszInpStart == pszInp) // must be the field terminator? ")},;". End.
153  break;
154  for (; StrChar::IsSpace(*pszInp); pszInp++)
155  {
156  }
157  if (pszInp[0] != ',')
158  break;
159  pszInp++;
160  }
161  return i;
162  }
163 
164  template < typename _TYPE >
165  StrLen_t GRAYCALL StrNum::ValArrayToA(OUT char* pszDst, StrLen_t iSizeDstMax, const _TYPE* pSrc, size_t nSrcQty) // static
166  {
173 
174  iSizeDstMax -= 4; // room to terminate < max sized number.
175  StrLen_t iLenOut = 0;
176  for (size_t i = 0; i < nSrcQty; i++)
177  {
178  if (i > 0)
179  {
180  pszDst[iLenOut++] = ',';
181  }
182 
183  StrLen_t iLenThis = StrNum::ValueToA<_TYPE>(pSrc[i], pszDst + iLenOut, iSizeDstMax - iLenOut);
184  if (iLenThis <= 0)
185  break;
186  iLenOut += iLenThis;
187  if (iLenOut >= iSizeDstMax)
188  break;
189  }
190  return iLenOut;
191  }
192 
193  template < typename _TYPE >
194  StrLen_t _cdecl StrNum::ValArrayToAF(OUT char* pszDst, StrLen_t iSizeDstMax, size_t nSrcQty, ...) // static
195  {
197  //
198  return -1;
199  }
200 }
201 
202 #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 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
< 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
static bool IsSpace(wchar_t ch) noexcept
Definition: StrChar.h:89
Definition: StrNum.h:19
static StrLen_t __stdcall ILtoA(INT64 nVal, OUT char *pszOut, StrLen_t iStrMax, RADIX_t nBaseRadix=10)
Definition: StrNum.cpp:239
static INT64 __stdcall toIL(const char *pszInp, const char **ppszInpEnd=(const char **) nullptr, RADIX_t nBaseRadix=10)
Definition: StrNum.cpp:129
static _TYPE toValue(const char *pszInp, const char **ppszInpEnd=(const char **) nullptr)
static size_t __stdcall ToValArray(OUT _TYPE *pOut, size_t iQtyMax, const char *pszInp)
Definition: StrNum.h:135
static UINT32 __stdcall toU(const char *pszInp, const char **ppszInpEnd=(const char **) nullptr, RADIX_t nBaseRadix=0)
Definition: StrNum.h:35
static StrLen_t __stdcall ItoA(INT32 nVal, OUT char *pszOut, StrLen_t iStrMax, RADIX_t nBaseRadix=10)
Definition: StrNum.h:57
static StrLen_t __stdcall DtoAG(double dVal, OUT char *pszOut, StrLen_t iStrMax, int iDecPlacesWanted=-1, char chE=- 'e')
Definition: StrNum.cpp:345
static double __stdcall toDouble(const char *pszInp, const char **ppszInpEnd=(const char **) nullptr)
Definition: StrNum.cpp:356
static StrLen_t ValueToA(_TYPE val, OUT char *pszOut, StrLen_t iStrMax)
static StrLen_t __stdcall UtoA(UINT32 nVal, OUT char *pszOut, StrLen_t iStrMax, RADIX_t nBaseRadix=10)
Definition: StrNum.h:52
static StrLen_t _cdecl ValArrayToAF(OUT char *pszDst, StrLen_t iSizeDstMax, size_t nSrcQty,...)
Definition: StrNum.h:194
static StrLen_t __stdcall ValArrayToA(OUT char *pszOut, StrLen_t nDstMax, const _TYPE *pSrc, size_t nSrcSize)
Definition: StrNum.h:165
static StrLen_t __stdcall ULtoA(UINT64 nVal, OUT char *pszOut, StrLen_t iStrMax, RADIX_t nBaseRadix=10)
Definition: StrNum.cpp:215
static INT32 __stdcall toI(const char *pszInp, const char **ppszInpEnd=(const char **) nullptr, RADIX_t nBaseRadix=10)
Definition: StrNum.h:40
static UINT64 __stdcall toUL(const char *pszInp, const char **ppszInpEnd=(const char **) nullptr, RADIX_t nBaseRadix=0)
Definition: StrNum.cpp:54