Mercurial > code
view cpp/is_number/is_number.hpp @ 645:2968cc4edd4c
to_int: use trailing return syntax
author | David Demelier <markand@malikania.fr> |
---|---|
date | Wed, 01 Aug 2018 14:06:59 +0200 |
parents | b327391f6a62 |
children | cba9782e10a7 |
line wrap: on
line source
/* * is_number.hpp -- check if string is a number * * Copyright (c) 2016-2018 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 bool is_int(const std::string& str, int base = 10) noexcept { 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 bool is_real(const std::string &str) noexcept { 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 bool is_number(const std::string& str) noexcept { return is_int(str) || is_real(str); } #endif // !IS_NUMBER_HPP