TString.hpp 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. #pragma once
  2. #include <tchar.h>
  3. #include <windows.h>
  4. #include <string>
  5. //
  6. // A TString can be either multiple-bytes string or wide-char string.
  7. // When multiple-bytes string, the code page is CP_ACP.
  8. // When wide-char string, it is UTF-16-LE encoded string.
  9. //
  10. #ifdef _UNICODE
  11. using TString = std::wstring;
  12. #else
  13. using TString = std::string;
  14. #endif
  15. //
  16. // For a multiple-bytes string, the default code-page is CP_ACP.
  17. // Of course, you can specify code-page to override it.
  18. //
  19. TString TStringBuilder(PCSTR LpMultiByteStr, UINT CodePage = CP_ACP);
  20. //
  21. // For a wide-char string, it must be UTF-16-LE encoded.
  22. // So `CodePage` is not needed.
  23. //
  24. TString TStringBuilder(PCWSTR LpWideCharStr);
  25. //
  26. // For a multiple-bytes string, the default code-page is CP_ACP.
  27. // Of course, you can specify code-page to override it.
  28. //
  29. TString TStringBuilder(const std::string& RefMultiByteStr, UINT CodePage = CP_ACP);
  30. //
  31. // For a wide-char string, it must be UTF-16-LE encoded.
  32. // So `CodePage` is not needed.
  33. //
  34. TString TStringBuilder(const std::wstring& RefWideCharStr);
  35. //
  36. // Convert a TString to a multiple-bytes string with specified code-page.
  37. //
  38. std::string TStringEncode(PTSTR LpTString, UINT CodePage);
  39. std::string TStringEncode(const TString& RefTStr, UINT CodePage);
  40. //
  41. // Convert a multiple-bytes string to a TString with specified code-page.
  42. //
  43. TString TStringDecode(PCSTR LpMultiByteStr, UINT CodePage = CP_ACP);
  44. TString TStringDecode(const std::string& RefMultiByteStr, UINT CodePage = CP_ACP);
  45. //
  46. // Format function for TString
  47. //
  48. template<typename... __Ts>
  49. TString TStringFormat(const TString& Format, __Ts&&... Args) {
  50. TString s;
  51. s.resize(_sctprintf(Format.c_str(), std::forward<__Ts>(Args)...) + 1);
  52. s.resize(_sntprintf_s(s.data(), s.capacity(), _TRUNCATE, Format.c_str(), std::forward<__Ts>(Args)...));
  53. return s;
  54. }