Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:39:20

0001 /*
0002  *          Copyright Andrey Semashev 2007 - 2015.
0003  * Distributed under the Boost Software License, Version 1.0.
0004  *    (See accompanying file LICENSE_1_0.txt or copy at
0005  *          http://www.boost.org/LICENSE_1_0.txt)
0006  */
0007 /*!
0008  * \file   code_conversion.hpp
0009  * \author Andrey Semashev
0010  * \date   08.11.2008
0011  *
0012  * \brief  This header is the Boost.Log library implementation, see the library documentation
0013  *         at http://www.boost.org/doc/libs/release/libs/log/doc/html/index.html.
0014  */
0015 
0016 #ifndef BOOST_LOG_DETAIL_CODE_CONVERSION_HPP_INCLUDED_
0017 #define BOOST_LOG_DETAIL_CODE_CONVERSION_HPP_INCLUDED_
0018 
0019 #include <cstddef>
0020 #include <locale>
0021 #include <string>
0022 #include <boost/core/enable_if.hpp>
0023 #include <boost/log/detail/config.hpp>
0024 #include <boost/log/detail/is_character_type.hpp>
0025 #include <boost/log/detail/header.hpp>
0026 
0027 #ifdef BOOST_HAS_PRAGMA_ONCE
0028 #pragma once
0029 #endif
0030 
0031 namespace boost {
0032 
0033 BOOST_LOG_OPEN_NAMESPACE
0034 
0035 namespace aux {
0036 
0037 // Implementation note: We have to implement char<->wchar_t conversions even in the absence of the native wchar_t
0038 // type. These conversions are used in sinks, e.g. to convert multibyte strings to wide-character filesystem paths.
0039 
0040 //! The function converts one string to the character type of another
0041 BOOST_LOG_API bool code_convert_impl(const wchar_t* str1, std::size_t len, std::string& str2, std::size_t max_size, std::locale const& loc = std::locale());
0042 //! The function converts one string to the character type of another
0043 BOOST_LOG_API bool code_convert_impl(const char* str1, std::size_t len, std::wstring& str2, std::size_t max_size, std::locale const& loc = std::locale());
0044 
0045 #if !defined(BOOST_LOG_NO_CXX11_CODECVT_FACETS)
0046 #if !defined(BOOST_NO_CXX11_CHAR16_T)
0047 //! The function converts one string to the character type of another
0048 BOOST_LOG_API bool code_convert_impl(const char16_t* str1, std::size_t len, std::string& str2, std::size_t max_size, std::locale const& loc = std::locale());
0049 //! The function converts one string to the character type of another
0050 BOOST_LOG_API bool code_convert_impl(const char* str1, std::size_t len, std::u16string& str2, std::size_t max_size, std::locale const& loc = std::locale());
0051 //! The function converts one string to the character type of another
0052 BOOST_LOG_API bool code_convert_impl(const char16_t* str1, std::size_t len, std::wstring& str2, std::size_t max_size, std::locale const& loc = std::locale());
0053 #endif
0054 #if !defined(BOOST_NO_CXX11_CHAR32_T)
0055 //! The function converts one string to the character type of another
0056 BOOST_LOG_API bool code_convert_impl(const char32_t* str1, std::size_t len, std::string& str2, std::size_t max_size, std::locale const& loc = std::locale());
0057 //! The function converts one string to the character type of another
0058 BOOST_LOG_API bool code_convert_impl(const char* str1, std::size_t len, std::u32string& str2, std::size_t max_size, std::locale const& loc = std::locale());
0059 //! The function converts one string to the character type of another
0060 BOOST_LOG_API bool code_convert_impl(const char32_t* str1, std::size_t len, std::wstring& str2, std::size_t max_size, std::locale const& loc = std::locale());
0061 #endif
0062 #if !defined(BOOST_NO_CXX11_CHAR16_T) && !defined(BOOST_NO_CXX11_CHAR32_T)
0063 //! The function converts one string to the character type of another
0064 BOOST_LOG_API bool code_convert_impl(const char16_t* str1, std::size_t len, std::u32string& str2, std::size_t max_size, std::locale const& loc = std::locale());
0065 //! The function converts one string to the character type of another
0066 BOOST_LOG_API bool code_convert_impl(const char32_t* str1, std::size_t len, std::u16string& str2, std::size_t max_size, std::locale const& loc = std::locale());
0067 #endif
0068 #endif // !defined(BOOST_LOG_NO_CXX11_CODECVT_FACETS)
0069 
0070 //! The function converts one string to the character type of another
0071 template< typename SourceCharT, typename TargetCharT, typename TargetTraitsT, typename TargetAllocatorT >
0072 inline typename boost::enable_if_c< is_character_type< SourceCharT >::value && is_character_type< TargetCharT >::value && sizeof(SourceCharT) == sizeof(TargetCharT), bool >::type
0073 code_convert(const SourceCharT* str1, std::size_t len, std::basic_string< TargetCharT, TargetTraitsT, TargetAllocatorT >& str2, std::size_t max_size, std::locale const& = std::locale())
0074 {
0075     std::size_t size_left = str2.size() < max_size ? max_size - str2.size() : static_cast< std::size_t >(0u);
0076     const bool overflow = len > size_left;
0077     str2.append(reinterpret_cast< const TargetCharT* >(str1), overflow ? size_left : len);
0078     return !overflow;
0079 }
0080 
0081 //! The function converts one string to the character type of another
0082 template< typename SourceCharT, typename SourceTraitsT, typename SourceAllocatorT, typename TargetCharT, typename TargetTraitsT, typename TargetAllocatorT >
0083 inline typename boost::enable_if_c< is_character_type< SourceCharT >::value && is_character_type< TargetCharT >::value && sizeof(SourceCharT) == sizeof(TargetCharT), bool >::type
0084 code_convert(std::basic_string< SourceCharT, SourceTraitsT, SourceAllocatorT > const& str1, std::basic_string< TargetCharT, TargetTraitsT, TargetAllocatorT >& str2, std::size_t max_size, std::locale const& loc = std::locale())
0085 {
0086     return aux::code_convert(str1.data(), str1.size(), str2, max_size, loc);
0087 }
0088 
0089 //! The function converts one string to the character type of another
0090 template< typename SourceCharT, typename SourceTraitsT, typename SourceAllocatorT, typename TargetCharT, typename TargetTraitsT, typename TargetAllocatorT >
0091 inline typename boost::enable_if_c< is_character_type< SourceCharT >::value && is_character_type< TargetCharT >::value && sizeof(SourceCharT) == sizeof(TargetCharT), bool >::type
0092 code_convert(std::basic_string< SourceCharT, SourceTraitsT, SourceAllocatorT > const& str1, std::basic_string< TargetCharT, TargetTraitsT, TargetAllocatorT >& str2, std::locale const& loc = std::locale())
0093 {
0094     return aux::code_convert(str1.data(), str1.size(), str2, str2.max_size(), loc);
0095 }
0096 //! The function converts one string to the character type of another
0097 template< typename SourceCharT, typename TargetCharT, typename TargetTraitsT, typename TargetAllocatorT >
0098 inline typename boost::enable_if_c< is_character_type< SourceCharT >::value && is_character_type< TargetCharT >::value && sizeof(SourceCharT) == sizeof(TargetCharT), bool >::type
0099 code_convert(const SourceCharT* str1, std::size_t len, std::basic_string< TargetCharT, TargetTraitsT, TargetAllocatorT >& str2, std::locale const& loc = std::locale())
0100 {
0101     return aux::code_convert(str1, len, str2, str2.max_size(), loc);
0102 }
0103 
0104 //! The function converts one string to the character type of another
0105 template< typename SourceCharT, typename SourceTraitsT, typename SourceAllocatorT, typename TargetCharT, typename TargetTraitsT, typename TargetAllocatorT >
0106 inline typename boost::enable_if_c< is_character_type< SourceCharT >::value && is_character_type< TargetCharT >::value && sizeof(SourceCharT) != sizeof(TargetCharT), bool >::type
0107 code_convert(std::basic_string< SourceCharT, SourceTraitsT, SourceAllocatorT > const& str1, std::basic_string< TargetCharT, TargetTraitsT, TargetAllocatorT >& str2, std::locale const& loc = std::locale())
0108 {
0109     return aux::code_convert_impl(str1.c_str(), str1.size(), str2, str2.max_size(), loc);
0110 }
0111 
0112 //! The function converts one string to the character type of another
0113 template< typename SourceCharT, typename TargetCharT, typename TargetTraitsT, typename TargetAllocatorT >
0114 inline typename boost::enable_if_c< is_character_type< SourceCharT >::value && is_character_type< TargetCharT >::value && sizeof(SourceCharT) != sizeof(TargetCharT), bool >::type
0115 code_convert(const SourceCharT* str1, std::size_t len, std::basic_string< TargetCharT, TargetTraitsT, TargetAllocatorT >& str2, std::locale const& loc = std::locale())
0116 {
0117     return aux::code_convert_impl(str1, len, str2, str2.max_size(), loc);
0118 }
0119 
0120 //! The function converts one string to the character type of another
0121 template< typename SourceCharT, typename SourceTraitsT, typename SourceAllocatorT, typename TargetCharT, typename TargetTraitsT, typename TargetAllocatorT >
0122 inline typename boost::enable_if_c< is_character_type< SourceCharT >::value && is_character_type< TargetCharT >::value && sizeof(SourceCharT) != sizeof(TargetCharT), bool >::type
0123 code_convert(std::basic_string< SourceCharT, SourceTraitsT, SourceAllocatorT > const& str1, std::basic_string< TargetCharT, TargetTraitsT, TargetAllocatorT >& str2, std::size_t max_size, std::locale const& loc = std::locale())
0124 {
0125     return aux::code_convert_impl(str1.c_str(), str1.size(), str2, max_size, loc);
0126 }
0127 
0128 //! The function converts one string to the character type of another
0129 template< typename SourceCharT, typename TargetCharT, typename TargetTraitsT, typename TargetAllocatorT >
0130 inline typename boost::enable_if_c< is_character_type< SourceCharT >::value && is_character_type< TargetCharT >::value && sizeof(SourceCharT) != sizeof(TargetCharT), bool >::type
0131 code_convert(const SourceCharT* str1, std::size_t len, std::basic_string< TargetCharT, TargetTraitsT, TargetAllocatorT >& str2, std::size_t max_size, std::locale const& loc = std::locale())
0132 {
0133     return aux::code_convert_impl(str1, len, str2, max_size, loc);
0134 }
0135 
0136 //! The function converts the passed string to the narrow-character encoding
0137 inline std::string const& to_narrow(std::string const& str)
0138 {
0139     return str;
0140 }
0141 
0142 //! The function converts the passed string to the narrow-character encoding
0143 inline std::string const& to_narrow(std::string const& str, std::locale const&)
0144 {
0145     return str;
0146 }
0147 
0148 //! The function converts the passed string to the narrow-character encoding
0149 inline std::string to_narrow(std::wstring const& str, std::locale const& loc = std::locale())
0150 {
0151     std::string res;
0152     aux::code_convert_impl(str.c_str(), str.size(), res, res.max_size(), loc);
0153 #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES) && !defined(BOOST_HAS_NRVO)
0154     return static_cast< std::string&& >(res);
0155 #else
0156     return res;
0157 #endif
0158 }
0159 
0160 //! The function converts the passed string to the wide-character encoding
0161 inline std::wstring const& to_wide(std::wstring const& str)
0162 {
0163     return str;
0164 }
0165 
0166 //! The function converts the passed string to the wide-character encoding
0167 inline std::wstring const& to_wide(std::wstring const& str, std::locale const&)
0168 {
0169     return str;
0170 }
0171 
0172 //! The function converts the passed string to the wide-character encoding
0173 inline std::wstring to_wide(std::string const& str, std::locale const& loc = std::locale())
0174 {
0175     std::wstring res;
0176     aux::code_convert_impl(str.c_str(), str.size(), res, res.max_size(), loc);
0177 #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES) && !defined(BOOST_HAS_NRVO)
0178     return static_cast< std::wstring&& >(res);
0179 #else
0180     return res;
0181 #endif
0182 }
0183 
0184 } // namespace aux
0185 
0186 BOOST_LOG_CLOSE_NAMESPACE // namespace log
0187 
0188 } // namespace boost
0189 
0190 #include <boost/log/detail/footer.hpp>
0191 
0192 #endif // BOOST_LOG_DETAIL_CODE_CONVERSION_HPP_INCLUDED_