mbconv.cpp 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: tests/benchmarks/mbconv.cpp
  3. // Purpose: MB<->WC conversion benchmarks
  4. // Author: Vadim Zeitlin
  5. // Created: 2008-10-17
  6. // Copyright: (c) 2008 Vadim Zeitlin <vadim@wxwidgets.org>
  7. // Licence: wxWindows licence
  8. /////////////////////////////////////////////////////////////////////////////
  9. #include "wx/strconv.h"
  10. #include "wx/string.h"
  11. #include "bench.h"
  12. namespace
  13. {
  14. const wchar_t *TEST_STRING =
  15. L"Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod"
  16. L"tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim"
  17. L"veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea"
  18. L"commodo consequat. Duis aute irure dolor in reprehenderit in voluptate"
  19. L"velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint"
  20. L"occaecat cupidatat non proident, sunt in culpa qui officia deserunt"
  21. L"mollit anim id est laborum."
  22. ;
  23. // just compute the length of the resulting multibyte string
  24. bool ComputeMBLength(const wxMBConv& conv)
  25. {
  26. // we suppose a fixed length encoding here (which happens to cover UTF-8
  27. // too as long as the test string is ASCII)
  28. return conv.FromWChar(NULL, 0, TEST_STRING)
  29. == (wcslen(TEST_STRING) + 1)*conv.GetMBNulLen();
  30. }
  31. // perform the conversion
  32. bool ConvertToMB(const wxMBConv& conv)
  33. {
  34. const size_t outlen = (wcslen(TEST_STRING) + 1)*conv.GetMBNulLen();
  35. wxCharBuffer buf(outlen - 1); // it adds 1 internally
  36. return conv.FromWChar(buf.data(), outlen, TEST_STRING) == outlen;
  37. }
  38. } // anonymous namespace
  39. BENCHMARK_FUNC(UTF16InitWX)
  40. {
  41. wxMBConvUTF16 conv;
  42. return true;
  43. }
  44. BENCHMARK_FUNC(UTF16InitSys)
  45. {
  46. wxCSConv conv("UTF-16LE");
  47. return conv.IsOk();
  48. }
  49. BENCHMARK_FUNC(UTF16LenWX)
  50. {
  51. return ComputeMBLength(wxMBConvUTF16());
  52. }
  53. BENCHMARK_FUNC(UTF16LenSys)
  54. {
  55. return ComputeMBLength(wxCSConv("UTF-16LE"));
  56. }
  57. BENCHMARK_FUNC(UTF16WX)
  58. {
  59. return ConvertToMB(wxMBConvUTF16());
  60. }
  61. BENCHMARK_FUNC(UTF16Sys)
  62. {
  63. return ConvertToMB(wxCSConv("UTF-16LE"));
  64. }