view cpp/is_boolean/is_boolean.hpp @ 648:5bd9424a523a

misc: extreme cleanup
author David Demelier <markand@malikania.fr>
date Thu, 04 Oct 2018 21:17:55 +0200
parents 0557eea4d373
children 87e1f4c7da76
line wrap: on
line source

/*
 * is_number.hpp -- check if string is a boolean
 *
 * 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_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 auto is_boolean(std::string value) noexcept -> bool
{
	std::transform(value.begin(), value.end(), value.begin(), [] (auto c) noexcept {
		return toupper(c);
	});

	return value == "1" || value == "YES" || value == "TRUE" || value == "ON";
}

#endif // !IS_BOOLEAN_HPP