Mercurial > hg > orthanc-stone
annotate OrthancStone/Sources/Toolbox/GenericToolbox.h @ 1591:5887a4f8594b
moving platform-specific files out of the "OrthancStone" folder
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Fri, 23 Oct 2020 13:15:03 +0200 |
parents | 244ad1e4e76a |
children | 4fb8fdf03314 |
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 |
1455
30deba7bc8e2
simplifying include_directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1331
diff
changeset
|
23 #include <Compatibility.h> |
30deba7bc8e2
simplifying include_directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1331
diff
changeset
|
24 #include <OrthancException.h> |
1328
fd616c4a5904
Added mechanism to prevent callbacks from being sent on dead WebAssemblyViewport objects
Benjamin Golinvaux <bgo@osimis.io>
parents:
1306
diff
changeset
|
25 |
fd616c4a5904
Added mechanism to prevent callbacks from being sent on dead WebAssemblyViewport objects
Benjamin Golinvaux <bgo@osimis.io>
parents:
1306
diff
changeset
|
26 #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
|
27 |
1080
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
28 #include <string> |
1101
141593f1aa88
fix build on ubuntu
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1080
diff
changeset
|
29 #include <stdint.h> |
1166
f68da12e852b
Added scientific notation support to StringToDouble
Benjamin Golinvaux <bgo@osimis.io>
parents:
1101
diff
changeset
|
30 #include <math.h> |
f68da12e852b
Added scientific notation support to StringToDouble
Benjamin Golinvaux <bgo@osimis.io>
parents:
1101
diff
changeset
|
31 |
1328
fd616c4a5904
Added mechanism to prevent callbacks from being sent on dead WebAssemblyViewport objects
Benjamin Golinvaux <bgo@osimis.io>
parents:
1306
diff
changeset
|
32 #include <memory> |
1080
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
33 |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
34 namespace OrthancStone |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
35 { |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
36 namespace GenericToolbox |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
37 { |
1306
fef1ec42a7db
Some docs + headers added to CMake for easier VS browsing + tiny predecl
Benjamin Golinvaux <bgo@osimis.io>
parents:
1279
diff
changeset
|
38 /** |
fef1ec42a7db
Some docs + headers added to CMake for easier VS browsing + tiny predecl
Benjamin Golinvaux <bgo@osimis.io>
parents:
1279
diff
changeset
|
39 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
|
40 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
|
41 /^[-]?[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
|
42 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
|
43 */ |
1080
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
44 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
|
45 { |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
46 const char* p = text; |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
47 if(*p == '-') |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
48 p++; |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
49 size_t period = 0; |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
50 while(*p != 0) |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
51 { |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
52 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
|
53 ++p; |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
54 else if(*p == '.') |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
55 { |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
56 if(period > 0) |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
57 return false; |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
58 else |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
59 period++; |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
60 ++p; |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
61 } |
1166
f68da12e852b
Added scientific notation support to StringToDouble
Benjamin Golinvaux <bgo@osimis.io>
parents:
1101
diff
changeset
|
62 else if (*p == 'e' || *p == 'E') |
f68da12e852b
Added scientific notation support to StringToDouble
Benjamin Golinvaux <bgo@osimis.io>
parents:
1101
diff
changeset
|
63 { |
f68da12e852b
Added scientific notation support to StringToDouble
Benjamin Golinvaux <bgo@osimis.io>
parents:
1101
diff
changeset
|
64 ++p; |
f68da12e852b
Added scientific notation support to StringToDouble
Benjamin Golinvaux <bgo@osimis.io>
parents:
1101
diff
changeset
|
65 if (*p == '-' || *p == '+') |
f68da12e852b
Added scientific notation support to StringToDouble
Benjamin Golinvaux <bgo@osimis.io>
parents:
1101
diff
changeset
|
66 ++p; |
f68da12e852b
Added scientific notation support to StringToDouble
Benjamin Golinvaux <bgo@osimis.io>
parents:
1101
diff
changeset
|
67 // "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
|
68 if (!(*p >= '0' && *p <= '9')) |
f68da12e852b
Added scientific notation support to StringToDouble
Benjamin Golinvaux <bgo@osimis.io>
parents:
1101
diff
changeset
|
69 return false; |
f68da12e852b
Added scientific notation support to StringToDouble
Benjamin Golinvaux <bgo@osimis.io>
parents:
1101
diff
changeset
|
70 |
f68da12e852b
Added scientific notation support to StringToDouble
Benjamin Golinvaux <bgo@osimis.io>
parents:
1101
diff
changeset
|
71 // these must be the last in the string |
f68da12e852b
Added scientific notation support to StringToDouble
Benjamin Golinvaux <bgo@osimis.io>
parents:
1101
diff
changeset
|
72 while(*p >= '0' && *p <= '9') |
f68da12e852b
Added scientific notation support to StringToDouble
Benjamin Golinvaux <bgo@osimis.io>
parents:
1101
diff
changeset
|
73 ++p; |
f68da12e852b
Added scientific notation support to StringToDouble
Benjamin Golinvaux <bgo@osimis.io>
parents:
1101
diff
changeset
|
74 |
f68da12e852b
Added scientific notation support to StringToDouble
Benjamin Golinvaux <bgo@osimis.io>
parents:
1101
diff
changeset
|
75 return (*p == 0); |
f68da12e852b
Added scientific notation support to StringToDouble
Benjamin Golinvaux <bgo@osimis.io>
parents:
1101
diff
changeset
|
76 } |
1080
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
77 else |
1166
f68da12e852b
Added scientific notation support to StringToDouble
Benjamin Golinvaux <bgo@osimis.io>
parents:
1101
diff
changeset
|
78 { |
1080
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
79 return false; |
1166
f68da12e852b
Added scientific notation support to StringToDouble
Benjamin Golinvaux <bgo@osimis.io>
parents:
1101
diff
changeset
|
80 } |
1080
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
81 } |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
82 return true; |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
83 } |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
84 |
1306
fef1ec42a7db
Some docs + headers added to CMake for easier VS browsing + tiny predecl
Benjamin Golinvaux <bgo@osimis.io>
parents:
1279
diff
changeset
|
85 /** |
fef1ec42a7db
Some docs + headers added to CMake for easier VS browsing + tiny predecl
Benjamin Golinvaux <bgo@osimis.io>
parents:
1279
diff
changeset
|
86 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
|
87 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
|
88 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
|
89 */ |
1080
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
90 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
|
91 { |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
92 const char* p = text; |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
93 if (*p == '-') |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
94 p++; |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
95 while (*p != 0) |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
96 { |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
97 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
|
98 ++p; |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
99 else |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
100 return false; |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
101 } |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
102 return true; |
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 |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
105 /* |
1306
fef1ec42a7db
Some docs + headers added to CMake for easier VS browsing + tiny predecl
Benjamin Golinvaux <bgo@osimis.io>
parents:
1279
diff
changeset
|
106 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
|
107 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
|
108 |
1080
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
109 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
|
110 */ |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
111 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
|
112 { |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
113 if(!LegitDoubleString(text)) |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
114 return false; |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
115 |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
116 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
|
117 { |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
118 1.0, |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
119 0.1, |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
120 0.01, |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
121 0.001, |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
122 0.0001, |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
123 0.00001, |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
124 0.000001, |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
125 0.0000001, |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
126 0.00000001, |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
127 0.000000001, |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
128 0.0000000001, |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
129 0.00000000001, |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
130 0.000000000001, |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
131 0.0000000000001, |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
132 0.00000000000001, |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
133 0.000000000000001, |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
134 0.0000000000000001, |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
135 0.00000000000000001, |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
136 0.000000000000000001, |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
137 0.0000000000000000001 |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
138 }; |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
139 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
|
140 |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
141 r = 0.0; |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
142 double neg = 1.0; |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
143 const char* p = text; |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
144 |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
145 if (*p == '-') |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
146 { |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
147 neg = -1.0; |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
148 ++p; |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
149 } |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
150 // 12345.67890 |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
151 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
|
152 { |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
153 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
|
154 ++p; |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
155 } |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
156 if (*p == '.') |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
157 { |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
158 double f = 0.0; |
1192 | 159 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
|
160 ++p; |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
161 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
|
162 { |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
163 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
|
164 ++p; |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
165 ++n; |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
166 } |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
167 r += f; |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
168 } |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
169 r *= neg; |
1166
f68da12e852b
Added scientific notation support to StringToDouble
Benjamin Golinvaux <bgo@osimis.io>
parents:
1101
diff
changeset
|
170 |
f68da12e852b
Added scientific notation support to StringToDouble
Benjamin Golinvaux <bgo@osimis.io>
parents:
1101
diff
changeset
|
171 // 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
|
172 // 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
|
173 while ((*p >= '0' && *p <= '9')) |
f68da12e852b
Added scientific notation support to StringToDouble
Benjamin Golinvaux <bgo@osimis.io>
parents:
1101
diff
changeset
|
174 ++p; |
f68da12e852b
Added scientific notation support to StringToDouble
Benjamin Golinvaux <bgo@osimis.io>
parents:
1101
diff
changeset
|
175 |
f68da12e852b
Added scientific notation support to StringToDouble
Benjamin Golinvaux <bgo@osimis.io>
parents:
1101
diff
changeset
|
176 if (*p == 0 ) |
f68da12e852b
Added scientific notation support to StringToDouble
Benjamin Golinvaux <bgo@osimis.io>
parents:
1101
diff
changeset
|
177 { |
1080
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
178 return true; |
1166
f68da12e852b
Added scientific notation support to StringToDouble
Benjamin Golinvaux <bgo@osimis.io>
parents:
1101
diff
changeset
|
179 } |
f68da12e852b
Added scientific notation support to StringToDouble
Benjamin Golinvaux <bgo@osimis.io>
parents:
1101
diff
changeset
|
180 else if ((*p == 'e') || (*p == 'E')) |
f68da12e852b
Added scientific notation support to StringToDouble
Benjamin Golinvaux <bgo@osimis.io>
parents:
1101
diff
changeset
|
181 { |
f68da12e852b
Added scientific notation support to StringToDouble
Benjamin Golinvaux <bgo@osimis.io>
parents:
1101
diff
changeset
|
182 // process the scientific notation |
f68da12e852b
Added scientific notation support to StringToDouble
Benjamin Golinvaux <bgo@osimis.io>
parents:
1101
diff
changeset
|
183 double sign; // no init is safe (read below) |
f68da12e852b
Added scientific notation support to StringToDouble
Benjamin Golinvaux <bgo@osimis.io>
parents:
1101
diff
changeset
|
184 ++p; |
f68da12e852b
Added scientific notation support to StringToDouble
Benjamin Golinvaux <bgo@osimis.io>
parents:
1101
diff
changeset
|
185 if (*p == '-') |
f68da12e852b
Added scientific notation support to StringToDouble
Benjamin Golinvaux <bgo@osimis.io>
parents:
1101
diff
changeset
|
186 { |
f68da12e852b
Added scientific notation support to StringToDouble
Benjamin Golinvaux <bgo@osimis.io>
parents:
1101
diff
changeset
|
187 sign = -1.0; |
f68da12e852b
Added scientific notation support to StringToDouble
Benjamin Golinvaux <bgo@osimis.io>
parents:
1101
diff
changeset
|
188 // point to first number |
f68da12e852b
Added scientific notation support to StringToDouble
Benjamin Golinvaux <bgo@osimis.io>
parents:
1101
diff
changeset
|
189 ++p; |
f68da12e852b
Added scientific notation support to StringToDouble
Benjamin Golinvaux <bgo@osimis.io>
parents:
1101
diff
changeset
|
190 } |
f68da12e852b
Added scientific notation support to StringToDouble
Benjamin Golinvaux <bgo@osimis.io>
parents:
1101
diff
changeset
|
191 else if (*p == '+') |
f68da12e852b
Added scientific notation support to StringToDouble
Benjamin Golinvaux <bgo@osimis.io>
parents:
1101
diff
changeset
|
192 { |
f68da12e852b
Added scientific notation support to StringToDouble
Benjamin Golinvaux <bgo@osimis.io>
parents:
1101
diff
changeset
|
193 sign = 1.0; |
f68da12e852b
Added scientific notation support to StringToDouble
Benjamin Golinvaux <bgo@osimis.io>
parents:
1101
diff
changeset
|
194 // point to first number |
f68da12e852b
Added scientific notation support to StringToDouble
Benjamin Golinvaux <bgo@osimis.io>
parents:
1101
diff
changeset
|
195 ++p; |
f68da12e852b
Added scientific notation support to StringToDouble
Benjamin Golinvaux <bgo@osimis.io>
parents:
1101
diff
changeset
|
196 } |
f68da12e852b
Added scientific notation support to StringToDouble
Benjamin Golinvaux <bgo@osimis.io>
parents:
1101
diff
changeset
|
197 else if (*p >= '0' && *p <= '9') |
f68da12e852b
Added scientific notation support to StringToDouble
Benjamin Golinvaux <bgo@osimis.io>
parents:
1101
diff
changeset
|
198 { |
f68da12e852b
Added scientific notation support to StringToDouble
Benjamin Golinvaux <bgo@osimis.io>
parents:
1101
diff
changeset
|
199 sign = 1.0; |
f68da12e852b
Added scientific notation support to StringToDouble
Benjamin Golinvaux <bgo@osimis.io>
parents:
1101
diff
changeset
|
200 } |
f68da12e852b
Added scientific notation support to StringToDouble
Benjamin Golinvaux <bgo@osimis.io>
parents:
1101
diff
changeset
|
201 else |
f68da12e852b
Added scientific notation support to StringToDouble
Benjamin Golinvaux <bgo@osimis.io>
parents:
1101
diff
changeset
|
202 { |
f68da12e852b
Added scientific notation support to StringToDouble
Benjamin Golinvaux <bgo@osimis.io>
parents:
1101
diff
changeset
|
203 // 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
|
204 return false; |
f68da12e852b
Added scientific notation support to StringToDouble
Benjamin Golinvaux <bgo@osimis.io>
parents:
1101
diff
changeset
|
205 } |
f68da12e852b
Added scientific notation support to StringToDouble
Benjamin Golinvaux <bgo@osimis.io>
parents:
1101
diff
changeset
|
206 // 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
|
207 double exp = 0; |
f68da12e852b
Added scientific notation support to StringToDouble
Benjamin Golinvaux <bgo@osimis.io>
parents:
1101
diff
changeset
|
208 while (*p >= '0' && *p <= '9') |
f68da12e852b
Added scientific notation support to StringToDouble
Benjamin Golinvaux <bgo@osimis.io>
parents:
1101
diff
changeset
|
209 { |
f68da12e852b
Added scientific notation support to StringToDouble
Benjamin Golinvaux <bgo@osimis.io>
parents:
1101
diff
changeset
|
210 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
|
211 ++p; |
f68da12e852b
Added scientific notation support to StringToDouble
Benjamin Golinvaux <bgo@osimis.io>
parents:
1101
diff
changeset
|
212 } |
f68da12e852b
Added scientific notation support to StringToDouble
Benjamin Golinvaux <bgo@osimis.io>
parents:
1101
diff
changeset
|
213 // 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
|
214 exp *= sign; |
f68da12e852b
Added scientific notation support to StringToDouble
Benjamin Golinvaux <bgo@osimis.io>
parents:
1101
diff
changeset
|
215 double scFac = ::pow(10.0, exp); |
f68da12e852b
Added scientific notation support to StringToDouble
Benjamin Golinvaux <bgo@osimis.io>
parents:
1101
diff
changeset
|
216 r *= scFac; |
f68da12e852b
Added scientific notation support to StringToDouble
Benjamin Golinvaux <bgo@osimis.io>
parents:
1101
diff
changeset
|
217 |
f68da12e852b
Added scientific notation support to StringToDouble
Benjamin Golinvaux <bgo@osimis.io>
parents:
1101
diff
changeset
|
218 // only allowed symbol here is EOS |
f68da12e852b
Added scientific notation support to StringToDouble
Benjamin Golinvaux <bgo@osimis.io>
parents:
1101
diff
changeset
|
219 return (*p == 0); |
f68da12e852b
Added scientific notation support to StringToDouble
Benjamin Golinvaux <bgo@osimis.io>
parents:
1101
diff
changeset
|
220 } |
1080
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
221 else |
1166
f68da12e852b
Added scientific notation support to StringToDouble
Benjamin Golinvaux <bgo@osimis.io>
parents:
1101
diff
changeset
|
222 { |
f68da12e852b
Added scientific notation support to StringToDouble
Benjamin Golinvaux <bgo@osimis.io>
parents:
1101
diff
changeset
|
223 // not allowed |
1080
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
224 return false; |
1166
f68da12e852b
Added scientific notation support to StringToDouble
Benjamin Golinvaux <bgo@osimis.io>
parents:
1101
diff
changeset
|
225 } |
1080
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 |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
228 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
|
229 { |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
230 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
|
231 } |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
232 |
1306
fef1ec42a7db
Some docs + headers added to CMake for easier VS browsing + tiny predecl
Benjamin Golinvaux <bgo@osimis.io>
parents:
1279
diff
changeset
|
233 /** |
fef1ec42a7db
Some docs + headers added to CMake for easier VS browsing + tiny predecl
Benjamin Golinvaux <bgo@osimis.io>
parents:
1279
diff
changeset
|
234 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
|
235 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
|
236 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
|
237 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
|
238 */ |
fef1ec42a7db
Some docs + headers added to CMake for easier VS browsing + tiny predecl
Benjamin Golinvaux <bgo@osimis.io>
parents:
1279
diff
changeset
|
239 |
1080
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
240 template<typename T> |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
241 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
|
242 { |
1306
fef1ec42a7db
Some docs + headers added to CMake for easier VS browsing + tiny predecl
Benjamin Golinvaux <bgo@osimis.io>
parents:
1279
diff
changeset
|
243 if (!LegitIntegerString(text)) |
1080
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
244 return false; |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
245 |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
246 r = 0; |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
247 T neg = 1; |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
248 const char* p = text; |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
249 |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
250 if (*p == '-') |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
251 { |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
252 neg = -1; |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
253 ++p; |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
254 } |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
255 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
|
256 { |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
257 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
|
258 ++p; |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
259 } |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
260 r *= neg; |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
261 if (*p == 0) |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
262 return true; |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
263 else |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
264 return false; |
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 |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
267 template<typename T> |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
268 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
|
269 { |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
270 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
|
271 } |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
272 |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
273 /** |
1306
fef1ec42a7db
Some docs + headers added to CMake for easier VS browsing + tiny predecl
Benjamin Golinvaux <bgo@osimis.io>
parents:
1279
diff
changeset
|
274 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
|
275 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
|
276 */ |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
277 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
|
278 |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
279 /** |
1306
fef1ec42a7db
Some docs + headers added to CMake for easier VS browsing + tiny predecl
Benjamin Golinvaux <bgo@osimis.io>
parents:
1279
diff
changeset
|
280 See main overload |
1080
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
281 */ |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
282 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
|
283 { |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
284 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
|
285 } |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
286 |
1306
fef1ec42a7db
Some docs + headers added to CMake for easier VS browsing + tiny predecl
Benjamin Golinvaux <bgo@osimis.io>
parents:
1279
diff
changeset
|
287 /** |
fef1ec42a7db
Some docs + headers added to CMake for easier VS browsing + tiny predecl
Benjamin Golinvaux <bgo@osimis.io>
parents:
1279
diff
changeset
|
288 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
|
289 */ |
1328
fd616c4a5904
Added mechanism to prevent callbacks from being sent on dead WebAssemblyViewport objects
Benjamin Golinvaux <bgo@osimis.io>
parents:
1306
diff
changeset
|
290 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
|
291 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
|
292 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
|
293 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
|
294 const char* text); |
1080
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
295 |
1306
fef1ec42a7db
Some docs + headers added to CMake for easier VS browsing + tiny predecl
Benjamin Golinvaux <bgo@osimis.io>
parents:
1279
diff
changeset
|
296 /** |
fef1ec42a7db
Some docs + headers added to CMake for easier VS browsing + tiny predecl
Benjamin Golinvaux <bgo@osimis.io>
parents:
1279
diff
changeset
|
297 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
|
298 */ |
1328
fd616c4a5904
Added mechanism to prevent callbacks from being sent on dead WebAssemblyViewport objects
Benjamin Golinvaux <bgo@osimis.io>
parents:
1306
diff
changeset
|
299 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
|
300 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
|
301 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
|
302 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
|
303 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
|
304 { |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
305 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
|
306 } |
1591
5887a4f8594b
moving platform-specific files out of the "OrthancStone" folder
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1512
diff
changeset
|
307 |
5887a4f8594b
moving platform-specific files out of the "OrthancStone" folder
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1512
diff
changeset
|
308 |
5887a4f8594b
moving platform-specific files out of the "OrthancStone" folder
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1512
diff
changeset
|
309 /** |
5887a4f8594b
moving platform-specific files out of the "OrthancStone" folder
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1512
diff
changeset
|
310 This method could have been called StripSpacesAndChangeToLower but we might want to |
5887a4f8594b
moving platform-specific files out of the "OrthancStone" folder
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1512
diff
changeset
|
311 add some UUID validation to the argument |
5887a4f8594b
moving platform-specific files out of the "OrthancStone" folder
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1512
diff
changeset
|
312 */ |
5887a4f8594b
moving platform-specific files out of the "OrthancStone" folder
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1512
diff
changeset
|
313 void NormalizeUuid(std::string& uuid); |
1080
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
314 } |
287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
315 } |