diff options
author | ridiculousfish <corydoras@ridiculousfish.com> | 2016-03-02 11:23:18 -0800 |
---|---|---|
committer | ridiculousfish <corydoras@ridiculousfish.com> | 2016-03-02 11:24:23 -0800 |
commit | 58d56f91f36b56d83423028e04e9faabd3bad3d2 (patch) | |
tree | 800dfc53954a820a14455a5547866edc00ef6109 /src/fish_tests.cpp | |
parent | 0e8a8a7c8023e05792b8b1d32ddbe7bcf2ea3549 (diff) |
Tweak UTF8 decoding interface
Previously, when decoding UTF-8, we would first run through the
array to compute the correct size, then allocate a buffer of that size,
then run through the array again to fill the buffer, and then copy it
into a std::wstring. With this fix we can copy it into the string
directly, reducing allocations and only requiring a single pass.
Diffstat (limited to 'src/fish_tests.cpp')
-rw-r--r-- | src/fish_tests.cpp | 24 |
1 files changed, 19 insertions, 5 deletions
diff --git a/src/fish_tests.cpp b/src/fish_tests.cpp index c5fa4385..bcfff032 100644 --- a/src/fish_tests.cpp +++ b/src/fish_tests.cpp @@ -1057,7 +1057,16 @@ static void test_utf82wchar(const char *src, size_t slen, const wchar_t *dst, si do { - size = utf8_to_wchar(src, slen, mem, dlen, flags); + if (mem == NULL) + { + size = utf8_to_wchar(src, slen, NULL, flags); + } + else + { + std::wstring buff; + size = utf8_to_wchar(src, slen, &buff, flags); + std::copy(buff.begin(), buff.begin() + std::min(dlen, buff.size()), mem); + } if (res != size) { err(L"u2w: %s: FAILED (rv: %lu, must be %lu)", descr, size, res); @@ -1219,8 +1228,10 @@ static void test_utf8() UTF8_IGNORE_ERROR, sizeof(wb1) / sizeof(*wb1), "ignore bad chars"); test_utf82wchar(um, sizeof(um), wm, sizeof(wm) / sizeof(*wm), 0, sizeof(wm) / sizeof(*wm), "mixed languages"); - test_utf82wchar(um, sizeof(um), wm, sizeof(wm) / sizeof(*wm) - 1, 0, - 0, "boundaries -1"); + // PCA this test was to ensure that if the output buffer was too small, we'd get 0 + // we no longer have statically sized result buffers, so this test is disabled + // test_utf82wchar(um, sizeof(um), wm, sizeof(wm) / sizeof(*wm) - 1, 0, + // 0, "boundaries -1"); test_utf82wchar(um, sizeof(um), wm, sizeof(wm) / sizeof(*wm) + 1, 0, sizeof(wm) / sizeof(*wm), "boundaries +1"); test_utf82wchar(um, sizeof(um), NULL, 0, 0, @@ -1235,8 +1246,11 @@ static void test_utf8() "invalid params, src buf not NULL"); test_utf82wchar((const char *)NULL, 10, NULL, 0, 0, 0, "invalid params, src length is not 0"); - test_utf82wchar(u1, sizeof(u1), w1, 0, 0, 0, - "invalid params, dst is not NULL"); + + // PCA this test was to ensure that converting into a zero length output buffer would return 0 + // we no longer statically size output buffers, so the test is disabled + // test_utf82wchar(u1, sizeof(u1), w1, 0, 0, 0, + // "invalid params, dst is not NULL"); /* * UCS-4 -> UTF-8 string. |