Mercurial > code
comparison cpp/is_number/is_number.hpp @ 648:5bd9424a523a
misc: extreme cleanup
author | David Demelier <markand@malikania.fr> |
---|---|
date | Thu, 04 Oct 2018 21:17:55 +0200 |
parents | cba9782e10a7 |
children | 87e1f4c7da76 |
comparison
equal
deleted
inserted
replaced
647:0557eea4d373 | 648:5bd9424a523a |
---|---|
29 * \param base the optional base | 29 * \param base the optional base |
30 * \return true if integer | 30 * \return true if integer |
31 */ | 31 */ |
32 inline auto is_int(const std::string& str, int base = 10) noexcept -> bool | 32 inline auto is_int(const std::string& str, int base = 10) noexcept -> bool |
33 { | 33 { |
34 if (str.empty()) | 34 if (str.empty()) |
35 return false; | 35 return false; |
36 | 36 |
37 char* ptr; | 37 char* ptr; |
38 | 38 |
39 std::strtol(str.c_str(), &ptr, base); | 39 std::strtol(str.c_str(), &ptr, base); |
40 | 40 |
41 return *ptr == 0; | 41 return *ptr == 0; |
42 } | 42 } |
43 | 43 |
44 /** | 44 /** |
45 * Check if the string is real. | 45 * Check if the string is real. |
46 * | 46 * |
47 * \param value the value | 47 * \param value the value |
48 * \return true if real | 48 * \return true if real |
49 */ | 49 */ |
50 inline auto is_real(const std::string &str) noexcept -> bool | 50 inline auto is_real(const std::string &str) noexcept -> bool |
51 { | 51 { |
52 if (str.empty()) | 52 if (str.empty()) |
53 return false; | 53 return false; |
54 | 54 |
55 char* ptr; | 55 char* ptr; |
56 | 56 |
57 std::strtod(str.c_str(), &ptr); | 57 std::strtod(str.c_str(), &ptr); |
58 | 58 |
59 return *ptr == 0; | 59 return *ptr == 0; |
60 } | 60 } |
61 | 61 |
62 /** | 62 /** |
63 * Check if the string is a number. | 63 * Check if the string is a number. |
64 * | 64 * |
65 * \param value the value | 65 * \param value the value |
66 * \return true if it is a number | 66 * \return true if it is a number |
67 */ | 67 */ |
68 inline auto is_number(const std::string& str) noexcept -> bool | 68 inline auto is_number(const std::string& str) noexcept -> bool |
69 { | 69 { |
70 return is_int(str) || is_real(str); | 70 return is_int(str) || is_real(str); |
71 } | 71 } |
72 | 72 |
73 #endif // !IS_NUMBER_HPP | 73 #endif // !IS_NUMBER_HPP |