view cpp/to_int/to_int.hpp @ 628:b327391f6a62

Misc: update copyrights
author David Demelier <markand@malikania.fr>
date Wed, 03 Jan 2018 09:13:20 +0100
parents 64884a4de16a
children 5dd3347df00d
line wrap: on
line source

/*
 * to_int.hpp -- safely convert string to integers
 *
 * Copyright (c) 2017-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 TO_INT_HPP
#define TO_INT_HPP

/**
 * \file to_int.hpp
 * \brief Safely convert string to integers.
 */

#include <cassert>
#include <limits>
#include <sstream>
#include <stdexcept>
#include <string>
#include <type_traits>

/**
 * \cond HIDDEN_SYMBOLS
 */

namespace detail {

std::invalid_argument make_invalid_argument(const std::string& str)
{
    std::ostringstream oss;

    oss << "invalid number '" << str << "'";

    return std::invalid_argument(oss.str());
}

template <typename T>
std::out_of_range make_out_of_range(const std::string& str, T min, T max)
{
    std::ostringstream oss;

    oss << "number '" << str << "' is out of range ";
    oss << min << ".." << max;

    return std::out_of_range(oss.str());
}

} // !detail

/**
 * \endcond
 */

/**
 * Convert the given string into a signed integer.
 *
 * \param str the string to convert
 * \param min the minimum value allowed
 * \param max the maximum value allowed
 * \throw std::invalid_argument if the number was not parsed
 * \throw std::out_or_range if the argument is out of the specified range
 */
template <typename T = int>
T to_int(const std::string& str, T min = std::numeric_limits<T>::min(), T max = std::numeric_limits<T>::max())
{
    static_assert(std::is_signed<T>::value, "must be signed");

    char* end;
    auto v = std::strtoll(str.c_str(), &end, 10);

    if (*end != '\0')
        throw detail::make_invalid_argument(str);
    if (v < min || v > max)
        throw detail::make_out_of_range(str, min, max);

    return static_cast<T>(v);
}

/**
 * Convert the given string into an unsigned integer.
 *
 * In contrast to the [std::strtoull][strtoull] function, this functions
 * verifies if the string starts with minus sign and throws an exception if any.
 *
 * Note, for this you need to have a trimmed string which contains no leading
 * whitespaces.
 *
 * \pre string must be trimmed
 * \param str the string to convert
 * \param min the minimum value allowed
 * \param max the maximum value allowed
 * \throw std::invalid_argument if the number was not parsed
 * \throw std::out_or_range if the argument is out of the specified range
 *
 * [strtoull]: http://en.cppreference.com/w/cpp/string/byte/strtoul
 */
template <typename T = unsigned>
T to_uint(const std::string& str, T min = std::numeric_limits<T>::min(), T max = std::numeric_limits<T>::max())
{
    static_assert(std::is_unsigned<T>::value, "must be unsigned");

    assert(str.empty() || !std::isspace(str[0]));

    if (str.size() > 0U && str[0] == '-')
        throw detail::make_out_of_range(str, min, max);

    char* end;
    auto v = std::strtoull(str.c_str(), &end, 10);

    if (*end != '\0')
        throw detail::make_invalid_argument(str);
    if (v < min || v > max)
        throw detail::make_out_of_range(str, min, max);

    return v;
}

#endif // !TO_INT_HPP