-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblConvertToNumber.hpp
480 lines (383 loc) · 13.7 KB
/
blConvertToNumber.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
#ifndef BL_CONVERTTONUMBER_HPP
#define BL_CONVERTTONUMBER_HPP
//-------------------------------------------------------------------
// FUNCTION: convertToNumber
//
//
//
// ARGUMENTS: - BeginIter
// - EndIter
// - DecimalPointDelimiter
// - ConvertedNumber
//
//
//
// TEMPLATE ARGUMENTS: - blStringIteratorType
// - blCharacterType
// - blNumberType
//
//
//
// PURPOSE: -- This function used to convert a sequence of
// characters into a floating point number
//
// -- The function accepts "begin" and "end" iterators
// and allows the user to specify the decimal point
// delimiter/token
//
// -- The function also return an iterator pointing to the
// place right after the last character used to convert
// to a number
//
// -- This Function is defined within the "blAlgorithmsLIB"
// namespace
//
//
//
// AUTHOR: Vincenzo Barbato
// http://www.barbatolabs.com
// navyenzo@gmail.com
//
//
//
// LISENSE: MIT-LICENCE
// http://www.opensource.org/licenses/mit-license.php
//-------------------------------------------------------------------
//-------------------------------------------------------------------
// Includes and libs needed for this file
//-------------------------------------------------------------------
#include <cmath>
#include <cstdint>
#include "blCyclicStlAlgorithms.hpp"
//-------------------------------------------------------------------
//-------------------------------------------------------------------
// NOTE: This class is defined within the blAlgorithmsLIB namespace
//-------------------------------------------------------------------
namespace blAlgorithmsLIB
{
//-------------------------------------------------------------------
//-------------------------------------------------------------------
// Function used to convert a string to a number, returning an
// iterator pointing
//-------------------------------------------------------------------
template<typename blStringIteratorType,
typename blCharacterType,
typename blNumberType>
inline blStringIteratorType convertToNumber(const blStringIteratorType& beginIter,
const blStringIteratorType& endIter,
const blCharacterType& decimalPointDelimiter,
blNumberType& convertedNumber,
const int& numberOfTimesToCycleIfIteratorIsCircular)
{
// First we check
// if the user
// passed a zero
// sized string
if(beginIter == endIter)
{
// In this case
// there's nothing
// to convert so we
// simply return
return endIter;
}
// Initialize the
// converted number
convertedNumber = blNumberType(0);
// Iterator used to
// parse the string
blStringIteratorType currentPos = beginIter;
// Boolean to check
// if the number is
// negative
bool isNumberNegative = false;
// Boolean and
// multiplier
// used to handle
// decimal point
// digits
bool hasDecimalPointBeenEncounteredAlready = false;
blNumberType decimalPointMultiplier = blNumberType(1);
// The first step
// is to check the
// first digit for
// special characters
if((*currentPos) == '-')
{
// This means the
// number is negative
isNumberNegative = true;
// Advance the position
++currentPos;
}
else if((*currentPos) == '+')
{
// This means the
// number is positive
// so we simply advance
// the position to the
// next character
++currentPos;
}
else if((*currentPos) == decimalPointDelimiter)
{
// This means the
// number starts with
// a decimal point
hasDecimalPointBeenEncounteredAlready = true;
++currentPos;
}
else if((*currentPos) == 'e' || (*currentPos) == 'E')
{
// In this case, the
// first character of
// the string is an 'e'
// or 'E', which means
// that the number is
// an exponent.
// Since it is the first
// character, then the
// number will be
// 10^Exponent.
// Therefore, we find
// the exponent and
// then raise 10
// to its power
++currentPos;
blNumberType exponent;
blStringIteratorType newPos = convertToNumber(currentPos,
endIter,
decimalPointDelimiter,
exponent,
numberOfTimesToCycleIfIteratorIsCircular - 1);
if(newPos == currentPos)
{
// We obviously
// had no exponent
// so we assume
// that it was e0
// or * 10^0, which
// means it is 1
convertedNumber = blNumberType(1);
return currentPos;
}
else
{
// In this case,
// we had a valid
// exponent
convertedNumber = blNumberType(std::pow(double(10),double(exponent)));
currentPos = newPos;
}
return currentPos;
}
// Keep track of how
// many times we go
// over the BeginIter
// in case of a circular
// iterator
int numberOfRepeats = 0;
// Now we loop through
// the remaining elements
// of the string and
// calculate the number
// accordingly.
// NOTE: Remember that we
// are looping from
// left to right, so
// from the highest
// digit to the lowest
// value digit (numerically
// speaking)
while((currentPos != endIter) &&
numberOfRepeats <= numberOfTimesToCycleIfIteratorIsCircular)
{
if((*currentPos) >= '0' && (*currentPos) <= '9')
{
// This means we
// have a valid
// numeric digit
if(hasDecimalPointBeenEncounteredAlready)
{
// This means that
// the digit is
// after the decimal
// point, so we add
// the current digit
// divided by its
// place after the
// decimal point
decimalPointMultiplier *= blNumberType(10);
convertedNumber = convertedNumber + blNumberType((*currentPos) - '0')/decimalPointMultiplier;
}
else
{
// This means that
// the digit is before
// the decimal point,
// so we multiply the
// current number by
// 10 and add the new
// digit
convertedNumber = convertedNumber * blNumberType(10) + blNumberType((*currentPos) - '0');
}
}
else if((*currentPos) == decimalPointDelimiter && !hasDecimalPointBeenEncounteredAlready)
{
// This means that
// we have just found
// the decimal point
// so we just store the
// fact that we found it
// in the boolean
hasDecimalPointBeenEncounteredAlready = true;
}
else if((*currentPos) == 'e' || (*currentPos) == 'E')
{
// In this case, it
// means that we're
// about to multiply
// the current number
// by 10^Exponent.
// Thus, we find the value of the
// exponent and then multiply
// First we move
// to the next
// position in the
// string (the one
// after the 'e' or 'E')
++currentPos;
// Then we calculate
// the exponent by
// recursively calling
// this function
blNumberType exponent;
blStringIteratorType newPos = convertToNumber(currentPos,
endIter,
decimalPointDelimiter,
exponent,
numberOfTimesToCycleIfIteratorIsCircular - numberOfRepeats);
if(newPos == currentPos)
{
// If the iterators
// are equal, that
// means that the
// function did not
// find a valid exponent.
// In this case, we
// assume the exponent
// was zero and we
// multiply the current
// number by 10^0,
// meaning multiply by 1.
// So we are done
break;
}
else
{
// In this case,
// we had a valid
// exponent, so
// we multiply the
// current number
// by 10^Exponent
convertedNumber = convertedNumber * blNumberType(std::pow(double(10),double(exponent)));
currentPos = newPos;
// We are done
break;
}
}
else
{
// In this case, we
// found a non-valid
// character, so we
// are done
break;
}
// Advance the
// iterator
++currentPos;
if(currentPos == beginIter)
++numberOfRepeats;
}
// Check if the number
// is negative
if(isNumberNegative)
convertedNumber *= blNumberType(-1);
// Now we return the
// current position
return currentPos;
}
//-------------------------------------------------------------------
//-------------------------------------------------------------------
// Convenient template functions to simplify the
// use of the string to number conversion function
//-------------------------------------------------------------------
template<typename blStringType,
typename blNumberType>
inline void convertStringToNumber(const blStringType& inputString,
blNumberType& convertedNumber)
{
convertToNumber(inputString.begin(),
inputString.end(),
'.',
convertedNumber,
0);
}
template<typename blStringType>
inline int convertStringToInteger(const blStringType& inputString)
{
int result = 0;
convertStringToNumber(inputString,result);
return result;
}
template<typename blStringType>
inline double convertStringToDouble(const blStringType& inputString)
{
double result = 0;
convertStringToNumber(inputString,result);
return result;
}
//-------------------------------------------------------------------
//-------------------------------------------------------------------
// Function used to convert a string of multiple numbers to
// an array of numbers
//-------------------------------------------------------------------
template<typename blCharacterType,
typename blNumberType,
int arrayLength>
inline void convertStringOfNumbersToArrayOfNumbers(const blCharacterType* inputRawString,
const int64_t& sizeInBytesOfInputRawString,
blNumberType (&parsedArrayOfNumbers)[arrayLength],
const blCharacterType& numberSeparatorDelimiter)
{
auto currentPosition = inputRawString;
auto endOfString = inputRawString + sizeInBytesOfInputRawString;
int numberIndex = 0;
while(currentPosition != endOfString &&
numberIndex < arrayLength)
{
// Convert the number while also getting
// an iterator to the next delimiter
currentPosition = convertToNumber(currentPosition,
endOfString,
'.',
parsedArrayOfNumbers[numberIndex],
0);
// We then move to the position
// just after the found delimiter
// where the next number should start
// unless we've reached the end of
// the string
if(currentPosition != endOfString)
++currentPosition;
++numberIndex;
}
}
//-------------------------------------------------------------------
//-------------------------------------------------------------------
// End of namespace
}
//-------------------------------------------------------------------
#endif // BL_CONVERTTONUMBER_HPP