<commit_before>// Secure input header.
// It contains the definition of sostream, which is a secured version of istream.
// This class is a high level class so it will not provide much functions.
// I know this could have been better but if you want to code a better version feel free to do so.

// This code is under MIT license.
// by zeFresk, 2015

#ifndef SIOSTREAMMM
#define SIOSTREAMMM

/**
 * @brief ASSERT_ON_FAILBIT_FLAG
 *
 * Uncomment if you want an assertion if failbit may throw an exception.
 *
 */
//#define ASSERT_ON_FAILBIT_FLAG

#include <iostream>
#include <algorithm>
#include <string>
#include <stdexcept>
#include <cassert>
#include <limits>
#include <functional>
#include <array>
#include <type_traits>
#include <sstream>

/**
 * @brief output_object
 * @typedef type : give basic_string associated type. (std::string or std::wstring)
 * @var object : give the output console object associated. (std::cout or std::wcout)
 *
 * Gives the type of basic_string associated (string/wstring) with template parameter and console output object (such as cout/wcout).
 *
 */
template <typename T>
struct output_object;
template <>
struct output_object<char>
{
    typedef typename std::string type;
    static std::ostream& object;
};
template <>
struct output_object<wchar_t>
{
    typedef typename std::wstring type;
    static std::wostream& object;
};

/**
 * @brief error_message
 *
 * Return the default error message.
 *
 */
template <typename CharT>
inline typename output_object<CharT>::type error_message() {}
template <>
inline typename output_object<char>::type error_message<char>() {return "Please enter a correct value.";}
template <>
inline typename output_object<wchar_t>::type error_message<wchar_t>() {return L"Please enter a correct value.";}

/**
 * @brief default_failure
 *
 * Default on_failure function.
 *
 */
template <typename CharT>
struct default_failure
{
    void operator()(typename output_object<CharT>::type const&) const {}
};

/**
 * @brief make_array
 * @param[in] ...values : set of values to put in a std::array
 *
 * @pre all values as to be convertible to an unique type.
 *
 * @return an std::array filled with values.
 */
template <typename... Args>
constexpr auto make_array(Args&&... values) -> std::array<typename std::common_type<Args...>::type, sizeof...(values)> //tricky way to std::array
{
    return {std::forward<Args>(values)...}; //double "{" to ensure initalizer-list.
}

/**
 * @brief operator_with_save
 * @param[in] is : we will extract data from here.
 * @param[out] input : variable which will stock user input if it succeed.
 * @param[out] str : last input.
 *
 * Same as istream& operator>> but the last input will be saved as a string in third parameter.
 *
 */
template <typename CharT, typename Traits, typename Input_Type>
std::basic_istream<CharT, Traits>& operator_with_save(std::basic_istream<CharT, Traits>& is, Input_Type& input, typename output_object<CharT>::type& str)
{
    is >> str;
    std::basic_stringstream<CharT, Traits> stream(str);
    return (stream >> input);
}

template <typename CharT, typename Traits, typename Type>
typename output_object<CharT>::type to_best_string(Type const& value)
{
    std::basic_stringstream<CharT, Traits> stream;
    stream << value;<commit_msg>Ajout d'un peu de doc.<commit_after>// Secure input header.
// It contains the definition of sostream, which is a secured version of istream.
// This class is a high level class so it will not provide much functions.
// I know this could have been better but if you want to code a better version feel free to do so.

// This code is under MIT license.
// by zeFresk, 2015

#ifndef SIOSTREAMMM
#define SIOSTREAMMM

/**
 * @brief ASSERT_ON_FAILBIT_FLAG
 *
 * Uncomment if you want an assertion if failbit may throw an exception.
 *
 */
//#define ASSERT_ON_FAILBIT_FLAG

#include <iostream>
#include <algorithm>
#include <string>
#include <stdexcept>
#include <cassert>
#include <limits>
#include <functional>
#include <array>
#include <type_traits>
#include <sstream>

/**
 * @brief output_object
 * @typedef type : give basic_string associated type. (std::string or std::wstring)
 * @var object : give the output console object associated. (std::cout or std::wcout)
 *
 * Gives the type of basic_string associated (string/wstring) with template parameter and console output object (such as cout/wcout).
 *
 */
template <typename T>
struct output_object;
template <>
struct output_object<char>
{
    typedef typename std::string type;
    static std::ostream& object;
};
template <>
struct output_object<wchar_t>
{
    typedef typename std::wstring type;
    static std::wostream& object;
};

/**
 * @brief error_message
 *
 * Return the default error message.
 *
 */
template <typename CharT>
inline typename output_object<CharT>::type error_message() {}
template <>
inline typename output_object<char>::type error_message<char>() {return "Please enter a correct value.";}
template <>
inline typename output_object<wchar_t>::type error_message<wchar_t>() {return L"Please enter a correct value.";}

/**
 * @brief default_failure
 *
 * Default on_failure function.
 *
 */
template <typename CharT>
struct default_failure
{
    void operator()(typename output_object<CharT>::type const&) const {}
};

/**
 * @brief make_array
 * @param[in] ...values : set of values to put in a std::array
 *
 * @pre all values as to be convertible to an unique type.
 *
 * @return an std::array filled with values.
 */
template <typename... Args>
constexpr auto make_array(Args&&... values) -> std::array<typename std::common_type<Args...>::type, sizeof...(values)> //tricky way to std::array
{
    return {std::forward<Args>(values)...}; //double "{" to ensure initalizer-list.
}

/**
 * @brief operator_with_save
 * @param[in] is : we will extract data from here.
 * @param[out] input : variable which will stock user input if it succeed.
 * @param[out] str : last input.
 *
 * Same as istream& operator>> but the last input will be saved as a string in third parameter.
 *
 */
template <typename CharT, typename Traits, typename Input_Type>
std::basic_istream<CharT, Traits>& operator_with_save(std::basic_istream<CharT, Traits>& is, Input_Type& input, typename output_object<CharT>::type& str)
{
    is >> str;
    std::basic_stringstream<CharT, Traits> stream(str);
    return (stream >> input);
}

/**
 * @brief to_best_string
 * @param[in] value : value to convert into a basic_string.
 * @return an std::string or std::wstring given template parameter is returned.
 *
 * Fusion of std::to_string and std::to_wstring.
 *
 */
template <typename CharT, typename Traits, typename Type>
typename output_object<CharT>::type to_best_string(Type const& value)
{
    std::basic_stringstream<CharT, Traits> stream;
    stream << value;
{'fraction_non_alphanumeric': 0.12163157119717273, 'fraction_numerical': 0.0011780297452510676, 'mean_word_length': 4.539967373572594, 'pattern_counts': {'":': 0, '<': 81, '<?xml version=': 0, '>': 91, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '590420', 'n_tokens_mistral': 2075, 'n_tokens_neox': 2026, 'n_words': 726}