changeset 623:2081e695bfbd

is_boolean: initial import, closes #719
author David Demelier <markand@malikania.fr>
date Fri, 20 Oct 2017 13:44:20 +0200
parents 4baec6555b02
children 01e01777ff50
files cpp/CMakeLists.txt cpp/is_boolean/CMakeLists.txt cpp/is_boolean/is_boolean.hpp cpp/is_boolean/test/main.cpp
diffstat 4 files changed, 109 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- 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)
--- /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 <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-boolean
+    SOURCES 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 <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_BOOLEAN_HPP
+#define IS_BOOLEAN_HPP
+
+#include <algorithm>
+#include <cctype>
+#include <string>
+
+/**
+ * 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
--- /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 <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_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"));
+}