3.cc 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. // { dg-do run { target c++11 } }
  2. // Copyright (C) 2015-2022 Free Software Foundation, Inc.
  3. //
  4. // This file is part of the GNU ISO C++ Library. This library is free
  5. // software; you can redistribute it and/or modify it under the
  6. // terms of the GNU General Public License as published by the
  7. // Free Software Foundation; either version 3, or (at your option)
  8. // any later version.
  9. // This library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. // You should have received a copy of the GNU General Public License along
  14. // with this library; see the file COPYING3. If not see
  15. // <http://www.gnu.org/licenses/>.
  16. // 22.3.3.2.2 String conversions
  17. #include <locale>
  18. #include <string>
  19. #include <testsuite_hooks.h>
  20. template<typename Elem>
  21. struct cvt : std::codecvt<Elem, char, std::mbstate_t> { };
  22. template<typename Elem>
  23. using str_conv = std::wstring_convert<cvt<Elem>, Elem>;
  24. using std::string;
  25. using std::u16string;
  26. using std::u32string;
  27. // test construction with state, for partial conversions
  28. void test01()
  29. {
  30. typedef str_conv<char> wsc;
  31. wsc c;
  32. string input = (const char*)u8"\u00a3 shillings pence";
  33. string woutput = c.from_bytes(input.substr(0, 1));
  34. auto partial_state = c.state();
  35. auto partial_count = c.converted();
  36. auto woutput2 = c.from_bytes("state reset on next conversion");
  37. VERIFY( woutput2 == "state reset on next conversion" );
  38. wsc c2(new cvt<char>, partial_state);
  39. woutput += c2.from_bytes(input.substr(partial_count));
  40. VERIFY( (const char*)u8"\u00a3 shillings pence" == woutput );
  41. string roundtrip = c2.to_bytes(woutput);
  42. VERIFY( input == roundtrip );
  43. }
  44. void test02()
  45. {
  46. typedef str_conv<char16_t> wsc;
  47. wsc c;
  48. string input = (const char*)u8"\u00a3 shillings pence";
  49. u16string woutput = c.from_bytes(input.substr(0, 1));
  50. auto partial_state = c.state();
  51. auto partial_count = c.converted();
  52. auto woutput2 = c.from_bytes("state reset on next conversion");
  53. VERIFY( woutput2 == u"state reset on next conversion" );
  54. wsc c2(new cvt<char16_t>, partial_state);
  55. woutput += c2.from_bytes(input.substr(partial_count));
  56. VERIFY( u"\u00a3 shillings pence" == woutput );
  57. string roundtrip = c2.to_bytes(woutput);
  58. VERIFY( input == roundtrip );
  59. }
  60. void test03()
  61. {
  62. typedef str_conv<char32_t> wsc;
  63. wsc c;
  64. string input = (const char*)u8"\u00a3 shillings pence";
  65. u32string woutput = c.from_bytes(input.substr(0, 1));
  66. auto partial_state = c.state();
  67. auto partial_count = c.converted();
  68. auto woutput2 = c.from_bytes("state reset on next conversion");
  69. VERIFY( woutput2 == U"state reset on next conversion" );
  70. wsc c2(new cvt<char32_t>, partial_state);
  71. woutput += c2.from_bytes(input.substr(partial_count));
  72. VERIFY( U"\u00a3 shillings pence" == woutput );
  73. string roundtrip = c2.to_bytes(woutput);
  74. VERIFY( input == roundtrip );
  75. }
  76. int main()
  77. {
  78. test01();
  79. test02();
  80. test03();
  81. }