This is the mail archive of the libstdc++@gcc.gnu.org mailing list for the libstdc++ project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: [RFCv2 2/2] libstdc++: Add codecvt<char32_t, char, mbstate_t>.


On 26/04/14 14:46 +0200, Rüdiger Sonderfeld wrote:
UTF-32/UTF-8 codec converter (codecvt) specialization.

C++11: [locale.codecvt]

I didn't use Rüdiger's code that he sent last year, but I'm adding a
slightly modified version of his new test to the testsuite (thanks!)

Tested x86_64-linux, committed to trunk.

commit 45d138343b2121e6075e0b51bc6f02aa5b235220
Author: Jonathan Wakely <jwakely@redhat.com>
Date:   Tue Feb 17 14:04:36 2015 +0000

    2015-02-17  RÃdiger Sonderfeld  <ruediger@c-plusplus.de>
    	    Jonathan Wakely  <jwakely@redhat.com>
    
    	* testsuite/22_locale/codecvt/char32_t.cc: New.

diff --git a/libstdc++-v3/testsuite/22_locale/codecvt/char32_t.cc b/libstdc++-v3/testsuite/22_locale/codecvt/char32_t.cc
new file mode 100644
index 0000000..07f72c4
--- /dev/null
+++ b/libstdc++-v3/testsuite/22_locale/codecvt/char32_t.cc
@@ -0,0 +1,98 @@
+// { dg-options "-std=gnu++11" }
+
+// 2014-04-24 RÃdiger Sonderfeld
+
+// Copyright (C) 2015 Free Software Foundation, Inc.
+//
+// This file is part of the GNU ISO C++ Library.  This library is free
+// software; you can redistribute it and/or modify it under the
+// terms of the GNU General Public License as published by the
+// Free Software Foundation; either version 3, or (at your option)
+// any later version.
+
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+
+// You should have received a copy of the GNU General Public License along
+// with this library; see the file COPYING3.  If not see
+// <http://www.gnu.org/licenses/>.
+
+// [locale.codecvt], C++11 22.4.1.4.  specialization.
+
+#include <locale>
+#include <cstring>
+#include <testsuite_hooks.h>
+
+void
+test01()
+{
+  using namespace std;
+  typedef codecvt<char32_t, char, mbstate_t> codecvt_c32;
+  locale loc_c = locale::classic();
+  VERIFY(has_facet<codecvt_c32>(loc_c));
+  const codecvt_c32* const cvt = &use_facet<codecvt_c32>(loc_c);
+
+  VERIFY(!cvt->always_noconv());
+  VERIFY(cvt->max_length() == 4);
+  VERIFY(cvt->encoding() == 0);
+
+  const char u8dat[] = u8"H\U000000E4ll\U000000F6 \U0001F63F \U000056FD "
+    u8"\U0000222B f(\U000003BA) exp(-2\U000003C0\U000003C9) d\U000003BA "
+    u8"\U0001F6BF \U0001F6BF \U0001F648 \U00000413\U00000435\U0000043E"
+    u8"\U00000433\U00000440\U00000430\U00000444\U00000438\U0000044F \U0000FB05";
+  const char* const u8dat_end = std::end(u8dat);
+
+  const char32_t u32dat[] = U"H\U000000E4ll\U000000F6 \U0001F63F \U000056FD "
+    U"\U0000222B f(\U000003BA) exp(-2\U000003C0\U000003C9) d\U000003BA "
+    U"\U0001F6BF \U0001F6BF \U0001F648 \U00000413\U00000435\U0000043E"
+    U"\U00000433\U00000440\U00000430\U00000444\U00000438\U0000044F \U0000FB05";
+  const char32_t* const u32dat_end = std::end(u32dat);
+
+  {
+    const size_t len = u32dat_end - u32dat + 1;
+    char32_t* const buffer = new char32_t[len];
+    char32_t* const buffer_end = buffer + len;
+
+    const char* from_next;
+    char32_t* to_next;
+
+    codecvt_c32::state_type state01;
+    state01 = {};
+    codecvt_base::result res = cvt->in(state01, u8dat, u8dat_end, from_next,
+                                       buffer, buffer_end, to_next);
+
+    VERIFY(res == codecvt_base::ok);
+    VERIFY(from_next == u8dat_end);
+    VERIFY(std::memcmp((void*)buffer, (void*)u32dat, sizeof(u32dat)) == 0);
+
+    delete[] buffer;
+  }
+
+  {
+    const size_t len = u8dat_end - u8dat + 1;
+    char* const buffer = new char[len];
+    char* const buffer_end = buffer + len;
+
+    const char32_t* from_next;
+    char* to_next;
+
+    codecvt_c32::state_type state01;
+    state01 = {};
+    codecvt_base::result res = cvt->out(state01, u32dat, u32dat_end, from_next,
+                                        buffer, buffer_end, to_next);
+
+    VERIFY(res == codecvt_base::ok);
+    VERIFY(from_next == u32dat_end);
+    VERIFY(std::memcmp((void*)buffer, (void*)u8dat, sizeof(u8dat)) == 0);
+
+    delete[] buffer;
+  }
+}
+
+int
+main()
+{
+  test01();
+}

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]