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. (See
6 // accompanying file LICENSE or copy at
7 // http://www.boost.org/LICENSE_1_0.txt)
8 //
9 #ifndef BOOST_NOWIDE_UTF_CONVERT_HPP_INCLUDED
10 #define BOOST_NOWIDE_UTF_CONVERT_HPP_INCLUDED
11 
12 #include <boost/nowide/detail/is_string_container.hpp>
14 #include <boost/nowide/utf/utf.hpp>
15 #include <iterator>
16 #include <string>
17 
18 namespace boost {
19 namespace nowide {
20  namespace utf {
21 
25  template<typename Char>
26  size_t strlen(const Char* s)
27  {
28  const Char* end = s;
29  while(*end)
30  end++;
31  return end - s;
32  }
33 
41  template<typename CharOut, typename CharIn>
42  CharOut*
43  convert_buffer(CharOut* buffer, size_t buffer_size, const CharIn* source_begin, const CharIn* source_end)
44  {
45  CharOut* rv = buffer;
46  if(buffer_size == 0)
47  return nullptr;
48  buffer_size--;
49  while(source_begin != source_end)
50  {
51  code_point c = utf_traits<CharIn>::decode(source_begin, source_end);
52  if(c == illegal || c == incomplete)
53  {
55  }
56  size_t width = utf_traits<CharOut>::width(c);
57  if(buffer_size < width)
58  {
59  rv = NULL;
60  break;
61  }
62  buffer = utf_traits<CharOut>::encode(c, buffer);
63  buffer_size -= width;
64  }
65  *buffer++ = 0;
66  return rv;
67  }
68 
74  template<typename CharOut, typename CharIn>
75  std::basic_string<CharOut> convert_string(const CharIn* begin, const CharIn* end)
76  {
77  std::basic_string<CharOut> result;
78  result.reserve(end - begin);
79  using inserter_type = std::back_insert_iterator<std::basic_string<CharOut>>;
80  inserter_type inserter(result);
81  code_point c;
82  while(begin != end)
83  {
84  c = utf_traits<CharIn>::decode(begin, end);
85  if(c == illegal || c == incomplete)
86  {
88  }
89  utf_traits<CharOut>::encode(c, inserter);
90  }
91  return result;
92  }
93 
94  } // namespace utf
95 } // namespace nowide
96 } // namespace boost
97 
98 #endif
std::basic_string< CharOut > convert_string(const CharIn *begin, const CharIn *end)
Definition: convert.hpp:75
Namespace that holds basic operations on UTF encoded sequences.
Definition: convert.hpp:20
static Iterator encode(code_point value, Iterator out)
static const code_point illegal
Special constant that defines illegal code point.
Definition: utf.hpp:33
uint32_t code_point
The integral type that can hold a Unicode code point.
Definition: utf.hpp:28
size_t strlen(const Char *s)
Definition: convert.hpp:26
#define BOOST_NOWIDE_REPLACEMENT_CHARACTER
Definition: replacement.hpp:16
CharOut * convert_buffer(CharOut *buffer, size_t buffer_size, const CharIn *source_begin, const CharIn *source_end)
Definition: convert.hpp:43
static const code_point incomplete
Special constant that defines incomplete code point.
Definition: utf.hpp:38
static int width(code_point value)
static code_point decode(Iterator &p, Iterator e)