[conversions.string]

git-svn-id: https://llvm.org/svn/llvm-project/libcxx/trunk@105254 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Howard Hinnant
2010-05-31 20:58:54 +00:00
parent 87d1a8a4d8
commit d23b464e21
10 changed files with 627 additions and 5 deletions

View File

@@ -0,0 +1,32 @@
//===----------------------------------------------------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
// <locale>
// wstring_convert<Codecvt, Elem, Wide_alloc, Byte_alloc>
// size_t converted() const;
#include <locale>
#include <codecvt>
#include <cassert>
int main()
{
typedef std::codecvt_utf8<wchar_t> Codecvt;
typedef std::wstring_convert<Codecvt> Myconv;
Myconv myconv;
assert(myconv.converted() == 0);
std::string bs = myconv.to_bytes(L"\x40003");
assert(myconv.converted() == 1);
bs = myconv.to_bytes(L"\x40003\x65");
assert(myconv.converted() == 2);
std::wstring ws = myconv.from_bytes("\xF1\x80\x80\x83");
assert(myconv.converted() == 4);
}

View File

@@ -0,0 +1,34 @@
//===----------------------------------------------------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
// <locale>
// wstring_convert<Codecvt, Elem, Wide_alloc, Byte_alloc>
// wstring_convert(Codecvt* pcvt = new Codecvt);
#include <locale>
#include <codecvt>
#include <cassert>
int main()
{
{
typedef std::codecvt_utf8<wchar_t> Codecvt;
typedef std::wstring_convert<Codecvt> Myconv;
Myconv myconv;
assert(myconv.converted() == 0);
}
{
typedef std::codecvt_utf8<wchar_t> Codecvt;
typedef std::wstring_convert<Codecvt> Myconv;
Myconv myconv(new Codecvt);
assert(myconv.converted() == 0);
}
}

View File

@@ -0,0 +1,28 @@
//===----------------------------------------------------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
// <locale>
// wstring_convert<Codecvt, Elem, Wide_alloc, Byte_alloc>
// wstring_convert(Codecvt* pcvt, state_type state);
#include <locale>
#include <codecvt>
#include <cassert>
int main()
{
{
typedef std::codecvt_utf8<wchar_t> Codecvt;
typedef std::wstring_convert<Codecvt> Myconv;
Myconv myconv(new Codecvt, std::mbstate_t());
assert(myconv.converted() == 0);
}
}

View File

@@ -0,0 +1,64 @@
//===----------------------------------------------------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
// <locale>
// wstring_convert<Codecvt, Elem, Wide_alloc, Byte_alloc>
// wstring_convert(const byte_string& byte_err,
// const wide_string& wide_err = wide_string());
#include <locale>
#include <codecvt>
#include <cassert>
int main()
{
typedef std::codecvt_utf8<wchar_t> Codecvt;
typedef std::wstring_convert<Codecvt> Myconv;
{
Myconv myconv;
try
{
myconv.to_bytes(L"\xDA83");
assert(false);
}
catch (const std::range_error&)
{
}
try
{
myconv.from_bytes('\xA5');
assert(false);
}
catch (const std::range_error&)
{
}
}
{
Myconv myconv("byte error");
std::string bs = myconv.to_bytes(L"\xDA83");
assert(bs == "byte error");
try
{
myconv.from_bytes('\xA5');
assert(false);
}
catch (const std::range_error&)
{
}
}
{
Myconv myconv("byte error", L"wide error");
std::string bs = myconv.to_bytes(L"\xDA83");
assert(bs == "byte error");
std::wstring ws = myconv.from_bytes('\xA5');
assert(ws == L"wide error");
}
}

View File

@@ -0,0 +1,37 @@
//===----------------------------------------------------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
// <locale>
// wstring_convert<Codecvt, Elem, Wide_alloc, Byte_alloc>
// wide_string from_bytes(char byte);
// wide_string from_bytes(const char* ptr);
// wide_string from_bytes(const byte_string& str);
// wide_string from_bytes(const char* first, const char* last);
#include <locale>
#include <codecvt>
#include <cassert>
int main()
{
{
std::wstring_convert<std::codecvt_utf8<wchar_t> > myconv;
std::string bs("\xF1\x80\x80\x83");
std::wstring ws = myconv.from_bytes('a');
assert(ws == L"a");
ws = myconv.from_bytes(bs.c_str());
assert(ws == L"\x40003");
ws = myconv.from_bytes(bs);
assert(ws == L"\x40003");
ws = myconv.from_bytes(bs.data(), bs.data() + bs.size());
assert(ws == L"\x40003");
}
}

View File

@@ -0,0 +1,25 @@
//===----------------------------------------------------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
// <locale>
// wstring_convert<Codecvt, Elem, Wide_alloc, Byte_alloc>
// state_type state() const;
#include <locale>
#include <codecvt>
int main()
{
typedef std::codecvt_utf8<wchar_t> Codecvt;
typedef std::wstring_convert<Codecvt> Myconv;
Myconv myconv;
std::mbstate_t s = myconv.state();
}

View File

@@ -0,0 +1,37 @@
//===----------------------------------------------------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
// <locale>
// wstring_convert<Codecvt, Elem, Wide_alloc, Byte_alloc>
// byte_string to_bytes(Elem wchar);
// byte_string to_bytes(const Elem* wptr);
// byte_string to_bytes(const wide_string& wstr);
// byte_string to_bytes(const Elem* first, const Elem* last);
#include <locale>
#include <codecvt>
#include <cassert>
int main()
{
{
std::wstring_convert<std::codecvt_utf8<wchar_t> > myconv;
std::wstring ws(1, L'\x40003');
std::string bs = myconv.to_bytes(ws[0]);
assert(bs == "\xF1\x80\x80\x83");
bs = myconv.to_bytes(ws.c_str());
assert(bs == "\xF1\x80\x80\x83");
bs = myconv.to_bytes(ws);
assert(bs == "\xF1\x80\x80\x83");
bs = myconv.to_bytes(ws.data(), ws.data() + ws.size());
assert(bs == "\xF1\x80\x80\x83");
}
}

View File

@@ -0,0 +1,35 @@
//===----------------------------------------------------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
// <locale>
// template<class Codecvt, class Elem = wchar_t,
// class Wide_alloc = allocator<Elem>,
// class Byte_alloc = allocator<char>>
// class wstring_convert
// {
// public:
// typedef basic_string<char, char_traits<char>, Byte_alloc> byte_string;
// typedef basic_string<Elem, char_traits<Elem>, Wide_alloc> wide_string;
// typedef typename Codecvt::state_type state_type;
// typedef typename wide_string::traits_type::int_type int_type;
#include <locale>
#include <codecvt>
int main()
{
{
typedef std::wstring_convert<std::codecvt_utf8<wchar_t> > myconv;
static_assert((std::is_same<myconv::byte_string, std::string>::value), "");
static_assert((std::is_same<myconv::wide_string, std::wstring>::value), "");
static_assert((std::is_same<myconv::state_type, std::mbstate_t>::value), "");
static_assert((std::is_same<myconv::int_type, std::char_traits<wchar_t>::int_type>::value), "");
}
}