annotate OrthancStone/Sources/Toolbox/GenericToolbox.h @ 1740:84d1402c98fe

backward compatibility for Orthanc framework 1.8.2 in IOrthancConnection
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 13 Jan 2021 09:07:57 +0100
parents 9ac2a65d4172
children b6a6ad64192a
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
1739
9ac2a65d4172 upgrade to year 2021
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1679
diff changeset
5 * Copyright (C) 2017-2021 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
1598
8563ea5d8ae4 relicensing some files, cf. osimis bm26 and chu agreement on 2020-05-20
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1596
diff changeset
8 * modify it under the terms of the GNU Lesser General Public License
1080
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
1598
8563ea5d8ae4 relicensing some files, cf. osimis bm26 and chu agreement on 2020-05-20
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1596
diff changeset
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
8563ea5d8ae4 relicensing some files, cf. osimis bm26 and chu agreement on 2020-05-20
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1596
diff changeset
15 * Lesser General Public License for more details.
1596
4fb8fdf03314 removed annoying whitespace
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1591
diff changeset
16 *
1598
8563ea5d8ae4 relicensing some files, cf. osimis bm26 and chu agreement on 2020-05-20
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1596
diff changeset
17 * You should have received a copy of the GNU Lesser General Public
8563ea5d8ae4 relicensing some files, cf. osimis bm26 and chu agreement on 2020-05-20
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1596
diff changeset
18 * License along with this program. If not, see
8563ea5d8ae4 relicensing some files, cf. osimis bm26 and chu agreement on 2020-05-20
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1596
diff changeset
19 * <http://www.gnu.org/licenses/>.
1080
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
287ec78f63b4 GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff changeset
22 #pragma once
287ec78f63b4 GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff changeset
23
1455
30deba7bc8e2 simplifying include_directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1331
diff changeset
24 #include <Compatibility.h>
30deba7bc8e2 simplifying include_directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1331
diff changeset
25 #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
26
fd616c4a5904 Added mechanism to prevent callbacks from being sent on dead WebAssemblyViewport objects
Benjamin Golinvaux <bgo@osimis.io>
parents: 1306
diff changeset
27 #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
28
1080
287ec78f63b4 GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff changeset
29 #include <string>
1101
141593f1aa88 fix build on ubuntu
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1080
diff changeset
30 #include <stdint.h>
1166
f68da12e852b Added scientific notation support to StringToDouble
Benjamin Golinvaux <bgo@osimis.io>
parents: 1101
diff changeset
31 #include <math.h>
f68da12e852b Added scientific notation support to StringToDouble
Benjamin Golinvaux <bgo@osimis.io>
parents: 1101
diff changeset
32
1328
fd616c4a5904 Added mechanism to prevent callbacks from being sent on dead WebAssemblyViewport objects
Benjamin Golinvaux <bgo@osimis.io>
parents: 1306
diff changeset
33 #include <memory>
1080
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 OrthancStone
287ec78f63b4 GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff changeset
36 {
287ec78f63b4 GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff changeset
37 namespace GenericToolbox
287ec78f63b4 GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff changeset
38 {
1306
fef1ec42a7db Some docs + headers added to CMake for easier VS browsing + tiny predecl
Benjamin Golinvaux <bgo@osimis.io>
parents: 1279
diff changeset
39 /**
fef1ec42a7db Some docs + headers added to CMake for easier VS browsing + tiny predecl
Benjamin Golinvaux <bgo@osimis.io>
parents: 1279
diff changeset
40 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
41 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
42 /^[-]?[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
43 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
44 */
1080
287ec78f63b4 GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff changeset
45 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
46 {
287ec78f63b4 GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff changeset
47 const char* p = text;
287ec78f63b4 GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff changeset
48 if(*p == '-')
287ec78f63b4 GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff changeset
49 p++;
287ec78f63b4 GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff changeset
50 size_t period = 0;
287ec78f63b4 GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff changeset
51 while(*p != 0)
287ec78f63b4 GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff changeset
52 {
287ec78f63b4 GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff changeset
53 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
54 ++p;
287ec78f63b4 GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff changeset
55 else if(*p == '.')
287ec78f63b4 GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff changeset
56 {
287ec78f63b4 GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff changeset
57 if(period > 0)
287ec78f63b4 GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff changeset
58 return false;
287ec78f63b4 GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff changeset
59 else
287ec78f63b4 GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff changeset
60 period++;
287ec78f63b4 GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff changeset
61 ++p;
287ec78f63b4 GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff changeset
62 }
1166
f68da12e852b Added scientific notation support to StringToDouble
Benjamin Golinvaux <bgo@osimis.io>
parents: 1101
diff changeset
63 else if (*p == 'e' || *p == 'E')
f68da12e852b Added scientific notation support to StringToDouble
Benjamin Golinvaux <bgo@osimis.io>
parents: 1101
diff changeset
64 {
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 if (*p == '-' || *p == '+')
f68da12e852b Added scientific notation support to StringToDouble
Benjamin Golinvaux <bgo@osimis.io>
parents: 1101
diff changeset
67 ++p;
f68da12e852b Added scientific notation support to StringToDouble
Benjamin Golinvaux <bgo@osimis.io>
parents: 1101
diff changeset
68 // "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
69 if (!(*p >= '0' && *p <= '9'))
f68da12e852b Added scientific notation support to StringToDouble
Benjamin Golinvaux <bgo@osimis.io>
parents: 1101
diff changeset
70 return false;
f68da12e852b Added scientific notation support to StringToDouble
Benjamin Golinvaux <bgo@osimis.io>
parents: 1101
diff changeset
71
f68da12e852b Added scientific notation support to StringToDouble
Benjamin Golinvaux <bgo@osimis.io>
parents: 1101
diff changeset
72 // these must be the last in the string
f68da12e852b Added scientific notation support to StringToDouble
Benjamin Golinvaux <bgo@osimis.io>
parents: 1101
diff changeset
73 while(*p >= '0' && *p <= '9')
f68da12e852b Added scientific notation support to StringToDouble
Benjamin Golinvaux <bgo@osimis.io>
parents: 1101
diff changeset
74 ++p;
f68da12e852b Added scientific notation support to StringToDouble
Benjamin Golinvaux <bgo@osimis.io>
parents: 1101
diff changeset
75
f68da12e852b Added scientific notation support to StringToDouble
Benjamin Golinvaux <bgo@osimis.io>
parents: 1101
diff changeset
76 return (*p == 0);
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 else
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 return false;
1166
f68da12e852b Added scientific notation support to StringToDouble
Benjamin Golinvaux <bgo@osimis.io>
parents: 1101
diff changeset
81 }
1080
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 return true;
287ec78f63b4 GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff changeset
84 }
287ec78f63b4 GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff changeset
85
1306
fef1ec42a7db Some docs + headers added to CMake for easier VS browsing + tiny predecl
Benjamin Golinvaux <bgo@osimis.io>
parents: 1279
diff changeset
86 /**
fef1ec42a7db Some docs + headers added to CMake for easier VS browsing + tiny predecl
Benjamin Golinvaux <bgo@osimis.io>
parents: 1279
diff changeset
87 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
88 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
89 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
90 */
1080
287ec78f63b4 GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff changeset
91 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
92 {
287ec78f63b4 GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff changeset
93 const char* p = text;
287ec78f63b4 GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff changeset
94 if (*p == '-')
287ec78f63b4 GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff changeset
95 p++;
287ec78f63b4 GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff changeset
96 while (*p != 0)
287ec78f63b4 GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff changeset
97 {
287ec78f63b4 GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff changeset
98 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
99 ++p;
287ec78f63b4 GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff changeset
100 else
287ec78f63b4 GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff changeset
101 return false;
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 return true;
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
287ec78f63b4 GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff changeset
106 /*
1306
fef1ec42a7db Some docs + headers added to CMake for easier VS browsing + tiny predecl
Benjamin Golinvaux <bgo@osimis.io>
parents: 1279
diff changeset
107 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
108 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
109
1080
287ec78f63b4 GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff changeset
110 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
111 */
287ec78f63b4 GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff changeset
112 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
113 {
287ec78f63b4 GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff changeset
114 if(!LegitDoubleString(text))
287ec78f63b4 GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff changeset
115 return false;
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 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
118 {
287ec78f63b4 GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff changeset
119 1.0,
287ec78f63b4 GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff changeset
120 0.1,
287ec78f63b4 GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff changeset
121 0.01,
287ec78f63b4 GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff changeset
122 0.001,
287ec78f63b4 GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff changeset
123 0.0001,
287ec78f63b4 GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff changeset
124 0.00001,
287ec78f63b4 GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff changeset
125 0.000001,
287ec78f63b4 GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff changeset
126 0.0000001,
287ec78f63b4 GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff changeset
127 0.00000001,
287ec78f63b4 GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff changeset
128 0.000000001,
287ec78f63b4 GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff changeset
129 0.0000000001,
287ec78f63b4 GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff changeset
130 0.00000000001,
287ec78f63b4 GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff changeset
131 0.000000000001,
287ec78f63b4 GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff changeset
132 0.0000000000001,
287ec78f63b4 GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff changeset
133 0.00000000000001,
287ec78f63b4 GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff changeset
134 0.000000000000001,
287ec78f63b4 GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff changeset
135 0.0000000000000001,
287ec78f63b4 GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff changeset
136 0.00000000000000001,
287ec78f63b4 GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff changeset
137 0.000000000000000001,
287ec78f63b4 GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff changeset
138 0.0000000000000000001
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 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
141
287ec78f63b4 GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff changeset
142 r = 0.0;
287ec78f63b4 GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff changeset
143 double neg = 1.0;
287ec78f63b4 GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff changeset
144 const char* p = text;
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 if (*p == '-')
287ec78f63b4 GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff changeset
147 {
287ec78f63b4 GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff changeset
148 neg = -1.0;
287ec78f63b4 GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff changeset
149 ++p;
287ec78f63b4 GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff changeset
150 }
287ec78f63b4 GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff changeset
151 // 12345.67890
287ec78f63b4 GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff changeset
152 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
153 {
287ec78f63b4 GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff changeset
154 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
155 ++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 if (*p == '.')
287ec78f63b4 GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff changeset
158 {
287ec78f63b4 GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff changeset
159 double f = 0.0;
1192
91d86144fb79 fix build
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1166
diff changeset
160 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
161 ++p;
287ec78f63b4 GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff changeset
162 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
163 {
287ec78f63b4 GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff changeset
164 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
165 ++p;
287ec78f63b4 GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff changeset
166 ++n;
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 += f;
287ec78f63b4 GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff changeset
169 }
287ec78f63b4 GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff changeset
170 r *= neg;
1166
f68da12e852b Added scientific notation support to StringToDouble
Benjamin Golinvaux <bgo@osimis.io>
parents: 1101
diff changeset
171
f68da12e852b Added scientific notation support to StringToDouble
Benjamin Golinvaux <bgo@osimis.io>
parents: 1101
diff changeset
172 // 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
173 // 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
174 while ((*p >= '0' && *p <= '9'))
f68da12e852b Added scientific notation support to StringToDouble
Benjamin Golinvaux <bgo@osimis.io>
parents: 1101
diff changeset
175 ++p;
f68da12e852b Added scientific notation support to StringToDouble
Benjamin Golinvaux <bgo@osimis.io>
parents: 1101
diff changeset
176
f68da12e852b Added scientific notation support to StringToDouble
Benjamin Golinvaux <bgo@osimis.io>
parents: 1101
diff changeset
177 if (*p == 0 )
f68da12e852b Added scientific notation support to StringToDouble
Benjamin Golinvaux <bgo@osimis.io>
parents: 1101
diff changeset
178 {
1080
287ec78f63b4 GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff changeset
179 return true;
1166
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 else if ((*p == 'e') || (*p == 'E'))
f68da12e852b Added scientific notation support to StringToDouble
Benjamin Golinvaux <bgo@osimis.io>
parents: 1101
diff changeset
182 {
f68da12e852b Added scientific notation support to StringToDouble
Benjamin Golinvaux <bgo@osimis.io>
parents: 1101
diff changeset
183 // process the scientific notation
f68da12e852b Added scientific notation support to StringToDouble
Benjamin Golinvaux <bgo@osimis.io>
parents: 1101
diff changeset
184 double sign; // no init is safe (read below)
f68da12e852b Added scientific notation support to StringToDouble
Benjamin Golinvaux <bgo@osimis.io>
parents: 1101
diff changeset
185 ++p;
f68da12e852b Added scientific notation support to StringToDouble
Benjamin Golinvaux <bgo@osimis.io>
parents: 1101
diff changeset
186 if (*p == '-')
f68da12e852b Added scientific notation support to StringToDouble
Benjamin Golinvaux <bgo@osimis.io>
parents: 1101
diff changeset
187 {
f68da12e852b Added scientific notation support to StringToDouble
Benjamin Golinvaux <bgo@osimis.io>
parents: 1101
diff changeset
188 sign = -1.0;
f68da12e852b Added scientific notation support to StringToDouble
Benjamin Golinvaux <bgo@osimis.io>
parents: 1101
diff changeset
189 // point to first number
f68da12e852b Added scientific notation support to StringToDouble
Benjamin Golinvaux <bgo@osimis.io>
parents: 1101
diff changeset
190 ++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 else if (*p == '+')
f68da12e852b Added scientific notation support to StringToDouble
Benjamin Golinvaux <bgo@osimis.io>
parents: 1101
diff changeset
193 {
f68da12e852b Added scientific notation support to StringToDouble
Benjamin Golinvaux <bgo@osimis.io>
parents: 1101
diff changeset
194 sign = 1.0;
f68da12e852b Added scientific notation support to StringToDouble
Benjamin Golinvaux <bgo@osimis.io>
parents: 1101
diff changeset
195 // point to first number
f68da12e852b Added scientific notation support to StringToDouble
Benjamin Golinvaux <bgo@osimis.io>
parents: 1101
diff changeset
196 ++p;
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 else if (*p >= '0' && *p <= '9')
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 sign = 1.0;
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 else
f68da12e852b Added scientific notation support to StringToDouble
Benjamin Golinvaux <bgo@osimis.io>
parents: 1101
diff changeset
203 {
f68da12e852b Added scientific notation support to StringToDouble
Benjamin Golinvaux <bgo@osimis.io>
parents: 1101
diff changeset
204 // 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
205 return false;
f68da12e852b Added scientific notation support to StringToDouble
Benjamin Golinvaux <bgo@osimis.io>
parents: 1101
diff changeset
206 }
f68da12e852b Added scientific notation support to StringToDouble
Benjamin Golinvaux <bgo@osimis.io>
parents: 1101
diff changeset
207 // 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
208 double exp = 0;
f68da12e852b Added scientific notation support to StringToDouble
Benjamin Golinvaux <bgo@osimis.io>
parents: 1101
diff changeset
209 while (*p >= '0' && *p <= '9')
f68da12e852b Added scientific notation support to StringToDouble
Benjamin Golinvaux <bgo@osimis.io>
parents: 1101
diff changeset
210 {
f68da12e852b Added scientific notation support to StringToDouble
Benjamin Golinvaux <bgo@osimis.io>
parents: 1101
diff changeset
211 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
212 ++p;
f68da12e852b Added scientific notation support to StringToDouble
Benjamin Golinvaux <bgo@osimis.io>
parents: 1101
diff changeset
213 }
f68da12e852b Added scientific notation support to StringToDouble
Benjamin Golinvaux <bgo@osimis.io>
parents: 1101
diff changeset
214 // 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
215 exp *= sign;
f68da12e852b Added scientific notation support to StringToDouble
Benjamin Golinvaux <bgo@osimis.io>
parents: 1101
diff changeset
216 double scFac = ::pow(10.0, exp);
f68da12e852b Added scientific notation support to StringToDouble
Benjamin Golinvaux <bgo@osimis.io>
parents: 1101
diff changeset
217 r *= scFac;
f68da12e852b Added scientific notation support to StringToDouble
Benjamin Golinvaux <bgo@osimis.io>
parents: 1101
diff changeset
218
f68da12e852b Added scientific notation support to StringToDouble
Benjamin Golinvaux <bgo@osimis.io>
parents: 1101
diff changeset
219 // only allowed symbol here is EOS
f68da12e852b Added scientific notation support to StringToDouble
Benjamin Golinvaux <bgo@osimis.io>
parents: 1101
diff changeset
220 return (*p == 0);
f68da12e852b Added scientific notation support to StringToDouble
Benjamin Golinvaux <bgo@osimis.io>
parents: 1101
diff changeset
221 }
1080
287ec78f63b4 GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff changeset
222 else
1166
f68da12e852b Added scientific notation support to StringToDouble
Benjamin Golinvaux <bgo@osimis.io>
parents: 1101
diff changeset
223 {
f68da12e852b Added scientific notation support to StringToDouble
Benjamin Golinvaux <bgo@osimis.io>
parents: 1101
diff changeset
224 // not allowed
1080
287ec78f63b4 GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff changeset
225 return false;
1166
f68da12e852b Added scientific notation support to StringToDouble
Benjamin Golinvaux <bgo@osimis.io>
parents: 1101
diff changeset
226 }
1080
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
287ec78f63b4 GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff changeset
229 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
230 {
287ec78f63b4 GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff changeset
231 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
232 }
287ec78f63b4 GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff changeset
233
1306
fef1ec42a7db Some docs + headers added to CMake for easier VS browsing + tiny predecl
Benjamin Golinvaux <bgo@osimis.io>
parents: 1279
diff changeset
234 /**
fef1ec42a7db Some docs + headers added to CMake for easier VS browsing + tiny predecl
Benjamin Golinvaux <bgo@osimis.io>
parents: 1279
diff changeset
235 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
236 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
237 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
238 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
239 */
fef1ec42a7db Some docs + headers added to CMake for easier VS browsing + tiny predecl
Benjamin Golinvaux <bgo@osimis.io>
parents: 1279
diff changeset
240
1080
287ec78f63b4 GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff changeset
241 template<typename T>
287ec78f63b4 GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff changeset
242 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
243 {
1306
fef1ec42a7db Some docs + headers added to CMake for easier VS browsing + tiny predecl
Benjamin Golinvaux <bgo@osimis.io>
parents: 1279
diff changeset
244 if (!LegitIntegerString(text))
1080
287ec78f63b4 GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff changeset
245 return false;
287ec78f63b4 GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff changeset
246
287ec78f63b4 GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff changeset
247 r = 0;
287ec78f63b4 GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff changeset
248 T neg = 1;
287ec78f63b4 GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff changeset
249 const char* p = text;
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 if (*p == '-')
287ec78f63b4 GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff changeset
252 {
287ec78f63b4 GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff changeset
253 neg = -1;
287ec78f63b4 GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff changeset
254 ++p;
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 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
257 {
1679
5b8b88e5bfd6 successfully running unit tests in WebAssembly
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1598
diff changeset
258 r = (r * 10) + static_cast<T>(*p - '0'); // 1 12 123 123 12345
1080
287ec78f63b4 GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff changeset
259 ++p;
287ec78f63b4 GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff changeset
260 }
287ec78f63b4 GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff changeset
261 r *= neg;
287ec78f63b4 GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff changeset
262 if (*p == 0)
287ec78f63b4 GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff changeset
263 return true;
287ec78f63b4 GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff changeset
264 else
287ec78f63b4 GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff changeset
265 return false;
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
287ec78f63b4 GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff changeset
268 template<typename T>
287ec78f63b4 GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff changeset
269 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
270 {
287ec78f63b4 GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff changeset
271 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
272 }
287ec78f63b4 GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff changeset
273
287ec78f63b4 GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff changeset
274 /**
1306
fef1ec42a7db Some docs + headers added to CMake for easier VS browsing + tiny predecl
Benjamin Golinvaux <bgo@osimis.io>
parents: 1279
diff changeset
275 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
276 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
277 */
287ec78f63b4 GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff changeset
278 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
279
287ec78f63b4 GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff changeset
280 /**
1306
fef1ec42a7db Some docs + headers added to CMake for easier VS browsing + tiny predecl
Benjamin Golinvaux <bgo@osimis.io>
parents: 1279
diff changeset
281 See main overload
1080
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 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
284 {
287ec78f63b4 GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff changeset
285 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
286 }
287ec78f63b4 GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff changeset
287
1306
fef1ec42a7db Some docs + headers added to CMake for easier VS browsing + tiny predecl
Benjamin Golinvaux <bgo@osimis.io>
parents: 1279
diff changeset
288 /**
fef1ec42a7db Some docs + headers added to CMake for easier VS browsing + tiny predecl
Benjamin Golinvaux <bgo@osimis.io>
parents: 1279
diff changeset
289 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
290 */
1328
fd616c4a5904 Added mechanism to prevent callbacks from being sent on dead WebAssemblyViewport objects
Benjamin Golinvaux <bgo@osimis.io>
parents: 1306
diff changeset
291 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
292 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
293 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
294 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
295 const char* text);
1080
287ec78f63b4 GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff changeset
296
1306
fef1ec42a7db Some docs + headers added to CMake for easier VS browsing + tiny predecl
Benjamin Golinvaux <bgo@osimis.io>
parents: 1279
diff changeset
297 /**
fef1ec42a7db Some docs + headers added to CMake for easier VS browsing + tiny predecl
Benjamin Golinvaux <bgo@osimis.io>
parents: 1279
diff changeset
298 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
299 */
1328
fd616c4a5904 Added mechanism to prevent callbacks from being sent on dead WebAssemblyViewport objects
Benjamin Golinvaux <bgo@osimis.io>
parents: 1306
diff changeset
300 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
301 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
302 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
303 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
304 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
305 {
287ec78f63b4 GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff changeset
306 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
307 }
1591
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 /**
5887a4f8594b moving platform-specific files out of the "OrthancStone" folder
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1512
diff changeset
311 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
312 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
313 */
5887a4f8594b moving platform-specific files out of the "OrthancStone" folder
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1512
diff changeset
314 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
315 }
287ec78f63b4 GenericToolbox (fast c-string --> double or integer) + refactoring to be able
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff changeset
316 }