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 if(n == (T_Int)0 - n) {
45 s += '1';
46 return zftrue;
47 }
48 n = (T_Int)0 - n;
49 }
50 else {
51 s += '0';
52 return zftrue;
53 }
54 }
55 else if(n == (T_Int)-1) {
56 s += '-';
57 s += '1';
58 return zftrue;
59 }
60
61 unsigned long long e = 1;
62 while(n / e) {e *= radix;}
63
64 const zfchar *token = useUpperCase
65 ? "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
66 : "0123456789abcdefghijklmnopqrstuvwxyz";
67 while(e /= radix) {
68 s += token[n / e];
69 n %= e;
70 }
71 return zftrue;
72}
73
76template<typename T_Int>
78 ZF_IN T_Int n
79 , ZF_IN_OPT zfindex radix = 10
80 , ZF_IN_OPT zfbool useUpperCase = zftrue
81 ) {
82 zfstring s;
83 zfsFromIntT(s, n, radix, useUpperCase);
84 return s;
85}
86
87// ============================================================
96template<typename T_Int>
98 ZF_OUT T_Int &ret
99 , ZF_IN const zfchar *src
100 , ZF_IN_OPT zfindex srcLen = zfindexMax()
101 , ZF_IN_OPT zfindex radix = 10
102 , ZF_IN_OPT zfbool allowNegative = zftrue
103 ) {
104 if(src == zfnull || srcLen == 0 || radix < 2 || radix > 36) {
105 return zffalse;
106 }
107
108 ret = 0;
109 const zfchar *p = src;
110 const zfchar *pEnd = ((srcLen == zfindexMax()) ? (p - 1) : (p + srcLen));
111 zfbool negative = zffalse;
112 if(*p == '-') {
113 if(!allowNegative) {
114 return zffalse;
115 }
116 negative = zftrue;
117 ++p;
118 }
119 while(*p != '\0' && p != pEnd) {
120 zfuint tmp = 0;
121 if(*p >= '0' && *p <= '9') {tmp = *p - '0';}
122 else if(*p >= 'a' && *p <= 'z') {tmp = 10 + *p - 'a';}
123 else if(*p >= 'A' && *p <= 'Z') {tmp = 10 + *p - 'A';}
124 else {break;}
125 if(tmp >= radix) {break;}
126 ret = (T_Int)((ret * (zfuint)radix) + tmp);
127 ++p;
128 }
129 if(negative) {
130 ret = (T_Int)0 - ret;
131 }
132
133 if(*p == '\0' || p == pEnd) {
134 return zftrue;
135 }
136 else {
137 return zffalse;
138 }
139}
140
143template<typename T_Int>
145 ZF_IN const zfchar *src
146 , ZF_IN_OPT zfindex srcLen = zfindexMax()
147 , ZF_IN_OPT zfindex radix = 10
148 , ZF_IN_OPT zfbool allowNegative = zftrue
149 ) {
150 T_Int ret = 0;
151 zfsToIntT(ret, src, srcLen, radix, allowNegative);
152 return ret;
153}
154
158 ZF_IN const zfchar *src
159 , ZF_IN_OPT zfindex srcLen = zfindexMax()
160 , ZF_IN_OPT zfindex radix = 10
161 , ZF_IN_OPT zfbool allowNegative = zftrue
162 ) {
163 return zfsToInt<zfint>(src, srcLen, radix, allowNegative);
164}
165
166// ============================================================
174template<typename T_Float>
177 , ZF_IN T_Float n
178 ) {
179 zfchar buf[64] = {0};
180 snprintf(buf, sizeof(buf), "%.15lf", (double)n);
181 const zfchar *p = buf;
182 while(*p && *p != '.') {++p;}
183 if(*p == '\0') {
184 s += buf;
185 return zftrue;
186 }
187 const zfchar *pEnd = p + zfslen(p) - 1;
188 if(*pEnd == '0') {
189 do {--pEnd;} while(*pEnd == '0');
190 if(pEnd == p) {
191 s.append(buf, p - buf);
192 }
193 else {
194 s.append(buf, pEnd + 1 - buf);
195 }
196 }
197 else {
198 s += buf;
199 }
200 return zftrue;
201}
202
205template<typename T_Float>
207 zfstring s;
208 zfsFromFloatT(s, n);
209 return s;
210}
211
212// ============================================================
221template<typename T_Float>
223 ZF_OUT T_Float &ret
224 , ZF_IN const zfchar *src
225 , ZF_IN_OPT zfindex srcLen = zfindexMax()
226 ) {
227 if(src == zfnull || srcLen == 0) {
228 return zffalse;
229 }
230
231 ret = (T_Float)0;
232 const zfchar *p = src;
233 const zfchar *pEnd = ((srcLen == zfindexMax()) ? (p - 1) : (p + srcLen));
234 zfbool negative = zffalse;
235 if(*p == '-') {
236 negative = zftrue;
237 ++p;
238 }
239
240 while(*p != '\0' && *p != '.' && p != pEnd) {
241 if(*p < '0' || *p > '9') {break;}
242 ret = ret * 10 + (*p - '0');
243 ++p;
244 }
245 if(*p != '\0' && *p != '.' && p != pEnd) {
246 return zffalse;
247 }
248
249 if(*p == '.') {
250 ++p;
251
252 unsigned long e = 10;
253 while(*p != '\0' && p != pEnd
254 && *p >= '0' && *p <= '9'
255 ) {
256 ret += (T_Float)(*p - '0') / e;
257 e *= 10;
258 ++p;
259 }
260 if(*p != '\0' && p != pEnd) {
261 return zffalse;
262 }
263 }
264
265 // 1.23e+21
266 // 1.23e-21
267 if(*p == 'e') {
268 if(*(p + 1) == '+') {
269 zfint e = -1;
270 if(zfsToIntT(e, p + 2, pEnd - (p + 2), 10, zffalse)
271 && e > 0
272 ) {
273 while(e > 0) {
274 ret *= 10;
275 --e;
276 }
277 p = pEnd;
278 }
279 }
280 else if(*(p + 1) == '-') {
281 zfint e = -1;
282 if(zfsToIntT(e, p + 2, pEnd - (p + 2), 10, zffalse)
283 && e > 0
284 ) {
285 while(e > 0) {
286 ret /= 10;
287 }
288 p = pEnd;
289 }
290 }
291 }
292
293 if(negative) {
294 ret = (T_Float)(0 - ret);
295 }
296
297 if(*p == '\0' || p == pEnd) {
298 return zftrue;
299 }
300 else {
301 return zffalse;
302 }
303}
304
307template<typename T_Float>
309 ZF_IN const zfchar *src
310 , ZF_IN_OPT zfindex srcLen = zfindexMax()
311 ) {
312 T_Float ret = 0;
313 zfsToFloatT(ret, src, srcLen);
314 return ret;
315}
316
320 ZF_IN const zfchar *src
321 , ZF_IN_OPT zfindex srcLen = zfindexMax()
322 ) {
323 return zfsToFloat<zffloat>(src, srcLen);
324}
325
326
327// ============================================================
333 , ZF_IN const void *p
334 ) {
335 zfchar buf[32] = {0};
336 if(p == zfnull) {
337 snprintf(buf, sizeof(buf), "<null>");
338 }
339 else {
340 snprintf(buf, sizeof(buf), "%p", p);
341 }
342 const zfchar *t = buf;
343 while(*t) {
344 s += *t++;
345 }
346 return zftrue;
347}
348
351inline zfstring zfsFromPointer(ZF_IN const void *p) {
352 zfstring s;
353 zfsFromPointerT(s, p);
354 return s;
355}
356
358
359#endif // #ifndef _ZFI_ZFCoreStringConvert_h_
360
zfbool zfsToFloatT(T_Float &ret, const zfchar *src, zfindex srcLen=((zfindex) -1))
convert string to float
Definition ZFCoreStringConvert.h:222
T_Int zfsToInt(const zfchar *src, zfindex srcLen=((zfindex) -1), zfindex radix=10, zfbool allowNegative=_ZFT_t_zftrue)
see zfsToIntT
Definition ZFCoreStringConvert.h:144
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:97
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:351
T_Float zfsToFloat(const zfchar *src, zfindex srcLen=((zfindex) -1))
see zfsToFloatT
Definition ZFCoreStringConvert.h:308
zfbool zfsFromFloatT(zfstring &s, T_Float n)
convert float number (int, long, etc) to string
Definition ZFCoreStringConvert.h:175
zfstring zfsFromFloat(T_Float n)
see zfsFromFloatT
Definition ZFCoreStringConvert.h:206
zfbool zfsFromPointerT(zfstring &s, const void *p)
convert pointer value to string
Definition ZFCoreStringConvert.h:331
zfstring zfsFromInt(T_Int n, zfindex radix=10, zfbool useUpperCase=_ZFT_t_zftrue)
see zfsFromIntT
Definition ZFCoreStringConvert.h:77
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:184
#define ZF_IN_OPT
dummy macro that shows the param used as optional input
Definition ZFCoreTypeDef_ClassType.h:188
#define ZF_OUT
dummy macro that shows the param used as required output
Definition ZFCoreTypeDef_ClassType.h:192
_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