convert.hpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. //
  2. // Copyright (c) 2012 Artyom Beilis (Tonkikh)
  3. //
  4. // Distributed under the Boost Software License, Version 1.0. (See
  5. // accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt)
  7. //
  8. #ifndef BOOST_NOWIDE_DETAIL_CONVERT_HPP_INCLUDED
  9. #define BOOST_NOWIDE_DETAIL_CONVERT_HPP_INCLUDED
  10. #include <boost/nowide/detail/utf.hpp>
  11. #include <boost/nowide/replacement.hpp>
  12. #include <iterator>
  13. #include <string>
  14. namespace boost {
  15. namespace nowide {
  16. /// \cond INTERNAL
  17. namespace detail {
  18. ///
  19. /// Convert a buffer of UTF sequences in the range [source_begin, source_end)
  20. /// from \tparam CharIn to \tparam CharOut to the output \a buffer of size \a buffer_size.
  21. ///
  22. /// \return original buffer containing the NULL terminated string or NULL
  23. ///
  24. /// If there is not enough room in the buffer NULL is returned, and the content of the buffer is undefined.
  25. /// Any illegal sequences are replaced with the replacement character, see #BOOST_NOWIDE_REPLACEMENT_CHARACTER
  26. ///
  27. template<typename CharOut, typename CharIn>
  28. CharOut*
  29. convert_buffer(CharOut* buffer, size_t buffer_size, const CharIn* source_begin, const CharIn* source_end)
  30. {
  31. CharOut* rv = buffer;
  32. if(buffer_size == 0)
  33. return 0;
  34. buffer_size--;
  35. while(source_begin != source_end)
  36. {
  37. using namespace detail::utf;
  38. code_point c = utf_traits<CharIn>::template decode(source_begin, source_end);
  39. if(c == illegal || c == incomplete)
  40. {
  41. c = BOOST_NOWIDE_REPLACEMENT_CHARACTER;
  42. }
  43. size_t width = utf_traits<CharOut>::width(c);
  44. if(buffer_size < width)
  45. {
  46. rv = NULL;
  47. break;
  48. }
  49. buffer = utf_traits<CharOut>::template encode(c, buffer);
  50. buffer_size -= width;
  51. }
  52. *buffer++ = 0;
  53. return rv;
  54. }
  55. ///
  56. /// Convert the UTF sequences in range [begin, end) from \tparam CharIn to \tparam CharOut
  57. /// and return it as a string
  58. ///
  59. /// Any illegal sequences are replaced with the replacement character, see #BOOST_NOWIDE_REPLACEMENT_CHARACTER
  60. ///
  61. template<typename CharOut, typename CharIn>
  62. std::basic_string<CharOut> convert_string(const CharIn* begin, const CharIn* end)
  63. {
  64. std::basic_string<CharOut> result;
  65. result.reserve(end - begin);
  66. typedef std::back_insert_iterator<std::basic_string<CharOut> > inserter_type;
  67. inserter_type inserter(result);
  68. using namespace detail::utf;
  69. code_point c;
  70. while(begin != end)
  71. {
  72. c = utf_traits<CharIn>::template decode(begin, end);
  73. if(c == illegal || c == incomplete)
  74. {
  75. c = BOOST_NOWIDE_REPLACEMENT_CHARACTER;
  76. }
  77. utf_traits<CharOut>::template encode(c, inserter);
  78. }
  79. return result;
  80. }
  81. /// Return the length of the given string.
  82. /// That is the number of characters until the first NULL character
  83. /// Equivalent to `std::strlen(s)` but can handle wide-strings
  84. template<typename Char>
  85. size_t strlen(const Char* s)
  86. {
  87. const Char* end = s;
  88. while(*end)
  89. end++;
  90. return end - s;
  91. }
  92. } // namespace detail
  93. /// \endcond
  94. } // namespace nowide
  95. } // namespace boost
  96. #endif