1 #include <ot/unit/unit.hpp>
2
3 namespace ot {
4
5 // Function: make_time_unit
make_time_unit(std::string_view str)6 std::optional<second_t> make_time_unit(std::string_view str) {
7
8 using namespace units::literals;
9
10 static const std::regex unit_regex(
11 "([\\+-]?\\d*\\.?\\d+)\\s*([fpnumkM]?)\\s*(s)",
12 std::regex::icase
13 );
14
15 if(std::cmatch pieces; std::regex_match(str.begin(), str.end(), pieces, unit_regex)) {
16
17 if(pieces.size() != 4) {
18 return std::nullopt;
19 }
20
21 auto s = std::stof(pieces[1].str());
22
23 if(const auto& b = pieces[2].str(); b.empty()) {
24 return s* 1_s;
25 }
26 else if(b == "f") {
27 return s * 1_fs;
28 }
29 else if(b == "p") {
30 return s * 1_ps;
31 }
32 else if(b == "n") {
33 return s * 1_ns;
34 }
35 else if(b == "u") {
36 return s * 1_us;
37 }
38 else if(b == "m") {
39 return s * 1_ms;
40 }
41 else if(b == "k" || b == "K") {
42 return s * 1_ks;
43 }
44 else if(b == "M") {
45 return s * 1_Ms;
46 }
47 else return std::nullopt;
48 }
49 else return std::nullopt;
50 }
51
52 // Function: make_power_unit
make_power_unit(std::string_view str)53 std::optional<watt_t> make_power_unit(std::string_view str) {
54
55 using namespace units::literals;
56
57 static const std::regex unit_regex(
58 "([\\+-]?\\d*\\.?\\d+)\\s*([fpnumkM]?)\\s*(W)",
59 std::regex::icase
60 );
61
62 if(std::cmatch pieces; std::regex_match(str.begin(), str.end(), pieces, unit_regex)) {
63
64 if(pieces.size() != 4) {
65 return std::nullopt;
66 }
67
68 auto s = std::stof(pieces[1].str());
69
70 if(const auto& b = pieces[2].str(); b.empty()) {
71 return s * 1_W;
72 }
73 else if(b == "f") {
74 return s * 1_fW;
75 }
76 else if(b == "p") {
77 return s * 1_pW;
78 }
79 else if(b == "n") {
80 return s * 1_nW;
81 }
82 else if(b == "u") {
83 return s * 1_uW;
84 }
85 else if(b == "m") {
86 return s * 1_mW;
87 }
88 else if(b == "k" || b == "K") {
89 return s * 1_kW;
90 }
91 else if(b == "M") {
92 return s * 1_MW;
93 }
94 else {
95 return std::nullopt;
96 }
97 }
98 else return std::nullopt;
99 }
100
101 // Function: make_capacitance_unit
make_capacitance_unit(std::string_view str)102 std::optional<farad_t> make_capacitance_unit(std::string_view str) {
103
104 using namespace units::literals;
105
106 static const std::regex unit_regex(
107 "([\\+-]?\\d*\\.?\\d+)\\s*([fpnumkM]?)\\s*(F)",
108 std::regex::icase
109 );
110
111 if(std::cmatch pieces; std::regex_match(str.begin(), str.end(), pieces, unit_regex)) {
112
113 if(pieces.size() != 4) {
114 return std::nullopt;
115 }
116
117 auto s = std::stof(pieces[1].str());
118
119 if(const auto& b = pieces[2].str(); b.empty()) {
120 return s * 1_F;
121 }
122 else if(b == "f") {
123 return s * 1_fF;
124 }
125 else if(b == "p") {
126 return s * 1_pF;
127 }
128 else if(b == "n") {
129 return s * 1_nF;
130 }
131 else if(b == "u") {
132 return s * 1_uF;
133 }
134 else if(b == "m") {
135 return s * 1_mF;
136 }
137 else if(b == "k" || b == "K") {
138 return s * 1_kF;
139 }
140 else if(b == "M") {
141 return s * 1_MF;
142 }
143 else {
144 return std::nullopt;
145 }
146 }
147 else return std::nullopt;
148 }
149
150 // Function: make_voltage_unit
make_voltage_unit(std::string_view str)151 std::optional<volt_t> make_voltage_unit(std::string_view str) {
152
153 using namespace units::literals;
154
155 static const std::regex unit_regex(
156 "([\\+-]?\\d*\\.?\\d+)\\s*([fpnumkM]?)\\s*(V)",
157 std::regex::icase
158 );
159
160 if(std::cmatch pieces; std::regex_match(str.begin(), str.end(), pieces, unit_regex)) {
161
162 if(pieces.size() != 4) {
163 return std::nullopt;
164 }
165
166 auto s = std::stof(pieces[1].str());
167
168 if(const auto& b = pieces[2].str(); b.empty()) {
169 return s * 1_V;
170 }
171 else if(b == "f") {
172 return s * 1_fV;
173 }
174 else if(b == "p") {
175 return s * 1_pV;
176 }
177 else if(b == "n") {
178 return s * 1_nV;
179 }
180 else if(b == "u") {
181 return s * 1_uV;
182 }
183 else if(b == "m") {
184 return s * 1_mV;
185 }
186 else if(b == "k" || b == "K") {
187 return s * 1_kV;
188 }
189 else if(b == "M") {
190 return s * 1_MV;
191 }
192 else {
193 return std::nullopt;
194 }
195 }
196 else return std::nullopt;
197 }
198
199 // Function: make_current_unit
make_current_unit(std::string_view str)200 std::optional<ampere_t> make_current_unit(std::string_view str) {
201
202 using namespace units::literals;
203
204 static const std::regex unit_regex(
205 "([\\+-]?\\d*\\.?\\d+)\\s*([fpnumkM]?)\\s*(A)",
206 std::regex::icase
207 );
208
209 if(std::cmatch pieces; std::regex_match(str.begin(), str.end(), pieces, unit_regex)) {
210
211 if(pieces.size() != 4) {
212 return std::nullopt;
213 }
214
215 auto s = std::stof(pieces[1].str());
216
217 if(const auto& b = pieces[2].str(); b.empty()) {
218 return s * 1_A;
219 }
220 else if(b == "f") {
221 return s * 1_fA;
222 }
223 else if(b == "p") {
224 return s * 1_pA;
225 }
226 else if(b == "n") {
227 return s * 1_nA;
228 }
229 else if(b == "u") {
230 return s * 1_uA;
231 }
232 else if(b == "m") {
233 return s * 1_mA;
234 }
235 else if(b == "k" || b == "K") {
236 return s * 1_kA;
237 }
238 else if(b == "M") {
239 return s * 1_MA;
240 }
241 else {
242 return std::nullopt;
243 }
244 }
245 else return std::nullopt;
246 }
247
248 // Function: make_resistance_unit
make_resistance_unit(std::string_view str)249 std::optional<ohm_t> make_resistance_unit(std::string_view str) {
250
251 using namespace units::literals;
252
253 static const std::regex unit_regex(
254 "([\\+-]?\\d*\\.?\\d+)\\s*([fpnumkM]?)\\s*(ohm)",
255 std::regex::icase
256 );
257
258 if(std::cmatch pieces; std::regex_match(str.begin(), str.end(), pieces, unit_regex)) {
259
260 if(pieces.size() != 4) {
261 return std::nullopt;
262 }
263
264 auto s = std::stof(pieces[1].str());
265
266 if(const auto& b = pieces[2].str(); b.empty()) {
267 return s * 1_Ohm;
268 }
269 else if(b == "f") {
270 return s * 1_fOhm;
271 }
272 else if(b == "p") {
273 return s * 1_pOhm;
274 }
275 else if(b == "n") {
276 return s * 1_nOhm;
277 }
278 else if(b == "u") {
279 return s * 1_uOhm;
280 }
281 else if(b == "m") {
282 return s * 1_mOhm;
283 }
284 else if(b == "k" || b == "K") {
285 return s * 1_kOhm;
286 }
287 else if(b == "M") {
288 return s * 1_MOhm;
289 }
290 else {
291 return std::nullopt;
292 }
293 }
294 else return std::nullopt;
295 }
296
297 }; // end of namespace ot. -----------------------------------------------------------------------
298
299
300
301
302
303