extern_type*& __to_next) const
{
result __ret = ok;
- // A temporary state must be used since the result of the last
- // conversion may be thrown away.
- state_type __tmp_state(__state);
+ state_type __tmp_state(__state);
#if __GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ > 2)
__c_locale __old = __uselocale(_M_c_locale_codecvt);
#endif
- // The conversion must be done by calling wcrtomb in a loop rather
- // than using wcsrtombs because wcsrtombs assumes that the input is
- // zero-terminated.
-
- // Either we can upper bound the total number of external characters to
- // something smaller than __to_end - __to or the conversion must be done
- // using a temporary destination buffer since it is not possible to
- // pass the size of the buffer to wcrtomb
- if (MB_CUR_MAX * (__from_end - __from) - (__to_end - __to) <= 0)
- while (__from < __from_end)
- {
- const size_t __conv = wcrtomb(__to, *__from, &__tmp_state);
- if (__conv == static_cast<size_t>(-1))
- {
- __ret = error;
- break;
- }
- __state = __tmp_state;
- __to += __conv;
- __from++;
- }
- else
+ // wcsnrtombs is *very* fast but stops if encounters NUL characters:
+ // in case we fall back to wcrtomb and then continue, in a loop.
+ // NB: wcsnrtombs is a GNU extension
+ __from_next = __from;
+ __to_next = __to;
+ while (__from_next < __from_end && __to_next < __to_end
+ && __ret == ok)
{
- extern_type __buf[MB_LEN_MAX];
- while (__from < __from_end && __to < __to_end)
+ const intern_type* __from_chunk_end = wmemchr(__from_next, L'\0',
+ __from_end - __from_next);
+ if (!__from_chunk_end)
+ __from_chunk_end = __from_end;
+
+ const intern_type* __tmp_from = __from_next;
+ const size_t __conv = wcsnrtombs(__to_next, &__from_next,
+ __from_chunk_end - __from_next,
+ __to_end - __to_next, &__state);
+ if (__conv == static_cast<size_t>(-1))
+ {
+ // In case of error, in order to stop at the exact place we
+ // have to start again from the beginning with a series of
+ // wcrtomb.
+ while (__tmp_from < __from_next)
+ __to_next += wcrtomb(__to_next, *__tmp_from++, &__tmp_state);
+ __state = __tmp_state;
+ __ret = error;
+ }
+ else if (__from_next && __from_next < __from_chunk_end)
+ {
+ __to_next += __conv;
+ __ret = partial;
+ }
+ else
{
- const size_t __conv = wcrtomb(__buf, *__from, &__tmp_state);
+ __from_next = __from_chunk_end;
+ __to_next += __conv;
+ }
+
+ if (__from_next < __from_end && __ret == ok)
+ {
+ extern_type __buf[MB_LEN_MAX];
+ __tmp_state = __state;
+ const size_t __conv = wcrtomb(__buf, *__from_next, &__tmp_state);
if (__conv == static_cast<size_t>(-1))
+ __ret = error;
+ else if (__conv > static_cast<size_t>(__to_end - __to_next))
+ __ret = partial;
+ else
{
- __ret = error;
- break;
- }
- else if (__conv > static_cast<size_t>(__to_end - __to))
- {
- __ret = partial;
- break;
+ memcpy(__to_next, __buf, __conv);
+ __state = __tmp_state;
+ __to_next += __conv;
+ ++__from_next;
}
-
- memcpy(__to, __buf, __conv);
- __state = __tmp_state;
- __to += __conv;
- __from++;
}
}
__uselocale(__old);
#endif
- if (__ret == ok && __from < __from_end)
- __ret = partial;
-
- __from_next = __from;
- __to_next = __to;
return __ret;
}
--- /dev/null
+// Copyright (C) 2003 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 2, 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 COPYING. If not, write to the Free
+// Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
+// USA.
+
+// As a special exception, you may use this file as part of a free software
+// library without restriction. Specifically, if other files instantiate
+// templates or use macros or inline functions from this file, or you compile
+// this file and link it with other files to produce an executable, this
+// file does not by itself cause the resulting executable to be covered by
+// the GNU General Public License. This exception does not however
+// invalidate any other reasons why the executable file might be covered by
+// the GNU General Public License.
+
+#include <cstdio>
+#include <cstring>
+#include <fstream>
+#include <langinfo.h>
+#include <iconv.h>
+#include <testsuite_performance.h>
+
+// libstdc++/11602
+int main(int argc, char** argv)
+{
+ using namespace std;
+ using namespace __gnu_test;
+
+ time_counter time;
+ resource_counter resource;
+ const int iters = 300000;
+
+ wchar_t wbuf[1024];
+ char cbuf[1024];
+
+ wmemset(wbuf, L'a', 1024);
+
+ // C (iconv)
+ iconv_t cd = iconv_open(nl_langinfo(CODESET), "WCHAR_T");
+ start_counters(time, resource);
+ for (int i = 0; i < iters; ++i)
+ {
+ size_t inbytesleft = 1024 * sizeof(wchar_t);
+ size_t outbytesleft = 1024;
+ char* in = reinterpret_cast<char*>(wbuf);
+ char* out = cbuf;
+ iconv(cd, &in, &inbytesleft, &out, &outbytesleft);
+ }
+ stop_counters(time, resource);
+ iconv_close(cd);
+ report_performance(__FILE__, "C (iconv)", time, resource);
+ clear_counters(time, resource);
+
+ // C++ (codecvt)
+ locale loc;
+ const codecvt<wchar_t, char, mbstate_t>& cvt =
+ use_facet<codecvt<wchar_t, char, mbstate_t> >(loc);
+ mbstate_t state;
+ memset(&state, 0, sizeof(state));
+ start_counters(time, resource);
+ for (int i = 0; i < iters; ++i)
+ {
+ const wchar_t* from_next;
+ char* to_next;
+ cvt.out(state, wbuf, wbuf + 1024, from_next,
+ cbuf, cbuf + 1024, to_next);
+ }
+ stop_counters(time, resource);
+ report_performance(__FILE__, "C++ (codecvt)", time, resource);
+
+ return 0;
+}