Boost.Nowide
stackstring.hpp
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_DETAILS_WIDESTR_H_INCLUDED
9 #define BOOST_NOWIDE_DETAILS_WIDESTR_H_INCLUDED
10 #include <boost/nowide/convert.hpp>
11 #include <string.h>
12 #include <algorithm>
13 
14 namespace boost {
15 namespace nowide {
16 
24 template<typename CharOut=wchar_t,typename CharIn = char,size_t BufferSize = 256>
26 public:
27 
28  static const size_t buffer_size = BufferSize;
29  typedef CharOut output_char;
30  typedef CharIn input_char;
31 
32  basic_stackstring(basic_stackstring const &other) :
33  mem_buffer_(0)
34  {
35  clear();
36  if(other.mem_buffer_) {
37  size_t len = 0;
38  while(other.mem_buffer_[len])
39  len ++;
40  mem_buffer_ = new output_char[len + 1];
41  memcpy(mem_buffer_,other.mem_buffer_,sizeof(output_char) * (len+1));
42  }
43  else {
44  memcpy(buffer_,other.buffer_,buffer_size * sizeof(output_char));
45  }
46  }
47 
48  void swap(basic_stackstring &other)
49  {
50  std::swap(mem_buffer_,other.mem_buffer_);
51  for(size_t i=0;i<buffer_size;i++)
52  std::swap(buffer_[i],other.buffer_[i]);
53  }
54  basic_stackstring &operator=(basic_stackstring const &other)
55  {
56  if(this != &other) {
57  basic_stackstring tmp(other);
58  swap(tmp);
59  }
60  return *this;
61  }
62 
63  basic_stackstring() : mem_buffer_(0)
64  {
65  }
66  bool convert(input_char const *input)
67  {
68  return convert(input,details::basic_strend(input));
69  }
70  bool convert(input_char const *begin,input_char const *end)
71  {
72  clear();
73 
74  size_t space = get_space(sizeof(input_char),sizeof(output_char),end - begin) + 1;
75  if(space <= buffer_size) {
76  if(basic_convert(buffer_,buffer_size,begin,end))
77  return true;
78  clear();
79  return false;
80  }
81  else {
82  mem_buffer_ = new output_char[space];
83  if(!basic_convert(mem_buffer_,space,begin,end)) {
84  clear();
85  return false;
86  }
87  return true;
88  }
89 
90  }
91  output_char *c_str()
92  {
93  if(mem_buffer_)
94  return mem_buffer_;
95  return buffer_;
96  }
97  output_char const *c_str() const
98  {
99  if(mem_buffer_)
100  return mem_buffer_;
101  return buffer_;
102  }
103  void clear()
104  {
105  if(mem_buffer_) {
106  delete [] mem_buffer_;
107  mem_buffer_=0;
108  }
109  buffer_[0] = 0;
110  }
112  {
113  clear();
114  }
115 private:
116  static size_t get_space(size_t insize,size_t outsize,size_t in)
117  {
118  if(insize <= outsize)
119  return in;
120  else if(insize == 2 && outsize == 1)
121  return 3 * in;
122  else if(insize == 4 && outsize == 1)
123  return 4 * in;
124  else // if(insize == 4 && outsize == 2)
125  return 2 * in;
126  }
127  output_char buffer_[buffer_size];
128  output_char *mem_buffer_;
129 }; //basic_stackstring
130 
147 
148 
149 } // nowide
150 } // namespace boost
151 
152 #endif
153 // vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4
basic_stackstring< wchar_t, char, 256 > wstackstring
Definition: stackstring.hpp:134
Definition: args.hpp:18
basic_stackstring< wchar_t, char, 16 > wshort_stackstring
Definition: stackstring.hpp:142
basic_stackstring< char, wchar_t, 16 > short_stackstring
Definition: stackstring.hpp:146
basic_stackstring< char, wchar_t, 256 > stackstring
Definition: stackstring.hpp:138
A class that allows to create a temporary wide or narrow UTF strings from wide or narrow UTF source...
Definition: stackstring.hpp:25
CharOut * basic_convert(CharOut *buffer, size_t buffer_size, CharIn const *source_begin, CharIn const *source_end)
Template function that converts a buffer of UTF sequences in range [source_begin,source_end) to the o...
Definition: convert.hpp:26