UnicodeConverter.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. //
  2. // UnicodeConverter.h
  3. //
  4. // Library: Foundation
  5. // Package: Text
  6. // Module: UnicodeConverter
  7. //
  8. // Definition of the UnicodeConverter class.
  9. //
  10. // Copyright (c) 2006, Applied Informatics Software Engineering GmbH.
  11. // and Contributors.
  12. //
  13. // SPDX-License-Identifier: BSL-1.0
  14. //
  15. #ifndef Foundation_UnicodeConverter_INCLUDED
  16. #define Foundation_UnicodeConverter_INCLUDED
  17. #include "Poco/Foundation.h"
  18. #include "Poco/UTFString.h"
  19. namespace Poco {
  20. class Foundation_API UnicodeConverter
  21. /// A convenience class that converts strings from
  22. /// UTF-8 encoded std::strings to UTF-16 or UTF-32 encoded std::wstrings
  23. /// and vice-versa.
  24. ///
  25. /// This class is mainly used for working with the Unicode Windows APIs
  26. /// and probably won't be of much use anywhere else ???
  27. {
  28. public:
  29. static void convert(const std::string& utf8String, UTF32String& utf32String);
  30. /// Converts the given UTF-8 encoded string into an UTF-32 encoded wide string.
  31. static void convert(const char* utf8String, std::size_t length, UTF32String& utf32String);
  32. /// Converts the given UTF-8 encoded character sequence into an UTF-32 encoded wide string.
  33. static void convert(const char* utf8String, UTF32String& utf32String);
  34. /// Converts the given zero-terminated UTF-8 encoded character sequence into an UTF-32 encoded wide string.
  35. static void convert(const std::string& utf8String, UTF16String& utf16String);
  36. /// Converts the given UTF-8 encoded string into an UTF-16 encoded wide string.
  37. static void convert(const char* utf8String, std::size_t length, UTF16String& utf16String);
  38. /// Converts the given UTF-8 encoded character sequence into an UTF-16 encoded wide string.
  39. static void convert(const char* utf8String, UTF16String& utf16String);
  40. /// Converts the given zero-terminated UTF-8 encoded character sequence into an UTF-16 encoded wide string.
  41. static void convert(const UTF16String& utf16String, std::string& utf8String);
  42. /// Converts the given UTF-16 encoded wide string into an UTF-8 encoded string.
  43. static void convert(const UTF32String& utf32String, std::string& utf8String);
  44. /// Converts the given UTF-32 encoded wide string into an UTF-8 encoded string.
  45. static void convert(const UTF16Char* utf16String, std::size_t length, std::string& utf8String);
  46. /// Converts the given zero-terminated UTF-16 encoded wide character sequence into an UTF-8 encoded string.
  47. static void convert(const UTF32Char* utf16String, std::size_t length, std::string& utf8String);
  48. /// Converts the given zero-terminated UTF-32 encoded wide character sequence into an UTF-8 encoded string.
  49. static void convert(const UTF16Char* utf16String, std::string& utf8String);
  50. /// Converts the given UTF-16 encoded zero terminated character sequence into an UTF-8 encoded string.
  51. static void convert(const UTF32Char* utf32String, std::string& utf8String);
  52. /// Converts the given UTF-32 encoded zero terminated character sequence into an UTF-8 encoded string.
  53. template <typename F, typename T>
  54. static void toUTF32(const F& f, T& t)
  55. {
  56. convert(f, t);
  57. }
  58. template <typename F, typename T>
  59. static void toUTF32(const F& f, std::size_t l, T& t)
  60. {
  61. convert(f, l, t);
  62. }
  63. template <typename F, typename T>
  64. static void toUTF16(const F& f, T& t)
  65. {
  66. convert(f, t);
  67. }
  68. template <typename F, typename T>
  69. static void toUTF16(const F& f, std::size_t l, T& t)
  70. {
  71. convert(f, l, t);
  72. }
  73. template <typename F, typename T>
  74. static void toUTF8(const F& f, T& t)
  75. {
  76. convert(f, t);
  77. }
  78. template <typename F, typename T>
  79. static void toUTF8(const F& f, std::size_t l, T& t)
  80. {
  81. convert(f, l, t);
  82. }
  83. template <typename T>
  84. static T to(const char* pChar)
  85. {
  86. T utfStr;
  87. Poco::UnicodeConverter::convert(pChar, utfStr);
  88. return utfStr;
  89. }
  90. template <typename T>
  91. static T to(const std::string& str)
  92. {
  93. T utfStr;
  94. Poco::UnicodeConverter::convert(str, utfStr);
  95. return utfStr;
  96. }
  97. template <typename T>
  98. static std::size_t UTFStrlen(const T* ptr)
  99. /// Returns the length (in characters) of a zero-terminated UTF string.
  100. {
  101. if (ptr == 0) return 0;
  102. const T* p;
  103. for (p = ptr; *p; ++p);
  104. return p - ptr;
  105. }
  106. };
  107. } // namespace Poco
  108. #endif // Foundation_UnicodeConverter_INCLUDED