# HG changeset patch # User David Demelier # Date 1508499860 -7200 # Node ID 2081e695bfbdab4eabd169ad8eeef18a53eb2e79 # Parent 4baec6555b027454923fa54c4163308b58cfc8a2 is_boolean: initial import, closes #719 diff -r 4baec6555b02 -r 2081e695bfbd cpp/CMakeLists.txt --- a/cpp/CMakeLists.txt Fri Oct 20 13:37:35 2017 +0200 +++ b/cpp/CMakeLists.txt Fri Oct 20 13:44:20 2017 +0200 @@ -16,6 +16,7 @@ # add_subdirectory(executable) +add_subdirectory(is_boolean) add_subdirectory(is_number) add_subdirectory(join) add_subdirectory(options) diff -r 4baec6555b02 -r 2081e695bfbd cpp/is_boolean/CMakeLists.txt --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/cpp/is_boolean/CMakeLists.txt Fri Oct 20 13:44:20 2017 +0200 @@ -0,0 +1,23 @@ +# +# CMakeLists.txt -- code building for common code +# +# Copyright (c) 2013-2017 David Demelier +# +# 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-boolean + SOURCES is_boolean.hpp +) + diff -r 4baec6555b02 -r 2081e695bfbd cpp/is_boolean/is_boolean.hpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/cpp/is_boolean/is_boolean.hpp Fri Oct 20 13:44:20 2017 +0200 @@ -0,0 +1,42 @@ +/* + * is_number.hpp -- check if string is a boolean + * + * Copyright (c) 2016-2017 David Demelier + * + * 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_BOOLEAN_HPP +#define IS_BOOLEAN_HPP + +#include +#include +#include + +/** + * Check if the value is a boolean, 1, yes and true are accepted. + * + * \param value the value (will be change to uppercase) + * \return true if is boolean + * \note this function is case-insensitive + */ +inline bool is_boolean(std::string value) noexcept +{ + std::transform(value.begin(), value.end(), value.begin(), [] (auto c) { + return toupper(c); + }); + + return value == "1" || value == "YES" || value == "TRUE" || value == "ON"; +} + +#endif // !IS_BOOLEAN_HPP diff -r 4baec6555b02 -r 2081e695bfbd cpp/is_boolean/test/main.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/cpp/is_boolean/test/main.cpp Fri Oct 20 13:44:20 2017 +0200 @@ -0,0 +1,43 @@ +/* + * main.cpp -- test is_boolean functions + * + * Copyright (c) 2013-2017 David Demelier + * + * 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 + +#include "is_boolean.hpp" + +BOOST_AUTO_TEST_CASE(simple_is_boolean) +{ + BOOST_REQUIRE(is_boolean("true")); + BOOST_REQUIRE(is_boolean("True")); + BOOST_REQUIRE(is_boolean("TRUE")); + BOOST_REQUIRE(is_boolean("TruE")); + BOOST_REQUIRE(is_boolean("yes")); + BOOST_REQUIRE(is_boolean("Yes")); + BOOST_REQUIRE(is_boolean("YES")); + BOOST_REQUIRE(is_boolean("YeS")); + BOOST_REQUIRE(is_boolean("on")); + BOOST_REQUIRE(is_boolean("On")); + BOOST_REQUIRE(is_boolean("oN")); + BOOST_REQUIRE(is_boolean("ON")); + BOOST_REQUIRE(is_boolean("1")); + BOOST_REQUIRE(!is_boolean("false")); + BOOST_REQUIRE(!is_boolean("lol")); + BOOST_REQUIRE(!is_boolean("")); + BOOST_REQUIRE(!is_boolean("0")); +}