Boost.Nowide
convert.hpp
1 //
2 // Copyright (c) 2012 Artyom Beilis (Tonkikh)
3 // Copyright (c) 2020 Alexander Grund
4 //
5 // Distributed under the Boost Software License, Version 1.0.
6 // https://www.boost.org/LICENSE_1_0.txt
7 
8 #ifndef BOOST_NOWIDE_UTF_CONVERT_HPP_INCLUDED
9 #define BOOST_NOWIDE_UTF_CONVERT_HPP_INCLUDED
10 
11 #include <boost/nowide/detail/is_string_container.hpp>
13 #include <boost/nowide/utf/utf.hpp>
14 #include <iterator>
15 #include <string>
16 
17 namespace boost {
18 namespace nowide {
19  namespace utf {
20 
24  template<typename Char>
25  size_t strlen(const Char* s)
26  {
27  const Char* end = s;
28  while(*end)
29  end++;
30  return end - s;
31  }
32 
40  template<typename CharOut, typename CharIn>
41  CharOut*
42  convert_buffer(CharOut* buffer, size_t buffer_size, const CharIn* source_begin, const CharIn* source_end)
43  {
44  CharOut* rv = buffer;
45  if(buffer_size == 0)
46  return nullptr;
47  buffer_size--;
48  while(source_begin != source_end)
49  {
50  code_point c = utf_traits<CharIn>::decode(source_begin, source_end);
51  if(c == illegal || c == incomplete)
52  {
54  }
55  size_t width = utf_traits<CharOut>::width(c);
56  if(buffer_size < width)
57  {
58  rv = nullptr;
59  break;
60  }
61  buffer = utf_traits<CharOut>::encode(c, buffer);
62  buffer_size -= width;
63  }
64  *buffer++ = 0;
65  return rv;
66  }
67 
73  template<typename CharOut, typename CharIn>
74  std::basic_string<CharOut> convert_string(const CharIn* begin, const CharIn* end)
75  {
76  std::basic_string<CharOut> result;
77  result.reserve(end - begin);
78  using inserter_type = std::back_insert_iterator<std::basic_string<CharOut>>;
79  inserter_type inserter(result);
80  code_point c;
81  while(begin != end)
82  {
83  c = utf_traits<CharIn>::decode(begin, end);
84  if(c == illegal || c == incomplete)
85  {
87  }
88  utf_traits<CharOut>::encode(c, inserter);
89  }
90  return result;
91  }
92 
98  template<typename CharOut, typename CharIn>
99  std::basic_string<CharOut> convert_string(const std::basic_string<CharIn>& s)
100  {
101  return convert_string<CharOut>(s.data(), s.data() + s.size());
102  }
103 
104  } // namespace utf
105 } // namespace nowide
106 } // namespace boost
107 
108 #endif
std::basic_string< CharOut > convert_string(const CharIn *begin, const CharIn *end)
Definition: convert.hpp:74
Namespace that holds basic operations on UTF encoded sequences.
Definition: convert.hpp:19
static Iterator encode(code_point value, Iterator out)
static const code_point illegal
Special constant that defines illegal code point.
Definition: utf.hpp:32
uint32_t code_point
The integral type that can hold a Unicode code point.
Definition: utf.hpp:27
size_t strlen(const Char *s)
Definition: convert.hpp:25
#define BOOST_NOWIDE_REPLACEMENT_CHARACTER
Definition: replacement.hpp:15
CharOut * convert_buffer(CharOut *buffer, size_t buffer_size, const CharIn *source_begin, const CharIn *source_end)
Definition: convert.hpp:42
static const code_point incomplete
Special constant that defines incomplete code point.
Definition: utf.hpp:37
static int width(code_point value)
static code_point decode(Iterator &p, Iterator e)