Mercurial > code
changeset 622:4baec6555b02
is_number: initial import, closes #718
author | David Demelier <markand@malikania.fr> |
---|---|
date | Fri, 20 Oct 2017 13:37:35 +0200 |
parents | ca5912ad2909 |
children | 2081e695bfbd |
files | cpp/CMakeLists.txt cpp/is_number/CMakeLists.txt cpp/is_number/is_number.hpp cpp/is_number/test/main.cpp |
diffstat | 4 files changed, 149 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- a/cpp/CMakeLists.txt Fri Oct 20 12:36:51 2017 +0200 +++ b/cpp/CMakeLists.txt Fri Oct 20 13:37:35 2017 +0200 @@ -16,5 +16,6 @@ # add_subdirectory(executable) +add_subdirectory(is_number) add_subdirectory(join) add_subdirectory(options)
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/cpp/is_number/CMakeLists.txt Fri Oct 20 13:37:35 2017 +0200 @@ -0,0 +1,22 @@ +# +# CMakeLists.txt -- code building for common code +# +# Copyright (c) 2013-2017 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. +# + +code_define_module( + NAME is-number + SOURCES is_number.hpp +)
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/cpp/is_number/is_number.hpp Fri Oct 20 13:37:35 2017 +0200 @@ -0,0 +1,73 @@ +/* + * is_number.hpp -- check if string is a number + * + * Copyright (c) 2016-2017 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
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/cpp/is_number/test/main.cpp Fri Oct 20 13:37:35 2017 +0200 @@ -0,0 +1,53 @@ +/* + * main.cpp -- test is_number functions + * + * Copyright (c) 2013-2017 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. + */ + +#define BOOST_TEST_MODULE "is-number" +#include <boost/test/unit_test.hpp> + +#include "is_number.hpp" + +BOOST_AUTO_TEST_CASE(simple_is_int) +{ + BOOST_TEST(is_int("123")); + BOOST_TEST(is_int("-126")); + BOOST_TEST(!is_int("")); + BOOST_TEST(!is_int("bdf")); + BOOST_TEST(!is_int("false")); +} + +BOOST_AUTO_TEST_CASE(simple_is_real) +{ + BOOST_TEST(is_real("123")); + BOOST_TEST(is_real("-126")); + BOOST_TEST(is_real("56.28")); + BOOST_TEST(is_real("1e5")); + BOOST_TEST(!is_real("")); + BOOST_TEST(!is_real("bdf")); + BOOST_TEST(!is_real("false")); +} + +BOOST_AUTO_TEST_CASE(simple_is_number) +{ + BOOST_TEST(is_number("123")); + BOOST_TEST(is_number("-126")); + BOOST_TEST(is_number("56.28")); + BOOST_TEST(is_number("1e5")); + BOOST_TEST(!is_number("")); + BOOST_TEST(!is_number("bdf")); + BOOST_TEST(!is_number("false")); +}