Mercurial > code
view cpp/is_number/is_number.hpp @ 653:87e1f4c7da76
misc: happy new year!
author | David Demelier <markand@malikania.fr> |
---|---|
date | Tue, 08 Jan 2019 21:19:17 +0100 |
parents | 5bd9424a523a |
children |
line wrap: on
line source
/* * is_number.hpp -- check if string is a number * * Copyright (c) 2016-2019 David Demelier <markand@malikania.fr> * * Permission to use, copy, modify, and/or distribute this software for any * purpose with or without fee is hereby granted, provided that the above * copyright notice and this permission notice appear in all copies. * * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ #ifndef IS_NUMBER_HPP #define IS_NUMBER_HPP #include <cstdlib> #include <string> /** * Check if the string is an integer. * * \param value the input * \param base the optional base * \return true if integer */ inline auto is_int(const std::string& str, int base = 10) noexcept -> bool { if (str.empty()) return false; char* ptr; std::strtol(str.c_str(), &ptr, base); return *ptr == 0; } /** * Check if the string is real. * * \param value the value * \return true if real */ inline auto is_real(const std::string &str) noexcept -> bool { if (str.empty()) return false; char* ptr; std::strtod(str.c_str(), &ptr); return *ptr == 0; } /** * Check if the string is a number. * * \param value the value * \return true if it is a number */ inline auto is_number(const std::string& str) noexcept -> bool { return is_int(str) || is_real(str); } #endif // !IS_NUMBER_HPP