ZFFramework
 
Loading...
Searching...
No Matches
ZFCoreStringConvert.h
Go to the documentation of this file.
1
5
6#ifndef _ZFI_ZFCoreStringConvert_h_
7#define _ZFI_ZFCoreStringConvert_h_
8
9#include "ZFCoreTypeDef.h"
10
12
13// ============================================================
31template<typename T_Int>
34 , ZF_IN T_Int n
35 , ZF_IN_OPT zfindex radix = 10
36 , ZF_IN_OPT zfbool useUpperCase = zftrue
37 ) {
38 if(radix < 2 || radix > 36) {return zffalse;}
39
40 if(n <= (T_Int)0) {
41 // tricks to solve the unsigned type warnings
42 if(n != (T_Int)0) {
43 s += '-';
44 n = (T_Int)0 - n;
45 }
46 else {
47 s += '0';
48 return zftrue;
49 }
50 }
51 else if(n == (T_Int)-1) {
52 s += '-';
53 s += '1';
54 return zftrue;
55 }
56
57 unsigned long long e = 1;
58 while(n / e) {e *= radix;}
59
60 const zfchar *token = useUpperCase
61 ? "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
62 : "0123456789abcdefghijklmnopqrstuvwxyz";
63 while(e /= radix) {
64 s += token[n / e];
65 n %= e;
66 }
67 return zftrue;
68}
69
72template<typename T_Int>
74 ZF_IN T_Int n
75 , ZF_IN_OPT zfindex radix = 10
76 , ZF_IN_OPT zfbool useUpperCase = zftrue
77 ) {
78 zfstring s;
79 zfsFromIntT(s, n, radix, useUpperCase);
80 return s;
81}
82
83// ============================================================
92template<typename T_Int>
94 ZF_OUT T_Int &ret
95 , ZF_IN const zfchar *src
96 , ZF_IN_OPT zfindex srcLen = zfindexMax()
97 , ZF_IN_OPT zfindex radix = 10
98 , ZF_IN_OPT zfbool allowNegative = zftrue
99 ) {
100 if(src == zfnull || srcLen == 0 || radix < 2 || radix > 36) {
101 return zffalse;
102 }
103
104 ret = 0;
105 const zfchar *p = src;
106 const zfchar *pEnd = ((srcLen == zfindexMax()) ? (p - 1) : (p + srcLen));
107 zfbool negative = zffalse;
108 if(*p == '-') {
109 if(!allowNegative) {
110 return zffalse;
111 }
112 negative = zftrue;
113 ++p;
114 }
115 while(*p != '\0' && p != pEnd) {
116 zfuint tmp = 0;
117 if(*p >= '0' && *p <= '9') {tmp = *p - '0';}
118 else if(*p >= 'a' && *p <= 'z') {tmp = 10 + *p - 'a';}
119 else if(*p >= 'A' && *p <= 'Z') {tmp = 10 + *p - 'A';}
120 else {break;}
121 if(tmp >= radix) {break;}
122 ret = (T_Int)((ret * (zfuint)radix) + tmp);
123 ++p;
124 }
125 if(negative) {
126 ret = (T_Int)0 - ret;
127 }
128
129 if(*p == '\0' || p == pEnd) {
130 return zftrue;
131 }
132 else {
133 return zffalse;
134 }
135}
136
139template<typename T_Int>
141 ZF_IN const zfchar *src
142 , ZF_IN_OPT zfindex srcLen = zfindexMax()
143 , ZF_IN_OPT zfindex radix = 10
144 , ZF_IN_OPT zfbool allowNegative = zftrue
145 ) {
146 T_Int ret = 0;
147 zfsToIntT(ret, src, srcLen, radix, allowNegative);
148 return ret;
149}
150
154 ZF_IN const zfchar *src
155 , ZF_IN_OPT zfindex srcLen = zfindexMax()
156 , ZF_IN_OPT zfindex radix = 10
157 , ZF_IN_OPT zfbool allowNegative = zftrue
158 ) {
159 return zfsToInt<zfint>(src, srcLen, radix, allowNegative);
160}
161
162// ============================================================
170template<typename T_Float>
173 , ZF_IN T_Float n
174 ) {
175 zfchar buf[64] = {0};
176 snprintf(buf, sizeof(buf), "%lf", (double)n);
177 const zfchar *p = buf;
178 while(*p && *p != '.') {++p;}
179 if(*p == '\0') {
180 s += buf;
181 return zftrue;
182 }
183 const zfchar *pEnd = p + zfslen(p) - 1;
184 if(*pEnd == '0') {
185 do {--pEnd;} while(*pEnd == '0');
186 if(pEnd == p) {
187 s.append(buf, p - buf);
188 }
189 else {
190 s.append(buf, pEnd + 1 - buf);
191 }
192 }
193 else {
194 s += buf;
195 }
196 return zftrue;
197}
198
201template<typename T_Float>
203 zfstring s;
204 zfsFromFloatT(s, n);
205 return s;
206}
207
208// ============================================================
217template<typename T_Float>
219 ZF_OUT T_Float &ret
220 , ZF_IN const zfchar *src
221 , ZF_IN_OPT zfindex srcLen = zfindexMax()
222 ) {
223 if(src == zfnull || srcLen == 0) {
224 return zffalse;
225 }
226
227 ret = (T_Float)0;
228 const zfchar *p = src;
229 const zfchar *pEnd = ((srcLen == zfindexMax()) ? (p - 1) : (p + srcLen));
230 zfbool negative = zffalse;
231 if(*p == '-') {
232 negative = zftrue;
233 ++p;
234 }
235
236 while(*p != '\0' && *p != '.' && p != pEnd) {
237 if(*p < '0' || *p > '9') {break;}
238 ret = ret * 10 + (*p - '0');
239 ++p;
240 }
241 if(*p != '\0' && *p != '.' && p != pEnd) {
242 return zffalse;
243 }
244
245 if(*p == '.') {
246 ++p;
247
248 unsigned long e = 10;
249 while(*p != '\0' && p != pEnd
250 && *p >= '0' && *p <= '9'
251 ) {
252 ret += (T_Float)(*p - '0') / e;
253 e *= 10;
254 ++p;
255 }
256 if(*p != '\0' && p != pEnd) {
257 return zffalse;
258 }
259 }
260
261 // 1.23e+21
262 // 1.23e-21
263 if(*p == 'e') {
264 if(*(p + 1) == '+') {
265 zfint e = -1;
266 if(zfsToIntT(e, p + 2, pEnd - (p + 2), 10, zffalse)
267 && e > 0
268 ) {
269 while(e > 0) {
270 ret *= 10;
271 --e;
272 }
273 p = pEnd;
274 }
275 }
276 else if(*(p + 1) == '-') {
277 zfint e = -1;
278 if(zfsToIntT(e, p + 2, pEnd - (p + 2), 10, zffalse)
279 && e > 0
280 ) {
281 while(e > 0) {
282 ret /= 10;
283 }
284 p = pEnd;
285 }
286 }
287 }
288
289 if(negative) {
290 ret = (T_Float)(0 - ret);
291 }
292
293 if(*p == '\0' || p == pEnd) {
294 return zftrue;
295 }
296 else {
297 return zffalse;
298 }
299}
300
303template<typename T_Float>
305 ZF_IN const zfchar *src
306 , ZF_IN_OPT zfindex srcLen = zfindexMax()
307 ) {
308 T_Float ret = 0;
309 zfsToFloatT(ret, src, srcLen);
310 return ret;
311}
312
316 ZF_IN const zfchar *src
317 , ZF_IN_OPT zfindex srcLen = zfindexMax()
318 ) {
319 return zfsToFloat<zffloat>(src, srcLen);
320}
321
322
323// ============================================================
329 , ZF_IN const void *p
330 ) {
331 zfchar buf[32] = {0};
332 if(p == zfnull) {
333 snprintf(buf, sizeof(buf), "<null>");
334 }
335 else {
336 snprintf(buf, sizeof(buf), "%p", p);
337 }
338 const zfchar *t = buf;
339 while(*t) {
340 s += *t++;
341 }
342 return zftrue;
343}
344
347inline zfstring zfsFromPointer(ZF_IN const void *p) {
348 zfstring s;
349 zfsFromPointerT(s, p);
350 return s;
351}
352
354
355#endif // #ifndef _ZFI_ZFCoreStringConvert_h_
356
zfbool zfsToFloatT(T_Float &ret, const zfchar *src, zfindex srcLen=((zfindex) -1))
convert string to float
Definition ZFCoreStringConvert.h:218
T_Int zfsToInt(const zfchar *src, zfindex srcLen=((zfindex) -1), zfindex radix=10, zfbool allowNegative=_ZFT_t_zftrue)
see zfsToIntT
Definition ZFCoreStringConvert.h:140
zfbool zfsToIntT(T_Int &ret, const zfchar *src, zfindex srcLen=((zfindex) -1), zfindex radix=10, zfbool allowNegative=_ZFT_t_zftrue)
convert string to int, return error position if failed, or null if success
Definition ZFCoreStringConvert.h:93
zfbool zfsFromIntT(zfstring &s, T_Int n, zfindex radix=10, zfbool useUpperCase=_ZFT_t_zftrue)
convert integer number (int, long, etc) to string
Definition ZFCoreStringConvert.h:32
zfstring zfsFromPointer(const void *p)
see zfsFromPointerT
Definition ZFCoreStringConvert.h:347
T_Float zfsToFloat(const zfchar *src, zfindex srcLen=((zfindex) -1))
see zfsToFloatT
Definition ZFCoreStringConvert.h:304
zfbool zfsFromFloatT(zfstring &s, T_Float n)
convert float number (int, long, etc) to string
Definition ZFCoreStringConvert.h:171
zfstring zfsFromFloat(T_Float n)
see zfsFromFloatT
Definition ZFCoreStringConvert.h:202
zfbool zfsFromPointerT(zfstring &s, const void *p)
convert pointer value to string
Definition ZFCoreStringConvert.h:327
zfstring zfsFromInt(T_Int n, zfindex radix=10, zfbool useUpperCase=_ZFT_t_zftrue)
see zfsFromIntT
Definition ZFCoreStringConvert.h:73
types for ZFFramework
zfindex zfslen(const zfchar *s)
strlen wrapper as zfchar type
Definition ZFCoreTypeDef_CharType.h:144
_ZFT_t_zfchar zfchar
char wrapper
Definition ZFCoreTypeDef_CharType.h:17
#define ZF_IN
dummy macro that shows the param used as required input
Definition ZFCoreTypeDef_ClassType.h:180
#define ZF_IN_OPT
dummy macro that shows the param used as optional input
Definition ZFCoreTypeDef_ClassType.h:184
#define ZF_OUT
dummy macro that shows the param used as required output
Definition ZFCoreTypeDef_ClassType.h:188
_ZFT_t_zfbool zfbool
bool type
Definition ZFCoreTypeDef_CoreType.h:103
_ZFT_t_zfindex zfindex
similar to size_t, used for index and size only
Definition ZFCoreTypeDef_CoreType.h:154
#define zftrue
bool true type
Definition ZFCoreTypeDef_CoreType.h:107
_ZFT_t_zfint zfint
same as int, see zfindex
Definition ZFCoreTypeDef_CoreType.h:165
#define zfindexMax()
(zfindex)-1, indicate a max index value, see zfindex
Definition ZFCoreTypeDef_CoreType.h:159
_zft_zffloat zffloat
same as float, see zfindex
Definition ZFCoreTypeDef_CoreType.h:183
#define zffalse
bool false type
Definition ZFCoreTypeDef_CoreType.h:111
#define zfnull
same as NULL, defined for future use
Definition ZFCoreTypeDef_CoreType.h:88
_ZFT_t_zfuint zfuint
same as unsigned int, see zfindex
Definition ZFCoreTypeDef_CoreType.h:169
zft_zfstring< zfchar > zfstring
see zft_zfstring
Definition ZFCoreTypeDef_StringType.h:15
#define ZF_NAMESPACE_GLOBAL_BEGIN
begin namespace ZFFramework
Definition ZFNamespace.h:97
#define ZF_NAMESPACE_GLOBAL_END
end namespace ZFFramework
Definition ZFNamespace.h:98