Debian Codegraph @

src/dmlc-core/include/dmlc/strtonum.h

raw symbols SCIP
1 /*!
2 * Copyright (c) 2015-2018 by Contributors
3 * \file strtonum.h
4 * \brief A faster implementation of strtof and strtod
5 */
6 #ifndef DMLC_STRTONUM_H_
7 #define DMLC_STRTONUM_H_
8
9 #if DMLC_USE_CXX11
10 #include <type_traits>
11 #endif
12
13 #include <cstdint>
14 #include <limits>
15 #include <string>
16
17 #include "./base.h"
18 #include "./logging.h"
19
20 namespace dmlc {
21 /*!
22 * \brief Inline implementation of isspace(). Tests whether the given character
23 * is a whitespace letter.
24 * \param c Character to test
25 * \return Result of the test
26 */
27 inline bool isspace(char c) {
28 return (c == ' ' || c == '\t' || c == '\r' || c == '\n' || c == '\f');
29 }
30
31 /*!
32 * \brief Inline implementation of isblank(). Tests whether the given character
33 * is a space or tab character.
34 * \param c Character to test
35 * \return Result of the test
36 */
37 inline bool isblank(char c) {
38 return (c == ' ' || c == '\t');
39 }
40
41 /*!
42 * \brief Inline implementation of isdigit(). Tests whether the given character
43 * is a decimal digit
44 * \param c Character to test
45 * \return Result of the test
46 */
47 inline bool isdigit(char c) {
48 return (c >= '0' && c <= '9');
49 }
50
51 /*!
52 * \brief Inline implementation of isalpha(). Tests whether the given character
53 * is an alphabet letter
54 * \param c Character to test
55 * \return Result of the test
56 */
57 inline bool isalpha(char c) {
58 static_assert(static_cast<int>('A') == 65 && static_cast<int>('Z' - 'A') == 25,
59 "Only system with ASCII character set is supported");
60 return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z');
61 }
62
63 /*!
64 * \brief Tests whether the given character is a valid letter in the string
65 * representation of a floating-point value, i.e. decimal digits,
66 * signs (+/-), decimal point (.), or exponent marker (e/E).
67 * \param c Character to test
68 * \return Result of the test
69 */
70 inline bool isdigitchars(char c) {
71 return (c >= '0' && c <= '9') || c == '+' || c == '-' || c == '.' || c == 'e' || c == 'E';
72 }
73
74 /*!
75 * \brief Maximum number of decimal digits dmlc::strtof() / dmlc::strtod()
76 * will process. Trailing digits will be ignored.
77 */
78 const int kStrtofMaxDigits = 19;
79
80 /*!
81 * \brief Common implementation for dmlc::strtof() and dmlc::strtod()
82 * TODO: the current version does not support hex number
83 * \param nptr Beginning of the string that's to be converted into a
84 * floating-point number
85 * \param endptr After the conversion, this pointer will be set to point one
86 * past the last character used in the conversion.
87 * \return Converted floating-point value, in FloatType
88 * \tparam FloatType Type of floating-point number to be obtained. This must
89 * be either float or double.
90 * \tparam CheckRange Whether to check for overflow. If set to true, an out-
91 * of-range value will cause errno to be set to ERANGE and
92 * ParseFloat() to return HUGE_VAL / HUGE_VALF; otherwise,
93 * all out-of-range vlaues will be silently clipped.
94 */
95 template <typename FloatType, bool CheckRange = false>
96 inline FloatType ParseFloat(const char *nptr, char **endptr) {
97 #if DMLC_USE_CXX11
98 static_assert(std::is_same<FloatType, double>::value || std::is_same<FloatType, float>::value,
99 "ParseFloat is defined only for 'float' and 'double' types");
100 constexpr unsigned kMaxExponent = (std::is_same<FloatType, double>::value ? 308U : 38U);
101 constexpr FloatType kMaxSignificandForMaxExponent = static_cast<FloatType>(
102 std::is_same<FloatType, double>::value ? 1.79769313486231570 : 3.402823466);
103 // If a floating-point value has kMaxExponent, what is
104 // the largest possible significand value?
105 constexpr FloatType kMaxSignificandForNegMaxExponent = static_cast<FloatType>(
106 std::is_same<FloatType, double>::value ? 2.22507385850720139 : 1.175494351);
107 // If a floating-point value has -kMaxExponent, what is
108 // the largest possible significand value?
109 #else
110 const unsigned kMaxExponent = (sizeof(FloatType) == sizeof(double) ? 308U : 38U);
111 const FloatType kMaxSignificandForMaxExponent = static_cast<FloatType>(
112 sizeof(FloatType) == sizeof(double) ? 1.79769313486231570 : 3.402823466);
113 const FloatType kMaxSignificandForNegMaxExponent = static_cast<FloatType>(
114 sizeof(FloatType) == sizeof(double) ? 2.22507385850720139 : 1.175494351);
115 #endif
116
117 const char *p = nptr;
118 // Skip leading white space, if any. Not necessary
119 while (isspace(*p)) {
120 ++p;
121 }
122
123 // Get sign, if any.
124 bool sign = true;
125 if (*p == '-') {
126 sign = false;
127 ++p;
128 } else if (*p == '+') {
129 ++p;
130 }
131
132 // Handle INF and NAN
133 {
134 int i = 0;
135 // case-insensitive match for INF and INFINITY
136 while (i < 8 && static_cast<char>((*p) | 32) == "infinity"[i]) {
137 ++i;
138 ++p;
139 }
140 if (i == 3 || i == 8) {
141 if (endptr) {
142 *endptr = (char *)p; // NOLINT(*)
143 }
144 return sign ? std::numeric_limits<FloatType>::infinity()
145 : -std::numeric_limits<FloatType>::infinity();
146 } else {
147 p -= i;
148 }
149
150 // case-insensitive match for NAN
151 i = 0;
152 while (i < 3 && static_cast<char>((*p) | 32) == "nan"[i]) {
153 ++i;
154 ++p;
155 }
156 if (i == 3) {
157 // Got NAN; check if the value is of form NAN(char_sequence)
158 if (*p == '(') {
159 ++p;
160 while (isdigit(*p) || isalpha(*p) || *p == '_') {
161 ++p;
162 }
163 CHECK_EQ(*p, ')') << "Invalid NAN literal";
164 ++p;
165 }
166 static_assert(
167 std::numeric_limits<FloatType>::has_quiet_NaN, "Only system with quiet NaN is supported");
168 if (endptr) {
169 *endptr = (char *)p; // NOLINT(*)
170 }
171 return std::numeric_limits<FloatType>::quiet_NaN();
172 } else {
173 p -= i;
174 }
175 }
176
177 // Get digits before decimal point or exponent, if any.
178 uint64_t predec; // to store digits before decimal point
179 for (predec = 0; isdigit(*p); ++p) {
180 predec = predec * 10ULL + static_cast<uint64_t>(*p - '0');
181 }
182 FloatType value = static_cast<FloatType>(predec);
183
184 // Get digits after decimal point, if any.
185 if (*p == '.') {
186 uint64_t pow10 = 1;
187 uint64_t val2 = 0;
188 int digit_cnt = 0;
189 ++p;
190 while (isdigit(*p)) {
191 if (digit_cnt < kStrtofMaxDigits) {
192 val2 = val2 * 10ULL + static_cast<uint64_t>(*p - '0');
193 pow10 *= 10ULL;
194 } // when kStrtofMaxDigits is read, ignored following digits
195 ++p;
196 ++digit_cnt;
197 }
198 value += static_cast<FloatType>(static_cast<double>(val2) / static_cast<double>(pow10));
199 }
200
201 // Handle exponent, if any.
202 if ((*p == 'e') || (*p == 'E')) {
203 ++p;
204 bool frac = false;
205 FloatType scale = static_cast<FloatType>(1.0f);
206 unsigned expon;
207 // Get sign of exponent, if any.
208 if (*p == '-') {
209 frac = true;
210 ++p;
211 } else if (*p == '+') {
212 ++p;
213 }
214 // Get digits of exponent, if any.
215 for (expon = 0; isdigit(*p); ++p) {
216 expon = expon * 10U + static_cast<unsigned>(*p - '0');
217 }
218 if (expon > kMaxExponent) { // out of range, clip or raise error
219 if (CheckRange) {
220 errno = ERANGE;
221 if (endptr) {
222 *endptr = (char *)p; // NOLINT(*)
223 }
224 return std::numeric_limits<FloatType>::infinity();
225 } else {
226 expon = kMaxExponent;
227 }
228 }
229 // handle edge case where exponent is exactly kMaxExponent
230 if (expon == kMaxExponent
231 && ((!frac && value > kMaxSignificandForMaxExponent)
232 || (frac && value < kMaxSignificandForNegMaxExponent))) {
233 if (CheckRange) {
234 errno = ERANGE;
235 if (endptr) {
236 *endptr = (char *)p; // NOLINT(*)
237 }
238 return std::numeric_limits<FloatType>::infinity();
239 } else {
240 value = (frac ? kMaxSignificandForNegMaxExponent : kMaxSignificandForMaxExponent);
241 }
242 }
243 // Calculate scaling factor.
244 while (expon >= 8U) {
245 scale *= static_cast<FloatType>(1E8f);
246 expon -= 8U;
247 }
248 while (expon > 0U) {
249 scale *= static_cast<FloatType>(10.0f);
250 expon -= 1U;
251 }
252 // Return signed and scaled floating point result.
253 value = frac ? (value / scale) : (value * scale);
254 }
255 // Consume 'f' suffix, if any
256 if (*p == 'f' || *p == 'F') {
257 ++p;
258 }
259
260 if (endptr) {
261 *endptr = (char *)p; // NOLINT(*)
262 }
263 return sign ? value : -value;
264 }
265
266 /*!
267 * \brief A faster implementation of strtof(). See documentation of
268 * std::strtof() for more information. Note that this function does not
269 * check for overflow. Use strtof_check_range() to check for overflow.
270 * TODO: the current version does not support hex number
271 * TODO: the current version does not handle long decimals: you may only have
272 * up to 19 digits after the decimal point, and you cannot have too many
273 * digits before the decimal point either.
274 * \param nptr Beginning of the string that's to be converted into float
275 * \param endptr After the conversion, this pointer will be set to point one
276 * past the last character used in the conversion.
277 * \return Converted floating-point value, in float type
278 */
279 inline float strtof(const char *nptr, char **endptr) {
280 return ParseFloat<float>(nptr, endptr);
281 }
282
283 /*!
284 * \brief A faster implementation of strtof(). See documentation of
285 * std::strtof() for more information. This function will check for
286 * overflow. If the converted value is outside the range for the float
287 * type, errno is set to ERANGE and HUGE_VALF is returned.
288 * TODO: the current version does not support hex number
289 * TODO: the current version does not handle long decimals: you may only have
290 * up to 19 digits after the decimal point, and you cannot have too many
291 * digits before the decimal point either.
292 * \param nptr Beginning of the string that's to be converted into float
293 * \param endptr After the conversion, this pointer will be set to point one
294 * past the last character used in the conversion.
295 * \return Converted floating-point value, in float type
296 */
297 inline float strtof_check_range(const char *nptr, char **endptr) {
298 return ParseFloat<float, true>(nptr, endptr);
299 }
300
301 /*!
302 * \brief A faster implementation of strtod(). See documentation of
303 * std::strtof() for more information. Note that this function does not
304 * check for overflow. Use strtod_check_range() to check for overflow.
305 * TODO: the current version does not support hex number
306 * TODO: the current version does not handle long decimals: you may only have
307 * up to 19 digits after the decimal point, and you cannot have too many
308 * digits before the decimal point either.
309 * \param nptr Beginning of the string that's to be converted into double
310 * \param endptr After the conversion, this pointer will be set to point one
311 * past the last character used in the conversion.
312 * \return Converted floating-point value, in double type
313 */
314 inline double strtod(const char *nptr, char **endptr) {
315 return ParseFloat<double>(nptr, endptr);
316 }
317
318 /*!
319 * \brief A faster implementation of strtod(). See documentation of
320 * std::strtod() for more information. This function will check for
321 * overflow. If the converted value is outside the range for the double
322 * type, errno is set to ERANGE and HUGE_VAL is returned.
323 * TODO: the current version does not support hex number
324 * TODO: the current version does not handle long decimals: you may only have
325 * up to 19 digits after the decimal point, and you cannot have too many
326 * digits before the decimal point either.
327 * \param nptr Beginning of the string that's to be converted into double
328 * \param endptr After the conversion, this pointer will be set to point one
329 * past the last character used in the conversion.
330 * \return Converted floating-point value, in float type
331 */
332 inline double strtod_check_range(const char *nptr, char **endptr) {
333 return ParseFloat<double, true>(nptr, endptr);
334 }
335
336 /*!
337 * \brief A fast string-to-integer convertor, for signed integers
338 * TODO: the current version supports only base <= 10
339 * \param nptr Beginning of the string that's to be converted into a signed
340 * integer
341 * \param endptr After the conversion, this pointer will be set to point one
342 * past the last character used in the conversion.
343 * \param base Base to use for integer conversion
344 * \return Converted value, in SignedIntType
345 * \tparam SignedIntType Type of signed integer to be obtained.
346 */
347 template <typename SignedIntType>
348 inline SignedIntType ParseSignedInt(const char *nptr, char **endptr, int base) {
349 #ifdef DMLC_USE_CXX11
350 static_assert(std::is_signed<SignedIntType>::value && std::is_integral<SignedIntType>::value,
351 "ParseSignedInt is defined for signed integers only");
352 #endif
353 CHECK(base <= 10 && base >= 2);
354 const char *p = nptr;
355 // Skip leading white space, if any. Not necessary
356 while (isspace(*p)) {
357 ++p;
358 }
359
360 // Get sign if any
361 bool sign = true;
362 if (*p == '-') {
363 sign = false;
364 ++p;
365 } else if (*p == '+') {
366 ++p;
367 }
368
369 SignedIntType value;
370 const SignedIntType base_val = static_cast<SignedIntType>(base);
371 for (value = 0; isdigit(*p); ++p) {
372 value = value * base_val + static_cast<SignedIntType>(*p - '0');
373 }
374
375 if (endptr) {
376 *endptr = (char *)p; // NOLINT(*)
377 }
378 return sign ? value : -value;
379 }
380
381 /*!
382 * \brief A fast string-to-integer convertor, for unsigned integers
383 * TODO: the current version supports only base <= 10
384 * \param nptr Beginning of the string that's to be converted into an unsigned
385 * integer
386 * \param endptr After the conversion, this pointer will be set to point one
387 * past the last character used in the conversion.
388 * \param base Base to use for integer conversion
389 * \return Converted value, in UnsignedIntType
390 * \tparam UnsignedIntType Type of unsigned integer to be obtained.
391 */
392 template <typename UnsignedIntType>
393 inline UnsignedIntType ParseUnsignedInt(const char *nptr, char **endptr, int base) {
394 #ifdef DMLC_USE_CXX11
395 static_assert(
396 std::is_unsigned<UnsignedIntType>::value && std::is_integral<UnsignedIntType>::value,
397 "ParseUnsignedInt is defined for unsigned integers only");
398 #endif
399 CHECK(base <= 10 && base >= 2);
400 const char *p = nptr;
401 // Skip leading white space, if any. Not necessary
402 while (isspace(*p)) {
403 ++p;
404 }
405
406 // Get sign if any
407 bool sign = true;
408 if (*p == '-') {
409 sign = false;
410 ++p;
411 } else if (*p == '+') {
412 ++p;
413 }
414
415 // we are parsing unsigned, so no minus sign should be found
416 CHECK_EQ(sign, true);
417
418 UnsignedIntType value;
419 const UnsignedIntType base_val = static_cast<UnsignedIntType>(base);
420 for (value = 0; isdigit(*p); ++p) {
421 value = value * base_val + static_cast<UnsignedIntType>(*p - '0');
422 }
423
424 if (endptr) {
425 *endptr = (char *)p; // NOLINT(*)
426 }
427 return value;
428 }
429
430 /*!
431 * \brief A faster implementation of strtoull(). See documentation of
432 * std::strtoull() for more information. Note that this function does not
433 * check for overflow.
434 * TODO: the current version supports only base <= 10
435 * \param nptr Beginning of the string that's to be converted into integer of
436 * type unsigned long long
437 * \param endptr After the conversion, this pointer will be set to point one
438 * past the last character used in the conversion.
439 * \param base Base to use for integer conversion
440 * \return Converted value, as unsigned 64-bit integer
441 */
442 inline uint64_t strtoull(const char *nptr, char **endptr, int base) {
443 return ParseUnsignedInt<uint64_t>(nptr, endptr, base);
444 }
445
446 /*!
447 * \brief A faster implementation of atol(). See documentation of std::atol()
448 * for more information. This function will use base 10. Note that this
449 * function does not check for overflow.
450 * \param p Beginning of the string that's to be converted into integer of
451 * type long
452 * \return Converted value, as long integer (width is system-dependent)
453 */
454 inline long atol(const char *p) { // NOLINT(*)
455 return ParseSignedInt<long>(p, 0, 10); // NOLINT(*)
456 }
457
458 /*!
459 * \brief A faster implementation of atof(). Unlike std::atof(), this function
460 * returns float type. Note that this function does not check for overflow.
461 * TODO: the current version does not support hex number
462 * TODO: the current version does not handle long decimals: you may only have
463 * up to 19 digits after the decimal point, and you cannot have too many
464 * digits before the decimal point either.
465 * \param nptr Beginning of the string that's to be converted into float
466 * \return Converted value, in float type
467 */
468 inline float atof(const char *nptr) {
469 return strtof(nptr, 0);
470 }
471
472 /*!
473 * \brief A faster implementation of stof(). See documentation of std::stof()
474 * for more information. This function will test for overflow and
475 * invalid arguments.
476 * TODO: the current version does not support hex number
477 * TODO: the current version does not handle long decimals: you may only have
478 * up to 19 digits after the decimal point, and you cannot have too many
479 * digits before the decimal point either.
480 * \param value The string to convert into float
481 * \param pos If not null, it will store the number of characters processed
482 * \return Converted value, in float type
483 * \throw std::out_of_range If the converted value would fall out of the range
484 * of the double type
485 * \throw std::invalid_argument If no conversion could be performed
486 */
487 inline float stof(const std::string &value, size_t *pos = nullptr) {
488 const char *str_source = value.c_str();
489 char *endptr;
490 const float parsed_value = dmlc::strtof_check_range(str_source, &endptr);
491 if (errno == ERANGE && parsed_value == std::numeric_limits<float>::infinity()) {
492 throw std::out_of_range("Out of range value");
493 } else if (const_cast<const char *>(endptr) == str_source) {
494 throw std::invalid_argument("No conversion could be performed");
495 }
496 if (pos) {
497 *pos = static_cast<size_t>(const_cast<const char *>(endptr) - str_source);
498 }
499 return parsed_value;
500 }
501
502 /*!
503 * \brief A faster implementation of stod(). See documentation of std::stod()
504 * for more information. This function will test for overflow and
505 * invalid arguments.
506 * TODO: the current version does not support hex number
507 * TODO: the current version does not handle long decimals: you may only have
508 * up to 19 digits after the decimal point, and you cannot have too many
509 * digits before the decimal point either.
510 * \param value The string to convert into double
511 * \param pos If not null, it will store the number of characters processed
512 * \return Converted value, in double type
513 * \throw std::out_of_range If the converted value would fall out of the range
514 * of the double type
515 * \throw std::invalid_argument If no conversion could be performed
516 */
517 inline double stod(const std::string &value, size_t *pos = nullptr) {
518 const char *str_source = value.c_str();
519 char *endptr;
520 const double parsed_value = dmlc::strtod_check_range(str_source, &endptr);
521 if (errno == ERANGE && parsed_value == std::numeric_limits<double>::infinity()) {
522 throw std::out_of_range("Out of range value");
523 } else if (const_cast<const char *>(endptr) == str_source) {
524 throw std::invalid_argument("No conversion could be performed");
525 }
526 if (pos) {
527 *pos = static_cast<size_t>(const_cast<const char *>(endptr) - str_source);
528 }
529 return parsed_value;
530 }
531
532 /*!
533 * \brief Interface class that defines a single method get() to convert
534 * a string into type T. Define template specialization of this class
535 * to define the conversion method for a particular type.
536 * \tparam Type of converted value
537 */
538 template <typename T>
539 class Str2T {
540 public:
541 /*!
542 * \brief Convert a string into type T
543 * \param begin Beginning of the string to convert
544 * \return Converted value, in type T
545 */
546 static inline T get(const char *begin);
547 };
548
549 /*!
550 * \brief Convenience function for converting string into type T
551 * \param begin Beginning of the string to convert
552 * \return Converted value, in type T
553 * \tparam Type of converted value
554 */
555 template <typename T>
556 inline T Str2Type(const char *begin) {
557 return Str2T<T>::get(begin);
558 }
559
560 /*!
561 * \brief Template specialization of Str2T<> interface for signed 32-bit integer
562 */
563 template <>
564 class Str2T<int32_t> {
565 public:
566 /*!
567 * \brief Convert a string into signed 32-bit integer
568 * \param begin Beginning of the string to convert
569 * \return Converted value, as signed 32-bit integer
570 */
571 static inline int32_t get(const char *begin) {
572 return ParseSignedInt<int32_t>(begin, NULL, 10);
573 }
574 };
575
576 /*!
577 * \brief Template specialization of Str2T<> interface for unsigned 32-bit integer
578 */
579 template <>
580 class Str2T<uint32_t> {
581 public:
582 /*!
583 * \brief Convert a string into unsigned 32-bit integer
584 * \param begin Beginning of the string to convert
585 * \return Converted value, as unsigned 32-bit integer
586 */
587 static inline uint32_t get(const char *begin) {
588 return ParseUnsignedInt<uint32_t>(begin, NULL, 10);
589 }
590 };
591
592 /*!
593 * \brief Template specialization of Str2T<> interface for signed 64-bit integer
594 */
595 template <>
596 class Str2T<int64_t> {
597 public:
598 /*!
599 * \brief Convert a string into signed 64-bit integer
600 * \param begin Beginning of the string to convert
601 * \return Converted value, as signed 64-bit integer
602 */
603 static inline int64_t get(const char *begin) {
604 return ParseSignedInt<int64_t>(begin, NULL, 10);
605 }
606 };
607
608 /*!
609 * \brief Template specialization of Str2T<> interface for unsigned 64-bit integer
610 */
611 template <>
612 class Str2T<uint64_t> {
613 public:
614 /*!
615 * \brief Convert a string into unsigned 64-bit integer
616 * \param begin Beginning of the string to convert
617 * \return Converted value, as unsigned 64-bit integer
618 */
619 static inline uint64_t get(const char *begin) {
620 return ParseUnsignedInt<uint64_t>(begin, NULL, 10);
621 }
622 };
623
624 /*!
625 * \brief Template specialization of Str2T<> interface for float type
626 */
627 template <>
628 class Str2T<float> {
629 public:
630 /*!
631 * \brief Convert a string into float
632 * \param begin Beginning of the string to convert
633 * \return Converted value, in float type
634 */
635 static inline float get(const char *begin) {
636 return atof(begin);
637 }
638 };
639
640 /*!
641 * \brief Template specialization of Str2T<> interface for double type
642 */
643 template <>
644 class Str2T<double> {
645 public:
646 /*!
647 * \brief Convert a string into double
648 * \param begin Beginning of the string to convert
649 * \return Converted value, in double type
650 */
651 static inline double get(const char *begin) {
652 return strtod(begin, 0);
653 }
654 };
655
656 /*!
657 * \brief Parse colon seperated pair v1[:v2]
658 * \param begin pointer to string
659 * \param end one past end of string
660 * \param endptr After conversion, will be set to one past of parsed string
661 * \param v1 first value in the pair
662 * \param v2 second value in the pair
663 * \return number of values parsed
664 * \tparam T1 type of v1
665 * \tparam T2 type of v2
666 */
667 template <typename T1, typename T2>
668 inline int ParsePair(
669 const char *begin, const char *end, const char **endptr, T1 &v1, T2 &v2) { // NOLINT(*)
670 const char *p = begin;
671 while (p != end && !isdigitchars(*p)) {
672 ++p;
673 }
674 if (p == end) {
675 *endptr = end;
676 return 0;
677 }
678 const char *q = p;
679 while (q != end && isdigitchars(*q)) {
680 ++q;
681 }
682 v1 = Str2Type<T1>(p);
683 p = q;
684 while (p != end && isblank(*p)) {
685 ++p;
686 }
687 if (p == end || *p != ':') {
688 // only v1
689 *endptr = p;
690 return 1;
691 }
692 p++;
693 while (p != end && !isdigitchars(*p)) {
694 ++p;
695 }
696 q = p;
697 while (q != end && isdigitchars(*q)) {
698 ++q;
699 }
700 *endptr = q;
701 v2 = Str2Type<T2>(p);
702 return 2;
703 }
704
705 /*!
706 * \brief Parse colon seperated triple v1:v2[:v3]
707 * \param begin pointer to string
708 * \param end one past end of string
709 * \param endptr After conversion, will be set to one past of parsed string
710 * \param v1 first value in the triple
711 * \param v2 second value in the triple
712 * \param v3 third value in the triple
713 * \return number of values parsed
714 * \tparam T1 type of v1
715 * \tparam T2 type of v2
716 * \tparam T3 type of v3
717 */
718 template <typename T1, typename T2, typename T3>
719 inline int ParseTriple(
720 const char *begin, const char *end, const char **endptr, T1 &v1, T2 &v2, T3 &v3) { // NOLINT(*)
721 const char *p = begin;
722 while (p != end && !isdigitchars(*p)) {
723 ++p;
724 }
725 if (p == end) {
726 *endptr = end;
727 return 0;
728 }
729 const char *q = p;
730 while (q != end && isdigitchars(*q)) {
731 ++q;
732 }
733 v1 = Str2Type<T1>(p);
734 p = q;
735 while (p != end && isblank(*p)) {
736 ++p;
737 }
738 if (p == end || *p != ':') {
739 // only v1
740 *endptr = p;
741 return 1;
742 }
743 p++;
744 while (p != end && !isdigitchars(*p)) {
745 ++p;
746 }
747 q = p;
748 while (q != end && isdigitchars(*q)) {
749 ++q;
750 }
751 v2 = Str2Type<T2>(p);
752 p = q;
753 while (p != end && isblank(*p)) {
754 ++p;
755 }
756 if (p == end || *p != ':') {
757 // only v1:v2
758 *endptr = p;
759 return 2;
760 }
761 p++;
762 while (p != end && !isdigitchars(*p)) {
763 ++p;
764 }
765 q = p;
766 while (q != end && isdigitchars(*q)) {
767 ++q;
768 }
769 *endptr = q;
770 v3 = Str2Type<T3>(p);
771 return 3;
772 }
773 } // namespace dmlc
774
775 #endif // DMLC_STRTONUM_H_