Home | History | Annotate | Download | only in locale.codecvt.members
      1 //===----------------------------------------------------------------------===//
      2 //
      3 //                     The LLVM Compiler Infrastructure
      4 //
      5 // This file is dual licensed under the MIT and the University of Illinois Open
      6 // Source Licenses. See LICENSE.TXT for details.
      7 //
      8 //===----------------------------------------------------------------------===//
      9 
     10 // <locale>
     11 
     12 // template <> class codecvt<wchar_t, char, mbstate_t>
     13 
     14 // result out(stateT& state,
     15 //            const internT* from, const internT* from_end, const internT*& from_next,
     16 //            externT* to, externT* to_end, externT*& to_next) const;
     17 
     18 #include <locale>
     19 #include <string>
     20 #include <vector>
     21 #include <cassert>
     22 
     23 typedef std::codecvt<wchar_t, char, std::mbstate_t> F;
     24 
     25 int main()
     26 {
     27     std::locale l = std::locale::classic();
     28     const F& f = std::use_facet<F>(l);
     29     {
     30         const std::basic_string<F::intern_type> from(L"some text");
     31         std::vector<char> to(from.size()+1);
     32         std::mbstate_t mbs = {0};
     33         const F::intern_type* from_next = 0;
     34         char* to_next = 0;
     35         F::result r = f.out(mbs, from.data(), from.data() + from.size(), from_next,
     36                                  to.data(), to.data() + to.size(), to_next);
     37         assert(r == F::ok);
     38         assert(from_next - from.data() == from.size());
     39         assert(to_next - to.data() == from.size());
     40         assert(to.data() == std::string("some text"));
     41     }
     42     {
     43         std::basic_string<F::intern_type> from(L"some text");
     44         from[4] = '\0';
     45         std::vector<char> to(from.size()+1);
     46         std::mbstate_t mbs = {0};
     47         const F::intern_type* from_next = 0;
     48         char* to_next = 0;
     49         F::result r = f.out(mbs, from.data(), from.data() + from.size(), from_next,
     50                                  to.data(), to.data() + to.size(), to_next);
     51         assert(r == F::ok);
     52         assert(from_next - from.data() == from.size());
     53         assert(to_next - to.data() == from.size());
     54         assert(memcmp(to.data(), "some\0text", from.size()) == 0);
     55     }
     56     {
     57         std::basic_string<F::intern_type> from(L"some text");
     58         std::vector<char> to(from.size()-1);
     59         std::mbstate_t mbs = {0};
     60         const F::intern_type* from_next = 0;
     61         char* to_next = 0;
     62         F::result r = f.out(mbs, from.data(), from.data() + from.size(), from_next,
     63                                  to.data(), to.data() + to.size()-1, to_next);
     64         assert(r == F::partial);
     65         assert(from_next - from.data() == to.size()-1);
     66         assert(to_next - to.data() == to.size()-1);
     67         assert(to.data() == std::string("some te"));
     68     }
     69 }
     70