Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit6f12166

Browse files
Move numeric to string conversion from std::to_string to own implementation: to_string_precise
1 parentb5a389d commit6f12166

File tree

2 files changed

+84
-30
lines changed

2 files changed

+84
-30
lines changed

‎blobstamper/helpers.h‎

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,76 @@
11

22
voidhexdump(void *pAddressIn,long lSize);
3+
4+
5+
/* Set of functions to_string_precise that acts as glibc to_string but
6+
keeps maximym precision for float types, and ads char and signed char type*/
7+
8+
/* For common int types just use to_string as is*/
9+
inline std::string
10+
to_string_precise(int __val)
11+
{returnstd::to_string(__val); }
12+
13+
inline std::string
14+
to_string_precise(unsigned __val)
15+
{returnstd::to_string(__val); }
16+
17+
inline std::string
18+
to_string_precise(long __val)
19+
{returnstd::to_string(__val); }
20+
21+
inline std::string
22+
to_string_precise(unsignedlong __val)
23+
{returnstd::to_string(__val); }
24+
25+
inline std::string
26+
to_string_precise(longlong __val)
27+
{returnstd::to_string(__val); }
28+
29+
inline std::string
30+
to_string_precise(unsignedlonglong __val)
31+
{returnstd::to_string(__val); }
32+
33+
/* Code grabbed from /usr/include/c++/8/bits/basic_string.h and
34+
adapted both for char and signed char, and float, double etc with
35+
max precision
36+
*/
37+
38+
inline std::string
39+
to_string_precise(char __val)
40+
{return __gnu_cxx::__to_xstring<std::string>(&std::vsnprintf,4 *sizeof(char),
41+
"%d", __val); }
42+
43+
inline std::string
44+
to_string_precise(signedchar __val)
45+
{return __gnu_cxx::__to_xstring<std::string>(&std::vsnprintf,4 *sizeof(signedchar),
46+
"%d", __val); }
47+
48+
49+
/* 999 here is really bad idea, but for now I can't figure out better way*/
50+
inline std::string
51+
to_string_precise(float __val)
52+
{
53+
constint __n =
54+
__gnu_cxx::__numeric_traits<float>::__max_exponent10 +999 +20;
55+
return __gnu_cxx::__to_xstring<std::string>(&std::vsnprintf, __n,
56+
"%.999g", __val);
57+
}
58+
59+
inline std::string
60+
to_string_precise(double __val)
61+
{
62+
constint __n =
63+
__gnu_cxx::__numeric_traits<double>::__max_exponent10 +999 +20;
64+
return __gnu_cxx::__to_xstring<std::string>(&std::vsnprintf, __n,
65+
"%.999g", __val);
66+
}
67+
68+
inline std::string
69+
to_string_precise(longdouble __val)
70+
{
71+
constint __n =
72+
__gnu_cxx::__numeric_traits<longdouble>::__max_exponent10 +999 +20;
73+
return __gnu_cxx::__to_xstring<std::string>(&std::vsnprintf, __n,
74+
"%.999g", __val);
75+
}
76+

‎blobstamper/stamp_atomic.cpp‎

Lines changed: 10 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include"blob.h"
22
#include"stamp.h"
33
#include"stamp_atomic.h"
4+
#include"helpers.h"
45

56

67
StampBinChar::StampBinChar() : StampFixed()
@@ -16,7 +17,7 @@ StampStrUInt8::ExtractStr(Blob &blob)
1617
char *pc = (char *)this->ExtractBin(blob);
1718
if (! pc)
1819
return"";
19-
res =std::to_string((int) *pc);
20+
res =to_string_precise((int) *pc);
2021
free(pc);
2122
return res;
2223
}
@@ -34,7 +35,7 @@ StampStrUInt16::ExtractStr(Blob &blob)
3435
unsignedshortint *pi = (unsignedshortint *)this->ExtractBin(blob);
3536
if (! pi)
3637
return"";
37-
res =std::to_string(*pi);
38+
res =to_string_precise(*pi);
3839
free(pi);
3940
return res;
4041
}
@@ -46,7 +47,7 @@ StampStrSInt16::ExtractStr(Blob &blob)
4647
signedshortint *pi = (signedshortint *)this->ExtractBin(blob);
4748
if (! pi)
4849
return"";
49-
res =std::to_string(*pi);
50+
res =to_string_precise(*pi);
5051
free(pi);
5152
return res;
5253
}
@@ -65,7 +66,7 @@ StampStrUInt32::ExtractStr(Blob &blob)
6566
unsignedint *pi = (unsignedint *)this->ExtractBin(blob);
6667
if (! pi)
6768
return"";
68-
res =std::to_string(*pi);
69+
res =to_string_precise(*pi);
6970
free(pi);
7071
return res;
7172
}
@@ -77,7 +78,7 @@ StampStrSInt32::ExtractStr(Blob &blob)
7778
signedint *pi = (signedint *)this->ExtractBin(blob);
7879
if (! pi)
7980
return"";
80-
res =std::to_string(*pi);
81+
res =to_string_precise(*pi);
8182
free(pi);
8283
return res;
8384
}
@@ -96,7 +97,7 @@ StampStrUInt64::ExtractStr(Blob &blob)
9697
unsignedlonglong *pi = (unsignedlonglong *)this->ExtractBin(blob);
9798
if (! pi)
9899
return"";
99-
res =std::to_string(*pi);
100+
res =to_string_precise(*pi);
100101
free(pi);
101102
return res;
102103
}
@@ -108,7 +109,7 @@ StampStrSInt64::ExtractStr(Blob &blob)
108109
signedlonglong *pi = (signedlonglong *)this->ExtractBin(blob);
109110
if (! pi)
110111
return"";
111-
res =std::to_string(*pi);
112+
res =to_string_precise(*pi);
112113
free(pi);
113114
return res;
114115
}
@@ -126,32 +127,11 @@ StampStrDouble::ExtractStr(Blob &blob)
126127
std::string res ="";
127128
double *pd = (double *)this->ExtractBin(blob);
128129
if (! pd)
129-
return res;
130-
131-
int size_s =snprintf(nullptr,0,"%.999g", *pd) +1;
132-
if (size_s <=0)
133-
{
134-
printf("ai-ai-ai\n");
135-
return"";
136-
}
137-
138-
char * resc =(char *)malloc(size_s);
139-
if (! resc)
140-
{
141-
printf("oh-oh-oh\n");
142130
return"";
143-
}
144131

145-
int ret =snprintf(resc,size_s,"%.999g", *pd);
146-
if (ret <=0)
147-
{
148-
printf("oi-oi-oi\n");
149-
free(resc);
150-
return"";
151-
}
152-
res = resc;
153-
free(resc);
132+
res =to_string_precise(*pd);
154133
free(pd);
155134
return res;
156135
}
136+
157137
/* ----*/

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp