Mercurial > hg > orthanc-stone
annotate Framework/Toolbox/GenericToolbox.h @ 1329:8d3e669f01a2 broker
Fixed UT
author | Benjamin Golinvaux <bgo@osimis.io> |
---|---|
date | Mon, 30 Mar 2020 08:47:30 +0200 |
parents | fd616c4a5904 |
children | ab81ee8fce1f |
rev | line source |
---|---|
1080
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
1 /** |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
2 * Stone of Orthanc |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
4 * Department, University Hospital of Liege, Belgium |
1270
2d8ab34c8c91
upgrade to year 2020
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1192
diff
changeset
|
5 * Copyright (C) 2017-2020 Osimis S.A., Belgium |
1080
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
6 * |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
7 * This program is free software: you can redistribute it and/or |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
8 * modify it under the terms of the GNU Affero General Public License |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
9 * as published by the Free Software Foundation, either version 3 of |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
10 * the License, or (at your option) any later version. |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
11 * |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
12 * This program is distributed in the hope that it will be useful, but |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
13 * WITHOUT ANY WARRANTY; without even the implied warranty of |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
15 * Affero General Public License for more details. |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
16 * |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
17 * You should have received a copy of the GNU Affero General Public License |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
18 * along with this program. If not, see <http://www.gnu.org/licenses/>. |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
19 **/ |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
20 |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
21 #pragma once |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
22 |
1328
fd616c4a5904
Added mechanism to prevent callbacks from being sent on dead WebAssemblyViewport objects
Benjamin Golinvaux <bgo@osimis.io>
parents:
1306
diff
changeset
|
23 #include <Core/Compatibility.h> |
fd616c4a5904
Added mechanism to prevent callbacks from being sent on dead WebAssemblyViewport objects
Benjamin Golinvaux <bgo@osimis.io>
parents:
1306
diff
changeset
|
24 |
fd616c4a5904
Added mechanism to prevent callbacks from being sent on dead WebAssemblyViewport objects
Benjamin Golinvaux <bgo@osimis.io>
parents:
1306
diff
changeset
|
25 #include <boost/shared_ptr.hpp> |
fd616c4a5904
Added mechanism to prevent callbacks from being sent on dead WebAssemblyViewport objects
Benjamin Golinvaux <bgo@osimis.io>
parents:
1306
diff
changeset
|
26 |
1080
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
27 #include <string> |
1101
141593f1aa88
fix build on ubuntu
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1080
diff
changeset
|
28 #include <stdint.h> |
1166
f68da12e852b
Added scientific notation support to StringToDouble
Benjamin Golinvaux <bgo@osimis.io>
parents:
1101
diff
changeset
|
29 #include <math.h> |
f68da12e852b
Added scientific notation support to StringToDouble
Benjamin Golinvaux <bgo@osimis.io>
parents:
1101
diff
changeset
|
30 |
1328
fd616c4a5904
Added mechanism to prevent callbacks from being sent on dead WebAssemblyViewport objects
Benjamin Golinvaux <bgo@osimis.io>
parents:
1306
diff
changeset
|
31 #include <memory> |
1080
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
32 |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
33 namespace OrthancStone |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
34 { |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
35 namespace GenericToolbox |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
36 { |
1306
fef1ec42a7db
Some docs + headers added to CMake for easier VS browsing + tiny predecl
Benjamin Golinvaux <bgo@osimis.io>
parents:
1279
diff
changeset
|
37 /** |
fef1ec42a7db
Some docs + headers added to CMake for easier VS browsing + tiny predecl
Benjamin Golinvaux <bgo@osimis.io>
parents:
1279
diff
changeset
|
38 Fast floating point string validation. |
fef1ec42a7db
Some docs + headers added to CMake for easier VS browsing + tiny predecl
Benjamin Golinvaux <bgo@osimis.io>
parents:
1279
diff
changeset
|
39 No trimming applied, so the input must match regex |
fef1ec42a7db
Some docs + headers added to CMake for easier VS browsing + tiny predecl
Benjamin Golinvaux <bgo@osimis.io>
parents:
1279
diff
changeset
|
40 /^[-]?[0-9]*\.?[0-9]*([eE][-+]?[0-9]+)?$/ |
fef1ec42a7db
Some docs + headers added to CMake for easier VS browsing + tiny predecl
Benjamin Golinvaux <bgo@osimis.io>
parents:
1279
diff
changeset
|
41 The following are allowed as edge cases: "" and "-" |
fef1ec42a7db
Some docs + headers added to CMake for easier VS browsing + tiny predecl
Benjamin Golinvaux <bgo@osimis.io>
parents:
1279
diff
changeset
|
42 */ |
1080
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
43 inline bool LegitDoubleString(const char* text) |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
44 { |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
45 const char* p = text; |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
46 if(*p == '-') |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
47 p++; |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
48 size_t period = 0; |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
49 while(*p != 0) |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
50 { |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
51 if (*p >= '0' && *p <= '9') |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
52 ++p; |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
53 else if(*p == '.') |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
54 { |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
55 if(period > 0) |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
56 return false; |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
57 else |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
58 period++; |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
59 ++p; |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
60 } |
1166
f68da12e852b
Added scientific notation support to StringToDouble
Benjamin Golinvaux <bgo@osimis.io>
parents:
1101
diff
changeset
|
61 else if (*p == 'e' || *p == 'E') |
f68da12e852b
Added scientific notation support to StringToDouble
Benjamin Golinvaux <bgo@osimis.io>
parents:
1101
diff
changeset
|
62 { |
f68da12e852b
Added scientific notation support to StringToDouble
Benjamin Golinvaux <bgo@osimis.io>
parents:
1101
diff
changeset
|
63 ++p; |
f68da12e852b
Added scientific notation support to StringToDouble
Benjamin Golinvaux <bgo@osimis.io>
parents:
1101
diff
changeset
|
64 if (*p == '-' || *p == '+') |
f68da12e852b
Added scientific notation support to StringToDouble
Benjamin Golinvaux <bgo@osimis.io>
parents:
1101
diff
changeset
|
65 ++p; |
f68da12e852b
Added scientific notation support to StringToDouble
Benjamin Golinvaux <bgo@osimis.io>
parents:
1101
diff
changeset
|
66 // "e+"/"E+" "e-"/"E-" or "e"/"E" must be followed by a number |
f68da12e852b
Added scientific notation support to StringToDouble
Benjamin Golinvaux <bgo@osimis.io>
parents:
1101
diff
changeset
|
67 if (!(*p >= '0' && *p <= '9')) |
f68da12e852b
Added scientific notation support to StringToDouble
Benjamin Golinvaux <bgo@osimis.io>
parents:
1101
diff
changeset
|
68 return false; |
f68da12e852b
Added scientific notation support to StringToDouble
Benjamin Golinvaux <bgo@osimis.io>
parents:
1101
diff
changeset
|
69 |
f68da12e852b
Added scientific notation support to StringToDouble
Benjamin Golinvaux <bgo@osimis.io>
parents:
1101
diff
changeset
|
70 // these must be the last in the string |
f68da12e852b
Added scientific notation support to StringToDouble
Benjamin Golinvaux <bgo@osimis.io>
parents:
1101
diff
changeset
|
71 while(*p >= '0' && *p <= '9') |
f68da12e852b
Added scientific notation support to StringToDouble
Benjamin Golinvaux <bgo@osimis.io>
parents:
1101
diff
changeset
|
72 ++p; |
f68da12e852b
Added scientific notation support to StringToDouble
Benjamin Golinvaux <bgo@osimis.io>
parents:
1101
diff
changeset
|
73 |
f68da12e852b
Added scientific notation support to StringToDouble
Benjamin Golinvaux <bgo@osimis.io>
parents:
1101
diff
changeset
|
74 return (*p == 0); |
f68da12e852b
Added scientific notation support to StringToDouble
Benjamin Golinvaux <bgo@osimis.io>
parents:
1101
diff
changeset
|
75 } |
1080
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
76 else |
1166
f68da12e852b
Added scientific notation support to StringToDouble
Benjamin Golinvaux <bgo@osimis.io>
parents:
1101
diff
changeset
|
77 { |
1080
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
78 return false; |
1166
f68da12e852b
Added scientific notation support to StringToDouble
Benjamin Golinvaux <bgo@osimis.io>
parents:
1101
diff
changeset
|
79 } |
1080
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
80 } |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
81 return true; |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
82 } |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
83 |
1306
fef1ec42a7db
Some docs + headers added to CMake for easier VS browsing + tiny predecl
Benjamin Golinvaux <bgo@osimis.io>
parents:
1279
diff
changeset
|
84 /** |
fef1ec42a7db
Some docs + headers added to CMake for easier VS browsing + tiny predecl
Benjamin Golinvaux <bgo@osimis.io>
parents:
1279
diff
changeset
|
85 Fast integer string validation. |
fef1ec42a7db
Some docs + headers added to CMake for easier VS browsing + tiny predecl
Benjamin Golinvaux <bgo@osimis.io>
parents:
1279
diff
changeset
|
86 No trimming applied, so the input must match regex /^-?[0-9]*$/ |
fef1ec42a7db
Some docs + headers added to CMake for easier VS browsing + tiny predecl
Benjamin Golinvaux <bgo@osimis.io>
parents:
1279
diff
changeset
|
87 The following are allowed as edge cases: "" and "-" |
fef1ec42a7db
Some docs + headers added to CMake for easier VS browsing + tiny predecl
Benjamin Golinvaux <bgo@osimis.io>
parents:
1279
diff
changeset
|
88 */ |
1080
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
89 inline bool LegitIntegerString(const char* text) |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
90 { |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
91 const char* p = text; |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
92 if (*p == '-') |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
93 p++; |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
94 while (*p != 0) |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
95 { |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
96 if (*p >= '0' && *p <= '9') |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
97 ++p; |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
98 else |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
99 return false; |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
100 } |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
101 return true; |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
102 } |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
103 |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
104 /* |
1306
fef1ec42a7db
Some docs + headers added to CMake for easier VS browsing + tiny predecl
Benjamin Golinvaux <bgo@osimis.io>
parents:
1279
diff
changeset
|
105 Fast string --> double conversion. |
fef1ec42a7db
Some docs + headers added to CMake for easier VS browsing + tiny predecl
Benjamin Golinvaux <bgo@osimis.io>
parents:
1279
diff
changeset
|
106 Must pass the LegitDoubleString test |
fef1ec42a7db
Some docs + headers added to CMake for easier VS browsing + tiny predecl
Benjamin Golinvaux <bgo@osimis.io>
parents:
1279
diff
changeset
|
107 |
1080
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
108 String to doubles with at most 18 digits |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
109 */ |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
110 inline bool StringToDouble(double& r, const char* text) |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
111 { |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
112 if(!LegitDoubleString(text)) |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
113 return false; |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
114 |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
115 static const double FRAC_FACTORS[] = |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
116 { |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
117 1.0, |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
118 0.1, |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
119 0.01, |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
120 0.001, |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
121 0.0001, |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
122 0.00001, |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
123 0.000001, |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
124 0.0000001, |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
125 0.00000001, |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
126 0.000000001, |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
127 0.0000000001, |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
128 0.00000000001, |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
129 0.000000000001, |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
130 0.0000000000001, |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
131 0.00000000000001, |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
132 0.000000000000001, |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
133 0.0000000000000001, |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
134 0.00000000000000001, |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
135 0.000000000000000001, |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
136 0.0000000000000000001 |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
137 }; |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
138 const size_t FRAC_FACTORS_LEN = sizeof(FRAC_FACTORS)/sizeof(double); |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
139 |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
140 r = 0.0; |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
141 double neg = 1.0; |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
142 const char* p = text; |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
143 |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
144 if (*p == '-') |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
145 { |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
146 neg = -1.0; |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
147 ++p; |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
148 } |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
149 // 12345.67890 |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
150 while (*p >= '0' && *p <= '9') |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
151 { |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
152 r = (r*10.0) + (*p - '0'); // 1 12 123 123 12345 |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
153 ++p; |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
154 } |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
155 if (*p == '.') |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
156 { |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
157 double f = 0.0; |
1192 | 158 size_t n = 1; |
1080
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
159 ++p; |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
160 while (*p >= '0' && *p <= '9' && n < FRAC_FACTORS_LEN) |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
161 { |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
162 f += (*p - '0') * FRAC_FACTORS[n]; |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
163 ++p; |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
164 ++n; |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
165 } |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
166 r += f; |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
167 } |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
168 r *= neg; |
1166
f68da12e852b
Added scientific notation support to StringToDouble
Benjamin Golinvaux <bgo@osimis.io>
parents:
1101
diff
changeset
|
169 |
f68da12e852b
Added scientific notation support to StringToDouble
Benjamin Golinvaux <bgo@osimis.io>
parents:
1101
diff
changeset
|
170 // skip the remaining numbers until we reach not-a-digit (either the |
f68da12e852b
Added scientific notation support to StringToDouble
Benjamin Golinvaux <bgo@osimis.io>
parents:
1101
diff
changeset
|
171 // end of the string OR the scientific notation symbol) |
f68da12e852b
Added scientific notation support to StringToDouble
Benjamin Golinvaux <bgo@osimis.io>
parents:
1101
diff
changeset
|
172 while ((*p >= '0' && *p <= '9')) |
f68da12e852b
Added scientific notation support to StringToDouble
Benjamin Golinvaux <bgo@osimis.io>
parents:
1101
diff
changeset
|
173 ++p; |
f68da12e852b
Added scientific notation support to StringToDouble
Benjamin Golinvaux <bgo@osimis.io>
parents:
1101
diff
changeset
|
174 |
f68da12e852b
Added scientific notation support to StringToDouble
Benjamin Golinvaux <bgo@osimis.io>
parents:
1101
diff
changeset
|
175 if (*p == 0 ) |
f68da12e852b
Added scientific notation support to StringToDouble
Benjamin Golinvaux <bgo@osimis.io>
parents:
1101
diff
changeset
|
176 { |
1080
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
177 return true; |
1166
f68da12e852b
Added scientific notation support to StringToDouble
Benjamin Golinvaux <bgo@osimis.io>
parents:
1101
diff
changeset
|
178 } |
f68da12e852b
Added scientific notation support to StringToDouble
Benjamin Golinvaux <bgo@osimis.io>
parents:
1101
diff
changeset
|
179 else if ((*p == 'e') || (*p == 'E')) |
f68da12e852b
Added scientific notation support to StringToDouble
Benjamin Golinvaux <bgo@osimis.io>
parents:
1101
diff
changeset
|
180 { |
f68da12e852b
Added scientific notation support to StringToDouble
Benjamin Golinvaux <bgo@osimis.io>
parents:
1101
diff
changeset
|
181 // process the scientific notation |
f68da12e852b
Added scientific notation support to StringToDouble
Benjamin Golinvaux <bgo@osimis.io>
parents:
1101
diff
changeset
|
182 double sign; // no init is safe (read below) |
f68da12e852b
Added scientific notation support to StringToDouble
Benjamin Golinvaux <bgo@osimis.io>
parents:
1101
diff
changeset
|
183 ++p; |
f68da12e852b
Added scientific notation support to StringToDouble
Benjamin Golinvaux <bgo@osimis.io>
parents:
1101
diff
changeset
|
184 if (*p == '-') |
f68da12e852b
Added scientific notation support to StringToDouble
Benjamin Golinvaux <bgo@osimis.io>
parents:
1101
diff
changeset
|
185 { |
f68da12e852b
Added scientific notation support to StringToDouble
Benjamin Golinvaux <bgo@osimis.io>
parents:
1101
diff
changeset
|
186 sign = -1.0; |
f68da12e852b
Added scientific notation support to StringToDouble
Benjamin Golinvaux <bgo@osimis.io>
parents:
1101
diff
changeset
|
187 // point to first number |
f68da12e852b
Added scientific notation support to StringToDouble
Benjamin Golinvaux <bgo@osimis.io>
parents:
1101
diff
changeset
|
188 ++p; |
f68da12e852b
Added scientific notation support to StringToDouble
Benjamin Golinvaux <bgo@osimis.io>
parents:
1101
diff
changeset
|
189 } |
f68da12e852b
Added scientific notation support to StringToDouble
Benjamin Golinvaux <bgo@osimis.io>
parents:
1101
diff
changeset
|
190 else if (*p == '+') |
f68da12e852b
Added scientific notation support to StringToDouble
Benjamin Golinvaux <bgo@osimis.io>
parents:
1101
diff
changeset
|
191 { |
f68da12e852b
Added scientific notation support to StringToDouble
Benjamin Golinvaux <bgo@osimis.io>
parents:
1101
diff
changeset
|
192 sign = 1.0; |
f68da12e852b
Added scientific notation support to StringToDouble
Benjamin Golinvaux <bgo@osimis.io>
parents:
1101
diff
changeset
|
193 // point to first number |
f68da12e852b
Added scientific notation support to StringToDouble
Benjamin Golinvaux <bgo@osimis.io>
parents:
1101
diff
changeset
|
194 ++p; |
f68da12e852b
Added scientific notation support to StringToDouble
Benjamin Golinvaux <bgo@osimis.io>
parents:
1101
diff
changeset
|
195 } |
f68da12e852b
Added scientific notation support to StringToDouble
Benjamin Golinvaux <bgo@osimis.io>
parents:
1101
diff
changeset
|
196 else if (*p >= '0' && *p <= '9') |
f68da12e852b
Added scientific notation support to StringToDouble
Benjamin Golinvaux <bgo@osimis.io>
parents:
1101
diff
changeset
|
197 { |
f68da12e852b
Added scientific notation support to StringToDouble
Benjamin Golinvaux <bgo@osimis.io>
parents:
1101
diff
changeset
|
198 sign = 1.0; |
f68da12e852b
Added scientific notation support to StringToDouble
Benjamin Golinvaux <bgo@osimis.io>
parents:
1101
diff
changeset
|
199 } |
f68da12e852b
Added scientific notation support to StringToDouble
Benjamin Golinvaux <bgo@osimis.io>
parents:
1101
diff
changeset
|
200 else |
f68da12e852b
Added scientific notation support to StringToDouble
Benjamin Golinvaux <bgo@osimis.io>
parents:
1101
diff
changeset
|
201 { |
f68da12e852b
Added scientific notation support to StringToDouble
Benjamin Golinvaux <bgo@osimis.io>
parents:
1101
diff
changeset
|
202 // only a sign char or a number is allowed |
f68da12e852b
Added scientific notation support to StringToDouble
Benjamin Golinvaux <bgo@osimis.io>
parents:
1101
diff
changeset
|
203 return false; |
f68da12e852b
Added scientific notation support to StringToDouble
Benjamin Golinvaux <bgo@osimis.io>
parents:
1101
diff
changeset
|
204 } |
f68da12e852b
Added scientific notation support to StringToDouble
Benjamin Golinvaux <bgo@osimis.io>
parents:
1101
diff
changeset
|
205 // now p points to the absolute value of the exponent |
f68da12e852b
Added scientific notation support to StringToDouble
Benjamin Golinvaux <bgo@osimis.io>
parents:
1101
diff
changeset
|
206 double exp = 0; |
f68da12e852b
Added scientific notation support to StringToDouble
Benjamin Golinvaux <bgo@osimis.io>
parents:
1101
diff
changeset
|
207 while (*p >= '0' && *p <= '9') |
f68da12e852b
Added scientific notation support to StringToDouble
Benjamin Golinvaux <bgo@osimis.io>
parents:
1101
diff
changeset
|
208 { |
f68da12e852b
Added scientific notation support to StringToDouble
Benjamin Golinvaux <bgo@osimis.io>
parents:
1101
diff
changeset
|
209 exp = (exp * 10.0) + static_cast<double>(*p - '0'); // 1 12 123 123 12345 |
f68da12e852b
Added scientific notation support to StringToDouble
Benjamin Golinvaux <bgo@osimis.io>
parents:
1101
diff
changeset
|
210 ++p; |
f68da12e852b
Added scientific notation support to StringToDouble
Benjamin Golinvaux <bgo@osimis.io>
parents:
1101
diff
changeset
|
211 } |
f68da12e852b
Added scientific notation support to StringToDouble
Benjamin Golinvaux <bgo@osimis.io>
parents:
1101
diff
changeset
|
212 // now we have our exponent. put a sign on it. |
f68da12e852b
Added scientific notation support to StringToDouble
Benjamin Golinvaux <bgo@osimis.io>
parents:
1101
diff
changeset
|
213 exp *= sign; |
f68da12e852b
Added scientific notation support to StringToDouble
Benjamin Golinvaux <bgo@osimis.io>
parents:
1101
diff
changeset
|
214 double scFac = ::pow(10.0, exp); |
f68da12e852b
Added scientific notation support to StringToDouble
Benjamin Golinvaux <bgo@osimis.io>
parents:
1101
diff
changeset
|
215 r *= scFac; |
f68da12e852b
Added scientific notation support to StringToDouble
Benjamin Golinvaux <bgo@osimis.io>
parents:
1101
diff
changeset
|
216 |
f68da12e852b
Added scientific notation support to StringToDouble
Benjamin Golinvaux <bgo@osimis.io>
parents:
1101
diff
changeset
|
217 // only allowed symbol here is EOS |
f68da12e852b
Added scientific notation support to StringToDouble
Benjamin Golinvaux <bgo@osimis.io>
parents:
1101
diff
changeset
|
218 return (*p == 0); |
f68da12e852b
Added scientific notation support to StringToDouble
Benjamin Golinvaux <bgo@osimis.io>
parents:
1101
diff
changeset
|
219 } |
1080
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
220 else |
1166
f68da12e852b
Added scientific notation support to StringToDouble
Benjamin Golinvaux <bgo@osimis.io>
parents:
1101
diff
changeset
|
221 { |
f68da12e852b
Added scientific notation support to StringToDouble
Benjamin Golinvaux <bgo@osimis.io>
parents:
1101
diff
changeset
|
222 // not allowed |
1080
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
223 return false; |
1166
f68da12e852b
Added scientific notation support to StringToDouble
Benjamin Golinvaux <bgo@osimis.io>
parents:
1101
diff
changeset
|
224 } |
1080
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
225 } |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
226 |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
227 inline bool StringToDouble(double& r, const std::string& text) |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
228 { |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
229 return StringToDouble(r, text.c_str()); |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
230 } |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
231 |
1306
fef1ec42a7db
Some docs + headers added to CMake for easier VS browsing + tiny predecl
Benjamin Golinvaux <bgo@osimis.io>
parents:
1279
diff
changeset
|
232 /** |
fef1ec42a7db
Some docs + headers added to CMake for easier VS browsing + tiny predecl
Benjamin Golinvaux <bgo@osimis.io>
parents:
1279
diff
changeset
|
233 Fast string to integer conversion. Leading zeroes and minus are accepted, |
fef1ec42a7db
Some docs + headers added to CMake for easier VS browsing + tiny predecl
Benjamin Golinvaux <bgo@osimis.io>
parents:
1279
diff
changeset
|
234 but a leading + sign is NOT. |
fef1ec42a7db
Some docs + headers added to CMake for easier VS browsing + tiny predecl
Benjamin Golinvaux <bgo@osimis.io>
parents:
1279
diff
changeset
|
235 Must pass the LegitIntegerString function test. |
fef1ec42a7db
Some docs + headers added to CMake for easier VS browsing + tiny predecl
Benjamin Golinvaux <bgo@osimis.io>
parents:
1279
diff
changeset
|
236 In addition, an empty string (or lone minus sign) yields 0. |
fef1ec42a7db
Some docs + headers added to CMake for easier VS browsing + tiny predecl
Benjamin Golinvaux <bgo@osimis.io>
parents:
1279
diff
changeset
|
237 */ |
fef1ec42a7db
Some docs + headers added to CMake for easier VS browsing + tiny predecl
Benjamin Golinvaux <bgo@osimis.io>
parents:
1279
diff
changeset
|
238 |
1080
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
239 template<typename T> |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
240 inline bool StringToInteger(T& r, const char* text) |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
241 { |
1306
fef1ec42a7db
Some docs + headers added to CMake for easier VS browsing + tiny predecl
Benjamin Golinvaux <bgo@osimis.io>
parents:
1279
diff
changeset
|
242 if (!LegitIntegerString(text)) |
1080
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
243 return false; |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
244 |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
245 r = 0; |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
246 T neg = 1; |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
247 const char* p = text; |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
248 |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
249 if (*p == '-') |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
250 { |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
251 neg = -1; |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
252 ++p; |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
253 } |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
254 while (*p >= '0' && *p <= '9') |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
255 { |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
256 r = (r * 10) + (*p - '0'); // 1 12 123 123 12345 |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
257 ++p; |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
258 } |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
259 r *= neg; |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
260 if (*p == 0) |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
261 return true; |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
262 else |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
263 return false; |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
264 } |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
265 |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
266 template<typename T> |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
267 inline bool StringToInteger(T& r, const std::string& text) |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
268 { |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
269 return StringToInteger<T>(r, text.c_str()); |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
270 } |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
271 |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
272 /** |
1306
fef1ec42a7db
Some docs + headers added to CMake for easier VS browsing + tiny predecl
Benjamin Golinvaux <bgo@osimis.io>
parents:
1279
diff
changeset
|
273 if input is "rgb(12,23,255)" --> function fills `red`, `green` and `blue` and returns true |
fef1ec42a7db
Some docs + headers added to CMake for easier VS browsing + tiny predecl
Benjamin Golinvaux <bgo@osimis.io>
parents:
1279
diff
changeset
|
274 else ("everything else") --> function returns false and leaves all values untouched |
1080
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
275 */ |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
276 bool GetRgbValuesFromString(uint8_t& red, uint8_t& green, uint8_t& blue, const char* text); |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
277 |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
278 /** |
1306
fef1ec42a7db
Some docs + headers added to CMake for easier VS browsing + tiny predecl
Benjamin Golinvaux <bgo@osimis.io>
parents:
1279
diff
changeset
|
279 See main overload |
1080
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
280 */ |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
281 inline bool GetRgbValuesFromString(uint8_t& red, uint8_t& green, uint8_t& blue, const std::string& text) |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
282 { |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
283 return GetRgbValuesFromString(red, green, blue, text.c_str()); |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
284 } |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
285 |
1306
fef1ec42a7db
Some docs + headers added to CMake for easier VS browsing + tiny predecl
Benjamin Golinvaux <bgo@osimis.io>
parents:
1279
diff
changeset
|
286 /** |
fef1ec42a7db
Some docs + headers added to CMake for easier VS browsing + tiny predecl
Benjamin Golinvaux <bgo@osimis.io>
parents:
1279
diff
changeset
|
287 Same as GetRgbValuesFromString |
fef1ec42a7db
Some docs + headers added to CMake for easier VS browsing + tiny predecl
Benjamin Golinvaux <bgo@osimis.io>
parents:
1279
diff
changeset
|
288 */ |
1328
fd616c4a5904
Added mechanism to prevent callbacks from being sent on dead WebAssemblyViewport objects
Benjamin Golinvaux <bgo@osimis.io>
parents:
1306
diff
changeset
|
289 bool GetRgbaValuesFromString(uint8_t& red, |
fd616c4a5904
Added mechanism to prevent callbacks from being sent on dead WebAssemblyViewport objects
Benjamin Golinvaux <bgo@osimis.io>
parents:
1306
diff
changeset
|
290 uint8_t& green, |
fd616c4a5904
Added mechanism to prevent callbacks from being sent on dead WebAssemblyViewport objects
Benjamin Golinvaux <bgo@osimis.io>
parents:
1306
diff
changeset
|
291 uint8_t& blue, |
fd616c4a5904
Added mechanism to prevent callbacks from being sent on dead WebAssemblyViewport objects
Benjamin Golinvaux <bgo@osimis.io>
parents:
1306
diff
changeset
|
292 uint8_t& alpha, |
fd616c4a5904
Added mechanism to prevent callbacks from being sent on dead WebAssemblyViewport objects
Benjamin Golinvaux <bgo@osimis.io>
parents:
1306
diff
changeset
|
293 const char* text); |
1080
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
294 |
1306
fef1ec42a7db
Some docs + headers added to CMake for easier VS browsing + tiny predecl
Benjamin Golinvaux <bgo@osimis.io>
parents:
1279
diff
changeset
|
295 /** |
fef1ec42a7db
Some docs + headers added to CMake for easier VS browsing + tiny predecl
Benjamin Golinvaux <bgo@osimis.io>
parents:
1279
diff
changeset
|
296 Same as GetRgbValuesFromString |
fef1ec42a7db
Some docs + headers added to CMake for easier VS browsing + tiny predecl
Benjamin Golinvaux <bgo@osimis.io>
parents:
1279
diff
changeset
|
297 */ |
1328
fd616c4a5904
Added mechanism to prevent callbacks from being sent on dead WebAssemblyViewport objects
Benjamin Golinvaux <bgo@osimis.io>
parents:
1306
diff
changeset
|
298 inline bool GetRgbaValuesFromString(uint8_t& red, |
fd616c4a5904
Added mechanism to prevent callbacks from being sent on dead WebAssemblyViewport objects
Benjamin Golinvaux <bgo@osimis.io>
parents:
1306
diff
changeset
|
299 uint8_t& green, |
fd616c4a5904
Added mechanism to prevent callbacks from being sent on dead WebAssemblyViewport objects
Benjamin Golinvaux <bgo@osimis.io>
parents:
1306
diff
changeset
|
300 uint8_t& blue, |
fd616c4a5904
Added mechanism to prevent callbacks from being sent on dead WebAssemblyViewport objects
Benjamin Golinvaux <bgo@osimis.io>
parents:
1306
diff
changeset
|
301 uint8_t& alpha, |
fd616c4a5904
Added mechanism to prevent callbacks from being sent on dead WebAssemblyViewport objects
Benjamin Golinvaux <bgo@osimis.io>
parents:
1306
diff
changeset
|
302 const std::string& text) |
1080
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
303 { |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
304 return GetRgbaValuesFromString(red, green, blue, alpha, text.c_str()); |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
305 } |
1328
fd616c4a5904
Added mechanism to prevent callbacks from being sent on dead WebAssemblyViewport objects
Benjamin Golinvaux <bgo@osimis.io>
parents:
1306
diff
changeset
|
306 |
fd616c4a5904
Added mechanism to prevent callbacks from being sent on dead WebAssemblyViewport objects
Benjamin Golinvaux <bgo@osimis.io>
parents:
1306
diff
changeset
|
307 |
fd616c4a5904
Added mechanism to prevent callbacks from being sent on dead WebAssemblyViewport objects
Benjamin Golinvaux <bgo@osimis.io>
parents:
1306
diff
changeset
|
308 template<typename Wrappee> |
fd616c4a5904
Added mechanism to prevent callbacks from being sent on dead WebAssemblyViewport objects
Benjamin Golinvaux <bgo@osimis.io>
parents:
1306
diff
changeset
|
309 struct HoldingRef |
fd616c4a5904
Added mechanism to prevent callbacks from being sent on dead WebAssemblyViewport objects
Benjamin Golinvaux <bgo@osimis.io>
parents:
1306
diff
changeset
|
310 { |
fd616c4a5904
Added mechanism to prevent callbacks from being sent on dead WebAssemblyViewport objects
Benjamin Golinvaux <bgo@osimis.io>
parents:
1306
diff
changeset
|
311 HoldingRef(Wrappee* object) |
fd616c4a5904
Added mechanism to prevent callbacks from being sent on dead WebAssemblyViewport objects
Benjamin Golinvaux <bgo@osimis.io>
parents:
1306
diff
changeset
|
312 { |
fd616c4a5904
Added mechanism to prevent callbacks from being sent on dead WebAssemblyViewport objects
Benjamin Golinvaux <bgo@osimis.io>
parents:
1306
diff
changeset
|
313 // a crash here means that the object is not stored in a shared_ptr |
fd616c4a5904
Added mechanism to prevent callbacks from being sent on dead WebAssemblyViewport objects
Benjamin Golinvaux <bgo@osimis.io>
parents:
1306
diff
changeset
|
314 // using shared_ptr is mandatory for this |
fd616c4a5904
Added mechanism to prevent callbacks from being sent on dead WebAssemblyViewport objects
Benjamin Golinvaux <bgo@osimis.io>
parents:
1306
diff
changeset
|
315 object_ = object->shared_from_this(); |
fd616c4a5904
Added mechanism to prevent callbacks from being sent on dead WebAssemblyViewport objects
Benjamin Golinvaux <bgo@osimis.io>
parents:
1306
diff
changeset
|
316 } |
fd616c4a5904
Added mechanism to prevent callbacks from being sent on dead WebAssemblyViewport objects
Benjamin Golinvaux <bgo@osimis.io>
parents:
1306
diff
changeset
|
317 boost::shared_ptr<Wrappee> object_; |
fd616c4a5904
Added mechanism to prevent callbacks from being sent on dead WebAssemblyViewport objects
Benjamin Golinvaux <bgo@osimis.io>
parents:
1306
diff
changeset
|
318 |
fd616c4a5904
Added mechanism to prevent callbacks from being sent on dead WebAssemblyViewport objects
Benjamin Golinvaux <bgo@osimis.io>
parents:
1306
diff
changeset
|
319 static void* Wrap(Wrappee* object) |
fd616c4a5904
Added mechanism to prevent callbacks from being sent on dead WebAssemblyViewport objects
Benjamin Golinvaux <bgo@osimis.io>
parents:
1306
diff
changeset
|
320 { |
fd616c4a5904
Added mechanism to prevent callbacks from being sent on dead WebAssemblyViewport objects
Benjamin Golinvaux <bgo@osimis.io>
parents:
1306
diff
changeset
|
321 std::unique_ptr<HoldingRef<Wrappee > > up(new HoldingRef<Wrappee>(object)); |
fd616c4a5904
Added mechanism to prevent callbacks from being sent on dead WebAssemblyViewport objects
Benjamin Golinvaux <bgo@osimis.io>
parents:
1306
diff
changeset
|
322 void* userData = reinterpret_cast<void*>(up.release()); |
fd616c4a5904
Added mechanism to prevent callbacks from being sent on dead WebAssemblyViewport objects
Benjamin Golinvaux <bgo@osimis.io>
parents:
1306
diff
changeset
|
323 return userData; |
fd616c4a5904
Added mechanism to prevent callbacks from being sent on dead WebAssemblyViewport objects
Benjamin Golinvaux <bgo@osimis.io>
parents:
1306
diff
changeset
|
324 } |
fd616c4a5904
Added mechanism to prevent callbacks from being sent on dead WebAssemblyViewport objects
Benjamin Golinvaux <bgo@osimis.io>
parents:
1306
diff
changeset
|
325 |
fd616c4a5904
Added mechanism to prevent callbacks from being sent on dead WebAssemblyViewport objects
Benjamin Golinvaux <bgo@osimis.io>
parents:
1306
diff
changeset
|
326 static boost::shared_ptr<Wrappee> Unwrap(void* userData) |
fd616c4a5904
Added mechanism to prevent callbacks from being sent on dead WebAssemblyViewport objects
Benjamin Golinvaux <bgo@osimis.io>
parents:
1306
diff
changeset
|
327 { |
fd616c4a5904
Added mechanism to prevent callbacks from being sent on dead WebAssemblyViewport objects
Benjamin Golinvaux <bgo@osimis.io>
parents:
1306
diff
changeset
|
328 // the stored shared_ptr will be deleted because of wrapping |
fd616c4a5904
Added mechanism to prevent callbacks from being sent on dead WebAssemblyViewport objects
Benjamin Golinvaux <bgo@osimis.io>
parents:
1306
diff
changeset
|
329 // the data in a RAII |
fd616c4a5904
Added mechanism to prevent callbacks from being sent on dead WebAssemblyViewport objects
Benjamin Golinvaux <bgo@osimis.io>
parents:
1306
diff
changeset
|
330 std::unique_ptr<HoldingRef<Wrappee > > |
fd616c4a5904
Added mechanism to prevent callbacks from being sent on dead WebAssemblyViewport objects
Benjamin Golinvaux <bgo@osimis.io>
parents:
1306
diff
changeset
|
331 up(reinterpret_cast<HoldingRef<Wrappee>*>(userData)); |
fd616c4a5904
Added mechanism to prevent callbacks from being sent on dead WebAssemblyViewport objects
Benjamin Golinvaux <bgo@osimis.io>
parents:
1306
diff
changeset
|
332 boost::shared_ptr<Wrappee> object = up->object_; |
fd616c4a5904
Added mechanism to prevent callbacks from being sent on dead WebAssemblyViewport objects
Benjamin Golinvaux <bgo@osimis.io>
parents:
1306
diff
changeset
|
333 return object; |
fd616c4a5904
Added mechanism to prevent callbacks from being sent on dead WebAssemblyViewport objects
Benjamin Golinvaux <bgo@osimis.io>
parents:
1306
diff
changeset
|
334 } |
fd616c4a5904
Added mechanism to prevent callbacks from being sent on dead WebAssemblyViewport objects
Benjamin Golinvaux <bgo@osimis.io>
parents:
1306
diff
changeset
|
335 }; |
1080
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
336 } |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
337 } |